Genderize Special:Preferences
[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 $offset = $list->getIterator()->key();
115 $count = $list->count();
116 $list->offsetUnset( $offset );
117 $this->assertEquals( $count - 1, $list->count() );
118 }
119
120 if ( !$list->isEmpty() ) {
121 $offset = $list->getIterator()->key();
122 $count = $list->count();
123 unset( $list[$offset] );
124 $this->assertEquals( $count - 1, $list->count() );
125 }
126
127 $exception = null;
128 try { $list->offsetUnset( 'sdfsedtgsrdysftu' ); } catch ( \Exception $exception ){}
129 $this->assertInstanceOf( '\Exception', $exception );
130 }
131
132 /**
133 * @dataProvider elementInstancesProvider
134 *
135 * @since 1.20
136 *
137 * @param array $elements
138 */
139 public function testAppend( array $elements ) {
140 $list = $this->getNew();
141
142 $listSize = count( $elements );
143
144 foreach ( $elements as $element ) {
145 $list->append( $element );
146 }
147
148 $this->assertEquals( $listSize, $list->count() );
149
150 $list = $this->getNew();
151
152 foreach ( $elements as $element ) {
153 $list[] = $element;
154 }
155
156 $this->assertEquals( $listSize, $list->count() );
157
158 $this->checkTypeChecks( function( GenericArrayObject $list, $element ) {
159 $list->append( $element );
160 } );
161 }
162
163 /**
164 * @since 1.20
165 *
166 * @param callback $function
167 */
168 protected function checkTypeChecks( $function ) {
169 $excption = null;
170 $list = $this->getNew();
171
172 $elementClass = $list->getObjectType();
173
174 foreach ( array( 42, 'foo', array(), new \stdClass(), 4.2 ) as $element ) {
175 $validValid = $element instanceof $elementClass;
176
177 try{
178 call_user_func( $function, $list, $element );
179 $valid = true;
180 }
181 catch ( InvalidArgumentException $exception ) {
182 $valid = false;
183 }
184
185 $this->assertEquals(
186 $validValid,
187 $valid,
188 'Object of invalid type got successfully added to a GenericArrayObject'
189 );
190 }
191 }
192
193 /**
194 * @dataProvider elementInstancesProvider
195 *
196 * @since 1.20
197 *
198 * @param array $elements
199 */
200 public function testOffsetSet( array $elements ) {
201 if ( $elements === array() ) {
202 $this->assertTrue( true );
203 return;
204 }
205
206 $list = $this->getNew();
207
208 $element = reset( $elements );
209 $list->offsetSet( 42, $element );
210 $this->assertEquals( $element, $list->offsetGet( 42 ) );
211
212 $list = $this->getNew();
213
214 $element = reset( $elements );
215 $list['oHai'] = $element;
216 $this->assertEquals( $element, $list['oHai'] );
217
218 $list = $this->getNew();
219
220 $element = reset( $elements );
221 $list->offsetSet( 9001, $element );
222 $this->assertEquals( $element, $list[9001] );
223
224 $list = $this->getNew();
225
226 $element = reset( $elements );
227 $list->offsetSet( null, $element );
228 $this->assertEquals( $element, $list[0] );
229
230 $list = $this->getNew();
231 $offset = 0;
232
233 foreach ( $elements as $element ) {
234 $list->offsetSet( null, $element );
235 $this->assertEquals( $element, $list[$offset++] );
236 }
237
238 $this->assertEquals( count( $elements ), $list->count() );
239
240 $this->checkTypeChecks( function( GenericArrayObject $list, $element ) {
241 $list->offsetSet( mt_rand(), $element );
242 } );
243 }
244
245 }