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 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
29 abstract class GenericArrayObjectTest
extends PHPUnit\Framework\TestCase
{
31 use MediaWikiCoversValidator
;
34 * Returns objects that can serve as elements in the concrete
35 * GenericArrayObject deriving class being tested.
41 abstract public function elementInstancesProvider();
44 * Returns the name of the concrete class being tested.
50 abstract public function getInstanceClass();
53 * Provides instances of the concrete class being tested.
59 public function instanceProvider() {
62 foreach ( $this->elementInstancesProvider() as $elementInstances ) {
63 $instances[] = $this->getNew( $elementInstances[0] );
66 return $this->arrayWrap( $instances );
72 * @param array $elements
74 * @return GenericArrayObject
76 protected function getNew( array $elements = [] ) {
77 $class = $this->getInstanceClass();
79 return new $class( $elements );
83 * @dataProvider elementInstancesProvider
87 * @param array $elements
89 * @covers GenericArrayObject::__construct
91 public function testConstructor( array $elements ) {
92 $arrayObject = $this->getNew( $elements );
94 $this->assertEquals( count( $elements ), $arrayObject->count() );
98 * @dataProvider elementInstancesProvider
102 * @param array $elements
104 * @covers GenericArrayObject::isEmpty
106 public function testIsEmpty( array $elements ) {
107 $arrayObject = $this->getNew( $elements );
109 $this->assertEquals( $elements === [], $arrayObject->isEmpty() );
113 * @dataProvider instanceProvider
117 * @param GenericArrayObject $list
119 * @covers GenericArrayObject::offsetUnset
121 public function testUnset( GenericArrayObject
$list ) {
122 if ( $list->isEmpty() ) {
123 $this->assertTrue( true ); // We cannot test unset if there are no elements
125 $offset = $list->getIterator()->key();
126 $count = $list->count();
127 $list->offsetUnset( $offset );
128 $this->assertEquals( $count - 1, $list->count() );
131 if ( !$list->isEmpty() ) {
132 $offset = $list->getIterator()->key();
133 $count = $list->count();
134 unset( $list[$offset] );
135 $this->assertEquals( $count - 1, $list->count() );
140 * @dataProvider elementInstancesProvider
144 * @param array $elements
146 * @covers GenericArrayObject::append
148 public function testAppend( array $elements ) {
149 $list = $this->getNew();
151 $listSize = count( $elements );
153 foreach ( $elements as $element ) {
154 $list->append( $element );
157 $this->assertEquals( $listSize, $list->count() );
159 $list = $this->getNew();
161 foreach ( $elements as $element ) {
165 $this->assertEquals( $listSize, $list->count() );
167 $this->checkTypeChecks( function ( GenericArrayObject
$list, $element ) {
168 $list->append( $element );
175 * @param callable $function
177 protected function checkTypeChecks( $function ) {
179 $list = $this->getNew();
181 $elementClass = $list->getObjectType();
183 foreach ( [ 42, 'foo', [], new stdClass(), 4.2 ] as $element ) {
184 $validValid = $element instanceof $elementClass;
187 call_user_func( $function, $list, $element );
189 } catch ( InvalidArgumentException
$exception ) {
196 'Object of invalid type got successfully added to a GenericArrayObject'
202 * @dataProvider elementInstancesProvider
206 * @param array $elements
207 * @covers GenericArrayObject::getObjectType
208 * @covers GenericArrayObject::offsetSet
210 public function testOffsetSet( array $elements ) {
211 if ( $elements === [] ) {
212 $this->assertTrue( true );
217 $list = $this->getNew();
219 $element = reset( $elements );
220 $list->offsetSet( 42, $element );
221 $this->assertEquals( $element, $list->offsetGet( 42 ) );
223 $list = $this->getNew();
225 $element = reset( $elements );
226 $list['oHai'] = $element;
227 $this->assertEquals( $element, $list['oHai'] );
229 $list = $this->getNew();
231 $element = reset( $elements );
232 $list->offsetSet( 9001, $element );
233 $this->assertEquals( $element, $list[9001] );
235 $list = $this->getNew();
237 $element = reset( $elements );
238 $list->offsetSet( null, $element );
239 $this->assertEquals( $element, $list[0] );
241 $list = $this->getNew();
244 foreach ( $elements as $element ) {
245 $list->offsetSet( null, $element );
246 $this->assertEquals( $element, $list[$offset++
] );
249 $this->assertEquals( count( $elements ), $list->count() );
251 $this->checkTypeChecks( function ( GenericArrayObject
$list, $element ) {
252 $list->offsetSet( mt_rand(), $element );
257 * @dataProvider instanceProvider
261 * @param GenericArrayObject $list
263 * @covers GenericArrayObject::getSerializationData
264 * @covers GenericArrayObject::serialize
265 * @covers GenericArrayObject::unserialize
267 public function testSerialization( GenericArrayObject
$list ) {
268 $serialization = serialize( $list );
269 $copy = unserialize( $serialization );
271 $this->assertEquals( $serialization, serialize( $copy ) );
272 $this->assertEquals( count( $list ), count( $copy ) );
274 $list = $list->getArrayCopy();
275 $copy = $copy->getArrayCopy();
277 $this->assertArrayEquals( $list, $copy, true, true );