Passed
Push — currentLimits ( af2764...193e29 )
by no
03:56
created

testGivenEqualSnakLists_getHashIsTheSame()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Snak;
4
5
use Comparable;
6
use DataValues\StringValue;
7
use Hashable;
8
use InvalidArgumentException;
9
use Wikibase\DataModel\Entity\PropertyId;
10
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
11
use Wikibase\DataModel\Snak\PropertyValueSnak;
12
use Wikibase\DataModel\Snak\Snak;
13
use Wikibase\DataModel\Snak\SnakList;
14
use Wikibase\DataModel\Tests\HashArray\HashArrayTest;
15
16
/**
17
 * @covers Wikibase\DataModel\Snak\SnakList
18
 * @uses DataValues\StringValue
19
 * @uses Wikibase\DataModel\Entity\PropertyId
20
 * @uses Wikibase\DataModel\Snak\PropertyNoValueSnak
21
 * @uses Wikibase\DataModel\Snak\PropertyValueSnak
22
 * @uses Wikibase\DataModel\Snak\Snak
23
 * @uses Wikibase\DataModel\Snak\SnakList
24
 * @uses Wikibase\DataModel\HashArray
25
 * @uses Wikibase\DataModel\Snak\SnakObject
26
 * @uses Wikibase\DataModel\Internal\MapValueHasher
27
 * @uses Wikibase\DataModel\Entity\EntityId
28
 *
29
 * @license GPL-2.0+
30
 * @author Jeroen De Dauw < [email protected] >
31
 * @author Addshore
32
 * @author Thiemo Mättig
33
 */
