Passed
Push — int32EntityId ( fa80fb )
by no
05:13
created

testGivenTraversableWithNonStatements_constructorThrowsException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Statement;
4
5
use ArrayObject;
6
use DataValues\StringValue;
7
use Wikibase\DataModel\Entity\PropertyId;
8
use Wikibase\DataModel\Reference;
9
use Wikibase\DataModel\ReferenceList;
10
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
11
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
12
use Wikibase\DataModel\Snak\PropertyValueSnak;
13
use Wikibase\DataModel\Snak\SnakList;
14
use Wikibase\DataModel\Statement\ReferencedStatementFilter;
15
use Wikibase\DataModel\Statement\Statement;
16
use Wikibase\DataModel\Statement\StatementList;
17
18
/**
19
 * @covers Wikibase\DataModel\Statement\StatementList
20
 *
21
 * @license GPL-2.0+
22
 * @author Jeroen De Dauw < [email protected] >
23
 * @author Thiemo Mättig
24
 */
25
class StatementListTest extends \PHPUnit_Framework_TestCase {
26
27
	/**
28
	 * @param int $propertyId
29
	 * @param string|null $guid
30
	 * @param int $rank
31
	 *
32
	 * @return Statement
33
	 */
34
	private function getStatement( $propertyId, $guid, $rank = Statement::RANK_NORMAL ) {
35
		$statement = $this->getMockBuilder( 'Wikibase\DataModel\Statement\Statement' )
36
			->disableOriginalConstructor()
37
			->getMock();
38
39
		$statement->expects( $this->any() )
40
			->method( 'getGuid' )
41
			->will( $this->returnValue( $guid ) );
42
43
		$statement->expects( $this->any() )
44
			->method( 'getPropertyId' )
45
			->will( $this->returnValue( PropertyId::newFromNumber( $propertyId ) ) );
46
47
		$statement->expects( $this->any() )
48
			->method( 'getRank' )
49
			->will( $this->returnValue( $rank ) );
50
51
		return $statement;
52
	}
53
54
	private function getStatementWithSnak( $propertyId, $stringValue ) {
55
		$snak = $this->newSnak( $propertyId, $stringValue );
56
		$statement = new Statement( $snak );
57
		$statement->setGuid( sha1( $snak->getHash() ) );
58
		return $statement;
59
	}
60
61
	private function newSnak( $propertyId, $stringValue ) {
62
		return new PropertyValueSnak( $propertyId, new StringValue( $stringValue ) );
63
	}
64
65
	public function testConstructorAcceptsDuplicatesWithNoGuid() {
66
		$list = new StatementList(
67
			$this->getStatement( 1, null ),
68
			$this->getStatement( 1, null )
69
		);
70
71
		$this->assertSame( 2, $list->count() );
72
	}
73
74
	public function testConstructorAcceptsDuplicatesWithSameGuid() {
75
		$list = new StatementList(
76
			$this->getStatement( 1, 'duplicate' ),
77
			$this->getStatement( 1, 'duplicate' )
78
		);
79
80
		$this->assertSame( 2, $list->count() );
81
	}
82
83
	public function testGivenNoStatements_getPropertyIdsReturnsEmptyArray() {
84
		$list = new StatementList();
85
		$this->assertSame( array(), $list->getPropertyIds() );
86
	}
87
88
	public function testGivenStatements_getPropertyIdsReturnsArrayWithoutDuplicates() {
89
		$list = new StatementList(
90
			$this->getStatement( 1, 'kittens' ),
91
			$this->getStatement( 3, 'foo' ),
92
			$this->getStatement( 2, 'bar' ),
93
			$this->getStatement( 2, 'baz' ),
94
			$this->getStatement( 1, 'bah' )
95
		);
96
97
		$this->assertEquals(
98
			array(
99
				'P1' => new PropertyId( 'P1' ),
100
				'P3' => new PropertyId( 'P3' ),
101
				'P2' => new PropertyId( 'P2' ),
102
			),
103
			$list->getPropertyIds()
104
		);
105
	}
106
107
	public function testGivenStatementsWithArrayKeys_toArrayReturnsReindexedArray() {
108
		$statement = $this->getStatement( 1, 'guid' );
109
		$list = new StatementList( array( 'ignore-me' => $statement ) );
110
111
		$this->assertSame( array( 0 => $statement ), $list->toArray() );
112
	}
113
114
	public function testGivenSparseArray_toArrayReturnsReindexedArray() {
115
		$statement = $this->getStatement( 1, 'guid' );
116
		$list = new StatementList( array( 1 => $statement ) );
117
118
		$this->assertSame( array( 0 => $statement ), $list->toArray() );
119
	}
120
121
	public function testCanIterate() {
122
		$statement = $this->getStatement( 1, 'kittens' );
123
		$list = new StatementList( $statement );
124
125
		foreach ( $list as $statementFormList ) {
126
			$this->assertEquals( $statement, $statementFormList );
127
		}
128
	}
129
130
	public function testGetUniqueMainSnaksReturnsListWithoutDuplicates() {
131
		$list = new StatementList(
132
			$this->getStatementWithSnak( 1, 'foo' ),
133
			$this->getStatementWithSnak( 2, 'foo' ),
134
			$this->getStatementWithSnak( 1, 'foo' ),
135
			$this->getStatementWithSnak( 2, 'bar' ),
136
			$this->getStatementWithSnak( 1, 'bar' )
137
		);
138
139
		$this->assertEquals(
140
			array(
141
				$this->getStatementWithSnak( 1, 'foo' ),
142
				$this->getStatementWithSnak( 2, 'foo' ),
143
				$this->getStatementWithSnak( 2, 'bar' ),
144
				$this->getStatementWithSnak( 1, 'bar' ),
145
			),
146
			array_values( $list->getWithUniqueMainSnaks()->toArray() )
147
		);
148
	}
149
150
	public function testGetAllSnaksReturnsAllSnaks() {
151
		$list = new StatementList(
152
			$this->getStatementWithSnak( 1, 'foo' ),
153
			$this->getStatementWithSnak( 2, 'foo' ),
154
			$this->getStatementWithSnak( 1, 'foo' ),
155
			$this->getStatementWithSnak( 2, 'bar' ),
156
			$this->getStatementWithSnak( 1, 'bar' )
157
		);
158
159
		$this->assertEquals(
160
			array(
161
				$this->newSnak( 1, 'foo' ),
162
				$this->newSnak( 2, 'foo' ),
163
				$this->newSnak( 1, 'foo' ),
164
				$this->newSnak( 2, 'bar' ),
165
				$this->newSnak( 1, 'bar' ),
166
			),
167
			$list->getAllSnaks()
168
		);
169
	}
170
171
	public function testAddStatementWithOnlyMainSnak() {
172
		$list = new StatementList();
173
174
		$list->addNewStatement( $this->newSnak( 42, 'foo' ) );
175
176
		$this->assertEquals(
177
			new StatementList( new Statement( $this->newSnak( 42, 'foo' ) ) ),
178
			$list
179
		);
180
	}
181
182
	public function testAddStatementWithQualifiersAsSnakArray() {
183
		$list = new StatementList();
184
185
		$list->addNewStatement(
186
			$this->newSnak( 42, 'foo' ),
187
			array(
188
				$this->newSnak( 1, 'bar' )
189
			)
190
		);
191
192
		$this->assertEquals(
193
			new StatementList(
194
				new Statement(
195
					$this->newSnak( 42, 'foo' ),
196
					new SnakList( array(
197
						$this->newSnak( 1, 'bar' )
198
					) )
199
				)
200
			),
201
			$list
202
		);
203
	}
204
205
	public function testAddStatementWithQualifiersAsSnakList() {
206
		$list = new StatementList();
207
		$snakList = new SnakList( array(
208
			$this->newSnak( 1, 'bar' )
209
		) );
210
211
		$list->addNewStatement(
212
			$this->newSnak( 42, 'foo' ),
213
			$snakList
214
		);
215
216
		$this->assertEquals(
217
			new StatementList( new Statement( $this->newSnak( 42, 'foo' ), $snakList ) ),
218
			$list
219
		);
220
	}
221
222
	public function testAddStatementWithGuid() {
223
		$list = new StatementList();
224
225
		$list->addNewStatement(
226
			$this->newSnak( 42, 'foo' ),
227
			null,
228
			null,
229
			'kittens'
230
		);
231
232
		$statement = new Statement( $this->newSnak( 42, 'foo' ) );
233
234
		$statement->setGuid( 'kittens' );
235
236
		$this->assertEquals( new StatementList( $statement ), $list );
237
	}
238
239
	public function testGivenNegativeIndex_addStatementFails() {
240
		$statement = new Statement( new PropertyNoValueSnak( 1 ) );
241
		$list = new StatementList();
242
243
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
244
		$list->addStatement( $statement, -1 );
245
	}
246
247
	public function testGivenLargeIndex_addStatementAppends() {
248
		$statement = new Statement( new PropertyNoValueSnak( 1 ) );
249
		$list = new StatementList();
250
251
		$list->addStatement( $statement, 1000 );
252
		$this->assertEquals( new StatementList( $statement ), $list );
253
	}
254
255
	public function testGivenZeroIndex_addStatementPrepends() {
256
		$statement1 = new Statement( new PropertyNoValueSnak( 1 ) );
257
		$statement2 = new Statement( new PropertyNoValueSnak( 2 ) );
258
		$list = new StatementList( $statement2 );
259
260
		$list->addStatement( $statement1, 0 );
261
		$this->assertEquals( new StatementList( $statement1, $statement2 ), $list );
262
	}
263
264
	public function testGivenValidIndex_addStatementInserts() {
265
		$statement1 = new Statement( new PropertyNoValueSnak( 1 ) );
266
		$statement2 = new Statement( new PropertyNoValueSnak( 2 ) );
267
		$statement3 = new Statement( new PropertyNoValueSnak( 3 ) );
268
		$list = new StatementList( $statement1, $statement3 );
269
270
		$list->addStatement( $statement2, 1 );
271
		$this->assertEquals( new StatementList( $statement1, $statement2, $statement3 ), $list );
272
		$this->assertSame( array( 0, 1, 2 ), array_keys( $list->toArray() ), 'array keys' );
273
	}
274
275
	public function testGivenGuidOfPresentStatement_statementIsRemoved() {
276
		$statement1 = new Statement( $this->newSnak( 24, 'foo' ), null, null, 'foo' );
277
		$statement2 = new Statement( $this->newSnak( 32, 'bar' ), null, null, 'bar' );
278
		$statement3 = new Statement( $this->newSnak( 32, 'bar' ), null, null, 'bar' );
279
280
		$list = new StatementList( array( $statement1, $statement2, $statement3 ) );
281
		$list->removeStatementsWithGuid( 'foo' );
282
283
		$this->assertEquals( new StatementList( $statement2, $statement3 ), $list );
284
	}
285
286
	public function testGivenGuidOfMultipleStatements_multipleStatementsAreRemoved() {
287
		$statement1 = new Statement( $this->newSnak( 24, 'foo' ), null, null, 'foo' );
288
		$statement2 = new Statement( $this->newSnak( 32, 'bar' ), null, null, 'bar' );
289
		$statement3 = new Statement( $this->newSnak( 32, 'bar' ), null, null, 'bar' );
290
291
		$list = new StatementList( array( $statement1, $statement2, $statement3 ) );
292
		$list->removeStatementsWithGuid( 'bar' );
293
294
		$this->assertEquals( new StatementList( $statement1 ), $list );
295
	}
296
297
	public function testGivenNotPresentGuid_listIsNotModified() {
298
		$statement1 = new Statement( $this->newSnak( 24, 'foo' ), null, null, 'foo' );
299
		$statement2 = new Statement( $this->newSnak( 32, 'bar' ), null, null, 'bar' );
300
		$statement3 = new Statement( $this->newSnak( 32, 'bar' ), null, null, 'bar' );
301
302
		$list = new StatementList( array( $statement1, $statement2, $statement3 ) );
303
		$list->removeStatementsWithGuid( 'baz' );
304
305
		$this->assertEquals( new StatementList( $statement1, $statement2, $statement3 ), $list );
306
	}
307
308
	public function testGivenNullGuid_allStatementsWithNoGuidAreRemoved() {
309
		$statement1 = new Statement( $this->newSnak( 24, 'foo' ), null, null, 'foo' );
310
		$statement2 = new Statement( $this->newSnak( 32, 'bar' ) );
311
		$statement3 = new Statement( $this->newSnak( 32, 'bar' ) );
312
313
		$list = new StatementList( array( $statement1, $statement2, $statement3 ) );
314
		$list->removeStatementsWithGuid( null );
315
316
		$this->assertEquals( new StatementList( $statement1 ), $list );
317
	}
318
319
	public function testCanConstructWithTraversableContainingOnlyStatements() {
320
		$statementArray = array(
321
			$this->getStatementWithSnak( 1, 'foo' ),
322
			$this->getStatementWithSnak( 2, 'bar' ),
323
		);
324
325
		$object = new ArrayObject( $statementArray );
326
		$list = new StatementList( $object );
327
328
		$this->assertEquals(
329
			$statementArray,
330
			array_values( $list->toArray() )
331
		);
332
	}
333
334
	public function testGivenTraversableWithNonStatements_constructorThrowsException() {
335
		$traversable = new ArrayObject( array(
336
			$this->getStatementWithSnak( 1, 'foo' ),
337
			new \stdClass(),
338
			$this->getStatementWithSnak( 2, 'bar' ),
339
		) );
340
341
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
342
		new StatementList( $traversable );
343
	}
344
345
	public function testGivenNonTraversableOrArgList_constructorThrowsException() {
346
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
347
		new StatementList( null );
348
	}
349
350
	public function testCanConstructWithStatement() {
351
		$statement = new Statement( $this->newSnak( 42, 'foo' ) );
352
353
		$this->assertEquals(
354
			new StatementList( array( $statement ) ),
355
			new StatementList( $statement )
356
		);
357
	}
358
359
	public function testCanConstructWithStatementArgumentList() {
360
		$statement0 = new Statement( $this->newSnak( 42, 'foo' ) );
361
		$statement1 = new Statement( $this->newSnak( 42, 'bar' ) );
362
		$statement2 = new Statement( $this->newSnak( 42, 'baz' ) );
363
364
		$this->assertEquals(
365
			new StatementList( array( $statement0, $statement1, $statement2 ) ),
366
			new StatementList( $statement0, $statement1, $statement2 )
367
		);
368
	}
369
370
	public function testGivenArgumentListWithNonStatement_constructorThrowsException() {
371
		$statement0 = new Statement( $this->newSnak( 42, 'foo' ) );
372
		$statement1 = new Statement( $this->newSnak( 42, 'bar' ) );
373
		$statement2 = new Statement( $this->newSnak( 42, 'baz' ) );
374
375
		$this->setExpectedException( 'InvalidArgumentException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
376
		new StatementList( $statement0, $statement1, array(), $statement2 );
377
	}
378
379
	public function testCountForEmptyList() {
380
		$list = new StatementList();
381
		$this->assertSame( 0, count( $list ) );
382
		$this->assertSame( 0, $list->count() );
383
	}
384
385
	public function testCountForNonEmptyList() {
386
		$list = new StatementList(
387
			$this->getStatementWithSnak( 1, 'foo' ),
388
			$this->getStatementWithSnak( 2, 'bar' )
389
		);
390
391
		$this->assertSame( 2, $list->count() );
392
	}
393
394
	/**
395
	 * @dataProvider statementArrayProvider
396
	 */
397
	public function testGivenIdenticalLists_equalsReturnsTrue( array $statements ) {
398
		$firstStatements = new StatementList( $statements );
399
		$secondStatements = new StatementList( $statements );
400
401
		$this->assertTrue( $firstStatements->equals( $secondStatements ) );
402
	}
403
404
	public function statementArrayProvider() {
405
		return array(
406
			array( array(
407
				$this->getStatementWithSnak( 1, 'foo' ),
408
				$this->getStatementWithSnak( 2, 'bar' ),
409
			) ),
410
			array( array(
411
				$this->getStatementWithSnak( 1, 'foo' ),
412
			) ),
413
			array( array(
414
			) ),
415
		);
416
	}
417
418
	public function testGivenDifferentLists_equalsReturnsFalse() {
419
		$firstStatements = new StatementList(
420
			$this->getStatementWithSnak( 1, 'foo' ),
421
			$this->getStatementWithSnak( 2, 'bar' )
422
		);
423
424
		$secondStatements = new StatementList(
425
			$this->getStatementWithSnak( 1, 'foo' ),
426
			$this->getStatementWithSnak( 2, 'SPAM' )
427
		);
428
429
		$this->assertFalse( $firstStatements->equals( $secondStatements ) );
430
	}
431
432
	public function testGivenListsWithDifferentDuplicates_equalsReturnsFalse() {
433
		$firstStatements = new StatementList(
434
			$this->getStatementWithSnak( 1, 'foo' ),
435
			$this->getStatementWithSnak( 1, 'foo' ),
436
			$this->getStatementWithSnak( 2, 'bar' )
437
		);
438
439
		$secondStatements = new StatementList(
440
			$this->getStatementWithSnak( 1, 'foo' ),
441
			$this->getStatementWithSnak( 2, 'bar' ),
442
			$this->getStatementWithSnak( 2, 'bar' )
443
		);
444
445
		$this->assertFalse( $firstStatements->equals( $secondStatements ) );
446
	}
447
448
	public function testGivenListsWithDifferentOrder_equalsReturnsFalse() {
449
		$firstStatements = new StatementList(
450
			$this->getStatementWithSnak( 1, 'foo' ),
451
			$this->getStatementWithSnak( 2, 'bar' ),
452
			$this->getStatementWithSnak( 3, 'baz' )
453
		);
454
455
		$secondStatements = new StatementList(
456
			$this->getStatementWithSnak( 1, 'foo' ),
457
			$this->getStatementWithSnak( 3, 'baz' ),
458
			$this->getStatementWithSnak( 2, 'bar' )
459
		);
460
461
		$this->assertFalse( $firstStatements->equals( $secondStatements ) );
462
	}
463
464
	public function testEmptyListDoesNotEqualNonEmptyList() {
465
		$firstStatements = new StatementList();
466
467
		$secondStatements = new StatementList(
468
			$this->getStatementWithSnak( 1, 'foo' ),
469
			$this->getStatementWithSnak( 3, 'baz' ),
470
			$this->getStatementWithSnak( 2, 'bar' )
471
		);
472
473
		$this->assertFalse( $firstStatements->equals( $secondStatements ) );
474
	}
475
476
	public function testNonEmptyListDoesNotEqualEmptyList() {
477
		$firstStatements = new StatementList(
478
			$this->getStatementWithSnak( 1, 'foo' ),
479
			$this->getStatementWithSnak( 3, 'baz' ),
480
			$this->getStatementWithSnak( 2, 'bar' )
481
		);
482
483
		$secondStatements = new StatementList();
484
485
		$this->assertFalse( $firstStatements->equals( $secondStatements ) );
486
	}
487
488
	public function testEmptyListIsEmpty() {
489
		$list = new StatementList();
490
491
		$this->assertTrue( $list->isEmpty() );
492
	}
493
494
	public function testNonEmptyListIsNotEmpty() {
495
		$list = new StatementList( $this->getStatementWithSnak( 1, 'foo' ) );
496
497
		$this->assertFalse( $list->isEmpty() );
498
	}
499
500
	public function testGetMainSnaks() {
501
		$list = new StatementList();
502
503
		$list->addNewStatement( new PropertyNoValueSnak( 42 ) );
504
		$list->addNewStatement( new PropertyNoValueSnak( 1337 ), array( new PropertyNoValueSnak( 32202 ) ) );
505
		$list->addNewStatement( new PropertyNoValueSnak( 9001 ) );
506
507
		$this->assertEquals(
508
			array(
509
				new PropertyNoValueSnak( 42 ),
510
				new PropertyNoValueSnak( 1337 ),
511
				new PropertyNoValueSnak( 9001 ),
512
			),
513
			$list->getMainSnaks()
514
		);
515
	}
516
517
	public function testGivenNotKnownPropertyId_getByPropertyIdReturnsEmptyList() {
518
		$list = new StatementList();
519
		$list->addNewStatement( new PropertyNoValueSnak( 42 ) );
520
521
		$this->assertEquals(
522
			new StatementList(),
523
			$list->getByPropertyId( new PropertyId( 'P2' ) )
524
		);
525
	}
526
527
	public function testGivenKnownPropertyId_getByPropertyIdReturnsListWithOnlyMatchingStatements() {
528
		$list = new StatementList();
529
		$list->addNewStatement( new PropertyNoValueSnak( 42 ) );
530
		$list->addNewStatement( new PropertyNoValueSnak( 9001 ) );
531
		$list->addNewStatement( new PropertySomeValueSnak( 42 ) );
532
		$list->addNewStatement( new PropertySomeValueSnak( 9001 ) );
533
534
		$expected = new StatementList();
535
		$expected->addNewStatement( new PropertyNoValueSnak( 42 ) );
536
		$expected->addNewStatement( new PropertySomeValueSnak( 42 ) );
537
538
		$this->assertEquals(
539
			$expected,
540
			$list->getByPropertyId( new PropertyId( 'P42' ) )
541
		);
542
	}
543
544
	public function testGivenInvalidRank_getByRankReturnsEmptyList() {
545
		$list = new StatementList();
546
		$this->assertEquals( new StatementList(), $list->getByRank( 42 ) );
547
	}
548
549
	public function testGivenValidRank_getByRankReturnsOnlyMatchingStatements() {
550
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
551
		$statement->setRank( Statement::RANK_PREFERRED );
552
553
		$secondStatement = new Statement( new PropertyNoValueSnak( 1337 ) );
554
		$secondStatement->setRank( Statement::RANK_NORMAL );
555
556
		$thirdStatement = new Statement( new PropertyNoValueSnak( 9001 ) );
557
		$thirdStatement->setRank( Statement::RANK_DEPRECATED );
558
559
		$list = new StatementList( $statement, $secondStatement, $thirdStatement );
560
561
		$this->assertEquals(
562
			new StatementList( $statement ),
563
			$list->getByRank( Statement::RANK_PREFERRED )
564
		);
565
566
		$this->assertEquals(
567
			new StatementList( $secondStatement, $thirdStatement ),
568
			$list->getByRank( array( Statement::RANK_NORMAL, Statement::RANK_DEPRECATED ) )
569
		);
570
	}
571
572
	public function testWhenListIsEmpty_getBestStatementsReturnsEmptyList() {
573
		$list = new StatementList();
574
		$this->assertEquals( new StatementList(), $list->getBestStatements() );
575
	}
576
577
	public function testWhenOnlyDeprecatedStatements_getBestStatementsReturnsEmptyList() {
578
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
579
		$statement->setRank( Statement::RANK_DEPRECATED );
580
581
		$secondStatement = new Statement( new PropertyNoValueSnak( 9001 ) );
582
		$secondStatement->setRank( Statement::RANK_DEPRECATED );
583
584
		$list = new StatementList( $statement, $secondStatement );
585
		$this->assertEquals( new StatementList(), $list->getBestStatements() );
586
	}
587
588
	public function testWhenPreferredStatements_getBestStatementsReturnsOnlyThose() {
589
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
590
		$statement->setRank( Statement::RANK_PREFERRED );
591
592
		$secondStatement = new Statement( new PropertyNoValueSnak( 1337 ) );
593
		$secondStatement->setRank( Statement::RANK_NORMAL );
594
595
		$thirdStatement = new Statement( new PropertyNoValueSnak( 9001 ) );
596
		$thirdStatement->setRank( Statement::RANK_DEPRECATED );
597
598
		$fourthStatement = new Statement( new PropertyNoValueSnak( 23 ) );
599
		$fourthStatement->setRank( Statement::RANK_PREFERRED );
600
601
		$list = new StatementList( $statement, $secondStatement, $thirdStatement, $fourthStatement );
602
		$this->assertEquals(
603
			new StatementList( $statement, $fourthStatement ),
604
			$list->getBestStatements()
605
		);
606
	}
607
608
	public function testWhenNoPreferredStatements_getBestStatementsReturnsOnlyNormalOnes() {
609
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
610
		$statement->setRank( Statement::RANK_NORMAL );
611
612
		$secondStatement = new Statement( new PropertyNoValueSnak( 1337 ) );
613
		$secondStatement->setRank( Statement::RANK_NORMAL );
614
615
		$thirdStatement = new Statement( new PropertyNoValueSnak( 9001 ) );
616
		$thirdStatement->setRank( Statement::RANK_DEPRECATED );
617
618
		$list = new StatementList( $statement, $secondStatement, $thirdStatement );
619
		$this->assertEquals(
620
			new StatementList( $statement, $secondStatement ),
621
			$list->getBestStatements()
622
		);
623
	}
624
625
	public function testGivenNotPresentStatement_getFirstStatementWithGuidReturnsNull() {
626
		$statements = new StatementList();
627
628
		$this->assertNull( $statements->getFirstStatementWithGuid( 'kittens' ) );
629
	}
630
631
	public function testGivenPresentStatement_getFirstStatementWithGuidReturnsStatement() {
632
		$statement1 = $this->getStatement( 1, 'guid1' );
633
		$statement2 = $this->getStatement( 2, 'guid2' );
634
		$statement3 = $this->getStatement( 3, 'guid3' );
635
		$statements = new StatementList( $statement1, $statement2, $statement3 );
636
637
		$actual = $statements->getFirstStatementWithGuid( 'guid2' );
638
		$this->assertSame( $statement2, $actual );
639
	}
640
641
	public function testGivenDoublyPresentStatement_getFirstStatementWithGuidReturnsFirstMatch() {
642
		$statement1 = $this->getStatement( 1, 'guid1' );
643
		$statement2 = $this->getStatement( 2, 'guid2' );
644
		$statement3 = $this->getStatement( 3, 'guid3' );
645
		$statement4 = $this->getStatement( 2, 'guid2' );
646
		$statements = new StatementList( $statement1, $statement2, $statement3, $statement4 );
647
648
		$actual = $statements->getFirstStatementWithGuid( 'guid2' );
649
		$this->assertSame( $statement2, $actual );
650
	}
651
652
	public function testGivenStatementsWithNoGuid_getFirstStatementWithGuidReturnsFirstMatch() {
653
		$statement1 = $this->getStatement( 1, null );
654
		$statement2 = $this->getStatement( 2, null );
655
		$statements = new StatementList( $statement1, $statement2 );
656
657
		$actual = $statements->getFirstStatementWithGuid( null );
658
		$this->assertSame( $statement1, $actual );
659
	}
660
661
	public function testGivenInvalidGuid_getFirstStatementWithGuidReturnsNull() {
662
		$statements = new StatementList();
663
664
		$this->assertNull( $statements->getFirstStatementWithGuid( false ) );
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string|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...
665
	}
666
667
	public function testFilter() {
668
		$statement1 = new Statement( new PropertyNoValueSnak( 1 ) );
669
		$statement2 = new Statement( new PropertyNoValueSnak( 2 ) );
670
		$statement3 = new Statement( new PropertyNoValueSnak( 3 ) );
671
		$statement4 = new Statement( new PropertyNoValueSnak( 4 ) );
672
673
		$statement2->setReferences( new ReferenceList( array(
674
			new Reference( array( new PropertyNoValueSnak( 20 ) ) )
675
		) ) );
676
677
		$statement3->setReferences( new ReferenceList( array(
678
			new Reference( array( new PropertyNoValueSnak( 30 ) ) )
679
		) ) );
680
681
		$statements = new StatementList( $statement1, $statement2, $statement3, $statement4 );
682
683
		$this->assertEquals(
684
			new StatementList( $statement2, $statement3 ),
685
			$statements->filter( new ReferencedStatementFilter() )
686
		);
687
	}
688
689
}
690