Passed
Push — release610 ( b97cda...1c2e28 )
by no
07:27 queued 03:58
created

ByPropertyIdArrayTest::testRemoveObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 22
rs 9.2
cc 1
eloc 14
nc 1
nop 1
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
 * @license GPL-2.0+
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' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
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' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
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' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
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
	 */
303
	public function testMoveObjectToIndex(
304
		array $objectsSource,
305
		PropertyIdProvider $object,
306
		$toIndex,
307
		array $objectsDestination
308
	) {
309
		$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...
310
		$indexedArray->buildIndex();
311
312
		$indexedArray->moveObjectToIndex( $object, $toIndex );
313
314
		// Not using $indexedArray->toFlatArray() here to test whether native array has been
315
		// exchanged:
316
		$reindexedArray = array();
317
		foreach ( $indexedArray as $o ) {
318
			$reindexedArray[] = $o;
319
		}
320
321
		$this->assertEquals( $objectsDestination, $reindexedArray );
322
	}
323
324
	public function testMoveThrowingOutOfBoundsExceptionIfObjectNotPresent() {
325
		$statements = $this->statementsProvider();
326
		$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...
327
		$indexedArray->buildIndex();
328
329
		$this->setExpectedException( 'OutOfBoundsException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
330
331
		$indexedArray->moveObjectToIndex( new Statement( new PropertyNoValueSnak( new PropertyId( 'P9999' ) ) ), 0 );
332
	}
333
334
	public function testMoveThrowingOutOfBoundsExceptionOnInvalidIndex() {
335
		$statements = $this->statementsProvider();
336
		$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...
337
		$indexedArray->buildIndex();
338
339
		$this->setExpectedException( 'OutOfBoundsException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

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

Loading history...
340
341
		$indexedArray->moveObjectToIndex( $statements[0], 9999 );
342
	}
343
344
	public function addProvider() {
345
		$c = $this->statementsProvider();
346
347
		$argLists = array();
348
349
		$argLists[] = array( array(), $c[0], null, array( $c[0] ) );
350
		$argLists[] = array( array(), $c[0], 1, array( $c[0] ) );
351
		$argLists[] = array( array( $c[0] ), $c[2], 0, array( $c[2], $c[0] ) );
352
		$argLists[] = array( array( $c[2], $c[1] ), $c[0], 0, array( $c[0], $c[1], $c[2] ) );
353
		$argLists[] = array(
354
			array( $c[0], $c[1], $c[3] ),
355
			$c[5],
356
			1,
357
			array( $c[0], $c[1], $c[5], $c[3] )
358
		);
359
		$argLists[] = array(
360
			array( $c[0], $c[1], $c[5], $c[3] ),
361
			$c[2],
362
			2,
363
			array( $c[0], $c[1], $c[2], $c[3], $c[5] )
364
		);
365
		$argLists[] = array(
366
			array( $c[0], $c[1], $c[2], $c[3], $c[5] ),
367
			$c[4],
368
			null,
369
			array( $c[0], $c[1], $c[2], $c[3], $c[4], $c[5] )
370
		);
371
372
		return $argLists;
373
	}
374
375
	/**
376
	 * @dataProvider addProvider
377
	 */
378
	public function testAddObjectAtIndex(
379
		array $objectsSource,
380
		PropertyIdProvider $object,
381
		$index,
382
		array $objectsDestination
383
	) {
384
		$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...
385
		$indexedArray->buildIndex();
386
387
		$indexedArray->addObjectAtIndex( $object, $index );
388
389
		$this->assertEquals( $objectsDestination, $indexedArray->toFlatArray() );
390
	}
391
392
}
393