Test Setup Failed
Push — master ( cd1faf...671641 )
by adam
02:13
created

StatementTest::testHashStability()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Statement;
4
5
use DataValues\StringValue;
6
use InvalidArgumentException;
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\Statement;
16
17
/**
18
 * @covers \Wikibase\DataModel\Statement\Statement
19
 *
20
 * @group Wikibase
21
 * @group WikibaseDataModel
22
 *
23
 * @license GPL-2.0-or-later
24
 * @author Jeroen De Dauw < [email protected] >
25
 */
26
class StatementTest extends \PHPUnit\Framework\TestCase {
27
28
	public function testMinimalConstructor() {
29
		$mainSnak = new PropertyNoValueSnak( 1 );
30
		$statement = new Statement( $mainSnak );
31
		$this->assertTrue( $mainSnak->equals( $statement->getMainSnak() ) );
32
	}
33
34
	/**
35
	 * @dataProvider validConstructorArgumentsProvider
36
	 */
37
	public function testConstructorWithValidArguments(
38
		Snak $mainSnak,
39
		?SnakList $qualifiers,
40
		?ReferenceList $references,
41
		$guid
42
	) {
43
		$statement = new Statement( $mainSnak, $qualifiers, $references, $guid );
44
		$this->assertTrue( $statement->getMainSnak()->equals( $mainSnak ) );
45
		$this->assertTrue( $statement->getQualifiers()->equals( $qualifiers ?: new SnakList() ) );
46
		$this->assertTrue( $statement->getReferences()->equals( $references ?: new ReferenceList() ) );
47
		$this->assertSame( $guid, $statement->getGuid() );
48
	}
49
50
	public function validConstructorArgumentsProvider() {
51
		$snak = new PropertyNoValueSnak( 1 );
52
		$qualifiers = new SnakList( [ $snak ] );
53
		$references = new ReferenceList( [ new Reference( [ $snak ] ) ] );
54
55
		return [
56
			[ $snak, null, null, null ],
57
			[ $snak, null, null, 'guid' ],
58
			[ $snak, $qualifiers, $references, 'guid' ],
59
		];
60
	}
61
62
	/**
63
	 * @dataProvider instanceProvider
64
	 */
65
	public function testSetGuid( Statement $statement ) {
66
		$statement->setGuid( 'foo-bar-baz' );
67
		$this->assertEquals( 'foo-bar-baz', $statement->getGuid() );
68
	}
69
70
	/**
71
	 * @dataProvider instanceProvider
72
	 */
73
	public function testGetGuid( Statement $statement ) {
74
		$guid = $statement->getGuid();
75
		$this->assertTrue( $guid === null || is_string( $guid ) );
76
		$this->assertEquals( $guid, $statement->getGuid() );
77
78
		$statement->setGuid( 'foobar' );
79
		$this->assertEquals( 'foobar', $statement->getGuid() );
80
	}
81
82
	public function testHashStability() {
83
		$mainSnak = new PropertyNoValueSnak( new PropertyId( 'P42' ) );
84
		$statement = new Statement( $mainSnak );
85
		$this->assertSame( '50c73da6759fd31868fb0cc9c218969fa776f62c', $statement->getHash() );
86
	}
87
88
	public function testSetAndGetMainSnak() {
89
		$mainSnak = new PropertyNoValueSnak( new PropertyId( 'P42' ) );
90
		$statement = new Statement( $mainSnak );
91
		$this->assertSame( $mainSnak, $statement->getMainSnak() );
92
	}
93
94
	public function testSetAndGetQualifiers() {
95
		$qualifiers = new SnakList( [
96
			new PropertyValueSnak( new PropertyId( 'P42' ), new StringValue( 'a' ) )
97
		] );
98
99
		$statement = new Statement(
100
			new PropertyNoValueSnak( new PropertyId( 'P42' ) ),
101
			$qualifiers
102
		);
103
104
		$this->assertSame( $qualifiers, $statement->getQualifiers() );
105
	}
106
107
	/**
108
	 * @dataProvider instanceProvider
109
	 */
110
	public function testSerialize( Statement $statement ) {
111
		$copy = unserialize( serialize( $statement ) );
112
113
		$this->assertEquals( $statement->getHash(), $copy->getHash(), 'Serialization roundtrip should not affect hash' );
114
	}
115
116
	public function testGuidDoesNotAffectHash() {
117
		$statement0 = new Statement( new PropertyNoValueSnak( 42 ) );
118
		$statement0->setGuid( 'statement0' );
119
120
		$statement1 = new Statement( new PropertyNoValueSnak( 42 ) );
121
		$statement1->setGuid( 'statement1' );
122
123
		$this->assertEquals( $statement0->getHash(), $statement1->getHash() );
124
	}
125
126
	/**
127
	 * @dataProvider invalidGuidProvider
128
	 */
129
	public function testGivenInvalidGuid_constructorThrowsException( $guid ) {
130
		$this->expectException( InvalidArgumentException::class );
131
		new Statement( new PropertyNoValueSnak( 1 ), null, null, $guid );
132
	}
133
134
	/**
135
	 * @dataProvider invalidGuidProvider
136
	 */
137
	public function testGivenInvalidGuid_setGuidThrowsException( $guid ) {
138
		$this->expectException( InvalidArgumentException::class );
139
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
140
		$statement->setGuid( $guid );
141
	}
142
143
	public function invalidGuidProvider() {
144
		$snak = new PropertyNoValueSnak( 1 );
145
146
		return [
147
			[ false ],
148
			[ 1 ],
149
			[ $snak ],
150
			[ new Statement( $snak ) ],
151
		];
152
	}
153
154
	public function instanceProvider() {
155
		$instances = [];
156
157
		$propertyId = new PropertyId( 'P42' );
158
		$baseInstance = new Statement( new PropertyNoValueSnak( $propertyId ) );
159
160
		$instances[] = $baseInstance;
161
162
		$instance = clone $baseInstance;
163
		$instance->setRank( Statement::RANK_PREFERRED );
164
165
		$instances[] = $instance;
166
167
		$newInstance = clone $instance;
168
169
		$instances[] = $newInstance;
170
171
		$instance = clone $baseInstance;
172
173
		$instance->setReferences( new ReferenceList( [
174
			new Reference( [
175
				new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) )
176
			] )
177
		] ) );
