Completed
Push — rm-hasharray ( 75dec0...40b512 )
by no
03:10
created

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