Completed
Pull Request — master (#603)
by no
06:45 queued 03:38
created

ByPropertyIdArrayTest::testAddObjectAtIndex()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 13
rs 9.4285
cc 1
eloc 9
nc 1
nop 4
1
<?php
2
3
namespace Wikibase\DataModel\Tests;
4
5
use ArrayObject;
6
use DataValues\StringValue;
7
use ReflectionClass;
8
use ReflectionMethod;
9
use Wikibase\DataModel\ByPropertyIdArray;
10
use Wikibase\DataModel\Entity\PropertyId;
11
use Wikibase\DataModel\PropertyIdProvider;
12
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
13
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
14
use Wikibase\DataModel\Snak\PropertyValueSnak;
15
use Wikibase\DataModel\Snak\Snak;
16
use Wikibase\DataModel\Statement\Statement;
17
18
/**
19
 * @covers Wikibase\DataModel\ByPropertyIdArray
20
 *
21
 * @group Wikibase
22
 * @group WikibaseDataModel
23
 * @group ByPropertyIdArrayTest
24
 *
25
 * @licence GNU GPL v2+
26
 * @author Jeroen De Dauw < [email protected] >
27
 * @author H. Snater < [email protected] >
28
 */
29
class ByPropertyIdArrayTest extends \PHPUnit_Framework_TestCase {
30
31
	public function testArrayObjectNotConstructedFromObject() {
32
		$statement1 = new Statement( new PropertyNoValueSnak( 1 ) );
33
		$statement1->setGuid( '1' );
34
		$statement2 = new Statement( new PropertyNoValueSnak( 2 ) );
35
		$statement2->setGuid( '2' );
36
37
		$object = new ArrayObject();
38
		$object->append( $statement1 );
39
40
		$byPropertyIdArray = new ByPropertyIdArray( $object );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
41
		// According to the documentation append() "cannot be called when the ArrayObject was
42
		// constructed from an object." This test makes sure it was not constructed from an object.
43
		$byPropertyIdArray->append( $statement2 );
44
45
		$this->assertCount( 2, $byPropertyIdArray );
46
	}
47
48
	/**
49
	 * Returns an accessible ReflectionMethod of ByPropertyIdArray.
50
	 *
51
	 * @param string $methodName
52
	 * @return ReflectionMethod
53
	 */
54
	protected static function getMethod( $methodName ) {
55
		$class = new ReflectionClass( 'Wikibase\DataModel\ByPropertyIdArray' );
56
		$method = $class->getMethod( $methodName );
57
		$method->setAccessible( true );
58
		return $method;
59
	}
60
61
	public function listProvider() {
62
		$lists = array();
63
64
		$snaks = array(
65
			new PropertyNoValueSnak( new PropertyId( 'P42' ) ),
66
			new PropertySomeValueSnak( new PropertyId( 'P42' ) ),
67
			new PropertySomeValueSnak( new PropertyId( 'P10' ) ),
68
			new PropertyValueSnak( new PropertyId( 'P10' ), new StringValue( 'ohi' ) ),
69
			new PropertySomeValueSnak( new PropertyId( 'P1' ) ),
70
		);
71
72
		$lists[] = $snaks;
73
74
		$lists[] = array_map(
75
			function( Snak $snak ) {
76
				return new Statement( $snak );
77
			},
78
			$snaks
79
		);
80
81
		$argLists = array();
82
83
		foreach ( $lists as $list ) {
84
			$argLists[] = array( $list );
85
		}
86
87
		return $argLists;
88
	}
89
90
	/**
91
	 * @return Statement[]
92
	 */
93
	protected function statementsProvider() {
94
		$snaks = array(
95
			new PropertyNoValueSnak( new PropertyId( 'P1' ) ),
96
			new PropertySomeValueSnak( new PropertyId( 'P1' ) ),
97
			new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'a' ) ),
98
			new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'b' ) ),
99
			new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'c' ) ),
100
			new PropertySomeValueSnak( new PropertyId( 'P3' ) ),
101
		);
102
103
		return array_map(
104
			function( Snak $snak ) {
105
				return new Statement( $snak );
106
			},
107
			$snaks
108
		);
109
	}
110
111
	/**
112
	 * @dataProvider listProvider
113
	 * @param PropertyIdProvider[] $objects
114
	 */
