testNotBuildExceptionIsThrownForGetPropertyIds()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests;
4
5
use ArrayObject;
6
use DataValues\StringValue;
7
use InvalidArgumentException;
8
use OutOfBoundsException;
9
use ReflectionClass;
10
use ReflectionMethod;
11
use RuntimeException;
12
use stdClass;
13
use Wikibase\DataModel\ByPropertyIdArray;
14
use Wikibase\DataModel\Entity\PropertyId;
15
use Wikibase\DataModel\PropertyIdProvider;
16
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
17
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
18
use Wikibase\DataModel\Snak\PropertyValueSnak;
19
use Wikibase\DataModel\Snak\Snak;
20
use Wikibase\DataModel\Statement\Statement;
21
22
/**
23
 * @covers \Wikibase\DataModel\ByPropertyIdArray
24
 *
25
 * @group Wikibase
26
 * @group WikibaseDataModel
27
 *
28
 * @license GPL-2.0-or-later
29
 * @author Jeroen De Dauw < [email protected] >
30
 * @author H. Snater < [email protected] >
31
 */
32
class ByPropertyIdArrayTest extends \PHPUnit\Framework\TestCase {
33
34
	public function testGivenNull_constructorAssumesEmptyArray() {
35
		$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...
36
37
		$this->assertSame( 0, $indexedArray->count() );
38
	}
39
40
	public function testGivenNonTraversableObject_constructorDoesNotCastObjectToArray() {
41
		$object = new stdClass();
42
		$object->property = true;
43
44
		$this->expectException( InvalidArgumentException::class );
45
		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...
46
	}
47
48
	public function testArrayObjectNotConstructedFromObject() {
49
		$statement1 = new Statement( new PropertyNoValueSnak( 1 ) );
50
		$statement1->setGuid( '1' );
51
		$statement2 = new Statement( new PropertyNoValueSnak( 2 ) );
52
		$statement2->setGuid( '2' );
53
54
		$object = new ArrayObject();
55
		$object->append( $statement1 );
56
57
		$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...
58
		// According to the documentation append() "cannot be called when the ArrayObject was
59
		// constructed from an object." This test makes sure it was not constructed from an object.
60
		$byPropertyIdArray->append( $statement2 );
61
62
		$this->assertCount( 2, $byPropertyIdArray );
63
	}
64
65
	/**
66
	 * Returns an accessible ReflectionMethod of ByPropertyIdArray.
67
	 *
68
	 * @param string $methodName
69
	 * @return ReflectionMethod
70
	 */
71
	protected static function getMethod( $methodName ) {
72
		$class = new ReflectionClass( ByPropertyIdArray::class );
73
		$method = $class->getMethod( $methodName );
74
		$method->setAccessible( true );
75
		return $method;
76
	}
77
78
	public function listProvider() {
79
		$lists = [];
80
81
		$snaks = [
82
			new PropertyNoValueSnak( new PropertyId( 'P42' ) ),
83
			new PropertySomeValueSnak( new PropertyId( 'P42' ) ),
84
			new PropertySomeValueSnak( new PropertyId( 'P10' ) ),
85
			new PropertyValueSnak( new PropertyId( 'P10' ), new StringValue( 'ohi' ) ),
86
			new PropertySomeValueSnak( new PropertyId( 'P1' ) ),
87
		];
88
89
		$lists[] = $snaks;
90
91
		$lists[] = array_map(
92
			function( Snak $snak ) {
93
				return new Statement( $snak );
94
			},
95
			$snaks
96
		);
97
98
		$argLists = [];
99
100
		foreach ( $lists as $list ) {
101
			$argLists[] = [ $list ];
102
		}
103
104
		return $argLists;
105
	}
106
107
	/**
108
	 * @return Statement[]
109
	 */
110
	protected function statementsProvider() {
111
		$snaks = [
112
			new PropertyNoValueSnak( new PropertyId( 'P1' ) ),
113
			new PropertySomeValueSnak( new PropertyId( 'P1' ) ),
114
			new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'a' ) ),
115
			new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'b' ) ),
116
			new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'c' ) ),
117
			new PropertySomeValueSnak( new PropertyId( 'P3' ) ),
118
		];
119
120
		return array_map(
121
			function( Snak $snak ) {
122
				return new Statement( $snak );
123
			},
124
			$snaks
125
		);