178
179
		$instances[] = $instance;
180
181
		$argLists = [];
182
183
		foreach ( $instances as $instance ) {
184
			$argLists[] = [ $instance ];
185
		}
186
187
		return $argLists;
188
	}
189
190
	/**
191
	 * @dataProvider instanceProvider
192
	 */
193
	public function testGetReferences( Statement $statement ) {
194
		$this->assertInstanceOf( ReferenceList::class, $statement->getReferences() );
195
	}
196
197
	/**
198
	 * @dataProvider instanceProvider
199
	 */
200
	public function testSetReferences( Statement $statement ) {
201
		$references = new ReferenceList( [
202
			new Reference( [
203
				new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) ),
204
			] )
205
		] );
206
207
		$statement->setReferences( $references );
208
209
		$this->assertEquals( $references, $statement->getReferences() );
210
	}
211
212
	/**
213
	 * @dataProvider instanceProvider
214
	 */
215
	public function testAddNewReferenceWithVariableArgumentsSyntax( Statement $statement ) {
216
		$snak1 = new PropertyNoValueSnak( 256 );
217
		$snak2 = new PropertySomeValueSnak( 42 );
218
		$statement->addNewReference( $snak1, $snak2 );
219
220
		$expectedSnaks = [ $snak1, $snak2 ];
221
		$this->assertTrue( $statement->getReferences()->hasReference( new Reference( $expectedSnaks ) ) );
222
	}
223
224
	/**
225
	 * @dataProvider instanceProvider
226
	 */