115
	public function testGetIds( array $objects ) {
116
		$indexedArray = new ByPropertyIdArray( $objects );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
117
118
		$expected = array();
119
120
		foreach ( $objects as $object ) {
121
			$expected[] = $object->getPropertyId();
122
		}
123
124
		$expected = array_unique( $expected );
125
126
		$indexedArray->buildIndex();
127
128
		$this->assertEquals(
129
			array_values( $expected ),
130
			array_values( $indexedArray->getPropertyIds() )
131
		);
132
	}
133
134
	/**
135
	 * @dataProvider listProvider
136
	 * @param PropertyIdProvider[] $objects
137
	 */
138
	public function testGetById( array $objects ) {
139
		$indexedArray = new ByPropertyIdArray( $objects );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
140
141
		$ids = array();
142
143
		foreach ( $objects as $object ) {
144
			$ids[] = $object->getPropertyId();
145
		}
146
147
		$ids = array_unique( $ids );
148
149
		$indexedArray->buildIndex();
150
151
		$allObtainedObjects = array();
152
153
		foreach ( $ids as $id ) {
154
			foreach ( $indexedArray->getByPropertyId( $id ) as $obtainedObject ) {
155
				$allObtainedObjects[] = $obtainedObject;
156
				$this->assertEquals( $id, $obtainedObject->getPropertyId() );
157
			}
158
		}
159
160
		$this->assertEquals(
161
			array_values( $objects ),
162
			array_values( $allObtainedObjects )
163
		);
164
	}
165
166
	/**
167
	 * @dataProvider listProvider
168
	 * @param PropertyIdProvider[] $objects
169
	 */
170
	public function testRemoveObject( array $objects ) {
171
		$lastIndex = count( $objects ) - 1;
172
		$indexedArray = new ByPropertyIdArray( $objects );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
173
		$indexedArray->buildIndex();
174
175
		$removeObject = self::getMethod( 'removeObject' );
176
177
		$removeObject->invokeArgs( $indexedArray, array( $objects[0] ) );
178
		$removeObject->invokeArgs( $indexedArray, array( $objects[$lastIndex] ) );
179
180
		$this->assertFalse(
181
			in_array( $objects[0], $indexedArray->getByPropertyId( $objects[0]->getPropertyId() ) )
182
		);
183
184
		$this->assertFalse( in_array(
185
			$objects[$lastIndex],
186
			$indexedArray->getByPropertyId( $objects[1]->getPropertyId() )
187
		) );
188
189
		$this->assertFalse( in_array( $objects[0], $indexedArray->toFlatArray() ) );
190
		$this->assertFalse( in_array( $objects[$lastIndex], $indexedArray->toFlatArray() ) );
191
	}
192
193
	public function testGetByNotSetIdThrowsException() {
194
		$indexedArray = new ByPropertyIdArray();
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
195
		$indexedArray->buildIndex();
196
197
		$this->setExpectedException( 'OutOfBoundsException' );
198
199
		$indexedArray->getByPropertyId( PropertyId::newFromNumber( 9000 ) );
200
	}
201
202
	public function testNotBuildExceptionIsThrownForByPropertyId() {
203
		$indexedArray = new ByPropertyIdArray();
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
204
205
		$this->setExpectedException( 'RuntimeException' );
206
		$indexedArray->getByPropertyId( PropertyId::newFromNumber( 9000 ) );
207
	}
208
209
	public function testNotBuildExceptionIsThrownForGetPropertyIds() {
210
		$indexedArray = new ByPropertyIdArray();
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
211
212
		$this->setExpectedException( 'RuntimeException' );
213
		$indexedArray->getPropertyIds();
214
	}
215
216
	/**
217
	 * @dataProvider listProvider
218
	 */
219
	public function testGetFlatArrayIndexOfObject( array $objects ) {
220
		$indexedArray = new ByPropertyIdArray( $objects );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
221
		$indexedArray->buildIndex();
222
223
		$indicesSource = array();
224
		$indicesDestination = array();
225
226
		$i = 0;
227
		foreach ( $objects as $object ) {
228
			$indicesSource[$i++] = $object;
229
			$indicesDestination[$indexedArray->getFlatArrayIndexOfObject( $object )] = $object;
230
		}
231
232
		$this->assertEquals( $indicesSource, $indicesDestination );
233
	}
234
235
	/**
236
	 * @dataProvider listProvider
237
	 */
238
	public function testToFlatArray( array $objects ) {
239
		$indexedArray = new ByPropertyIdArray( $objects );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
240
		$indexedArray->buildIndex();
241
242
		$this->assertEquals( $objects, $indexedArray->toFlatArray() );
243
	}
