Passed
Push — currentLimits ( f9050f...3572e3 )
by no
08:17 queued 04:47
created

testArrayObjectNotConstructedFromObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests;
4
5
use ArrayObject;
6
use DataValues\StringValue;
7
use PHPUnit_Framework_TestCase;
8
use ReflectionClass;
9
use ReflectionMethod;
10
use stdClass;
11
use Wikibase\DataModel\ByPropertyIdArray;
12
use Wikibase\DataModel\Entity\PropertyId;
13
use Wikibase\DataModel\PropertyIdProvider;
14
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
15
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
16
use Wikibase\DataModel\Snak\PropertyValueSnak;
17
use Wikibase\DataModel\Snak\Snak;
18
use Wikibase\DataModel\Statement\Statement;
19
20
/**
21
 * @covers Wikibase\DataModel\ByPropertyIdArray
22
 *
23
 * @group Wikibase
24
 * @group WikibaseDataModel
25
 *
26
 * @license GPL-2.0+
27
 * @author Jeroen De Dauw < [email protected] >
28
 * @author H. Snater < [email protected] >
29
 */
30
class ByPropertyIdArrayTest extends PHPUnit_Framework_TestCase {
31
32
	public function testGivenNull_constructorAssumesEmptyArray() {
33
		$indexedArray = new ByPropertyIdArray( null );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
34
35
		$this->assertSame( 0, $indexedArray->count() );
36
	}
37
38
	public function testGivenNonTraversableObject_constructorDoesNotCastObjectToArray() {
39
		$object = new stdClass();
40
		$object->property = true;
41
42
		$this->setExpectedException( 'InvalidArgumentException' );
43
		new ByPropertyIdArray( $object );
0 ignored issues
show
Documentation introduced by
$object is of type object<stdClass>, but the function expects a array<integer,object<Wik...bject<Traversable>|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
44
	}
45
46
	public function testArrayObjectNotConstructedFromObject() {
47
		$statement1 = new Statement( new PropertyNoValueSnak( 1 ) );
48
		$statement1->setGuid( '1' );
49
		$statement2 = new Statement( new PropertyNoValueSnak( 2 ) );
50
		$statement2->setGuid( '2' );
51
52
		$object = new ArrayObject();
53
		$object->append( $statement1 );
54
55
		$byPropertyIdArray = new ByPropertyIdArray( $object );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
56
		// According to the documentation append() "cannot be called when the ArrayObject was
57
		// constructed from an object." This test makes sure it was not constructed from an object.
58
		$byPropertyIdArray->append( $statement2 );
59
60
		$this->assertCount( 2, $byPropertyIdArray );
61
	}
62
63
	/**
64
	 * Returns an accessible ReflectionMethod of ByPropertyIdArray.
65
	 *
66
	 * @param string $methodName
67
	 * @return ReflectionMethod
68
	 */
69
	protected static function getMethod( $methodName ) {
70
		$class = new ReflectionClass( 'Wikibase\DataModel\ByPropertyIdArray' );
71
		$method = $class->getMethod( $methodName );
72
		$method->setAccessible( true );
73
		return $method;
74
	}
75
76
	public function listProvider() {
77
		$lists = [];
78
79
		$snaks = [
80
			new PropertyNoValueSnak( new PropertyId( 'P42' ) ),
81
			new PropertySomeValueSnak( new PropertyId( 'P42' ) ),
82
			new PropertySomeValueSnak( new PropertyId( 'P10' ) ),
83
			new PropertyValueSnak( new PropertyId( 'P10' ), new StringValue( 'ohi' ) ),
84
			new PropertySomeValueSnak( new PropertyId( 'P1' ) ),
85
		];
86
87
		$lists[] = $snaks;
88
89
		$lists[] = array_map(
90
			function( Snak $snak ) {
91
				return new Statement( $snak );
92
			},
93
			$snaks
94
		);
95
96
		$argLists = [];
97
98
		foreach ( $lists as $list ) {
99
			$argLists[] = [ $list ];
100
		}
101
102
		return $argLists;
103
	}
104
105
	/**
106
	 * @return Statement[]
107
	 */
108
	protected function statementsProvider() {
109
		$snaks = [
110
			new PropertyNoValueSnak( new PropertyId( 'P1' ) ),
111
			new PropertySomeValueSnak( new PropertyId( 'P1' ) ),
112
			new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'a' ) ),
113
			new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'b' ) ),
114
			new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'c' ) ),
115
			new PropertySomeValueSnak( new PropertyId( 'P3' ) ),
116
		];