227
	public function testAddNewReferenceWithAnArrayOfSnaks( Statement $statement ) {
228
		$snaks = [
229
			new PropertyNoValueSnak( 256 ),
230
			new PropertySomeValueSnak( 42 ),
231
		];
232
		$statement->addNewReference( $snaks );
233
234
		$this->assertTrue( $statement->getReferences()->hasReference( new Reference( $snaks ) ) );
235
	}
236
237
	/**
238
	 * @dataProvider instanceProvider
239
	 */
240
	public function testGetRank( Statement $statement ) {
241
		$rank = $statement->getRank();
242
		$this->assertIsInt( $rank );
243
244
		$ranks = [ Statement::RANK_DEPRECATED, Statement::RANK_NORMAL, Statement::RANK_PREFERRED ];
245
		$this->assertTrue( in_array( $rank, $ranks ), true );
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a string.

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...
246
	}
247
248
	/**
249
	 * @dataProvider instanceProvider
250
	 */
251
	public function testSetRank( Statement $statement ) {
252
		$statement->setRank( Statement::RANK_DEPRECATED );
253
		$this->assertEquals( Statement::RANK_DEPRECATED, $statement->getRank() );
254
	}
255
256
	/**
257
	 * @dataProvider instanceProvider
258
	 */
259
	public function testSetInvalidRank( Statement $statement ) {
260
		$this->expectException( InvalidArgumentException::class );
261
		$statement->setRank( 9001 );
262
	}
263
264
	/**
265
	 * @dataProvider instanceProvider
266
	 */
267
	public function testGetPropertyId( Statement $statement ) {
268
		$this->assertEquals(
269
			$statement->getMainSnak()->getPropertyId(),
270
			$statement->getPropertyId()
271
		);
272
	}
273
274
	/**
275
	 * @dataProvider instanceProvider
276
	 */
277
	public function testGetAllSnaks( Statement $statement ) {
278
		$snaks = $statement->getAllSnaks();
279
280
		$c = count( $statement->getQualifiers() ) + 1;
281
282
		/* @var Reference $reference */
283
		foreach ( $statement->getReferences() as $reference ) {
284
			$c += count( $reference->getSnaks() );
285
		}
286
287
		$this->assertGreaterThanOrEqual( $c, count( $snaks ), 'At least one snak per Qualifier and Reference' );
288
	}
289
290
	public function testGivenNonStatement_equalsReturnsFalse() {
291
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
292
293
		$this->assertFalse( $statement->equals( null ) );
294
		$this->assertFalse( $statement->equals( 42 ) );
295
		$this->assertFalse( $statement->equals( new \stdClass() ) );
296
	}
297
298
	public function testGivenSameStatement_equalsReturnsTrue() {
299
		$statement = new Statement(
300
			new PropertyNoValueSnak( 42 ),
301
			new SnakList( [
302
				new PropertyNoValueSnak( 1337 ),
303
			] ),
304
			new ReferenceList( [
305
				new Reference( [ new PropertyNoValueSnak( 1337 ) ] ),
306
			] )
307
		);
308
309
		$statement->setGuid( 'kittens' );
310
311
		$this->assertTrue( $statement->equals( $statement ) );
312
		$this->assertTrue( $statement->equals( clone $statement ) );
313
	}
314
315
	public function testGivenStatementWithDifferentProperty_equalsReturnsFalse() {
316
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
317
		$this->assertFalse( $statement->equals( new Statement( new PropertyNoValueSnak( 43 ) ) ) );
318
	}
319
320
	public function testGivenStatementWithDifferentSnakType_equalsReturnsFalse() {
321
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
322
		$this->assertFalse( $statement->equals( new Statement( new PropertySomeValueSnak( 42 ) ) ) );
323
	}
324
325
	public function testStatementWithDifferentQualifiers_equalsReturnsFalse() {
326
		$statement = new Statement(
327
			new PropertyNoValueSnak( 42 ),
328
			new SnakList( [
329
				new PropertyNoValueSnak( 1337 ),
330
			] )
331
		);
332
333
		$differentStatement = new Statement(
334
			new PropertyNoValueSnak( 42 ),
335
			new SnakList( [
336
				new PropertyNoValueSnak( 32202 ),
337
			] )
338
		);
339
340
		$this->assertFalse( $statement->equals( $differentStatement ) );
341
	}
