1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
<?php
/**
* Tests for the GenericArrayObject and deriving classes.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @since 1.20
*
* @ingroup Test
* @group GenericArrayObject
*
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
abstract class GenericArrayObjectTest extends PHPUnit_Framework_TestCase {
/**
* Returns objects that can serve as elements in the concrete
* GenericArrayObject deriving class being tested.
*
* @since 1.20
*
* @return array
*/
abstract public function elementInstancesProvider();
/**
* Returns the name of the concrete class being tested.
*
* @since 1.20
*
* @return string
*/
abstract public function getInstanceClass();
/**
* Provides instances of the concrete class being tested.
*
* @since 1.20
*
* @return array
*/
public function instanceProvider() {
$instances = array();
foreach ( $this->elementInstancesProvider() as $elementInstances ) {
$instances[] = $this->getNew( $elementInstances[0] );
}
return $this->arrayWrap( $instances );
}
/**
* @since 1.20
*
* @param array $elements
*
* @return GenericArrayObject
*/
protected function getNew( array $elements = array() ) {
$class = $this->getInstanceClass();
return new $class( $elements );
}
/**
* @dataProvider elementInstancesProvider
*
* @since 1.20
*
* @param array $elements
*
* @covers GenericArrayObject::__construct
*/
public function testConstructor( array $elements ) {
$arrayObject = $this->getNew( $elements );
$this->assertEquals( count( $elements ), $arrayObject->count() );
}
/**
* @dataProvider elementInstancesProvider
*
* @since 1.20
*
* @param array $elements
*
* @covers GenericArrayObject::isEmpty
*/
public function testIsEmpty( array $elements ) {
$arrayObject = $this->getNew( $elements );
$this->assertEquals( $elements === array(), $arrayObject->isEmpty() );
}
/**
* @dataProvider instanceProvider
*
* @since 1.20
*
* @param GenericArrayObject $list
*
* @covers GenericArrayObject::offsetUnset
*/
public function testUnset( GenericArrayObject $list ) {
if ( $list->isEmpty() ) {
$this->assertTrue( true ); // We cannot test unset if there are no elements
} else {
$offset = $list->getIterator()->key();
$count = $list->count();
$list->offsetUnset( $offset );
$this->assertEquals( $count - 1, $list->count() );
}
if ( !$list->isEmpty() ) {
$offset = $list->getIterator()->key();
$count = $list->count();
unset( $list[$offset] );
$this->assertEquals( $count - 1, $list->count() );
}
}
/**
* @dataProvider elementInstancesProvider
*
* @since 1.20
*
* @param array $elements
*
* @covers GenericArrayObject::append
*/
public function testAppend( array $elements ) {
$list = $this->getNew();
$listSize = count( $elements );
foreach ( $elements as $element ) {
$list->append( $element );
}
$this->assertEquals( $listSize, $list->count() );
$list = $this->getNew();
foreach ( $elements as $element ) {
$list[] = $element;
}
$this->assertEquals( $listSize, $list->count() );
$this->checkTypeChecks( function ( GenericArrayObject $list, $element ) {
$list->append( $element );
} );
}
/**
* @since 1.20
*
* @param callable $function
*
* @covers GenericArrayObject::getObjectType
*/
protected function checkTypeChecks( $function ) {
$excption = null;
$list = $this->getNew();
$elementClass = $list->getObjectType();
foreach ( array( 42, 'foo', array(), new stdClass(), 4.2 ) as $element ) {
$validValid = $element instanceof $elementClass;
try {
call_user_func( $function, $list, $element );
$valid = true;
} catch ( InvalidArgumentException $exception ) {
$valid = false;
}
$this->assertEquals(
$validValid,
$valid,
'Object of invalid type got successfully added to a GenericArrayObject'
);
}
}
/**
* @dataProvider elementInstancesProvider
*
* @since 1.20
*
* @param array $elements
*
* @covers GenericArrayObject::offsetSet
*/
public function testOffsetSet( array $elements ) {
if ( $elements === array() ) {
$this->assertTrue( true );
return;
}
$list = $this->getNew();
$element = reset( $elements );
$list->offsetSet( 42, $element );
$this->assertEquals( $element, $list->offsetGet( 42 ) );
$list = $this->getNew();
$element = reset( $elements );
$list['oHai'] = $element;
$this->assertEquals( $element, $list['oHai'] );
$list = $this->getNew();
$element = reset( $elements );
$list->offsetSet( 9001, $element );
$this->assertEquals( $element, $list[9001] );
$list = $this->getNew();
$element = reset( $elements );
$list->offsetSet( null, $element );
$this->assertEquals( $element, $list[0] );
$list = $this->getNew();
$offset = 0;
foreach ( $elements as $element ) {
$list->offsetSet( null, $element );
$this->assertEquals( $element, $list[$offset++] );
}
$this->assertEquals( count( $elements ), $list->count() );
$this->checkTypeChecks( function ( GenericArrayObject $list, $element ) {
$list->offsetSet( mt_rand(), $element );
} );
}
/**
* @dataProvider instanceProvider
*
* @since 1.21
*
* @param GenericArrayObject $list
*
* @covers GenericArrayObject::getSerializationData
* @covers GenericArrayObject::serialize
* @covers GenericArrayObject::unserialize
*/
public function testSerialization( GenericArrayObject $list ) {
$serialization = serialize( $list );
$copy = unserialize( $serialization );
$this->assertEquals( $serialization, serialize( $copy ) );
$this->assertEquals( count( $list ), count( $copy ) );
$list = $list->getArrayCopy();
$copy = $copy->getArrayCopy();
$this->assertArrayEquals( $list, $copy, true, true );
}
}
|