MediaWiki  REL1_22
GenericArrayObjectTest.php
Go to the documentation of this file.
00001 <?php
00002 
00030 abstract class GenericArrayObjectTest extends MediaWikiTestCase {
00031 
00039     abstract public function elementInstancesProvider();
00040 
00048     abstract public function getInstanceClass();
00049 
00057     public function instanceProvider() {
00058         $instances = array();
00059 
00060         foreach ( $this->elementInstancesProvider() as $elementInstances ) {
00061             $instances[] = $this->getNew( $elementInstances[0] );
00062         }
00063 
00064         return $this->arrayWrap( $instances );
00065     }
00066 
00074     protected function getNew( array $elements = array() ) {
00075         $class = $this->getInstanceClass();
00076 
00077         return new $class( $elements );
00078     }
00079 
00087     public function testConstructor( array $elements ) {
00088         $arrayObject = $this->getNew( $elements );
00089 
00090         $this->assertEquals( count( $elements ), $arrayObject->count() );
00091     }
00092 
00100     public function testIsEmpty( array $elements ) {
00101         $arrayObject = $this->getNew( $elements );
00102 
00103         $this->assertEquals( $elements === array(), $arrayObject->isEmpty() );
00104     }
00105 
00113     public function testUnset( GenericArrayObject $list ) {
00114         if ( $list->isEmpty() ) {
00115             $this->assertTrue( true ); // We cannot test unset if there are no elements
00116         } else {
00117             $offset = $list->getIterator()->key();
00118             $count = $list->count();
00119             $list->offsetUnset( $offset );
00120             $this->assertEquals( $count - 1, $list->count() );
00121         }
00122 
00123         if ( !$list->isEmpty() ) {
00124             $offset = $list->getIterator()->key();
00125             $count = $list->count();
00126             unset( $list[$offset] );
00127             $this->assertEquals( $count - 1, $list->count() );
00128         }
00129     }
00130 
00138     public function testAppend( array $elements ) {
00139         $list = $this->getNew();
00140 
00141         $listSize = count( $elements );
00142 
00143         foreach ( $elements as $element ) {
00144             $list->append( $element );
00145         }
00146 
00147         $this->assertEquals( $listSize, $list->count() );
00148 
00149         $list = $this->getNew();
00150 
00151         foreach ( $elements as $element ) {
00152             $list[] = $element;
00153         }
00154 
00155         $this->assertEquals( $listSize, $list->count() );
00156 
00157         $this->checkTypeChecks( function ( GenericArrayObject $list, $element ) {
00158             $list->append( $element );
00159         } );
00160     }
00161 
00167     protected function checkTypeChecks( $function ) {
00168         $excption = null;
00169         $list = $this->getNew();
00170 
00171         $elementClass = $list->getObjectType();
00172 
00173         foreach ( array( 42, 'foo', array(), new stdClass(), 4.2 ) as $element ) {
00174             $validValid = $element instanceof $elementClass;
00175 
00176             try {
00177                 call_user_func( $function, $list, $element );
00178                 $valid = true;
00179             } catch ( InvalidArgumentException $exception ) {
00180                 $valid = false;
00181             }
00182 
00183             $this->assertEquals(
00184                 $validValid,
00185                 $valid,
00186                 'Object of invalid type got successfully added to a GenericArrayObject'
00187             );
00188         }
00189     }
00190 
00198     public function testOffsetSet( array $elements ) {
00199         if ( $elements === array() ) {
00200             $this->assertTrue( true );
00201 
00202             return;
00203         }
00204 
00205         $list = $this->getNew();
00206 
00207         $element = reset( $elements );
00208         $list->offsetSet( 42, $element );
00209         $this->assertEquals( $element, $list->offsetGet( 42 ) );
00210 
00211         $list = $this->getNew();
00212 
00213         $element = reset( $elements );
00214         $list['oHai'] = $element;
00215         $this->assertEquals( $element, $list['oHai'] );
00216 
00217         $list = $this->getNew();
00218 
00219         $element = reset( $elements );
00220         $list->offsetSet( 9001, $element );
00221         $this->assertEquals( $element, $list[9001] );
00222 
00223         $list = $this->getNew();
00224 
00225         $element = reset( $elements );
00226         $list->offsetSet( null, $element );
00227         $this->assertEquals( $element, $list[0] );
00228 
00229         $list = $this->getNew();
00230         $offset = 0;
00231 
00232         foreach ( $elements as $element ) {
00233             $list->offsetSet( null, $element );
00234             $this->assertEquals( $element, $list[$offset++] );
00235         }
00236 
00237         $this->assertEquals( count( $elements ), $list->count() );
00238 
00239         $this->checkTypeChecks( function ( GenericArrayObject $list, $element ) {
00240             $list->offsetSet( mt_rand(), $element );
00241         } );
00242     }
00243 
00251     public function testSerialization( GenericArrayObject $list ) {
00252         $serialization = serialize( $list );
00253         $copy = unserialize( $serialization );
00254 
00255         $this->assertEquals( $serialization, serialize( $copy ) );
00256         $this->assertEquals( count( $list ), count( $copy ) );
00257 
00258         $list = $list->getArrayCopy();
00259         $copy = $copy->getArrayCopy();
00260 
00261         $this->assertArrayEquals( $list, $copy, true, true );
00262     }
00263 }