126
	}
127
128
	/**
129
	 * @dataProvider listProvider
130
	 * @param PropertyIdProvider[] $objects
131
	 */
132
	public function testGetIds( array $objects ) {
133
		$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...
134
135
		$expected = [];
136
137
		foreach ( $objects as $object ) {
138
			$expected[] = $object->getPropertyId();
139
		}
140
141
		$expected = array_unique( $expected );
142
143
		$indexedArray->buildIndex();
144
145
		$this->assertEquals(
146
			array_values( $expected ),
147
			array_values( $indexedArray->getPropertyIds() )
148
		);
149
	}
150
151
	/**
152
	 * @dataProvider listProvider
153
	 * @param PropertyIdProvider[] $objects
154
	 */
155
	public function testGetById( array $objects ) {
156
		$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...
157
158
		$ids = [];
159
160
		foreach ( $objects as $object ) {
161
			$ids[] = $object->getPropertyId();
162
		}
163
164
		$ids = array_unique( $ids );
165
166
		$indexedArray->buildIndex();
167
168
		$allObtainedObjects = [];
169
170
		foreach ( $ids as $id ) {
171
			foreach ( $indexedArray->getByPropertyId( $id ) as $obtainedObject ) {
172
				$allObtainedObjects[] = $obtainedObject;
173
				$this->assertEquals( $id, $obtainedObject->getPropertyId() );
174
			}
175
		}
176
177
		$this->assertEquals(
178
			array_values( $objects ),
179
			array_values( $allObtainedObjects )
180
		);
181
	}
182
183
	/**
184
	 * @dataProvider listProvider
185
	 * @param PropertyIdProvider[] $objects
186
	 */
187
	public function testRemoveObject( array $objects ) {
188
		$lastIndex = count( $objects ) - 1;
189
		$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...
190
		$indexedArray->buildIndex();
191
192
		$removeObject = self::getMethod( 'removeObject' );
193
194
		$removeObject->invokeArgs( $indexedArray, [ $objects[0] ] );
195
		$removeObject->invokeArgs( $indexedArray, [ $objects[$lastIndex] ] );
196
197
		$this->assertFalse(
198
			in_array( $objects[0], $indexedArray->getByPropertyId( $objects[0]->getPropertyId() ) )
199
		);
200
201
		$this->assertFalse( in_array(
202
			$objects[$lastIndex],
203
			$indexedArray->getByPropertyId( $objects[1]->getPropertyId() )
204
		) );
205
206
		$this->assertFalse( in_array( $objects[0], $indexedArray->toFlatArray() ) );
207
		$this->assertFalse( in_array( $objects[$lastIndex], $indexedArray->toFlatArray() ) );
208
	}
209
210
	public function testGetByNotSetIdThrowsException() {
211
		$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...
212
		$indexedArray->buildIndex();
213
214
		$this->expectException( OutOfBoundsException::class );
215
216
		$indexedArray->getByPropertyId( new PropertyId( 'P9000' ) );
217
	}
218
219
	public function testNotBuildExceptionIsThrownForByPropertyId() {
220
		$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...
221
222
		$this->expectException( RuntimeException::class );
223
		$indexedArray->getByPropertyId( new PropertyId( 'P9000' ) );
224
	}
225
226
	public function testNotBuildExceptionIsThrownForGetPropertyIds() {
227
		$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...
228
229
		$this->expectException( RuntimeException::class );
230
		$indexedArray->getPropertyIds();
231
	}
232
233
	/**
234
	 * @dataProvider listProvider
235
	 */
236
	public function testGetFlatArrayIndexOfObject( array $objects ) {
237
		$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...
238
		$indexedArray->buildIndex();
239
240
		$indicesSource = [];
241
		$indicesDestination = [];
242
243
		$i = 0;
244
		foreach ( $objects as $object ) {
245
			$indicesSource[$i++] = $object;
246
			$indicesDestination[$indexedArray->getFlatArrayIndexOfObject( $object )] = $object;
247
		}
248
249
		$this->assertEquals( $indicesSource, $indicesDestination );
250
	}
251
252
	/**
253
	 * @dataProvider listProvider
254
	 */
255
	public function testToFlatArray( array $objects ) {
256
		$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...
257
		$indexedArray->buildIndex();
258
259
		$this->assertEquals( $objects, $indexedArray->toFlatArray() );
260
	}
