Merge "Added release notes for 'ContentHandler::runLegacyHooks' removal"
[mediawiki.git] / tests / phpunit / includes / libs / GenericArrayObjectTest.php
blob12c57871c7249dcfb608a76a97a21eab89f21237
1 <?php
3 /**
4 * Tests for the GenericArrayObject and deriving classes.
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.
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.
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
21 * @file
22 * @since 1.20
24 * @ingroup Test
25 * @group GenericArrayObject
27 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
29 abstract class GenericArrayObjectTest extends PHPUnit_Framework_TestCase {
31 /**
32 * Returns objects that can serve as elements in the concrete
33 * GenericArrayObject deriving class being tested.
35 * @since 1.20
37 * @return array
39 abstract public function elementInstancesProvider();
41 /**
42 * Returns the name of the concrete class being tested.
44 * @since 1.20
46 * @return string
48 abstract public function getInstanceClass();
50 /**
51 * Provides instances of the concrete class being tested.
53 * @since 1.20
55 * @return array
57 public function instanceProvider() {
58 $instances = [];
60 foreach ( $this->elementInstancesProvider() as $elementInstances ) {
61 $instances[] = $this->getNew( $elementInstances[0] );
64 return $this->arrayWrap( $instances );
67 /**
68 * @since 1.20
70 * @param array $elements
72 * @return GenericArrayObject
74 protected function getNew( array $elements = [] ) {
75 $class = $this->getInstanceClass();
77 return new $class( $elements );
80 /**
81 * @dataProvider elementInstancesProvider
83 * @since 1.20
85 * @param array $elements
87 * @covers GenericArrayObject::__construct
89 public function testConstructor( array $elements ) {
90 $arrayObject = $this->getNew( $elements );
92 $this->assertEquals( count( $elements ), $arrayObject->count() );
95 /**
96 * @dataProvider elementInstancesProvider
98 * @since 1.20
100 * @param array $elements
102 * @covers GenericArrayObject::isEmpty
104 public function testIsEmpty( array $elements ) {
105 $arrayObject = $this->getNew( $elements );
107 $this->assertEquals( $elements === [], $arrayObject->isEmpty() );
111 * @dataProvider instanceProvider
113 * @since 1.20
115 * @param GenericArrayObject $list
117 * @covers GenericArrayObject::offsetUnset
119 public function testUnset( GenericArrayObject $list ) {
120 if ( $list->isEmpty() ) {
121 $this->assertTrue( true ); // We cannot test unset if there are no elements
122 } else {
123 $offset = $list->getIterator()->key();
124 $count = $list->count();
125 $list->offsetUnset( $offset );
126 $this->assertEquals( $count - 1, $list->count() );
129 if ( !$list->isEmpty() ) {
130 $offset = $list->getIterator()->key();
131 $count = $list->count();
132 unset( $list[$offset] );
133 $this->assertEquals( $count - 1, $list->count() );
138 * @dataProvider elementInstancesProvider
140 * @since 1.20
142 * @param array $elements
144 * @covers GenericArrayObject::append
146 public function testAppend( array $elements ) {
147 $list = $this->getNew();
149 $listSize = count( $elements );
151 foreach ( $elements as $element ) {
152 $list->append( $element );
155 $this->assertEquals( $listSize, $list->count() );
157 $list = $this->getNew();
159 foreach ( $elements as $element ) {
160 $list[] = $element;
163 $this->assertEquals( $listSize, $list->count() );
165 $this->checkTypeChecks( function ( GenericArrayObject $list, $element ) {
166 $list->append( $element );
167 } );
171 * @since 1.20
173 * @param callable $function
175 * @covers GenericArrayObject::getObjectType
177 protected function checkTypeChecks( $function ) {
178 $excption = null;
179 $list = $this->getNew();
181 $elementClass = $list->getObjectType();
183 foreach ( [ 42, 'foo', [], new stdClass(), 4.2 ] as $element ) {
184 $validValid = $element instanceof $elementClass;
186 try {
187 call_user_func( $function, $list, $element );
188 $valid = true;
189 } catch ( InvalidArgumentException $exception ) {
190 $valid = false;
193 $this->assertEquals(
194 $validValid,
195 $valid,
196 'Object of invalid type got successfully added to a GenericArrayObject'
202 * @dataProvider elementInstancesProvider
204 * @since 1.20
206 * @param array $elements
208 * @covers GenericArrayObject::offsetSet
210 public function testOffsetSet( array $elements ) {
211 if ( $elements === [] ) {
212 $this->assertTrue( true );
214 return;
217 $list = $this->getNew();
219 $element = reset( $elements );
220 $list->offsetSet( 42, $element );
221 $this->assertEquals( $element, $list->offsetGet( 42 ) );
223 $list = $this->getNew();
225 $element = reset( $elements );
226 $list['oHai'] = $element;
227 $this->assertEquals( $element, $list['oHai'] );
229 $list = $this->getNew();
231 $element = reset( $elements );
232 $list->offsetSet( 9001, $element );
233 $this->assertEquals( $element, $list[9001] );
235 $list = $this->getNew();
237 $element = reset( $elements );
238 $list->offsetSet( null, $element );
239 $this->assertEquals( $element, $list[0] );
241 $list = $this->getNew();
242 $offset = 0;
244 foreach ( $elements as $element ) {
245 $list->offsetSet( null, $element );
246 $this->assertEquals( $element, $list[$offset++] );
249 $this->assertEquals( count( $elements ), $list->count() );
251 $this->checkTypeChecks( function ( GenericArrayObject $list, $element ) {
252 $list->offsetSet( mt_rand(), $element );
253 } );
257 * @dataProvider instanceProvider
259 * @since 1.21
261 * @param GenericArrayObject $list
263 * @covers GenericArrayObject::getSerializationData
264 * @covers GenericArrayObject::serialize
265 * @covers GenericArrayObject::unserialize
267 public function testSerialization( GenericArrayObject $list ) {
268 $serialization = serialize( $list );
269 $copy = unserialize( $serialization );
271 $this->assertEquals( $serialization, serialize( $copy ) );
272 $this->assertEquals( count( $list ), count( $copy ) );
274 $list = $list->getArrayCopy();
275 $copy = $copy->getArrayCopy();
277 $this->assertArrayEquals( $list, $copy, true, true );