244
245
	public function moveProvider() {
246
		$c = $this->statementsProvider();
247
		$argLists = array();
248
249
		$argLists[] = array( $c, $c[0], 0, $c );
250
		$argLists[] = array( $c, $c[0], 1, array( $c[1], $c[0], $c[2], $c[3], $c[4], $c[5] ) );
251
		$argLists[] = array( $c, $c[0], 2, array( $c[1], $c[0], $c[2], $c[3], $c[4], $c[5] ) );
252
		$argLists[] = array( $c, $c[0], 3, array( $c[2], $c[3], $c[4], $c[1], $c[0], $c[5] ) );
253
		$argLists[] = array( $c, $c[0], 4, array( $c[2], $c[3], $c[4], $c[1], $c[0], $c[5] ) );
254
		$argLists[] = array( $c, $c[0], 5, array( $c[2], $c[3], $c[4], $c[1], $c[0], $c[5] ) );
255
		$argLists[] = array( $c, $c[0], 6, array( $c[2], $c[3], $c[4], $c[5], $c[1], $c[0] ) );
256
257
		$argLists[] = array( $c, $c[1], 0, array( $c[1], $c[0], $c[2], $c[3], $c[4], $c[5] ) );
258
		$argLists[] = array( $c, $c[1], 1, $c );
259
		$argLists[] = array( $c, $c[1], 2, $c );
260
		$argLists[] = array( $c, $c[1], 3, array( $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ) );
261
		$argLists[] = array( $c, $c[1], 4, array( $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ) );
262
		$argLists[] = array( $c, $c[1], 5, array( $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ) );
263
		$argLists[] = array( $c, $c[1], 6, array( $c[2], $c[3], $c[4], $c[5], $c[0], $c[1] ) );
264
265
		$argLists[] = array( $c, $c[2], 0, array( $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ) );
266
		$argLists[] = array( $c, $c[2], 1, $c );
267
		$argLists[] = array( $c, $c[2], 2, $c );
268
		$argLists[] = array( $c, $c[2], 3, array( $c[0], $c[1], $c[3], $c[2], $c[4], $c[5] ) );
269
		$argLists[] = array( $c, $c[2], 4, array( $c[0], $c[1], $c[3], $c[4], $c[2], $c[5] ) );
270
		$argLists[] = array( $c, $c[2], 5, array( $c[0], $c[1], $c[3], $c[4], $c[2], $c[5] ) );
271
		$argLists[] = array( $c, $c[2], 6, array( $c[0], $c[1], $c[5], $c[3], $c[4], $c[2] ) );
272
273
		$argLists[] = array( $c, $c[3], 0, array( $c[3], $c[2], $c[4], $c[0], $c[1], $c[5] ) );
274
		$argLists[] = array( $c, $c[3], 1, array( $c[0], $c[1], $c[3], $c[2], $c[4], $c[5] ) );
275
		$argLists[] = array( $c, $c[3], 2, array( $c[0], $c[1], $c[3], $c[2], $c[4], $c[5] ) );
276
		$argLists[] = array( $c, $c[3], 3, $c );
277
		$argLists[] = array( $c, $c[3], 4, array( $c[0], $c[1], $c[2], $c[4], $c[3], $c[5] ) );
278
		$argLists[] = array( $c, $c[3], 5, array( $c[0], $c[1], $c[2], $c[4], $c[3], $c[5] ) );
279
		$argLists[] = array( $c, $c[3], 6, array( $c[0], $c[1], $c[5], $c[2], $c[4], $c[3] ) );
280
281
		$argLists[] = array( $c, $c[4], 0, array( $c[4], $c[2], $c[3], $c[0], $c[1], $c[5] ) );
282
		$argLists[] = array( $c, $c[4], 1, array( $c[0], $c[1], $c[4], $c[2], $c[3], $c[5] ) );
283
		$argLists[] = array( $c, $c[4], 2, array( $c[0], $c[1], $c[4], $c[2], $c[3], $c[5] ) );
284
		$argLists[] = array( $c, $c[4], 3, array( $c[0], $c[1], $c[2], $c[4], $c[3], $c[5] ) );
285
		$argLists[] = array( $c, $c[4], 4, $c );
286
		$argLists[] = array( $c, $c[4], 5, $c );
287
		$argLists[] = array( $c, $c[4], 6, array( $c[0], $c[1], $c[5], $c[2], $c[3], $c[4] ) );
288
289
		$argLists[] = array( $c, $c[5], 0, array( $c[5], $c[0], $c[1], $c[2], $c[3], $c[4] ) );
290
		$argLists[] = array( $c, $c[5], 1, array( $c[0], $c[1], $c[5], $c[2], $c[3], $c[4] ) );
291
		$argLists[] = array( $c, $c[5], 2, array( $c[0], $c[1], $c[5], $c[2], $c[3], $c[4] ) );
292
		$argLists[] = array( $c, $c[5], 3, $c );
293
		$argLists[] = array( $c, $c[5], 4, $c );
294
		$argLists[] = array( $c, $c[5], 5, $c );
295
		$argLists[] = array( $c, $c[5], 6, $c );
296
297
		return $argLists;
298
	}
