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 public abstract function elementInstancesProvider();
42 * Provides instances of the concrete class being tested.
48 public abstract function instanceProvider();
51 * Returns the name of the concrete class being tested.
57 public abstract function getInstanceClass();
62 * @param array $elements
64 * @return GenericArrayObject
66 protected function getNew( array $elements = array() ) {
67 $class = $this->getInstanceClass();
68 return new $class( $elements );
72 * @dataProvider elementInstancesProvider
76 * @param array $elements
78 public function testConstructor( array $elements ) {
79 $arrayObject = $this->getNew( $elements );
81 $this->assertEquals( count( $elements ), $arrayObject->count() );
85 * @dataProvider elementInstancesProvider
89 * @param array $elements
91 public function testIsEmpty( array $elements ) {
92 $arrayObject = $this->getNew( $elements );
94 $this->assertEquals( $elements === array(), $arrayObject->isEmpty() );
98 * @dataProvider instanceProvider
102 * @param GenericArrayObject $list
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() );
112 if ( !$list->isEmpty() ) {
113 $offset = $list->getIterator()->key();
114 $count = $list->count();
115 unset( $list[$offset] );
116 $this->assertEquals( $count - 1, $list->count() );
120 try { $list->offsetUnset( 'sdfsedtgsrdysftu' ); } catch ( \Exception
$exception ){}
121 $this->assertInstanceOf( '\Exception', $exception );
125 * @dataProvider elementInstancesProvider
129 * @param array $elements
131 public function testAppend( array $elements ) {
132 $list = $this->getNew();
134 $listSize = count( $elements );
136 foreach ( $elements as $element ) {
137 $list->append( $element );
140 $this->assertEquals( $listSize, $list->count() );
142 $list = $this->getNew();
144 foreach ( $elements as $element ) {
148 $this->assertEquals( $listSize, $list->count() );
150 $this->checkTypeChecks( function( GenericArrayObject
$list, $element ) {
151 $list->append( $element );
158 * @param callback $function
160 protected function checkTypeChecks( $function ) {
162 $list = $this->getNew();
164 $elementClass = $list->getObjectType();
166 foreach ( array( 42, 'foo', array(), new \
stdClass(), 4.2 ) as $element ) {
167 $validValid = $element instanceof $elementClass;
170 call_user_func( $function, $list, $element );
173 catch ( InvalidArgumentException
$exception ) {
180 'Object of invalid type got successfully added to a GenericArrayObject'
186 * @dataProvider elementInstancesProvider
190 * @param array $elements
192 public function testOffsetSet( array $elements ) {
193 if ( $elements === array() ) {
194 $this->assertTrue( true );
198 $list = $this->getNew();
200 $element = reset( $elements );
201 $list->offsetSet( 42, $element );
202 $this->assertEquals( $element, $list->offsetGet( 42 ) );
204 $list = $this->getNew();
206 $element = reset( $elements );
207 $list['oHai'] = $element;
208 $this->assertEquals( $element, $list['oHai'] );
210 $list = $this->getNew();
212 $element = reset( $elements );
213 $list->offsetSet( 9001, $element );
214 $this->assertEquals( $element, $list[9001] );
216 $list = $this->getNew();
218 $element = reset( $elements );
219 $list->offsetSet( null, $element );
220 $this->assertEquals( $element, $list[0] );
222 $list = $this->getNew();
225 foreach ( $elements as $element ) {
226 $list->offsetSet( null, $element );
227 $this->assertEquals( $element, $list[$offset++
] );
230 $this->assertEquals( count( $elements ), $list->count() );
232 $this->checkTypeChecks( function( GenericArrayObject
$list, $element ) {
233 $list->offsetSet( mt_rand(), $element );