Passed
Push — master ( c7b7b3...d0017a )
by Daniel
07:28
created

StatementListTest   D

Complexity

Total Complexity 58

Size/Duplication

Total Lines 690
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 14

Importance

Changes 0
Metric Value
wmc 58
lcom 1
cbo 14
dl 0
loc 690
rs 4.6666
c 0
b 0
f 0

57 Methods

Rating   Name   Duplication   Size   Complexity  
A getStatement() 0 19 1
A getStatementWithSnak() 0 6 1
A newSnak() 0 6 1
A testConstructorAcceptsDuplicatesWithNoGuid() 0 8 1
A testConstructorAcceptsDuplicatesWithSameGuid() 0 8 1
A testGivenNoStatements_getPropertyIdsReturnsEmptyArray() 0 4 1
A testGivenStatements_getPropertyIdsReturnsArrayWithoutDuplicates() 0 18 1
A testGivenStatementsWithArrayKeys_toArrayReturnsReindexedArray() 0 6 1
A testGivenSparseArray_toArrayReturnsReindexedArray() 0 6 1
A testCanIterate() 0 8 2
A testGetUniqueMainSnaksReturnsListWithoutDuplicates() 0 19 1
A testGetAllSnaksReturnsAllSnaks() 0 20 1
A testAddStatementWithOnlyMainSnak() 0 10 1
A testAddStatementWithQualifiersAsSnakArray() 0 22 1
A testAddStatementWithQualifiersAsSnakList() 0 16 1
A testAddStatementWithGuid() 0 16 1
A testGivenNegativeIndex_addStatementFails() 0 7 1
A testGivenLargeIndex_addStatementAppends() 0 7 1
A testGivenZeroIndex_addStatementPrepends() 0 8 1
A testGivenValidIndex_addStatementInserts() 0 10 1
A testGivenGuidOfPresentStatement_statementIsRemoved() 0 10 1
A testGivenGuidOfMultipleStatements_multipleStatementsAreRemoved() 0 10 1
A testGivenNotPresentGuid_listIsNotModified() 0 10 1
A testGivenNullGuid_allStatementsWithNoGuidAreRemoved() 0 10 1
A testCanConstructWithTraversableContainingOnlyStatements() 0 14 1
A testGivenTraversableWithNonStatements_constructorThrowsException() 0 10 1
A testGivenNonTraversableOrArgList_constructorThrowsException() 0 4 1
A testCanConstructWithStatement() 0 8 1
A testCanConstructWithStatementArgumentList() 0 10 1
A testGivenArgumentListWithNonStatement_constructorThrowsException() 0 8 1
A testCountForEmptyList() 0 5 1
A testCountForNonEmptyList() 0 8 1
A testGivenIdenticalLists_equalsReturnsTrue() 0 6 1
A statementArrayProvider() 0 13 1
A testGivenDifferentLists_equalsReturnsFalse() 0 13 1
A testGivenListsWithDifferentDuplicates_equalsReturnsFalse() 0 15 1
A testGivenListsWithDifferentOrder_equalsReturnsFalse() 0 15 1
A testEmptyListDoesNotEqualNonEmptyList() 0 11 1
A testNonEmptyListDoesNotEqualEmptyList() 0 11 1
A testEmptyListIsEmpty() 0 5 1
A testNonEmptyListIsNotEmpty() 0 5 1
A testGetMainSnaks() 0 16 1
A testGivenNotKnownPropertyId_getByPropertyIdReturnsEmptyList() 0 9 1
A testGivenKnownPropertyId_getByPropertyIdReturnsListWithOnlyMatchingStatements() 0 16 1
A testGivenInvalidRank_getByRankReturnsEmptyList() 0 4 1
A testGivenValidRank_getByRankReturnsOnlyMatchingStatements() 0 22 1
A testWhenListIsEmpty_getBestStatementsReturnsEmptyList() 0 4 1
A testWhenOnlyDeprecatedStatements_getBestStatementsReturnsEmptyList() 0 10 1
A testWhenPreferredStatements_getBestStatementsReturnsOnlyThose() 0 19 1
A testWhenNoPreferredStatements_getBestStatementsReturnsOnlyNormalOnes() 0 16 1
A testGivenNotPresentStatement_getFirstStatementWithGuidReturnsNull() 0 5 1
A testGivenPresentStatement_getFirstStatementWithGuidReturnsStatement() 0 9 1
A testGivenDoublyPresentStatement_getFirstStatementWithGuidReturnsFirstMatch() 0 10 1
A testGivenStatementsWithNoGuid_getFirstStatementWithGuidReturnsFirstMatch() 0 8 1
A testGivenInvalidGuid_getFirstStatementWithGuidReturnsNull() 0 5 1
A testFilter() 0 21 1
A testClear() 0 9 1

