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