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 * Returns the name of the concrete class being tested.
48 public abstract 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();
76 return new $class( $elements );
80 * @dataProvider elementInstancesProvider
84 * @param array $elements
86 public function testConstructor( array $elements ) {
87 $arrayObject = $this->getNew( $elements );
89 $this->assertEquals( count( $elements ), $arrayObject->count() );
93 * @dataProvider elementInstancesProvider
97 * @param array $elements
99 public function testIsEmpty( array $elements ) {
100 $arrayObject = $this->getNew( $elements );
102 $this->assertEquals( $elements === array(), $arrayObject->isEmpty() );
106 * @dataProvider instanceProvider
110 * @param GenericArrayObject $list
112 public function testUnset( GenericArrayObject
$list ) {
113 if ( !$list->isEmpty() ) {
114 $offset = $list->getIterator()->key();
115 $count = $list->count();
116 $list->offsetUnset( $offset );
117 $this->assertEquals( $count - 1, $list->count() );
120 if ( !$list->isEmpty() ) {
121 $offset = $list->getIterator()->key();
122 $count = $list->count();
123 unset( $list[$offset] );
124 $this->assertEquals( $count - 1, $list->count() );
128 try { $list->offsetUnset( 'sdfsedtgsrdysftu' ); } catch ( \Exception
$exception ){}
129 $this->assertInstanceOf( '\Exception', $exception );
133 * @dataProvider elementInstancesProvider
137 * @param array $elements
139 public function testAppend( array $elements ) {
140 $list = $this->getNew();
142 $listSize = count( $elements );
144 foreach ( $elements as $element ) {
145 $list->append( $element );
148 $this->assertEquals( $listSize, $list->count() );
150 $list = $this->getNew();
152 foreach ( $elements as $element ) {
156 $this->assertEquals( $listSize, $list->count() );
158 $this->checkTypeChecks( function( GenericArrayObject
$list, $element ) {
159 $list->append( $element );
166 * @param callback $function
168 protected function checkTypeChecks( $function ) {
170 $list = $this->getNew();
172 $elementClass = $list->getObjectType();
174 foreach ( array( 42, 'foo', array(), new \
stdClass(), 4.2 ) as $element ) {
175 $validValid = $element instanceof $elementClass;
178 call_user_func( $function, $list, $element );
181 catch ( InvalidArgumentException
$exception ) {
188 'Object of invalid type got successfully added to a GenericArrayObject'
194 * @dataProvider elementInstancesProvider
198 * @param array $elements
200 public function testOffsetSet( array $elements ) {
201 if ( $elements === array() ) {
202 $this->assertTrue( true );
206 $list = $this->getNew();
208 $element = reset( $elements );
209 $list->offsetSet( 42, $element );
210 $this->assertEquals( $element, $list->offsetGet( 42 ) );
212 $list = $this->getNew();
214 $element = reset( $elements );
215 $list['oHai'] = $element;
216 $this->assertEquals( $element, $list['oHai'] );
218 $list = $this->getNew();
220 $element = reset( $elements );
221 $list->offsetSet( 9001, $element );
222 $this->assertEquals( $element, $list[9001] );
224 $list = $this->getNew();
226 $element = reset( $elements );
227 $list->offsetSet( null, $element );
228 $this->assertEquals( $element, $list[0] );
230 $list = $this->getNew();
233 foreach ( $elements as $element ) {
234 $list->offsetSet( null, $element );
235 $this->assertEquals( $element, $list[$offset++
] );
238 $this->assertEquals( count( $elements ), $list->count() );
240 $this->checkTypeChecks( function( GenericArrayObject
$list, $element ) {
241 $list->offsetSet( mt_rand(), $element );