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