Convert most libs/ tests to use PHPUnit_Framework_TestCase
[mediawiki.git] / tests / phpunit / includes / libs / GenericArrayObjectTest.php
blobfd9f80d283475130ce6050a5fe41c33f12ad0218
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 * @licence GNU GPL v2+
28 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
30 abstract class GenericArrayObjectTest extends PHPUnit_Framework_TestCase {
32 /**
33 * Returns objects that can serve as elements in the concrete
34 * GenericArrayObject deriving class being tested.
36 * @since 1.20
38 * @return array
40 abstract public function elementInstancesProvider();
42 /**
43 * Returns the name of the concrete class being tested.
45 * @since 1.20
47 * @return string
49 abstract public function getInstanceClass();
51 /**
52 * Provides instances of the concrete class being tested.
54 * @since 1.20
56 * @return array
58 public function instanceProvider() {
59 $instances = array();
61 foreach ( $this->elementInstancesProvider() as $elementInstances ) {
62 $instances[] = $this->getNew( $elementInstances[0] );
65 return $this->arrayWrap( $instances );
68 /**
69 * @since 1.20
71 * @param array $elements
73 * @return GenericArrayObject
75 protected function getNew( array $elements = array() ) {
76 $class = $this->getInstanceClass();
78 return new $class( $elements );
81 /**
82 * @dataProvider elementInstancesProvider
84 * @since 1.20
86 * @param array $elements
88 * @covers GenericArrayObject::__construct
90 public function testConstructor( array $elements ) {
91 $arrayObject = $this->getNew( $elements );
93 $this->assertEquals( count( $elements ), $arrayObject->count() );
96 /**
97 * @dataProvider elementInstancesProvider
99 * @since 1.20
101 * @param array $elements
103 * @covers GenericArrayObject::isEmpty
105 public function testIsEmpty( array $elements ) {
106 $arrayObject = $this->getNew( $elements );
108 $this->assertEquals( $elements === array(), $arrayObject->isEmpty() );
112 * @dataProvider instanceProvider
114 * @since 1.20
116 * @param GenericArrayObject $list
118 * @covers GenericArrayObject::offsetUnset
120 public function testUnset( GenericArrayObject $list ) {
121 if ( $list->isEmpty() ) {
122 $this->assertTrue( true ); // We cannot test unset if there are no elements
123 } else {
124 $offset = $list->getIterator()->key();
125 $count = $list->count();
126 $list->offsetUnset( $offset );
127 $this->assertEquals( $count - 1, $list->count() );
130 if ( !$list->isEmpty() ) {
131 $offset = $list->getIterator()->key();
132 $count = $list->count();
133 unset( $list[$offset] );
134 $this->assertEquals( $count - 1, $list->count() );
139 * @dataProvider elementInstancesProvider
141 * @since 1.20
143 * @param array $elements
145 * @covers GenericArrayObject::append
147 public function testAppend( array $elements ) {
148 $list = $this->getNew();
150 $listSize = count( $elements );
152 foreach ( $elements as $element ) {
153 $list->append( $element );
156 $this->assertEquals( $listSize, $list->count() );
158 $list = $this->getNew();
160 foreach ( $elements as $element ) {
161 $list[] = $element;
164 $this->assertEquals( $listSize, $list->count() );
166 $this->checkTypeChecks( function ( GenericArrayObject $list, $element ) {
167 $list->append( $element );
168 } );
172 * @since 1.20
174 * @param callable $function
176 * @covers GenericArrayObject::getObjectType
178 protected function checkTypeChecks( $function ) {
179 $excption = null;
180 $list = $this->getNew();
182 $elementClass = $list->getObjectType();
184 foreach ( array( 42, 'foo', array(), new stdClass(), 4.2 ) as $element ) {
185 $validValid = $element instanceof $elementClass;
187 try {
188 call_user_func( $function, $list, $element );
189 $valid = true;
190 } catch ( InvalidArgumentException $exception ) {
191 $valid = false;
194 $this->assertEquals(
195 $validValid,
196 $valid,
197 'Object of invalid type got successfully added to a GenericArrayObject'
203 * @dataProvider elementInstancesProvider
205 * @since 1.20
207 * @param array $elements
209 * @covers GenericArrayObject::offsetSet
211 public function testOffsetSet( array $elements ) {
212 if ( $elements === array() ) {
213 $this->assertTrue( true );
215 return;
218 $list = $this->getNew();
220 $element = reset( $elements );
221 $list->offsetSet( 42, $element );
222 $this->assertEquals( $element, $list->offsetGet( 42 ) );
224 $list = $this->getNew();
226 $element = reset( $elements );
227 $list['oHai'] = $element;
228 $this->assertEquals( $element, $list['oHai'] );
230 $list = $this->getNew();
232 $element = reset( $elements );
233 $list->offsetSet( 9001, $element );
234 $this->assertEquals( $element, $list[9001] );
236 $list = $this->getNew();
238 $element = reset( $elements );
239 $list->offsetSet( null, $element );
240 $this->assertEquals( $element, $list[0] );
242 $list = $this->getNew();
243 $offset = 0;
245 foreach ( $elements as $element ) {
246 $list->offsetSet( null, $element );
247 $this->assertEquals( $element, $list[$offset++] );
250 $this->assertEquals( count( $elements ), $list->count() );
252 $this->checkTypeChecks( function ( GenericArrayObject $list, $element ) {
253 $list->offsetSet( mt_rand(), $element );
254 } );
258 * @dataProvider instanceProvider
260 * @since 1.21
262 * @param GenericArrayObject $list
264 * @covers GenericArrayObject::getSerializationData
265 * @covers GenericArrayObject::serialize
266 * @covers GenericArrayObject::unserialize
268 public function testSerialization( GenericArrayObject $list ) {
269 $serialization = serialize( $list );
270 $copy = unserialize( $serialization );
272 $this->assertEquals( $serialization, serialize( $copy ) );
273 $this->assertEquals( count( $list ), count( $copy ) );
275 $list = $list->getArrayCopy();
276 $copy = $copy->getArrayCopy();
278 $this->assertArrayEquals( $list, $copy, true, true );