Merge "HTMLTextAreaField: Allow sizes to be overridden by child classes"
[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
77 return new $class( $elements );
78 }
79
80 /**
81 * @dataProvider elementInstancesProvider
82 *
83 * @since 1.20
84 *
85 * @param array $elements
86 */
87 public function testConstructor( array $elements ) {
88 $arrayObject = $this->getNew( $elements );
89
90 $this->assertEquals( count( $elements ), $arrayObject->count() );
91 }
92
93 /**
94 * @dataProvider elementInstancesProvider
95 *
96 * @since 1.20
97 *
98 * @param array $elements
99 */
100 public function testIsEmpty( array $elements ) {
101 $arrayObject = $this->getNew( $elements );
102
103 $this->assertEquals( $elements === array(), $arrayObject->isEmpty() );
104 }
105
106 /**
107 * @dataProvider instanceProvider
108 *
109 * @since 1.20
110 *
111 * @param GenericArrayObject $list
112 */
113 public function testUnset( GenericArrayObject $list ) {
114 if ( $list->isEmpty() ) {
115 $this->assertTrue( true ); // We cannot test unset if there are no elements
116 } else {
117 $offset = $list->getIterator()->key();
118 $count = $list->count();
119 $list->offsetUnset( $offset );
120 $this->assertEquals( $count - 1, $list->count() );
121 }
122
123 if ( !$list->isEmpty() ) {
124 $offset = $list->getIterator()->key();
125 $count = $list->count();
126 unset( $list[$offset] );
127 $this->assertEquals( $count - 1, $list->count() );
128 }
129 }
130
131 /**
132 * @dataProvider elementInstancesProvider
133 *
134 * @since 1.20
135 *
136 * @param array $elements
137 */
138 public function testAppend( array $elements ) {
139 $list = $this->getNew();
140
141 $listSize = count( $elements );
142
143 foreach ( $elements as $element ) {
144 $list->append( $element );
145 }
146
147 $this->assertEquals( $listSize, $list->count() );
148
149 $list = $this->getNew();
150
151 foreach ( $elements as $element ) {
152 $list[] = $element;
153 }
154
155 $this->assertEquals( $listSize, $list->count() );
156
157 $this->checkTypeChecks( function ( GenericArrayObject $list, $element ) {
158 $list->append( $element );
159 } );
160 }
161
162 /**
163 * @since 1.20
164 *
165 * @param callback $function
166 */
167 protected function checkTypeChecks( $function ) {
168 $excption = null;
169 $list = $this->getNew();
170
171 $elementClass = $list->getObjectType();
172
173 foreach ( array( 42, 'foo', array(), new stdClass(), 4.2 ) as $element ) {
174 $validValid = $element instanceof $elementClass;
175
176 try {
177 call_user_func( $function, $list, $element );
178 $valid = true;
179 } catch ( InvalidArgumentException $exception ) {
180 $valid = false;
181 }
182
183 $this->assertEquals(
184 $validValid,
185 $valid,
186 'Object of invalid type got successfully added to a GenericArrayObject'
187 );
188 }
189 }
190
191 /**
192 * @dataProvider elementInstancesProvider
193 *
194 * @since 1.20
195 *
196 * @param array $elements
197 */
198 public function testOffsetSet( array $elements ) {
199 if ( $elements === array() ) {
200 $this->assertTrue( true );
201
202 return;
203 }
204
205 $list = $this->getNew();
206
207 $element = reset( $elements );
208 $list->offsetSet( 42, $element );
209 $this->assertEquals( $element, $list->offsetGet( 42 ) );
210
211 $list = $this->getNew();
212
213 $element = reset( $elements );
214 $list['oHai'] = $element;
215 $this->assertEquals( $element, $list['oHai'] );
216
217 $list = $this->getNew();
218
219 $element = reset( $elements );
220 $list->offsetSet( 9001, $element );
221 $this->assertEquals( $element, $list[9001] );
222
223 $list = $this->getNew();
224
225 $element = reset( $elements );
226 $list->offsetSet( null, $element );
227 $this->assertEquals( $element, $list[0] );
228
229 $list = $this->getNew();
230 $offset = 0;
231
232 foreach ( $elements as $element ) {
233 $list->offsetSet( null, $element );
234 $this->assertEquals( $element, $list[$offset++] );
235 }
236
237 $this->assertEquals( count( $elements ), $list->count() );
238
239 $this->checkTypeChecks( function ( GenericArrayObject $list, $element ) {
240 $list->offsetSet( mt_rand(), $element );
241 } );
242 }
243
244 /**
245 * @dataProvider instanceProvider
246 *
247 * @since 1.21
248 *
249 * @param GenericArrayObject $list
250 */
251 public function testSerialization( GenericArrayObject $list ) {
252 $serialization = serialize( $list );
253 $copy = unserialize( $serialization );
254
255 $this->assertEquals( $serialization, serialize( $copy ) );
256 $this->assertEquals( count( $list ), count( $copy ) );
257
258 $list = $list->getArrayCopy();
259 $copy = $copy->getArrayCopy();
260
261 $this->assertArrayEquals( $list, $copy, true, true );
262 }
263 }