342
343
	public function testGivenStatementWithDifferentGuids_equalsReturnsFalse() {
344
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
345
346
		$differentStatement = new Statement( new PropertyNoValueSnak( 42 ) );
347
		$differentStatement->setGuid( 'kittens' );
348
349
		$this->assertFalse( $statement->equals( $differentStatement ) );
350
	}
351
352
	public function testStatementWithDifferentReferences_equalsReturnsFalse() {
353
		$statement = new Statement(
354
			new PropertyNoValueSnak( 42 ),
355
			new SnakList(),
356
			new ReferenceList( [
357
				new Reference( [ new PropertyNoValueSnak( 1337 ) ] ),
358
			] )
359
		);
360
361
		$differentStatement = new Statement(
362
			new PropertyNoValueSnak( 42 ),
363
			new SnakList(),
364
			new ReferenceList( [
365
				new Reference( [ new PropertyNoValueSnak( 32202 ) ] ),
366
			] )
367
		);
368
369
		$this->assertFalse( $statement->equals( $differentStatement ) );
370
	}
371
372
	public function testEquals() {
373
		$statement = $this->newStatement();
374
		$target = $this->newStatement();
375
376
		$this->assertTrue( $statement->equals( $target ) );
377
	}
378
379
	/**
380
	 * @dataProvider notEqualsProvider
381
	 */
382
	public function testNotEquals( Statement $statement, Statement $target, $message ) {
383
		$this->assertFalse( $statement->equals( $target ), $message );
384
	}
385
386
	public function notEqualsProvider() {
387
		$statement = $this->newStatement();
388
389
		$statementWithoutQualifiers = $this->newStatement();
390
		$statementWithoutQualifiers->setQualifiers( new SnakList() );
391
392
		$statementWithoutReferences = $this->newStatement();
393
		$statementWithoutReferences->setReferences( new ReferenceList() );
394
395
		$statementWithPreferredRank = $this->newStatement();
396
		$statementWithPreferredRank->setRank( Statement::RANK_PREFERRED );
397
398
		$statementMainSnakNotEqual = $this->newStatement();
399
		$statementMainSnakNotEqual->setMainSnak( new PropertyNoValueSnak( 9000 ) );
400
401
		return [
402
			[ $statement, $statementWithoutQualifiers, 'qualifiers not equal' ],
403
			[ $statement, $statementWithoutReferences, 'references not equal' ],
404
			[ $statement, $statementWithPreferredRank, 'rank not equal' ],
405
			[ $statement, $statementMainSnakNotEqual, 'main snak not equal' ]
406
		];
407
	}
408
409
	private function newStatement() {
410
		$qualifiers = new SnakList( [ new PropertyNoValueSnak( 23 ) ] );
411
412
		$statement = new Statement(
413
			new PropertyNoValueSnak( 42 ),
414
			$qualifiers,
415
			new ReferenceList( [
416
				new Reference( [ new PropertyNoValueSnak( 1337 ) ] ),
417
			] )
418
		);
419
420
		$statement->setRank( Statement::RANK_NORMAL );
421
422
		return $statement;
423
	}
424
425
	public function testHashesOfDifferentStatementsAreNotTheSame() {
426
		$this->assertNotSame(
427
			( new Statement( new PropertyNoValueSnak( 1 ) ) )->getHash(),
428
			( new Statement( new PropertyNoValueSnak( 2 ) ) )->getHash()
429
		);
430
	}
431
432
	public function testHashesOfEqualStatementsAreTheSame() {
433
		$this->assertSame(
434
			( new Statement( new PropertyNoValueSnak( 1 ) ) )->getHash(),
435
			( new Statement( new PropertyNoValueSnak( 1 ) ) )->getHash()
436
		);
437
	}
438
439
}
440