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