How to fix   Complexity   

Complex Class

Complex classes like StatementListTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use StatementListTest, and based on these observations, apply Extract Interface, too.

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\Snak;
14
use Wikibase\DataModel\Snak\SnakList;
15
use Wikibase\DataModel\Statement\ReferencedStatementFilter;
16
use Wikibase\DataModel\Statement\Statement;
17
use Wikibase\DataModel\Statement\StatementList;
18
19
/**
20
 * @covers Wikibase\DataModel\Statement\StatementList
21
 *
22
 * @license GPL-2.0+
23
 * @author Jeroen De Dauw < [email protected] >
24
 * @author Thiemo Mättig
25
 */
26
class StatementListTest extends \PHPUnit_Framework_TestCase {
27
28
	/**
29
	 * @param string $propertyId
30
	 * @param string|null $guid
31
	 * @param int $rank
32
	 *
33
	 * @return Statement
34
	 */
35
	private function getStatement( $propertyId, $guid, $rank = Statement::RANK_NORMAL ) {
36
		$statement = $this->getMockBuilder( 'Wikibase\DataModel\Statement\Statement' )
37
			->disableOriginalConstructor()
38
			->getMock();
39
40
		$statement->expects( $this->any() )
41
			->method( 'getGuid' )
42
			->will( $this->returnValue( $guid ) );
43
44
		$statement->expects( $this->any() )
45
			->method( 'getPropertyId' )
46
			->will( $this->returnValue( new PropertyId( $propertyId ) ) );
47
48
		$statement->expects( $this->any() )
49
			->method( 'getRank' )
50
			->will( $this->returnValue( $rank ) );
51
52
		return $statement;
53
	}
54
55
	/**
56
	 * @param string $propertyId
57
	 * @param string $stringValue
58
	 *
59
	 * @return Statement
60
	 */
61
	private function getStatementWithSnak( $propertyId, $stringValue ) {
62
		$snak = $this->newSnak( $propertyId, $stringValue );
63
		$statement = new Statement( $snak );
64
		$statement->setGuid( sha1( $snak->getHash() ) );
65
		return $statement;
66
	}
67
68
	/**
69
	 * @param string $propertyId
70
	 * @param string $stringValue
71
	 *
72
	 * @return Snak
73
	 */
74
	private function newSnak( $propertyId, $stringValue ) {
75
		return new PropertyValueSnak(
76
			new PropertyId( $propertyId ),
77
			new StringValue( $stringValue )
78
		);
79
	}
80
81
	public function testConstructorAcceptsDuplicatesWithNoGuid() {
82
		$list = new StatementList(
83
			$this->getStatement( 'P1', null ),
84
			$this->getStatement( 'P1', null )
85
		);
86
87
		$this->assertSame( 2, $list->count() );
88
	}
89
90
	public function testConstructorAcceptsDuplicatesWithSameGuid() {
91
		$list = new StatementList(
92
			$this->getStatement( 'P1', 'duplicate' ),
93
			$this->getStatement( 'P1', 'duplicate' )
94
		);
95
96
		$this->assertSame( 2, $list->count() );
97
	}
98
99
	public function testGivenNoStatements_getPropertyIdsReturnsEmptyArray() {
100
		$list = new StatementList();
101
		$this->assertSame( [], $list->getPropertyIds() );
102
	}
103
104
	public function testGivenStatements_getPropertyIdsReturnsArrayWithoutDuplicates() {
105
		$list = new StatementList(
106
			$this->getStatement( 'P1', 'kittens' ),
107
			$this->getStatement( 'P3', 'foo' ),
108
			$this->getStatement( 'P2', 'bar' ),
109
			$this->getStatement( 'P2', 'baz' ),
110
			$this->getStatement( 'P1', 'bah' )
111
		);
112
113
		$this->assertEquals(
114
			[
115
				'P1' => new PropertyId( 'P1' ),
116
				'P3' => new PropertyId( 'P3' ),
117
				'P2' => new PropertyId( 'P2' ),
118
			],
119
			$list->getPropertyIds()
120
		);
121
	}
122
123
	public function testGivenStatementsWithArrayKeys_toArrayReturnsReindexedArray() {
124
		$statement = $this->getStatement( 'P1', 'guid' );
125
		$list = new StatementList( [ 'ignore-me' => $statement ] );
126
127
		$this->assertSame( [ 0 => $statement ], $list->toArray() );
128
	}
129
130
	public function testGivenSparseArray_toArrayReturnsReindexedArray() {
131
		$statement = $this->getStatement( 'P1', 'guid' );
132
		$list = new StatementList( [ 1 => $statement ] );
133
134
		$this->assertSame( [ 0 => $statement ], $list->toArray() );
135
	}
136
137
	public function testCanIterate() {
138
		$statement = $this->getStatement( 'P1', 'kittens' );
139
		$list = new StatementList( $statement );
140
141
		foreach ( $list as $statementFormList ) {
142
			$this->assertEquals( $statement, $statementFormList );
143
		}
144
	}
145
146
	public function testGetUniqueMainSnaksReturnsListWithoutDuplicates() {
147
		$list = new StatementList(
148
			$this->getStatementWithSnak( 'P1', 'foo' ),
149
			$this->getStatementWithSnak( 'P2', 'foo' ),
150
			$this->getStatementWithSnak( 'P1', 'foo' ),
151
			$this->getStatementWithSnak( 'P2', 'bar' ),
152
			$this->getStatementWithSnak( 'P1', 'bar' )
153
		);
154
155
		$this->assertEquals(
156
			[
157
				$this->getStatementWithSnak( 'P1', 'foo' ),
158
				$this->getStatementWithSnak( 'P2', 'foo' ),
159
				$this->getStatementWithSnak( 'P2', 'bar' ),
160
				$this->getStatementWithSnak( 'P1', 'bar' ),
161
			],
162
			array_values( $list->getWithUniqueMainSnaks()->toArray() )
163
		);
164
	}
165
166
	public function testGetAllSnaksReturnsAllSnaks() {
167
		$list = new StatementList(
168
			$this->getStatementWithSnak( 'P1', 'foo' ),
169
			$this->getStatementWithSnak( 'P2', 'foo' ),
170
			$this->getStatementWithSnak( 'P1', 'foo' ),
171
			$this->getStatementWithSnak( 'P2', 'bar' ),
172
			$this->getStatementWithSnak( 'P1', 'bar' )
173
		);
174
175
		$this->assertEquals(
176
			[
177
				$this->newSnak( 'P1', 'foo' ),
178
				$this->newSnak( 'P2', 'foo' ),
179
				$this->newSnak( 'P1', 'foo' ),
180
				$this->newSnak( 'P2', 'bar' ),
181
				$this->newSnak( 'P1', 'bar' ),
182
			],
183
			$list->getAllSnaks()
184
		);
185
	}
186
187
	public function testAddStatementWithOnlyMainSnak() {
188
		$list = new StatementList();
189
190
		$list->addNewStatement( $this->newSnak( 'P42', 'foo' ) );
191
192
		$this->assertEquals(
193
			new StatementList( new Statement( $this->newSnak( 'P42', 'foo' ) ) ),
194
			$list
195
		);
196
	}
197
198
	public function testAddStatementWithQualifiersAsSnakArray() {
199
		$list = new StatementList();
200
201
		$list->addNewStatement(
202
			$this->newSnak( 'P42', 'foo' ),
203
			[
204
				$this->newSnak( 'P1', 'bar' )
205
			]
206
		);
207
208
		$this->assertEquals(
209
			new StatementList(
210
				new Statement(
211
					$this->newSnak( 'P42', 'foo' ),
212
					new SnakList( [
213
						$this->newSnak( 'P1', 'bar' )
214
					] )
215
				)
216
			),
217
			$list
218
		);
219
	}
220
221
	public function testAddStatementWithQualifiersAsSnakList() {
222
		$list = new StatementList();
223
		$snakList = new SnakList( [
224
			$this->newSnak( 'P1', 'bar' )
225
		] );
226
227
		$list->addNewStatement(
228
			$this->newSnak( 'P42', 'foo' ),
229
			$snakList
230
		);
231
232
		$this->assertEquals(
233
			new StatementList( new Statement( $this->newSnak( 'P42', 'foo' ), $snakList ) ),
234
			$list
235
		);
236
	}
237
238
	public function testAddStatementWithGuid() {
239
		$list = new StatementList();
240
241
		$list->addNewStatement(
242
			$this->newSnak( 'P42', 'foo' ),
243
			null,
244
			null,
245
			'kittens'
246
		);
247
248
		$statement = new Statement( $this->newSnak( 'P42', 'foo' ) );
249
250
		$statement->setGuid( 'kittens' );
251
252
		$this->assertEquals( new StatementList( $statement ), $list );
253
	}
254
255
	public function testGivenNegativeIndex_addStatementFails() {
256
		$statement = new Statement( new PropertyNoValueSnak( 1 ) );
257
		$list = new StatementList();
258
259
		$this->setExpectedException( 'InvalidArgumentException' );
260
		$list->addStatement( $statement, -1 );
261
	}
262
263
	public function testGivenLargeIndex_addStatementAppends() {
264
		$statement = new Statement( new PropertyNoValueSnak( 1 ) );
265
		$list = new StatementList();
266
267
		$list->addStatement( $statement, 1000 );
268
		$this->assertEquals( new StatementList( $statement ), $list );
269
	}
270
271
	public function testGivenZeroIndex_addStatementPrepends() {
272
		$statement1 = new Statement( new PropertyNoValueSnak( 1 ) );
273
		$statement2 = new Statement( new PropertyNoValueSnak( 2 ) );
274
		$list = new StatementList( $statement2 );
275
276
		$list->addStatement( $statement1, 0 );
277
		$this->assertEquals( new StatementList( $statement1, $statement2 ), $list );
278
	}
279
280
	public function testGivenValidIndex_addStatementInserts() {
281
		$statement1 = new Statement( new PropertyNoValueSnak( 1 ) );
282
		$statement2 = new Statement( new PropertyNoValueSnak( 2 ) );
283
		$statement3 = new Statement( new PropertyNoValueSnak( 3 ) );
284
		$list = new StatementList( $statement1, $statement3 );
285
286
		$list->addStatement( $statement2, 1 );
287
		$this->assertEquals( new StatementList( $statement1, $statement2, $statement3 ), $list );
288
		$this->assertSame( [ 0, 1, 2 ], array_keys( $list->toArray() ), 'array keys' );
289
	}
290
291
	public function testGivenGuidOfPresentStatement_statementIsRemoved() {
292
		$statement1 = new Statement( $this->newSnak( 'P24', 'foo' ), null, null, 'foo' );
293
		$statement2 = new Statement( $this->newSnak( 'P32', 'bar' ), null, null, 'bar' );
294
		$statement3 = new Statement( $this->newSnak( 'P32', 'bar' ), null, null, 'bar' );
295
296
		$list = new StatementList( [ $statement1, $statement2, $statement3 ] );
297
		$list->removeStatementsWithGuid( 'foo' );
298
299
		$this->assertEquals( new StatementList( $statement2, $statement3 ), $list );
300
	}
301
302
	public function testGivenGuidOfMultipleStatements_multipleStatementsAreRemoved() {
303
		$statement1 = new Statement( $this->newSnak( 'P24', 'foo' ), null, null, 'foo' );
304
		$statement2 = new Statement( $this->newSnak( 'P32', 'bar' ), null, null, 'bar' );
305
		$statement3 = new Statement( $this->newSnak( 'P32', 'bar' ), null, null, 'bar' );
306
307
		$list = new StatementList( [ $statement1, $statement2, $statement3 ] );
308
		$list->removeStatementsWithGuid( 'bar' );
309
310
		$this->assertEquals( new StatementList( $statement1 ), $list );
311
	}
312
313
	public function testGivenNotPresentGuid_listIsNotModified() {
314
		$statement1 = new Statement( $this->newSnak( 'P24', 'foo' ), null, null, 'foo' );
315
		$statement2 = new Statement( $this->newSnak( 'P32', 'bar' ), null, null, 'bar' );
316
		$statement3 = new Statement( $this->newSnak( 'P32', 'bar' ), null, null, 'bar' );
317
318
		$list = new StatementList( [ $statement1, $statement2, $statement3 ] );
319
		$list->removeStatementsWithGuid( 'baz' );
320
321
		$this->assertEquals( new StatementList( $statement1, $statement2, $statement3 ), $list );
322
	}
323
324
	public function testGivenNullGuid_allStatementsWithNoGuidAreRemoved() {
325
		$statement1 = new Statement( $this->newSnak( 'P24', 'foo' ), null, null, 'foo' );
326
		$statement2 = new Statement( $this->newSnak( 'P32', 'bar' ) );
327
		$statement3 = new Statement( $this->newSnak( 'P32', 'bar' ) );
328
329
		$list = new StatementList( [ $statement1, $statement2, $statement3 ] );
330
		$list->removeStatementsWithGuid( null );
331
332
		$this->assertEquals( new StatementList( $statement1 ), $list );
333
	}
334
335
	public function testCanConstructWithTraversableContainingOnlyStatements() {
336
		$statementArray = [
337
			$this->getStatementWithSnak( 'P1', 'foo' ),
338
			$this->getStatementWithSnak( 'P2', 'bar' ),
339
		];
340
341
		$object = new ArrayObject( $statementArray );
342
		$list = new StatementList( $object );
343
344
		$this->assertEquals(
345
			$statementArray,
346
			array_values( $list->toArray() )
347
		);
348
	}
349
350
	public function testGivenTraversableWithNonStatements_constructorThrowsException() {
351
		$traversable = new ArrayObject( [
352
			$this->getStatementWithSnak( 'P1', 'foo' ),
353
			new \stdClass(),
354
			$this->getStatementWithSnak( 'P2', 'bar' ),
355
		] );
356
357
		$this->setExpectedException( 'InvalidArgumentException' );
358
		new StatementList( $traversable );
359
	}
360
361
	public function testGivenNonTraversableOrArgList_constructorThrowsException() {
362
		$this->setExpectedException( 'InvalidArgumentException' );
363
		new StatementList( null );
364
	}
365
366
	public function testCanConstructWithStatement() {
367
		$statement = new Statement( $this->newSnak( 'P42', 'foo' ) );
368
369
		$this->assertEquals(
370
			new StatementList( [ $statement ] ),
371
			new StatementList( $statement )
372
		);
373
	}
374
375
	public function testCanConstructWithStatementArgumentList() {
376
		$statement0 = new Statement( $this->newSnak( 'P42', 'foo' ) );
377
		$statement1 = new Statement( $this->newSnak( 'P42', 'bar' ) );
378
		$statement2 = new Statement( $this->newSnak( 'P42', 'baz' ) );
379
380
		$this->assertEquals(
381
			new StatementList( [ $statement0, $statement1, $statement2 ] ),
382
			new StatementList( $statement0, $statement1, $statement2 )
383
		);
384
	}
385
386
	public function testGivenArgumentListWithNonStatement_constructorThrowsException() {
387
		$statement0 = new Statement( $this->newSnak( 'P42', 'foo' ) );
388
		$statement1 = new Statement( $this->newSnak( 'P42', 'bar' ) );
389
		$statement2 = new Statement( $this->newSnak( 'P42', 'baz' ) );
390
391
		$this->setExpectedException( 'InvalidArgumentException' );
392
		new StatementList( $statement0, $statement1, [], $statement2 );
393
	}
394
395
	public function testCountForEmptyList() {
396
		$list = new StatementList();
397
		$this->assertSame( 0, count( $list ) );
398
		$this->assertSame( 0, $list->count() );
399
	}
400
401
	public function testCountForNonEmptyList() {
402
		$list = new StatementList(
403
			$this->getStatementWithSnak( 'P1', 'foo' ),
404
			$this->getStatementWithSnak( 'P2', 'bar' )
405
		);
406
407
		$this->assertSame( 2, $list->count() );
408
	}
409
410
	/**
411
	 * @dataProvider statementArrayProvider
412
	 */
413
	public function testGivenIdenticalLists_equalsReturnsTrue( array $statements ) {
414
		$firstStatements = new StatementList( $statements );
415
		$secondStatements = new StatementList( $statements );
416
417
		$this->assertTrue( $firstStatements->equals( $secondStatements ) );
418
	}
419
420
	public function statementArrayProvider() {
421
		return [
422
			[ [
423
				$this->getStatementWithSnak( 'P1', 'foo' ),
424
				$this->getStatementWithSnak( 'P2', 'bar' ),
425
			] ],
426
			[ [
427
				$this->getStatementWithSnak( 'P1', 'foo' ),
428
			] ],
429
			[ [
430
			] ],
431
		];
432
	}
433
434
	public function testGivenDifferentLists_equalsReturnsFalse() {
435
		$firstStatements = new StatementList(
436
			$this->getStatementWithSnak( 'P1', 'foo' ),
437
			$this->getStatementWithSnak( 'P2', 'bar' )
438
		);
439
440
		$secondStatements = new StatementList(
441
			$this->getStatementWithSnak( 'P1', 'foo' ),
442
			$this->getStatementWithSnak( 'P2', 'SPAM' )
443
		);
444
445
		$this->assertFalse( $firstStatements->equals( $secondStatements ) );
446
	}
447
448
	public function testGivenListsWithDifferentDuplicates_equalsReturnsFalse() {
449
		$firstStatements = new StatementList(
450
			$this->getStatementWithSnak( 'P1', 'foo' ),
451
			$this->getStatementWithSnak( 'P1', 'foo' ),
452
			$this->getStatementWithSnak( 'P2', 'bar' )
453
		);
454
455
		$secondStatements = new StatementList(
456
			$this->getStatementWithSnak( 'P1', 'foo' ),
457
			$this->getStatementWithSnak( 'P2', 'bar' ),
458
			$this->getStatementWithSnak( 'P2', 'bar' )
459
		);
460
461
		$this->assertFalse( $firstStatements->equals( $secondStatements ) );
462
	}
463
464
	public function testGivenListsWithDifferentOrder_equalsReturnsFalse() {
465
		$firstStatements = new StatementList(
466
			$this->getStatementWithSnak( 'P1', 'foo' ),
467
			$this->getStatementWithSnak( 'P2', 'bar' ),
468
			$this->getStatementWithSnak( 'P3', 'baz' )
469
		);
470
471
		$secondStatements = new StatementList(
472
			$this->getStatementWithSnak( 'P1', 'foo' ),
473
			$this->getStatementWithSnak( 'P3', 'baz' ),
474
			$this->getStatementWithSnak( 'P2', 'bar' )
475
		);
476
477
		$this->assertFalse( $firstStatements->equals( $secondStatements ) );
478
	}
479
480
	public function testEmptyListDoesNotEqualNonEmptyList() {
481
		$firstStatements = new StatementList();
482
483
		$secondStatements = new StatementList(
484
			$this->getStatementWithSnak( 'P1', 'foo' ),
485
			$this->getStatementWithSnak( 'P3', 'baz' ),
486
			$this->getStatementWithSnak( 'P2', 'bar' )
487
		);
488
489
		$this->assertFalse( $firstStatements->equals( $secondStatements ) );
490
	}
491
492
	public function testNonEmptyListDoesNotEqualEmptyList() {
493
		$firstStatements = new StatementList(
494
			$this->getStatementWithSnak( 'P1', 'foo' ),
495
			$this->getStatementWithSnak( 'P3', 'baz' ),
496
			$this->getStatementWithSnak( 'P2', 'bar' )
497
		);
498
499
		$secondStatements = new StatementList();
500
501
		$this->assertFalse( $firstStatements->equals( $secondStatements ) );
502
	}
503
504
	public function testEmptyListIsEmpty() {
505
		$list = new StatementList();
506
507
		$this->assertTrue( $list->isEmpty() );
508
	}
509
510
	public function testNonEmptyListIsNotEmpty() {
511
		$list = new StatementList( $this->getStatementWithSnak( 'P1', 'foo' ) );
512
513
		$this->assertFalse( $list->isEmpty() );
514
	}
515
516
	public function testGetMainSnaks() {
517
		$list = new StatementList();
518
519
		$list->addNewStatement( new PropertyNoValueSnak( 42 ) );
520
		$list->addNewStatement( new PropertyNoValueSnak( 1337 ), [ new PropertyNoValueSnak( 32202 ) ] );
521
		$list->addNewStatement( new PropertyNoValueSnak( 9001 ) );
522
523
		$this->assertEquals(
524
			[
525
				new PropertyNoValueSnak( 42 ),
526
				new PropertyNoValueSnak( 1337 ),
527
				new PropertyNoValueSnak( 9001 ),
528
			],
529
			$list->getMainSnaks()
530
		);
531
	}
532
533
	public function testGivenNotKnownPropertyId_getByPropertyIdReturnsEmptyList() {
534
		$list = new StatementList();
535
		$list->addNewStatement( new PropertyNoValueSnak( 42 ) );
536
537
		$this->assertEquals(
538
			new StatementList(),
539
			$list->getByPropertyId( new PropertyId( 'P2' ) )
540
		);
541
	}
542
543
	public function testGivenKnownPropertyId_getByPropertyIdReturnsListWithOnlyMatchingStatements() {
544
		$list = new StatementList();
545
		$list->addNewStatement( new PropertyNoValueSnak( 42 ) );
546
		$list->addNewStatement( new PropertyNoValueSnak( 9001 ) );
547
		$list->addNewStatement( new PropertySomeValueSnak( 42 ) );
548
		$list->addNewStatement( new PropertySomeValueSnak( 9001 ) );
549
550
		$expected = new StatementList();
551
		$expected->addNewStatement( new PropertyNoValueSnak( 42 ) );
552
		$expected->addNewStatement( new PropertySomeValueSnak( 42 ) );
553
554
		$this->assertEquals(
555
			$expected,
556
			$list->getByPropertyId( new PropertyId( 'P42' ) )
557
		);
558
	}
559
560
	public function testGivenInvalidRank_getByRankReturnsEmptyList() {
561
		$list = new StatementList();
562
		$this->assertEquals( new StatementList(), $list->getByRank( 42 ) );
563
	}
564
565
	public function testGivenValidRank_getByRankReturnsOnlyMatchingStatements() {
566
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
567
		$statement->setRank( Statement::RANK_PREFERRED );
568
569
		$secondStatement = new Statement( new PropertyNoValueSnak( 1337 ) );
570
		$secondStatement->setRank( Statement::RANK_NORMAL );
571
572
		$thirdStatement = new Statement( new PropertyNoValueSnak( 9001 ) );
573
		$thirdStatement->setRank( Statement::RANK_DEPRECATED );
574
575
		$list = new StatementList( $statement, $secondStatement, $thirdStatement );
576
577
		$this->assertEquals(
578
			new StatementList( $statement ),
579
			$list->getByRank( Statement::RANK_PREFERRED )
580
		);
581
582
		$this->assertEquals(
583
			new StatementList( $secondStatement, $thirdStatement ),
584
			$list->getByRank( [ Statement::RANK_NORMAL, Statement::RANK_DEPRECATED ] )
585
		);
586
	}
587
588
	public function testWhenListIsEmpty_getBestStatementsReturnsEmptyList() {
589
		$list = new StatementList();
590
		$this->assertEquals( new StatementList(), $list->getBestStatements() );
591
	}
592
593
	public function testWhenOnlyDeprecatedStatements_getBestStatementsReturnsEmptyList() {
594
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
595
		$statement->setRank( Statement::RANK_DEPRECATED );
596
597
		$secondStatement = new Statement( new PropertyNoValueSnak( 9001 ) );
598
		$secondStatement->setRank( Statement::RANK_DEPRECATED );
599
600
		$list = new StatementList( $statement, $secondStatement );
601
		$this->assertEquals( new StatementList(), $list->getBestStatements() );
602
	}
603
604
	public function testWhenPreferredStatements_getBestStatementsReturnsOnlyThose() {
605
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
606
		$statement->setRank( Statement::RANK_PREFERRED );
607
608
		$secondStatement = new Statement( new PropertyNoValueSnak( 1337 ) );
609
		$secondStatement->setRank( Statement::RANK_NORMAL );
610
611
		$thirdStatement = new Statement( new PropertyNoValueSnak( 9001 ) );
612
		$thirdStatement->setRank( Statement::RANK_DEPRECATED );
613
614
		$fourthStatement = new Statement( new PropertyNoValueSnak( 23 ) );
615
		$fourthStatement->setRank( Statement::RANK_PREFERRED );
616
617
		$list = new StatementList( $statement, $secondStatement, $thirdStatement, $fourthStatement );
618
		$this->assertEquals(
619
			new StatementList( $statement, $fourthStatement ),
620
			$list->getBestStatements()
621
		);
622
	}
623
624
	public function testWhenNoPreferredStatements_getBestStatementsReturnsOnlyNormalOnes() {
625
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
626
		$statement->setRank( Statement::RANK_NORMAL );
627
628
		$secondStatement = new Statement( new PropertyNoValueSnak( 1337 ) );
629
		$secondStatement->setRank( Statement::RANK_NORMAL );
630
631
		$thirdStatement = new Statement( new PropertyNoValueSnak( 9001 ) );
632
		$thirdStatement->setRank( Statement::RANK_DEPRECATED );
633
634
		$list = new StatementList( $statement, $secondStatement, $thirdStatement );
635
		$this->assertEquals(
636
			new StatementList( $statement, $secondStatement ),
637
			$list->getBestStatements()
638
		);
639
	}
640
641
	public function testGivenNotPresentStatement_getFirstStatementWithGuidReturnsNull() {
642
		$statements = new StatementList();
643
644
		$this->assertNull( $statements->getFirstStatementWithGuid( 'kittens' ) );
645
	}
646
647
	public function testGivenPresentStatement_getFirstStatementWithGuidReturnsStatement() {
648
		$statement1 = $this->getStatement( 'P1', 'guid1' );
649
		$statement2 = $this->getStatement( 'P2', 'guid2' );
650
		$statement3 = $this->getStatement( 'P3', 'guid3' );
651
		$statements = new StatementList( $statement1, $statement2, $statement3 );
652
653
		$actual = $statements->getFirstStatementWithGuid( 'guid2' );
654
		$this->assertSame( $statement2, $actual );
655
	}
656
657
	public function testGivenDoublyPresentStatement_getFirstStatementWithGuidReturnsFirstMatch() {
658
		$statement1 = $this->getStatement( 'P1', 'guid1' );
659
		$statement2 = $this->getStatement( 'P2', 'guid2' );
660
		$statement3 = $this->getStatement( 'P3', 'guid3' );
661
		$statement4 = $this->getStatement( 'P2', 'guid2' );
662
		$statements = new StatementList( $statement1, $statement2, $statement3, $statement4 );
663
664
		$actual = $statements->getFirstStatementWithGuid( 'guid2' );
665
		$this->assertSame( $statement2, $actual );
666
	}
667
668
	public function testGivenStatementsWithNoGuid_getFirstStatementWithGuidReturnsFirstMatch() {
669
		$statement1 = $this->getStatement( 'P1', null );
670
		$statement2 = $this->getStatement( 'P2', null );
671
		$statements = new StatementList( $statement1, $statement2 );
672
673
		$actual = $statements->getFirstStatementWithGuid( null );
674
		$this->assertSame( $statement1, $actual );
675
	}
676
677
	public function testGivenInvalidGuid_getFirstStatementWithGuidReturnsNull() {
678
		$statements = new StatementList();
679
680
		$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...
681
	}
682
683
	public function testFilter() {
684
		$statement1 = new Statement( new PropertyNoValueSnak( 1 ) );
685
		$statement2 = new Statement( new PropertyNoValueSnak( 2 ) );
686
		$statement3 = new Statement( new PropertyNoValueSnak( 3 ) );
687
		$statement4 = new Statement( new PropertyNoValueSnak( 4 ) );
688
689
		$statement2->setReferences( new ReferenceList( [
690
			new Reference( [ new PropertyNoValueSnak( 20 ) ] )
691
		] ) );
692
693
		$statement3->setReferences( new ReferenceList( [
694
			new Reference( [ new PropertyNoValueSnak( 30 ) ] )
695
		] ) );
696
697
		$statements = new StatementList( $statement1, $statement2, $statement3, $statement4 );
698
699
		$this->assertEquals(
700
			new StatementList( $statement2, $statement3 ),
701
			$statements->filter( new ReferencedStatementFilter() )
702
		);
703
	}
704
705
	public function testClear() {
706
		$statement1 = $this->getStatement( 'P1', null );
707
		$statement2 = $this->getStatement( 'P2', null );
708
		$statements = new StatementList( $statement1, $statement2 );
709
710
		$statements->clear();
711
712
		$this->assertEquals( new StatementList(), $statements );
713
	}
714
715
}
716