merging latest master
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / GenericArrayObjectTest.php
1 <?php
2
3
4 /**
5 * Tests for the GenericArrayObject and deriving classes.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 * @since 1.20
24 *
25 * @ingroup Test
26 * @group GenericArrayObject
27 *
28 * @licence GNU GPL v2+
29 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
30 */
31 abstract class GenericArrayObjectTest extends MediaWikiTestCase {
32
33 /**
34 * Returns objects that can serve as elements in the concrete GenericArrayObject deriving class being tested.
35 *
36 * @since 1.20
37 *
38 * @return array
39 */
40 public abstract function elementInstancesProvider();
41
42 /**
43 * Provides instances of the concrete class being tested.
44 *
45 * @since 1.20
46 *
47 * @return array
48 */
49 public abstract function instanceProvider();
50
51 /**
52 * Returns the name of the concrete class being tested.
53 *
54 * @since 1.20
55 *
56 * @return string
57 */
58 public abstract function getInstanceClass();
59
60 /**
61 * @since 1.20
62 *
63 * @param array $elements
64 *
65 * @return GenericArrayObject
66 */
67 protected function getNew( array $elements = array() ) {
68 $class = $this->getInstanceClass();
69 return new $class( $elements );
70 }
71
72 /**
73 * @dataProvider elementInstancesProvider
74 *
75 * @since 1.20
76 *
77 * @param array $elements
78 */
79 public function testConstructor( array $elements ) {
80 $arrayObject = $this->getNew( $elements );
81
82 $this->assertEquals( count( $elements ), $arrayObject->count() );
83 }
84
85 /**
86 * @dataProvider elementInstancesProvider
87 *
88 * @since 1.20
89 *
90 * @param array $elements
91 */
92 public function testIsEmpty( array $elements ) {
93 $arrayObject = $this->getNew( $elements );
94
95 $this->assertEquals( $elements === array(), $arrayObject->isEmpty() );
96 }
97
98 /**
99 * @dataProvider instanceProvider
100 *
101 * @since 1.20
102 *
103 * @param GenericArrayObject $list
104 */
105 public function testUnset( GenericArrayObject $list ) {
106 if ( !$list->isEmpty() ) {
107 $offset = $list->getIterator()->key();
108 $count = $list->count();
109 $list->offsetUnset( $offset );
110 $this->assertEquals( $count - 1, $list->count() );
111 }
112
113 if ( !$list->isEmpty() ) {
114 $offset = $list->getIterator()->key();
115 $count = $list->count();
116 unset( $list[$offset] );
117 $this->assertEquals( $count - 1, $list->count() );
118 }
119
120 $exception = null;
121 try { $list->offsetUnset( 'sdfsedtgsrdysftu' ); } catch ( \Exception $exception ){}
122 $this->assertInstanceOf( '\Exception', $exception );
123 }
124
125 /**
126 * @dataProvider elementInstancesProvider
127 *
128 * @since 1.20
129 *
130 * @param array $elements
131 */
132 public function testAppend( array $elements ) {
133 $list = $this->getNew();
134
135 $listSize = count( $elements );
136
137 foreach ( $elements as $element ) {
138 $list->append( $element );
139 }
140
141 $this->assertEquals( $listSize, $list->count() );
142
143 $list = $this->getNew();
144
145 foreach ( $elements as $element ) {
146 $list[] = $element;
147 }
148
149 $this->assertEquals( $listSize, $list->count() );
150
151 $this->checkTypeChecks( function( GenericArrayObject $list, $element ) {
152 $list->append( $element );
153 } );
154 }
155
156 /**
157 * @since 1.20
158 *
159 * @param callback $function
160 */
161 protected function checkTypeChecks( $function ) {
162 $excption = null;
163 $list = $this->getNew();
164
165 $elementClass = $list->getObjectType();
166
167 foreach ( array( 42, 'foo', array(), new \stdClass(), 4.2 ) as $element ) {
168 $validValid = $element instanceof $elementClass;
169
170 try{
171 call_user_func( $function, $list, $element );
172 $valid = true;
173 }
174 catch ( \MWException $exception ) {
175 $valid = false;
176 }
177
178 $this->assertEquals(
179 $validValid,
180 $valid,
181 'Object of invalid type got successfully added to a GenericArrayObject'
182 );
183 }
184 }
185
186 /**
187 * @dataProvider elementInstancesProvider
188 *
189 * @since 1.20
190 *
191 * @param array $elements
192 */
193 public function testOffsetSet( array $elements ) {
194 if ( $elements === array() ) {
195 $this->assertTrue( true );
196 return;
197 }
198
199 $list = $this->getNew();
200
201 $element = reset( $elements );
202 $list->offsetSet( 42, $element );
203 $this->assertEquals( $element, $list->offsetGet( 42 ) );
204
205 $list = $this->getNew();
206
207 $element = reset( $elements );
208 $list['oHai'] = $element;
209 $this->assertEquals( $element, $list['oHai'] );
210
211 $list = $this->getNew();
212
213 $element = reset( $elements );
214 $list->offsetSet( 9001, $element );
215 $this->assertEquals( $element, $list[9001] );
216
217 $list = $this->getNew();
218
219 $element = reset( $elements );
220 $list->offsetSet( null, $element );
221 $this->assertEquals( $element, $list[0] );
222
223 $list = $this->getNew();
224 $offset = 0;
225
226 foreach ( $elements as $element ) {
227 $list->offsetSet( null, $element );
228 $this->assertEquals( $element, $list[$offset++] );
229 }
230
231 $this->assertEquals( count( $elements ), $list->count() );
232
233 $this->checkTypeChecks( function( GenericArrayObject $list, $element ) {
234 $list->offsetSet( mt_rand(), $element );
235 } );
236 }
237
238 }