299
300
	/**
301
	 * @dataProvider moveProvider
302
	 * @param object[] $objectsSource
303
	 * @param object $object
304
	 * @param int $toIndex
305
	 * @param object[] $objectsDestination
306
	 */
307
	public function testMoveObjectToIndex(
308
		array $objectsSource,
309
		$object,
310
		$toIndex,
311
		array $objectsDestination
312
	) {
313
		$indexedArray = new ByPropertyIdArray( $objectsSource );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
314
		$indexedArray->buildIndex();
315
316
		$indexedArray->moveObjectToIndex( $object, $toIndex );
317
318
		// Not using $indexedArray->toFlatArray() here to test whether native array has been
319
		// exchanged:
320
		$reindexedArray = array();
321
		foreach ( $indexedArray as $o ) {
322
			$reindexedArray[] = $o;
323
		}
324
325
		$this->assertEquals( $objectsDestination, $reindexedArray );
326
	}
327
328
	public function testMoveThrowingOutOfBoundsExceptionIfObjectNotPresent() {
329
		$statements = $this->statementsProvider();
330
		$indexedArray = new ByPropertyIdArray( $statements );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
331
		$indexedArray->buildIndex();
332
333
		$this->setExpectedException( 'OutOfBoundsException' );
334
335
		$indexedArray->moveObjectToIndex( new Statement( new PropertyNoValueSnak( new PropertyId( 'P9999' ) ) ), 0 );
336
	}
337
338
	public function testMoveThrowingOutOfBoundsExceptionOnInvalidIndex() {
339
		$statements = $this->statementsProvider();
340
		$indexedArray = new ByPropertyIdArray( $statements );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
341
		$indexedArray->buildIndex();
342
343
		$this->setExpectedException( 'OutOfBoundsException' );
344
345
		$indexedArray->moveObjectToIndex( $statements[0], 9999 );
346
	}
347
348
	public function addProvider() {
349
		$c = $this->statementsProvider();
350
351
		$argLists = array();
352
353
		$argLists[] = array( array(), $c[0], null, array( $c[0] ) );
354
		$argLists[] = array( array(), $c[0], 1, array( $c[0] ) );
355
		$argLists[] = array( array( $c[0] ), $c[2], 0, array( $c[2], $c[0] ) );
356
		$argLists[] = array( array( $c[2], $c[1] ), $c[0], 0, array( $c[0], $c[1], $c[2] ) );
357
		$argLists[] = array(
358
			array( $c[0], $c[1], $c[3] ),
359
			$c[5],
360
			1,
361
			array( $c[0], $c[1], $c[5], $c[3] )
362
		);
363
		$argLists[] = array(
364
			array( $c[0], $c[1], $c[5], $c[3] ),
365
			$c[2],
366
			2,
367
			array( $c[0], $c[1], $c[2], $c[3], $c[5] )
368
		);
369
		$argLists[] = array(
370
			array( $c[0], $c[1], $c[2], $c[3], $c[5] ),
371
			$c[4],
372
			null,
373
			array( $c[0], $c[1], $c[2], $c[3], $c[4], $c[5] )
374
		);
375
376
		return $argLists;
377
	}
378
379
	/**
380
	 * @dataProvider addProvider
381
	 * @param object[] $objectsSource
382
	 * @param object $object
383
	 * @param int|null $index
384
	 * @param object[] $objectsDestination
385
	 */
386
	public function testAddObjectAtIndex(
387
		array $objectsSource,
388
		$object,
389
		$index,
390
		array $objectsDestination
391
	) {
392
		$indexedArray = new ByPropertyIdArray( $objectsSource );
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\ByPropertyIdArray has been deprecated with message: since 5.0, use a DataModel Service instead

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
393
		$indexedArray->buildIndex();
394
395
		$indexedArray->addObjectAtIndex( $object, $index );
396
397
		$this->assertEquals( $objectsDestination, $indexedArray->toFlatArray() );
398
	}
399
400
}
401