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
25 * @group GenericArrayObject
27 * @licence GNU GPL v2+
28 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
30 abstract class GenericArrayObjectTest
extends MediaWikiTestCase
{
33 * Returns objects that can serve as elements in the concrete GenericArrayObject deriving class being tested.
39 abstract public function elementInstancesProvider();
42 * Returns the name of the concrete class being tested.
48 abstract public function getInstanceClass();
51 * Provides instances of the concrete class being tested.
57 public function instanceProvider() {
60 foreach ( $this->elementInstancesProvider() as $elementInstances ) {
61 $instances[] = $this->getNew( $elementInstances[0] );
64 return $this->arrayWrap( $instances );
70 * @param array $elements
72 * @return GenericArrayObject
74 protected function getNew( array $elements = array() ) {
75 $class = $this->getInstanceClass();
77 return new $class( $elements );
81 * @dataProvider elementInstancesProvider
85 * @param array $elements
87 public function testConstructor( array $elements ) {
88 $arrayObject = $this->getNew( $elements );
90 $this->assertEquals( count( $elements ), $arrayObject->count() );
94 * @dataProvider elementInstancesProvider
98 * @param array $elements
100 public function testIsEmpty( array $elements ) {
101 $arrayObject = $this->getNew( $elements );
103 $this->assertEquals( $elements === array(), $arrayObject->isEmpty() );
107 * @dataProvider instanceProvider
111 * @param GenericArrayObject $list
113 public function testUnset( GenericArrayObject
$list ) {
114 if ( $list->isEmpty() ) {
115 $this->assertTrue( true ); // We cannot test unset if there are no elements
117 $offset = $list->getIterator()->key();
118 $count = $list->count();
119 $list->offsetUnset( $offset );
120 $this->assertEquals( $count - 1, $list->count() );
123 if ( !$list->isEmpty() ) {
124 $offset = $list->getIterator()->key();
125 $count = $list->count();
126 unset( $list[$offset] );
127 $this->assertEquals( $count - 1, $list->count() );
132 * @dataProvider elementInstancesProvider
136 * @param array $elements
138 public function testAppend( array $elements ) {
139 $list = $this->getNew();
141 $listSize = count( $elements );
143 foreach ( $elements as $element ) {
144 $list->append( $element );
147 $this->assertEquals( $listSize, $list->count() );
149 $list = $this->getNew();
151 foreach ( $elements as $element ) {
155 $this->assertEquals( $listSize, $list->count() );
157 $this->checkTypeChecks( function ( GenericArrayObject
$list, $element ) {
158 $list->append( $element );
165 * @param callback $function
167 protected function checkTypeChecks( $function ) {
169 $list = $this->getNew();
171 $elementClass = $list->getObjectType();
173 foreach ( array( 42, 'foo', array(), new stdClass(), 4.2 ) as $element ) {
174 $validValid = $element instanceof $elementClass;
177 call_user_func( $function, $list, $element );
179 } catch ( InvalidArgumentException
$exception ) {
186 'Object of invalid type got successfully added to a GenericArrayObject'
192 * @dataProvider elementInstancesProvider
196 * @param array $elements
198 public function testOffsetSet( array $elements ) {
199 if ( $elements === array() ) {
200 $this->assertTrue( true );
205 $list = $this->getNew();
207 $element = reset( $elements );
208 $list->offsetSet( 42, $element );
209 $this->assertEquals( $element, $list->offsetGet( 42 ) );
211 $list = $this->getNew();
213 $element = reset( $elements );
214 $list['oHai'] = $element;
215 $this->assertEquals( $element, $list['oHai'] );
217 $list = $this->getNew();
219 $element = reset( $elements );
220 $list->offsetSet( 9001, $element );
221 $this->assertEquals( $element, $list[9001] );
223 $list = $this->getNew();
225 $element = reset( $elements );
226 $list->offsetSet( null, $element );
227 $this->assertEquals( $element, $list[0] );
229 $list = $this->getNew();
232 foreach ( $elements as $element ) {
233 $list->offsetSet( null, $element );
234 $this->assertEquals( $element, $list[$offset++
] );
237 $this->assertEquals( count( $elements ), $list->count() );
239 $this->checkTypeChecks( function ( GenericArrayObject
$list, $element ) {
240 $list->offsetSet( mt_rand(), $element );
245 * @dataProvider instanceProvider
249 * @param GenericArrayObject $list
251 public function testSerialization( GenericArrayObject
$list ) {
252 $serialization = serialize( $list );
253 $copy = unserialize( $serialization );
255 $this->assertEquals( $serialization, serialize( $copy ) );
256 $this->assertEquals( count( $list ), count( $copy ) );
258 $list = $list->getArrayCopy();
259 $copy = $copy->getArrayCopy();
261 $this->assertArrayEquals( $list, $copy, true, true );