117
118
		return array_map(
119
			function( Snak $snak ) {
120
				return new Statement( $snak );
121
			},
122
			$snaks
123
		);
124
	}
125
126
	/**
127
	 * @dataProvider listProvider
128
	 * @param PropertyIdProvider[] $objects
129
	 */
130
	public function testGetIds( array $objects ) {
131
		$indexedArray = new ByPropertyIdArray( $objects );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
132
133
		$expected = [];
134
135
		foreach ( $objects as $object ) {
136
			$expected[] = $object->getPropertyId();
137
		}
138
139
		$expected = array_unique( $expected );
140
141
		$indexedArray->buildIndex();
142
143
		$this->assertEquals(
144
			array_values( $expected ),
145
			array_values( $indexedArray->getPropertyIds() )
146
		);
147
	}
148
149
	/**
150
	 * @dataProvider listProvider
151
	 * @param PropertyIdProvider[] $objects
152
	 */
153
	public function testGetById( array $objects ) {
154
		$indexedArray = new ByPropertyIdArray( $objects );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
155
156
		$ids = [];
157
158
		foreach ( $objects as $object ) {
159
			$ids[] = $object->getPropertyId();
160
		}
161
162
		$ids = array_unique( $ids );
163
164
		$indexedArray->buildIndex();
165
166
		$allObtainedObjects = [];
167
168
		foreach ( $ids as $id ) {
169
			foreach ( $indexedArray->getByPropertyId( $id ) as $obtainedObject ) {
170
				$allObtainedObjects[] = $obtainedObject;
171
				$this->assertEquals( $id, $obtainedObject->getPropertyId() );
172
			}
173
		}
174
175
		$this->assertEquals(
176
			array_values( $objects ),
177
			array_values( $allObtainedObjects )
178
		);
179
	}
180
181
	/**
182
	 * @dataProvider listProvider
183
	 * @param PropertyIdProvider[] $objects
184
	 */
185
	public function testRemoveObject( array $objects ) {
186
		$lastIndex = count( $objects ) - 1;
187
		$indexedArray = new ByPropertyIdArray( $objects );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
188
		$indexedArray->buildIndex();
189
190
		$removeObject = self::getMethod( 'removeObject' );
191
192
		$removeObject->invokeArgs( $indexedArray, [ $objects[0] ] );
193
		$removeObject->invokeArgs( $indexedArray, [ $objects[$lastIndex] ] );
194
195
		$this->assertFalse(
196
			in_array( $objects[0], $indexedArray->getByPropertyId( $objects[0]->getPropertyId() ) )
197
		);
198
199
		$this->assertFalse( in_array(
200
			$objects[$lastIndex],
201
			$indexedArray->getByPropertyId( $objects[1]->getPropertyId() )
202
		) );
203
204
		$this->assertFalse( in_array( $objects[0], $indexedArray->toFlatArray() ) );
205
		$this->assertFalse( in_array( $objects[$lastIndex], $indexedArray->toFlatArray() ) );
206
	}
207
208
	public function testGetByNotSetIdThrowsException() {
209
		$indexedArray = new ByPropertyIdArray();
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
210
		$indexedArray->buildIndex();
211
212
		$this->setExpectedException( 'OutOfBoundsException' );
213
214
		$indexedArray->getByPropertyId( new PropertyId( 'P9000' ) );
215
	}
216
217
	public function testNotBuildExceptionIsThrownForByPropertyId() {
218
		$indexedArray = new ByPropertyIdArray();
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
219
220
		$this->setExpectedException( 'RuntimeException' );
221
		$indexedArray->getByPropertyId( new PropertyId( 'P9000' ) );
222
	}
223
224
	public function testNotBuildExceptionIsThrownForGetPropertyIds() {
225
		$indexedArray = new ByPropertyIdArray();
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
226
227
		$this->setExpectedException( 'RuntimeException' );
228
		$indexedArray->getPropertyIds();
229
	}
230
231
	/**
232
	 * @dataProvider listProvider
233
	 */
234
	public function testGetFlatArrayIndexOfObject( array $objects ) {
235
		$indexedArray = new ByPropertyIdArray( $objects );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
236
		$indexedArray->buildIndex();
237
238
		$indicesSource = [];
239
		$indicesDestination = [];
240
241
		$i = 0;
242
		foreach ( $objects as $object ) {
243
			$indicesSource[$i++] = $object;
244
			$indicesDestination[$indexedArray->getFlatArrayIndexOfObject( $object )] = $object;
245
		}
246
247
		$this->assertEquals( $indicesSource, $indicesDestination );
248
	}
249
250
	/**
251
	 * @dataProvider listProvider
252
	 */
