Merge "ParserOutput::addLanguageLink needs a string"
[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 public abstract 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 public abstract 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 }
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 }
180 catch ( InvalidArgumentException $exception ) {
181 $valid = false;
182 }
183
184 $this->assertEquals(
185 $validValid,
186 $valid,
187 'Object of invalid type got successfully added to a GenericArrayObject'
188 );
189 }
190 }
191
192 /**
193 * @dataProvider elementInstancesProvider
194 *
195 * @since 1.20
196 *
197 * @param array $elements
198 */
199 public function testOffsetSet( array $elements ) {
200 if ( $elements === array() ) {
201 $this->assertTrue( true );
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 }