34
class SnakListTest extends HashArrayTest {
35
36
	public function elementInstancesProvider() {
37
		$id42 = new PropertyId( 'P42' );
38
39
		$argLists = [];
40
41
		$argLists[] = [ [ new PropertyNoValueSnak( $id42 ) ] ];
42
		$argLists[] = [ [ new PropertyNoValueSnak( new PropertyId( 'P9001' ) ) ] ];
43
		$argLists[] = [ [ new PropertyValueSnak( $id42, new StringValue( 'a' ) ) ] ];
44
45
		return $argLists;
46
	}
47
48
	public function instanceProvider() {
49
		$id42 = new PropertyId( 'P42' );
50
		$id9001 = new PropertyId( 'P9001' );
51
52
		return [
53
			[ new SnakList() ],
54
			[ new SnakList( [
55
				new PropertyNoValueSnak( $id42 )
56
			] ) ],
57
			[ new SnakList( [
58
				new PropertyNoValueSnak( $id42 ),
59
				new PropertyNoValueSnak( $id9001 ),
60
			] ) ],
61
			[ new SnakList( [
62
				new PropertyNoValueSnak( $id42 ),
63
				new PropertyNoValueSnak( $id9001 ),
64
				new PropertyValueSnak( $id42, new StringValue( 'a' ) ),
65
			] ) ],
66
		];
67
	}
68
69
	/**
70
	 * @dataProvider invalidConstructorArgumentsProvider
71
	 * @expectedException InvalidArgumentException
72
	 */
73
	public function testGivenInvalidConstructorArguments_constructorThrowsException( $input ) {
74
		new SnakList( $input );
75
	}
76
77
	public function invalidConstructorArgumentsProvider() {
78
		$id1 = new PropertyId( 'P1' );
79
80
		return [
81
			[ null ],
82
			[ false ],
83
			[ 1 ],
84
			[ 0.1 ],
85
			[ 'string' ],
86
			[ $id1 ],
87
			[ new PropertyNoValueSnak( $id1 ) ],
88
			[ new PropertyValueSnak( $id1, new StringValue( 'a' ) ) ],
89
			[ [ null ] ],
90
			[ [ $id1 ] ],
91
			[ [ new SnakList() ] ],
92
		];
93
	}
94
95
	public function testGivenAssociativeArray_constructorPreservesArrayKeys() {
96
		$snakList = new SnakList( [ 'key' => new PropertyNoValueSnak( 1 ) ] );
97
		$this->assertSame( [ 'key' ], array_keys( iterator_to_array( $snakList ) ) );
98
	}
99
100
	/**
101
	 * @dataProvider instanceProvider
102
	 * @param SnakList $array
103
	 */
104
	public function testHasSnak( SnakList $array ) {
105
		/**
106
		 * @var Snak $hashable
107
		 */
108
		foreach ( iterator_to_array( $array ) as $hashable ) {
109
			$this->assertTrue( $array->hasSnak( $hashable ) );
110
			$this->assertTrue( $array->hasSnakHash( $hashable->getHash() ) );
111
			$array->removeSnak( $hashable );
112
			$this->assertFalse( $array->hasSnak( $hashable ) );
113
			$this->assertFalse( $array->hasSnakHash( $hashable->getHash() ) );
114
		}
115
116
		$this->assertTrue( true );
117
	}
118
119
	/**
120
	 * @dataProvider instanceProvider
121
	 * @param SnakList $array
122
	 */
123
	public function testRemoveSnak( SnakList $array ) {
124
		$elementCount = $array->count();
125
126
		/**
127
		 * @var Snak $element
128
		 */
129
		foreach ( iterator_to_array( $array ) as $element ) {
130
			$this->assertTrue( $array->hasSnak( $element ) );
131
132
			if ( $elementCount % 2 === 0 ) {
133
				$array->removeSnak( $element );
134
			} else {
135
				$array->removeSnakHash( $element->getHash() );
136
			}
137
138
			$this->assertFalse( $array->hasSnak( $element ) );
139
			$this->assertEquals( --$elementCount, $array->count() );
140
		}
141
142
		$element = new PropertyNoValueSnak( new PropertyId( 'P42' ) );
143
144
		$array->removeSnak( $element );
145
		$array->removeSnakHash( $element->getHash() );
146
147
		$this->assertTrue( true );
148
	}
149
150
	/**
151
	 * @dataProvider instanceProvider
152
	 * @param SnakList $array
153
	 */
154
	public function testAddSnak( SnakList $array ) {
155
		$elementCount = $array->count();
156
157
		$elements = $this->elementInstancesProvider();
158
		$element = array_shift( $elements );
159
		$element = $element[0][0];
160
161
		if ( !$array->hasSnak( $element ) ) {
162
			++$elementCount;
163
		}
164
165
		$this->assertEquals( !$array->hasSnak( $element ), $array->addSnak( $element ) );
166
167
		$this->assertEquals( $elementCount, $array->count() );
168
169
		$this->assertFalse( $array->addSnak( $element ) );
170
171
		$this->assertEquals( $elementCount, $array->count() );
172
	}
173
174
	public function orderByPropertyProvider() {
175
		$id1 = new PropertyId( 'P1' );
176
		$id2 = new PropertyId( 'P2' );
177
		$id3 = new PropertyId( 'P3' );
178
179
		/**
180
		 * List of test data containing snaks to initialize SnakList objects. The first list of
181
		 * snaks represents the snak list to be used as test input while the second represents the
182
		 * expected result.
183
		 * @var array
184
		 */
185
		$rawArguments = [
186
			'Default order' => [
187
				[],
188
				[],
189
			],
190
			'Unknown id in order' => [
191
				[],
192
				[],
193
				[ 'P1' ]
194
			],
195
			[
196
				[ new PropertyNoValueSnak( $id1 ) ],
197
				[ new PropertyNoValueSnak( $id1 ) ],
198
			],
199
			[
200
				[
201
					new PropertyNoValueSnak( $id2 ),
202
					new PropertyNoValueSnak( $id1 ),
203
				],
204
				[
205
					new PropertyNoValueSnak( $id2 ),
206
					new PropertyNoValueSnak( $id1 ),
207
				],
208
			],
209
			[
210
				[
211
					new PropertyNoValueSnak( $id1 ),
212
					new PropertyNoValueSnak( $id2 ),
213
					new PropertyValueSnak( $id1, new StringValue( 'a' ) ),
214
				],
215
				[
216
					new PropertyNoValueSnak( $id1 ),
217
					new PropertyValueSnak( $id1, new StringValue( 'a' ) ),
218
					new PropertyNoValueSnak( $id2 ),
219
				],
220
			],
221
			'With additional order' => [
222
				[
223
					new PropertyNoValueSnak( $id3 ),
224
					new PropertyNoValueSnak( $id2 ),
225
					new PropertyValueSnak( $id1, new StringValue( 'a' ) ),
226
				],
227
				[
228
					new PropertyNoValueSnak( $id2 ),
229
					new PropertyNoValueSnak( $id3 ),
230
					new PropertyValueSnak( $id1, new StringValue( 'a' ) ),
231
				],
232
				[ 'P2' ]
233
			],
234
			[
235
				[
236
					new PropertyNoValueSnak( $id3 ),
237
					new PropertyNoValueSnak( $id2 ),
238
					new PropertyNoValueSnak( $id2 ),
239
					new PropertyValueSnak( $id1, new StringValue( 'a' ) ),
240
					new PropertyNoValueSnak( $id1 ),
241
				],
242
				[
243
					new PropertyValueSnak( $id1, new StringValue( 'a' ) ),
244
					new PropertyNoValueSnak( $id1 ),
245
					new PropertyNoValueSnak( $id3 ),
246
					new PropertyNoValueSnak( $id2 ),
247
					new PropertyNoValueSnak( $id2 ),
248
				],
249
				[ 'P1' ]
250
			],
251
		];
252
253
		$arguments = [];
254
255
		foreach ( $rawArguments as $key => $rawArgument ) {
256
			$arguments[$key] = [
257
				new SnakList( $rawArgument[0] ),
258
				new SnakList( $rawArgument[1] ),
259
				array_key_exists( 2, $rawArgument ) ? $rawArgument[2] : []
260
			];
261
		}
262
263
		return $arguments;
264
	}
265
266
	/**
267
	 * @dataProvider orderByPropertyProvider
268
	 */
269
	public function testOrderByProperty( SnakList $snakList, SnakList $expected, array $order = [] ) {
270
		$initialSnakList = new SnakList( array_values( iterator_to_array( $snakList ) ) );
271
272
		$snakList->orderByProperty( $order );
273
274
		// Instantiate new SnakList resetting the snaks' array keys. This allows comparing the
275
		// reordered SnakList to the expected SnakList.
276
		$orderedSnakList = new SnakList( array_values( iterator_to_array( $snakList ) ) );
277
278
		$this->assertEquals( $expected, $orderedSnakList );
279
280
		if ( $orderedSnakList->equals( $initialSnakList ) ) {
281
			$this->assertSame( $initialSnakList->getHash(), $snakList->getHash() );
282
		} else {
283
			$this->assertNotSame( $initialSnakList->getHash(), $snakList->getHash() );
284
		}
285
	}
286
287
	public function testComparableInterface() {
288
		$this->assertInstanceOf( Comparable::class, new SnakList() );
289
	}
290
291
	/**
292
	 * @dataProvider equalsProvider
293
	 */
294
	public function testEquals( SnakList $list1, SnakList $list2, $expected ) {
295
		$this->assertSame( $expected, $list1->equals( $list2 ) );
296
	}
297
298
	public function equalsProvider() {
299
		$empty = new SnakList();
300
		$oneSnak = new SnakList( [ new PropertyNoValueSnak( 1 ) ] );
301
302
		return [
303
			'empty object is equal to itself' => [
304
				$empty,
305
				$empty,
306
				true
307
			],
308
			'non-empty object is equal to itself' => [
309
				$oneSnak,
310
				$oneSnak,
311
				true
312
			],
313
			'different empty objects are equal' => [
314
				$empty,
315
				new SnakList(),
316
				true
317
			],
318
			'different objects with same content are equal' => [
319
				$oneSnak,
320
				new SnakList( [ new PropertyNoValueSnak( 1 ) ] ),
321
				true
322
			],
323
			'different objects with different content are not equal' => [
324
				$oneSnak,
325
				new SnakList( [ new PropertyNoValueSnak( 2 ) ] ),
326
				false
327
			],
328
		];
329
	}
330
331
	public function testHashableInterface() {
332
		$this->assertInstanceOf( Hashable::class, new SnakList() );
333
	}
334
335
	public function testGetHash() {
336
		$snakList = new SnakList( [ new PropertyNoValueSnak( 1 ) ] );
337
		$hash = $snakList->getHash();
338
339
		$this->assertInternalType( 'string', $hash, 'must be a string' );
340
		$this->assertNotSame( '', $hash, 'must not be empty' );
341
		$this->assertSame( $hash, $snakList->getHash(), 'second call must return the same hash' );
342
343
		$otherList = new SnakList( [ new PropertyNoValueSnak( 2 ) ] );
344
		$this->assertNotSame( $hash, $otherList->getHash() );
345
	}
346
347
	/**
348
	 * This integration test (relies on SnakObject::getHash) is supposed to break whenever the hash
349
	 * calculation changes.
350
	 */
351
	public function testHashStability() {
352
		$snakList = new SnakList();
353
		$this->assertSame( 'da39a3ee5e6b4b0d3255bfef95601890afd80709', $snakList->getHash() );
354
355
		$snakList = new SnakList( [ new PropertyNoValueSnak( 1 ) ] );
356
		$this->assertSame( '4327ac5109aaf437ccce05580c563a5857d96c82', $snakList->getHash() );
357
	}
358
359
	/**
360
	 * @dataProvider provideEqualSnakLists
361
	 */
362
	public function testGivenEqualSnakLists_getHashIsTheSame( SnakList $self, SnakList $other ) {
363
		$this->assertSame( $self->getHash(), $other->getHash() );
364
	}
365
366
	public function provideEqualSnakLists() {
367
		$empty = new SnakList();
368
		$oneSnak = new SnakList( [ new PropertyNoValueSnak( 1 ) ] );
369
370
		return [
371
			'same empty object' => [ $empty, $empty ],
372
			'same non-empty object' => [ $oneSnak, $oneSnak ],
373
			'equal empty objects' => [ $empty, new SnakList() ],
374
			'equal non-empty objects' => [ $oneSnak, new SnakList( [ new PropertyNoValueSnak( 1 ) ] ) ],
375
		];
376
	}
377
378
}
379