MediaWiki  1.33.0
GenericArrayObjectTest.php
Go to the documentation of this file.
1 <?php
2 
29 abstract class GenericArrayObjectTest extends PHPUnit\Framework\TestCase {
30 
31  use MediaWikiCoversValidator;
32 
41  abstract public function elementInstancesProvider();
42 
50  abstract public function getInstanceClass();
51 
59  public function instanceProvider() {
60  $instances = [];
61 
62  foreach ( $this->elementInstancesProvider() as $elementInstances ) {
63  $instances[] = $this->getNew( $elementInstances[0] );
64  }
65 
66  return $this->arrayWrap( $instances );
67  }
68 
76  protected function getNew( array $elements = [] ) {
77  $class = $this->getInstanceClass();
78 
79  return new $class( $elements );
80  }
81 
91  public function testConstructor( array $elements ) {
92  $arrayObject = $this->getNew( $elements );
93 
94  $this->assertEquals( count( $elements ), $arrayObject->count() );
95  }
96 
106  public function testIsEmpty( array $elements ) {
107  $arrayObject = $this->getNew( $elements );
108 
109  $this->assertEquals( $elements === [], $arrayObject->isEmpty() );
110  }
111 
121  public function testUnset( GenericArrayObject $list ) {
122  if ( $list->isEmpty() ) {
123  $this->assertTrue( true ); // We cannot test unset if there are no elements
124  } else {
125  $offset = $list->getIterator()->key();
126  $count = $list->count();
127  $list->offsetUnset( $offset );
128  $this->assertEquals( $count - 1, $list->count() );
129  }
130 
131  if ( !$list->isEmpty() ) {
132  $offset = $list->getIterator()->key();
133  $count = $list->count();
134  unset( $list[$offset] );
135  $this->assertEquals( $count - 1, $list->count() );
136  }
137  }
138 
148  public function testAppend( array $elements ) {
149  $list = $this->getNew();
150 
151  $listSize = count( $elements );
152 
153  foreach ( $elements as $element ) {
154  $list->append( $element );
155  }
156 
157  $this->assertEquals( $listSize, $list->count() );
158 
159  $list = $this->getNew();
160 
161  foreach ( $elements as $element ) {
162  $list[] = $element;
163  }
164 
165  $this->assertEquals( $listSize, $list->count() );
166 
167  $this->checkTypeChecks( function ( GenericArrayObject $list, $element ) {
168  $list->append( $element );
169  } );
170  }
171 
177  protected function checkTypeChecks( $function ) {
178  $excption = null;
179  $list = $this->getNew();
180 
181  $elementClass = $list->getObjectType();
182 
183  foreach ( [ 42, 'foo', [], 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 === [] ) {
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 }
GenericArrayObjectTest\testUnset
testUnset(GenericArrayObject $list)
instanceProvider
Definition: GenericArrayObjectTest.php:121
GenericArrayObjectTest\testSerialization
testSerialization(GenericArrayObject $list)
instanceProvider
Definition: GenericArrayObjectTest.php:267
captcha-old.count
count
Definition: captcha-old.py:249
GenericArrayObject
Definition: GenericArrayObject.php:35
serialize
serialize()
Definition: ApiMessageTrait.php:134
GenericArrayObjectTest\checkTypeChecks
checkTypeChecks( $function)
Definition: GenericArrayObjectTest.php:177
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
GenericArrayObject\isEmpty
isEmpty()
Returns if the ArrayObject has no elements.
Definition: GenericArrayObject.php:236
GenericArrayObjectTest
Definition: GenericArrayObjectTest.php:29
GenericArrayObjectTest\elementInstancesProvider
elementInstancesProvider()
Returns objects that can serve as elements in the concrete GenericArrayObject deriving class being te...
GenericArrayObjectTest\testIsEmpty
testIsEmpty(array $elements)
elementInstancesProvider
Definition: GenericArrayObjectTest.php:106
GenericArrayObject\offsetSet
offsetSet( $index, $value)
Definition: GenericArrayObject.php:107
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
GenericArrayObjectTest\testOffsetSet
testOffsetSet(array $elements)
elementInstancesProvider
Definition: GenericArrayObjectTest.php:210
GenericArrayObjectTest\getNew
getNew(array $elements=[])
Definition: GenericArrayObjectTest.php:76
GenericArrayObjectTest\testConstructor
testConstructor(array $elements)
elementInstancesProvider
Definition: GenericArrayObjectTest.php:91
unserialize
unserialize( $serialized)
Definition: ApiMessageTrait.php:142
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)
elementInstancesProvider
Definition: GenericArrayObjectTest.php:148
GenericArrayObjectTest\instanceProvider
instanceProvider()
Provides instances of the concrete class being tested.
Definition: GenericArrayObjectTest.php:59
GenericArrayObject\append
append( $value)
Definition: GenericArrayObject.php:95