Merge "Chinese Conversion Table Update 2017-6"
[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 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
28 */
29 abstract class GenericArrayObjectTest extends PHPUnit_Framework_TestCase {
30
31 use MediaWikiCoversValidator;
32
33 /**
34 * Returns objects that can serve as elements in the concrete
35 * GenericArrayObject deriving class being tested.
36 *
37 * @since 1.20
38 *
39 * @return array
40 */
41 abstract public function elementInstancesProvider();
42
43 /**
44 * Returns the name of the concrete class being tested.
45 *
46 * @since 1.20
47 *
48 * @return string
49 */
50 abstract public function getInstanceClass();
51
52 /**
53 * Provides instances of the concrete class being tested.
54 *
55 * @since 1.20
56 *
57 * @return array
58 */
59 public function instanceProvider() {
60 $instances = [];
61
62 foreach ( $this->elementInstancesProvider() as $elementInstances ) {
63 $instances[] = $this->getNew( $elementInstances[0] );
64 }
65
66 return $this->arrayWrap( $instances );
67 }
68
69 /**
70 * @since 1.20
71 *
72 * @param array $elements
73 *
74 * @return GenericArrayObject
75 */
76 protected function getNew( array $elements = [] ) {
77 $class = $this->getInstanceClass();
78
79 return new $class( $elements );
80 }
81
82 /**
83 * @dataProvider elementInstancesProvider
84 *
85 * @since 1.20
86 *
87 * @param array $elements
88 *
89 * @covers GenericArrayObject::__construct
90 */
91 public function testConstructor( array $elements ) {
92 $arrayObject = $this->getNew( $elements );
93
94 $this->assertEquals( count( $elements ), $arrayObject->count() );
95 }
96
97 /**
98 * @dataProvider elementInstancesProvider
99 *
100 * @since 1.20
101 *
102 * @param array $elements
103 *
104 * @covers GenericArrayObject::isEmpty
105 */
106 public function testIsEmpty( array $elements ) {
107 $arrayObject = $this->getNew( $elements );
108
109 $this->assertEquals( $elements === [], $arrayObject->isEmpty() );
110 }
111
112 /**
113 * @dataProvider instanceProvider
114 *
115 * @since 1.20
116 *
117 * @param GenericArrayObject $list
118 *
119 * @covers GenericArrayObject::offsetUnset
120 */
121 public function testUnset( GenericArrayObject $list ) {
122 if ( $list->isEmpty() ) {
123 $this->assertTrue( true ); // We cannot test unset if there are no elements
124 } else {
125 $offset = $list->getIterator()->key();
126 $count = $list->count();
127 $list->offsetUnset( $offset );
128 $this->assertEquals( $count - 1, $list->count() );
129 }
130
131 if ( !$list->isEmpty() ) {
132 $offset = $list->getIterator()->key();
133 $count = $list->count();
134 unset( $list[$offset] );
135 $this->assertEquals( $count - 1, $list->count() );
136 }
137 }
138
139 /**
140 * @dataProvider elementInstancesProvider
141 *
142 * @since 1.20
143 *
144 * @param array $elements
145 *
146 * @covers GenericArrayObject::append
147 */
148 public function testAppend( array $elements ) {
149 $list = $this->getNew();
150
151 $listSize = count( $elements );
152
153 foreach ( $elements as $element ) {
154 $list->append( $element );
155 }
156
157 $this->assertEquals( $listSize, $list->count() );
158
159 $list = $this->getNew();
160
161 foreach ( $elements as $element ) {
162 $list[] = $element;
163 }
164
165 $this->assertEquals( $listSize, $list->count() );
166
167 $this->checkTypeChecks( function ( GenericArrayObject $list, $element ) {
168 $list->append( $element );
169 } );
170 }
171
172 /**
173 * @since 1.20
174 *
175 * @param callable $function
176 *
177 * @covers GenericArrayObject::getObjectType
178 */
179 protected function checkTypeChecks( $function ) {
180 $excption = null;
181 $list = $this->getNew();
182
183 $elementClass = $list->getObjectType();
184
185 foreach ( [ 42, 'foo', [], new stdClass(), 4.2 ] as $element ) {
186 $validValid = $element instanceof $elementClass;
187
188 try {
189 call_user_func( $function, $list, $element );
190 $valid = true;
191 } catch ( InvalidArgumentException $exception ) {
192 $valid = false;
193 }
194
195 $this->assertEquals(
196 $validValid,
197 $valid,
198 'Object of invalid type got successfully added to a GenericArrayObject'
199 );
200 }
201 }
202
203 /**
204 * @dataProvider elementInstancesProvider
205 *
206 * @since 1.20
207 *
208 * @param array $elements
209 *
210 * @covers GenericArrayObject::offsetSet
211 */
212 public function testOffsetSet( array $elements ) {
213 if ( $elements === [] ) {
214 $this->assertTrue( true );
215
216 return;
217 }
218
219 $list = $this->getNew();
220
221 $element = reset( $elements );
222 $list->offsetSet( 42, $element );
223 $this->assertEquals( $element, $list->offsetGet( 42 ) );
224
225 $list = $this->getNew();
226
227 $element = reset( $elements );
228 $list['oHai'] = $element;
229 $this->assertEquals( $element, $list['oHai'] );
230
231 $list = $this->getNew();
232
233 $element = reset( $elements );
234 $list->offsetSet( 9001, $element );
235 $this->assertEquals( $element, $list[9001] );
236
237 $list = $this->getNew();
238
239 $element = reset( $elements );
240 $list->offsetSet( null, $element );
241 $this->assertEquals( $element, $list[0] );
242
243 $list = $this->getNew();
244 $offset = 0;
245
246 foreach ( $elements as $element ) {
247 $list->offsetSet( null, $element );
248 $this->assertEquals( $element, $list[$offset++] );
249 }
250
251 $this->assertEquals( count( $elements ), $list->count() );
252
253 $this->checkTypeChecks( function ( GenericArrayObject $list, $element ) {
254 $list->offsetSet( mt_rand(), $element );
255 } );
256 }
257
258 /**
259 * @dataProvider instanceProvider
260 *
261 * @since 1.21
262 *
263 * @param GenericArrayObject $list
264 *
265 * @covers GenericArrayObject::getSerializationData
266 * @covers GenericArrayObject::serialize
267 * @covers GenericArrayObject::unserialize
268 */
269 public function testSerialization( GenericArrayObject $list ) {
270 $serialization = serialize( $list );
271 $copy = unserialize( $serialization );
272
273 $this->assertEquals( $serialization, serialize( $copy ) );
274 $this->assertEquals( count( $list ), count( $copy ) );
275
276 $list = $list->getArrayCopy();
277 $copy = $copy->getArrayCopy();
278
279 $this->assertArrayEquals( $list, $copy, true, true );
280 }
281 }