261
262
	public function moveProvider() {
263
		$c = $this->statementsProvider();
264
		$argLists = [];
265
266
		$argLists[] = [ $c, $c[0], 0, $c ];
267
		$argLists[] = [ $c, $c[0], 1, [ $c[1], $c[0], $c[2], $c[3], $c[4], $c[5] ] ];
268
		$argLists[] = [ $c, $c[0], 2, [ $c[1], $c[0], $c[2], $c[3], $c[4], $c[5] ] ];
269
		$argLists[] = [ $c, $c[0], 3, [ $c[2], $c[3], $c[4], $c[1], $c[0], $c[5] ] ];
270
		$argLists[] = [ $c, $c[0], 4, [ $c[2], $c[3], $c[4], $c[1], $c[0], $c[5] ] ];
271
		$argLists[] = [ $c, $c[0], 5, [ $c[2], $c[3], $c[4], $c[1], $c[0], $c[5] ] ];
272
		$argLists[] = [ $c, $c[0], 6, [ $c[2], $c[3], $c[4], $c[5], $c[1], $c[0] ] ];
273
274
		$argLists[] = [ $c, $c[1], 0, [ $c[1], $c[0], $c[2], $c[3], $c[4], $c[5] ] ];
275
		$argLists[] = [ $c, $c[1], 1, $c ];
276
		$argLists[] = [ $c, $c[1], 2, $c ];
277
		$argLists[] = [ $c, $c[1], 3, [ $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ] ];
278
		$argLists[] = [ $c, $c[1], 4, [ $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ] ];
279
		$argLists[] = [ $c, $c[1], 5, [ $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ] ];
280
		$argLists[] = [ $c, $c[1], 6, [ $c[2], $c[3], $c[4], $c[5], $c[0], $c[1] ] ];
281
282
		$argLists[] = [ $c, $c[2], 0, [ $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ] ];
283
		$argLists[] = [ $c, $c[2], 1, $c ];
284
		$argLists[] = [ $c, $c[2], 2, $c ];
285
		$argLists[] = [ $c, $c[2], 3, [ $c[0], $c[1], $c[3], $c[2], $c[4], $c[5] ] ];
286
		$argLists[] = [ $c, $c[2], 4, [ $c[0], $c[1], $c[3], $c[4], $c[2], $c[5] ] ];
287
		$argLists[] = [ $c, $c[2], 5, [ $c[0], $c[1], $c[3], $c[4], $c[2], $c[5] ] ];
288
		$argLists[] = [ $c, $c[2], 6, [ $c[0], $c[1], $c[5], $c[3], $c[4], $c[2] ] ];
289
290
		$argLists[] = [ $c, $c[3], 0, [ $c[3], $c[2], $c[4], $c[0], $c[1], $c[5] ] ];
291
		$argLists[] = [ $c, $c[3], 1, [ $c[0], $c[1], $c[3], $c[2], $c[4], $c[5] ] ];
292
		$argLists[] = [ $c, $c[3], 2, [ $c[0], $c[1], $c[3], $c[2], $c[4], $c[5] ] ];
293
		$argLists[] = [ $c, $c[3], 3, $c ];
294
		$argLists[] = [ $c, $c[3], 4, [ $c[0], $c[1], $c[2], $c[4], $c[3], $c[5] ] ];
295
		$argLists[] = [ $c, $c[3], 5, [ $c[0], $c[1], $c[2], $c[4], $c[3], $c[5] ] ];
296
		$argLists[] = [ $c, $c[3], 6, [ $c[0], $c[1], $c[5], $c[2], $c[4], $c[3] ] ];
297
298
		$argLists[] = [ $c, $c[4], 0, [ $c[4], $c[2], $c[3], $c[0], $c[1], $c[5] ] ];
299
		$argLists[] = [ $c, $c[4], 1, [ $c[0], $c[1], $c[4], $c[2], $c[3], $c[5] ] ];
300
		$argLists[] = [ $c, $c[4], 2, [ $c[0], $c[1], $c[4], $c[2], $c[3], $c[5] ] ];
301
		$argLists[] = [ $c, $c[4], 3, [ $c[0], $c[1], $c[2], $c[4], $c[3], $c[5] ] ];
302
		$argLists[] = [ $c, $c[4], 4, $c ];
303
		$argLists[] = [ $c, $c[4], 5, $c ];
304
		$argLists[] = [ $c, $c[4], 6, [ $c[0], $c[1], $c[5], $c[2], $c[3], $c[4] ] ];
305
306
		$argLists[] = [ $c, $c[5], 0, [ $c[5], $c[0], $c[1], $c[2], $c[3], $c[4] ] ];
307
		$argLists[] = [ $c, $c[5], 1, [ $c[0], $c[1], $c[5], $c[2], $c[3], $c[4] ] ];
308
		$argLists[] = [ $c, $c[5], 2, [ $c[0], $c[1], $c[5], $c[2], $c[3], $c[4] ] ];
309
		$argLists[] = [ $c, $c[5], 3, $c ];
310
		$argLists[] = [ $c, $c[5], 4, $c ];
311
		$argLists[] = [ $c, $c[5], 5, $c ];
312
		$argLists[] = [ $c, $c[5], 6, $c ];
313
314
		return $argLists;
315
	}
