5e4cda1eead06277fb3565f5ddf7fbe5a8a3a22a
5 * Tests for the GenericArrayObject and deriving classes.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
26 * @group GenericArrayObject
28 * @licence GNU GPL v2+
29 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
31 abstract class GenericArrayObjectTest
extends MediaWikiTestCase
{
34 * Returns objects that can serve as elements in the concrete GenericArrayObject deriving class being tested.
40 public abstract function elementInstancesProvider();
43 * Provides instances of the concrete class being tested.
49 public abstract function instanceProvider();
52 * Returns the name of the concrete class being tested.
58 public abstract function getInstanceClass();
63 * @param array $elements
65 * @return GenericArrayObject
67 protected function getNew( array $elements = array() ) {
68 $class = $this->getInstanceClass();
69 return new $class( $elements );
73 * @dataProvider elementInstancesProvider
77 * @param array $elements
79 public function testConstructor( array $elements ) {
80 $arrayObject = $this->getNew( $elements );
82 $this->assertEquals( count( $elements ), $arrayObject->count() );
86 * @dataProvider elementInstancesProvider
90 * @param array $elements
92 public function testIsEmpty( array $elements ) {
93 $arrayObject = $this->getNew( $elements );
95 $this->assertEquals( $elements === array(), $arrayObject->isEmpty() );
99 * @dataProvider instanceProvider
103 * @param GenericArrayObject $list
105 public function testUnset( GenericArrayObject
$list ) {
106 if ( !$list->isEmpty() ) {
107 $offset = $list->getIterator()->key();
108 $count = $list->count();
109 $list->offsetUnset( $offset );
110 $this->assertEquals( $count - 1, $list->count() );
113 if ( !$list->isEmpty() ) {
114 $offset = $list->getIterator()->key();
115 $count = $list->count();
116 unset( $list[$offset] );
117 $this->assertEquals( $count - 1, $list->count() );
121 try { $list->offsetUnset( 'sdfsedtgsrdysftu' ); } catch ( \Exception
$exception ){}
122 $this->assertInstanceOf( '\Exception', $exception );
126 * @dataProvider elementInstancesProvider
130 * @param array $elements
132 public function testAppend( array $elements ) {
133 $list = $this->getNew();
135 $listSize = count( $elements );
137 foreach ( $elements as $element ) {
138 $list->append( $element );
141 $this->assertEquals( $listSize, $list->count() );
143 $list = $this->getNew();
145 foreach ( $elements as $element ) {
149 $this->assertEquals( $listSize, $list->count() );
151 $this->checkTypeChecks( function( GenericArrayObject
$list, $element ) {
152 $list->append( $element );
159 * @param callback $function
161 protected function checkTypeChecks( $function ) {
163 $list = $this->getNew();
165 $elementClass = $list->getObjectType();
167 foreach ( array( 42, 'foo', array(), new \
stdClass(), 4.2 ) as $element ) {
168 $validValid = $element instanceof $elementClass;
171 call_user_func( $function, $list, $element );
174 catch ( \MWException
$exception ) {
181 'Object of invalid type got successfully added to a GenericArrayObject'
187 * @dataProvider elementInstancesProvider
191 * @param array $elements
193 public function testOffsetSet( array $elements ) {
194 if ( $elements === array() ) {
195 $this->assertTrue( true );
199 $list = $this->getNew();
201 $element = reset( $elements );
202 $list->offsetSet( 42, $element );
203 $this->assertEquals( $element, $list->offsetGet( 42 ) );
205 $list = $this->getNew();
207 $element = reset( $elements );
208 $list['oHai'] = $element;
209 $this->assertEquals( $element, $list['oHai'] );
211 $list = $this->getNew();
213 $element = reset( $elements );
214 $list->offsetSet( 9001, $element );
215 $this->assertEquals( $element, $list[9001] );
217 $list = $this->getNew();
219 $element = reset( $elements );
220 $list->offsetSet( null, $element );
221 $this->assertEquals( $element, $list[0] );
223 $list = $this->getNew();
226 foreach ( $elements as $element ) {
227 $list->offsetSet( null, $element );
228 $this->assertEquals( $element, $list[$offset++
] );
231 $this->assertEquals( count( $elements ), $list->count() );
233 $this->checkTypeChecks( function( GenericArrayObject
$list, $element ) {
234 $list->offsetSet( mt_rand(), $element );