MediaWiki  1.23.6
GenericArrayObjectTest.php
Go to the documentation of this file.
1 <?php
2 
30 abstract class GenericArrayObjectTest extends MediaWikiTestCase {
31 
39  abstract public function elementInstancesProvider();
40 
48  abstract public function getInstanceClass();
49 
57  public function instanceProvider() {
58  $instances = array();
59 
60  foreach ( $this->elementInstancesProvider() as $elementInstances ) {
61  $instances[] = $this->getNew( $elementInstances[0] );
62  }
63 
64  return $this->arrayWrap( $instances );
65  }
66 
74  protected function getNew( array $elements = array() ) {
75  $class = $this->getInstanceClass();
76 
77  return new $class( $elements );
78  }
79 
89  public function testConstructor( array $elements ) {
90  $arrayObject = $this->getNew( $elements );
91 
92  $this->assertEquals( count( $elements ), $arrayObject->count() );
93  }
94 
104  public function testIsEmpty( array $elements ) {
105  $arrayObject = $this->getNew( $elements );
106 
107  $this->assertEquals( $elements === array(), $arrayObject->isEmpty() );
108  }
109 
119  public function testUnset( GenericArrayObject $list ) {
120  if ( $list->isEmpty() ) {
121  $this->assertTrue( true ); // We cannot test unset if there are no elements
122  } else {
123  $offset = $list->getIterator()->key();
124  $count = $list->count();
125  $list->offsetUnset( $offset );
126  $this->assertEquals( $count - 1, $list->count() );
127  }
128 
129  if ( !$list->isEmpty() ) {
130  $offset = $list->getIterator()->key();
131  $count = $list->count();
132  unset( $list[$offset] );
133  $this->assertEquals( $count - 1, $list->count() );
134  }
135  }
136 
146  public function testAppend( array $elements ) {
147  $list = $this->getNew();
148 
149  $listSize = count( $elements );
150 
151  foreach ( $elements as $element ) {
152  $list->append( $element );
153  }
154 
155  $this->assertEquals( $listSize, $list->count() );
156 
157  $list = $this->getNew();
158 
159  foreach ( $elements as $element ) {
160  $list[] = $element;
161  }
162 
163  $this->assertEquals( $listSize, $list->count() );
164 
165  $this->checkTypeChecks( function ( GenericArrayObject $list, $element ) {
166  $list->append( $element );
167  } );
168  }
169 
177  protected function checkTypeChecks( $function ) {
178  $excption = null;
179  $list = $this->getNew();
180 
181  $elementClass = $list->getObjectType();
182 
183  foreach ( array( 42, 'foo', array(), new stdClass(), 4.2 ) as $element ) {
184  $validValid = $element instanceof $elementClass;
185 
186  try {
187  call_user_func( $function, $list, $element );
188  $valid = true;
189  } catch ( InvalidArgumentException $exception ) {
190  $valid = false;
191  }
192 
193  $this->assertEquals(
194  $validValid,
195  $valid,
196  'Object of invalid type got successfully added to a GenericArrayObject'
197  );
198  }
199  }
200 
210  public function testOffsetSet( array $elements ) {
211  if ( $elements === array() ) {
212  $this->assertTrue( true );
213 
214  return;
215  }
216 
217  $list = $this->getNew();
218 
219  $element = reset( $elements );
220  $list->offsetSet( 42, $element );
221  $this->assertEquals( $element, $list->offsetGet( 42 ) );
222 
223  $list = $this->getNew();
224 
225  $element = reset( $elements );
226  $list['oHai'] = $element;
227  $this->assertEquals( $element, $list['oHai'] );
228 
229  $list = $this->getNew();
230 
231  $element = reset( $elements );
232  $list->offsetSet( 9001, $element );
233  $this->assertEquals( $element, $list[9001] );
234 
235  $list = $this->getNew();
236 
237  $element = reset( $elements );
238  $list->offsetSet( null, $element );
239  $this->assertEquals( $element, $list[0] );
240 
241  $list = $this->getNew();
242  $offset = 0;
243 
244  foreach ( $elements as $element ) {
245  $list->offsetSet( null, $element );
246  $this->assertEquals( $element, $list[$offset++] );
247  }
248 
249  $this->assertEquals( count( $elements ), $list->count() );
250 
251  $this->checkTypeChecks( function ( GenericArrayObject $list, $element ) {
252  $list->offsetSet( mt_rand(), $element );
253  } );
254  }
255 
267  public function testSerialization( GenericArrayObject $list ) {
268  $serialization = serialize( $list );
269  $copy = unserialize( $serialization );
270 
271  $this->assertEquals( $serialization, serialize( $copy ) );
272  $this->assertEquals( count( $list ), count( $copy ) );
273 
274  $list = $list->getArrayCopy();
275  $copy = $copy->getArrayCopy();
276 
277  $this->assertArrayEquals( $list, $copy, true, true );
278  }
279 }
MediaWikiTestCase\assertArrayEquals
assertArrayEquals(array $expected, array $actual, $ordered=false, $named=false)
Assert that two arrays are equal.
Definition: MediaWikiTestCase.php:764
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
GenericArrayObjectTest\testUnset
testUnset(GenericArrayObject $list)
@dataProvider instanceProvider
Definition: GenericArrayObjectTest.php:119
GenericArrayObjectTest\testSerialization
testSerialization(GenericArrayObject $list)
@dataProvider instanceProvider
Definition: GenericArrayObjectTest.php:267
GenericArrayObject
Definition: GenericArrayObject.php:35
GenericArrayObjectTest\checkTypeChecks
checkTypeChecks( $function)
Definition: GenericArrayObjectTest.php:177
GenericArrayObject\isEmpty
isEmpty()
Returns if the ArrayObject has no elements.
Definition: GenericArrayObject.php:236
GenericArrayObjectTest
Definition: GenericArrayObjectTest.php:30
GenericArrayObjectTest\elementInstancesProvider
elementInstancesProvider()
Returns objects that can serve as elements in the concrete GenericArrayObject deriving class being te...
GenericArrayObjectTest\testIsEmpty
testIsEmpty(array $elements)
@dataProvider elementInstancesProvider
Definition: GenericArrayObjectTest.php:104
GenericArrayObject\offsetSet
offsetSet( $index, $value)
Definition: GenericArrayObject.php:108
MediaWikiTestCase
Definition: MediaWikiTestCase.php:6
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
MediaWikiTestCase\arrayWrap
arrayWrap(array $elements)
Utility method taking an array of elements and wrapping each element in it's own array.
Definition: MediaWikiTestCase.php:743
GenericArrayObjectTest\getNew
getNew(array $elements=array())
Definition: GenericArrayObjectTest.php:74
GenericArrayObjectTest\testOffsetSet
testOffsetSet(array $elements)
@dataProvider elementInstancesProvider
Definition: GenericArrayObjectTest.php:210
GenericArrayObjectTest\testConstructor
testConstructor(array $elements)
@dataProvider elementInstancesProvider
Definition: GenericArrayObjectTest.php:89
$count
$count
Definition: UtfNormalTest2.php:96
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
GenericArrayObjectTest\getInstanceClass
getInstanceClass()
Returns the name of the concrete class being tested.
GenericArrayObjectTest\testAppend
testAppend(array $elements)
@dataProvider elementInstancesProvider
Definition: GenericArrayObjectTest.php:146
GenericArrayObjectTest\instanceProvider
instanceProvider()
Provides instances of the concrete class being tested.
Definition: GenericArrayObjectTest.php:57
GenericArrayObject\append
append( $value)
Definition: GenericArrayObject.php:96