253
	public function testToFlatArray( array $objects ) {
254
		$indexedArray = new ByPropertyIdArray( $objects );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
255
		$indexedArray->buildIndex();
256
257
		$this->assertEquals( $objects, $indexedArray->toFlatArray() );
258
	}
259
260
	public function moveProvider() {
261
		$c = $this->statementsProvider();
262
		$argLists = [];
263
264
		$argLists[] = [ $c, $c[0], 0, $c ];
265
		$argLists[] = [ $c, $c[0], 1, [ $c[1], $c[0], $c[2], $c[3], $c[4], $c[5] ] ];
266
		$argLists[] = [ $c, $c[0], 2, [ $c[1], $c[0], $c[2], $c[3], $c[4], $c[5] ] ];
267
		$argLists[] = [ $c, $c[0], 3, [ $c[2], $c[3], $c[4], $c[1], $c[0], $c[5] ] ];
268
		$argLists[] = [ $c, $c[0], 4, [ $c[2], $c[3], $c[4], $c[1], $c[0], $c[5] ] ];
269
		$argLists[] = [ $c, $c[0], 5, [ $c[2], $c[3], $c[4], $c[1], $c[0], $c[5] ] ];
270
		$argLists[] = [ $c, $c[0], 6, [ $c[2], $c[3], $c[4], $c[5], $c[1], $c[0] ] ];
271
272
		$argLists[] = [ $c, $c[1], 0, [ $c[1], $c[0], $c[2], $c[3], $c[4], $c[5] ] ];
273
		$argLists[] = [ $c, $c[1], 1, $c ];
274
		$argLists[] = [ $c, $c[1], 2, $c ];
275
		$argLists[] = [ $c, $c[1], 3, [ $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ] ];
276
		$argLists[] = [ $c, $c[1], 4, [ $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ] ];
277
		$argLists[] = [ $c, $c[1], 5, [ $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ] ];
278
		$argLists[] = [ $c, $c[1], 6, [ $c[2], $c[3], $c[4], $c[5], $c[0], $c[1] ] ];
279
280
		$argLists[] = [ $c, $c[2], 0, [ $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ] ];
281
		$argLists[] = [ $c, $c[2], 1, $c ];
282
		$argLists[] = [ $c, $c[2], 2, $c ];
283
		$argLists[] = [ $c, $c[2], 3, [ $c[0], $c[1], $c[3], $c[2], $c[4], $c[5] ] ];
284
		$argLists[] = [ $c, $c[2], 4, [ $c[0], $c[1], $c[3], $c[4], $c[2], $c[5] ] ];
285
		$argLists[] = [ $c, $c[2], 5, [ $c[0], $c[1], $c[3], $c[4], $c[2], $c[5] ] ];
286
		$argLists[] = [ $c, $c[2], 6, [ $c[0], $c[1], $c[5], $c[3], $c[4], $c[2] ] ];
287
288
		$argLists[] = [ $c, $c[3], 0, [ $c[3], $c[2], $c[4], $c[0], $c[1], $c[5] ] ];
289
		$argLists[] = [ $c, $c[3], 1, [ $c[0], $c[1], $c[3], $c[2], $c[4], $c[5] ] ];
290
		$argLists[] = [ $c, $c[3], 2, [ $c[0], $c[1], $c[3], $c[2], $c[4], $c[5] ] ];
291
		$argLists[] = [ $c, $c[3], 3, $c ];
292
		$argLists[] = [ $c, $c[3], 4, [ $c[0], $c[1], $c[2], $c[4], $c[3], $c[5] ] ];
293
		$argLists[] = [ $c, $c[3], 5, [ $c[0], $c[1], $c[2], $c[4], $c[3], $c[5] ] ];
294
		$argLists[] = [ $c, $c[3], 6, [ $c[0], $c[1], $c[5], $c[2], $c[4], $c[3] ] ];
295
296
		$argLists[] = [ $c, $c[4], 0, [ $c[4], $c[2], $c[3], $c[0], $c[1], $c[5] ] ];
297
		$argLists[] = [ $c, $c[4], 1, [ $c[0], $c[1], $c[4], $c[2], $c[3], $c[5] ] ];
298
		$argLists[] = [ $c, $c[4], 2, [ $c[0], $c[1], $c[4], $c[2], $c[3], $c[5] ] ];
299
		$argLists[] = [ $c, $c[4], 3, [ $c[0], $c[1], $c[2], $c[4], $c[3], $c[5] ] ];
300
		$argLists[] = [ $c, $c[4], 4, $c ];
301
		$argLists[] = [ $c, $c[4], 5, $c ];
302
		$argLists[] = [ $c, $c[4], 6, [ $c[0], $c[1], $c[5], $c[2], $c[3], $c[4] ] ];
303
304
		$argLists[] = [ $c, $c[5], 0, [ $c[5], $c[0], $c[1], $c[2], $c[3], $c[4] ] ];
305
		$argLists[] = [ $c, $c[5], 1, [ $c[0], $c[1], $c[5], $c[2], $c[3], $c[4] ] ];
306
		$argLists[] = [ $c, $c[5], 2, [ $c[0], $c[1], $c[5], $c[2], $c[3], $c[4] ] ];
307
		$argLists[] = [ $c, $c[5], 3, $c ];
308
		$argLists[] = [ $c, $c[5], 4, $c ];
309
		$argLists[] = [ $c, $c[5], 5, $c ];
310
		$argLists[] = [ $c, $c[5], 6, $c ];
311
312
		return $argLists;
313
	}