316
317
	/**
318
	 * @dataProvider moveProvider
319
	 */
320
	public function testMoveObjectToIndex(
321
		array $objectsSource,
322
		PropertyIdProvider $object,
323
		$toIndex,
324
		array $objectsDestination
325
	) {
326
		$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...
327
		$indexedArray->buildIndex();
328
329
		$indexedArray->moveObjectToIndex( $object, $toIndex );
330
331
		// Not using $indexedArray->toFlatArray() here to test whether native array has been
332
		// exchanged:
333
		$reindexedArray = [];
334
		foreach ( $indexedArray as $o ) {
335
			$reindexedArray[] = $o;
336
		}
337
338
		$this->assertEquals( $objectsDestination, $reindexedArray );
339
	}
340
341
	public function testMoveThrowingOutOfBoundsExceptionIfObjectNotPresent() {
342
		$statements = $this->statementsProvider();
343
		$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...
344
		$indexedArray->buildIndex();
345
346
		$this->expectException( OutOfBoundsException::class );
347
348
		$indexedArray->moveObjectToIndex( new Statement( new PropertyNoValueSnak( new PropertyId( 'P9999' ) ) ), 0 );
349
	}
350
351
	public function testMoveThrowingOutOfBoundsExceptionOnInvalidIndex() {
352
		$statements = $this->statementsProvider();
353
		$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...
354
		$indexedArray->buildIndex();
355
356
		$this->expectException( OutOfBoundsException::class );
357
358
		$indexedArray->moveObjectToIndex( $statements[0], 9999 );
359
	}
360
361
	public function addProvider() {
362
		$c = $this->statementsProvider();
363
364
		$argLists = [];
365
366
		$argLists[] = [ [], $c[0], null, [ $c[0] ] ];
367
		$argLists[] = [ [], $c[0], 1, [ $c[0] ] ];
368
		$argLists[] = [ [ $c[0] ], $c[2], 0, [ $c[2], $c[0] ] ];
369
		$argLists[] = [ [ $c[2], $c[1] ], $c[0], 0, [ $c[0], $c[1], $c[2] ] ];
370
		$argLists[] = [
371
			[ $c[0], $c[1], $c[3] ],
372
			$c[5],
373
			1,
374
			[ $c[0], $c[1], $c[5], $c[3] ]
375
		];
376
		$argLists[] = [
377
			[ $c[0], $c[1], $c[5], $c[3] ],
378
			$c[2],
379
			2,
380
			[ $c[0], $c[1], $c[2], $c[3], $c[5] ]
381
		];
382
		$argLists[] = [
383
			[ $c[0], $c[1], $c[2], $c[3], $c[5] ],
384
			$c[4],
385
			null,
386
			[ $c[0], $c[1], $c[2], $c[3], $c[4], $c[5] ]
387
		];
388
389
		return $argLists;
390
	}
391
392
	/**
393
	 * @dataProvider addProvider
394
	 */
395
	public function testAddObjectAtIndex(
396
		array $objectsSource,
397
		PropertyIdProvider $object,
398
		$index,
399
		array $objectsDestination
400
	) {
401
		$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...
402
		$indexedArray->buildIndex();
403
404
		$indexedArray->addObjectAtIndex( $object, $index );
405
406
		$this->assertEquals( $objectsDestination, $indexedArray->toFlatArray() );
407
	}
408
409
}
410