314
315
	/**
316
	 * @dataProvider moveProvider
317
	 */
318
	public function testMoveObjectToIndex(
319
		array $objectsSource,
320
		PropertyIdProvider $object,
321
		$toIndex,
322
		array $objectsDestination
323
	) {
324
		$indexedArray = new ByPropertyIdArray( $objectsSource );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
325
		$indexedArray->buildIndex();
326
327
		$indexedArray->moveObjectToIndex( $object, $toIndex );
328
329
		// Not using $indexedArray->toFlatArray() here to test whether native array has been
330
		// exchanged:
331
		$reindexedArray = [];
332
		foreach ( $indexedArray as $o ) {
333
			$reindexedArray[] = $o;
334
		}
335
336
		$this->assertEquals( $objectsDestination, $reindexedArray );
337
	}
338
339
	public function testMoveThrowingOutOfBoundsExceptionIfObjectNotPresent() {
340
		$statements = $this->statementsProvider();
341
		$indexedArray = new ByPropertyIdArray( $statements );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
342
		$indexedArray->buildIndex();
343
344
		$this->setExpectedException( 'OutOfBoundsException' );
345
346
		$indexedArray->moveObjectToIndex( new Statement( new PropertyNoValueSnak( new PropertyId( 'P9999' ) ) ), 0 );
347
	}
348
349
	public function testMoveThrowingOutOfBoundsExceptionOnInvalidIndex() {
350
		$statements = $this->statementsProvider();
351
		$indexedArray = new ByPropertyIdArray( $statements );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
352
		$indexedArray->buildIndex();
353
354
		$this->setExpectedException( 'OutOfBoundsException' );
355
356
		$indexedArray->moveObjectToIndex( $statements[0], 9999 );
357
	}
358
359
	public function addProvider() {
360
		$c = $this->statementsProvider();
361
362
		$argLists = [];
363
364
		$argLists[] = [ [], $c[0], null, [ $c[0] ] ];
365
		$argLists[] = [ [], $c[0], 1, [ $c[0] ] ];
366
		$argLists[] = [ [ $c[0] ], $c[2], 0, [ $c[2], $c[0] ] ];
367
		$argLists[] = [ [ $c[2], $c[1] ], $c[0], 0, [ $c[0], $c[1], $c[2] ] ];
368
		$argLists[] = [
369
			[ $c[0], $c[1], $c[3] ],
370
			$c[5],
371
			1,
372
			[ $c[0], $c[1], $c[5], $c[3] ]
373
		];
374
		$argLists[] = [
375
			[ $c[0], $c[1], $c[5], $c[3] ],
376
			$c[2],
377
			2,
378
			[ $c[0], $c[1], $c[2], $c[3], $c[5] ]
379
		];
380
		$argLists[] = [
381
			[ $c[0], $c[1], $c[2], $c[3], $c[5] ],
382
			$c[4],
383
			null,
384
			[ $c[0], $c[1], $c[2], $c[3], $c[4], $c[5] ]
385
		];
386
387
		return $argLists;
388
	}
389
390
	/**
391
	 * @dataProvider addProvider
392
	 */
393
	public function testAddObjectAtIndex(
394
		array $objectsSource,
395
		PropertyIdProvider $object,
396
		$index,
397
		array $objectsDestination
398
	) {
399
		$indexedArray = new ByPropertyIdArray( $objectsSource );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
400
		$indexedArray->buildIndex();
401
402
		$indexedArray->addObjectAtIndex( $object, $index );
403
404
		$this->assertEquals( $objectsDestination, $indexedArray->toFlatArray() );
405
	}
406
407
}
408