Completed
Pull Request — master (#657)
by Bene
06:46 queued 03:25
created

testGetByNotSetIdThrowsException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 8
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
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
 *
24
 * @licence GNU GPL v2+
25
 * @author Jeroen De Dauw < [email protected] >
26
 * @author H. Snater < [email protected] >
27
 */
28
class ByPropertyIdArrayTest extends \PHPUnit_Framework_TestCase {
29
30
	public function testArrayObjectNotConstructedFromObject() {
31
		$statement1 = new Statement( new PropertyNoValueSnak( 1 ) );
32
		$statement1->setGuid( '1' );
33
		$statement2 = new Statement( new PropertyNoValueSnak( 2 ) );
34
		$statement2->setGuid( '2' );
35
36
		$object = new ArrayObject();
37
		$object->append( $statement1 );
38
39
		$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...
40
		// According to the documentation append() "cannot be called when the ArrayObject was
41
		// constructed from an object." This test makes sure it was not constructed from an object.
42
		$byPropertyIdArray->append( $statement2 );
43
44
		$this->assertCount( 2, $byPropertyIdArray );
45
	}
46
47
	/**
48
	 * Returns an accessible ReflectionMethod of ByPropertyIdArray.
49
	 *
50
	 * @param string $methodName
51
	 * @return ReflectionMethod
52
	 */
53
	protected static function getMethod( $methodName ) {
54
		$class = new ReflectionClass( 'Wikibase\DataModel\ByPropertyIdArray' );
55
		$method = $class->getMethod( $methodName );
56
		$method->setAccessible( true );
57
		return $method;
58
	}
59
60
	public function listProvider() {
61
		$lists = array();
62
63
		$snaks = array(
64
			new PropertyNoValueSnak( new PropertyId( 'P42' ) ),
65
			new PropertySomeValueSnak( new PropertyId( 'P42' ) ),
66
			new PropertySomeValueSnak( new PropertyId( 'P10' ) ),
67
			new PropertyValueSnak( new PropertyId( 'P10' ), new StringValue( 'ohi' ) ),
68
			new PropertySomeValueSnak( new PropertyId( 'P1' ) ),
69
		);
70
71
		$lists[] = $snaks;
72
73
		$lists[] = array_map(
74
			function( Snak $snak ) {
75
				return new Statement( $snak );
76
			},
77
			$snaks
78
		);
79
80
		$argLists = array();
81
82
		foreach ( $lists as $list ) {
83
			$argLists[] = array( $list );
84
		}
85
86
		return $argLists;
87
	}
88
89
	/**
90
	 * @return Statement[]
91
	 */
92
	protected function statementsProvider() {
93
		$snaks = array(
94
			new PropertyNoValueSnak( new PropertyId( 'P1' ) ),
95
			new PropertySomeValueSnak( new PropertyId( 'P1' ) ),
96
			new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'a' ) ),
97
			new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'b' ) ),
98
			new PropertyValueSnak( new PropertyId( 'P2' ), new StringValue( 'c' ) ),
99
			new PropertySomeValueSnak( new PropertyId( 'P3' ) ),
100
		);
101
102
		return array_map(
103
			function( Snak $snak ) {
104
				return new Statement( $snak );
105
			},
106
			$snaks
107
		);
108
	}
109
110
	/**
111
	 * @dataProvider listProvider
112
	 * @param PropertyIdProvider[] $objects
113
	 */
114
	public function testGetIds( array $objects ) {
115
		$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...
116
117
		$expected = array();
118
119
		foreach ( $objects as $object ) {
120
			$expected[] = $object->getPropertyId();
121
		}
122
123
		$expected = array_unique( $expected );
124
125
		$indexedArray->buildIndex();
126
127
		$this->assertEquals(
128
			array_values( $expected ),
129
			array_values( $indexedArray->getPropertyIds() )
130
		);
131
	}
132
133
	/**
134
	 * @dataProvider listProvider
135
	 * @param PropertyIdProvider[] $objects
136
	 */
137
	public function testGetById( array $objects ) {
138
		$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...
139
140
		$ids = array();
141
142
		foreach ( $objects as $object ) {
143
			$ids[] = $object->getPropertyId();
144
		}
145
146
		$ids = array_unique( $ids );
147
148
		$indexedArray->buildIndex();
149
150
		$allObtainedObjects = array();
151
152
		foreach ( $ids as $id ) {
153
			foreach ( $indexedArray->getByPropertyId( $id ) as $obtainedObject ) {
154
				$allObtainedObjects[] = $obtainedObject;
155
				$this->assertEquals( $id, $obtainedObject->getPropertyId() );
156
			}
157
		}
158
159
		$this->assertEquals(
160
			array_values( $objects ),
161
			array_values( $allObtainedObjects )
162
		);
163
	}
164
165
	/**
166
	 * @dataProvider listProvider
167
	 * @param PropertyIdProvider[] $objects
168
	 */
169
	public function testRemoveObject( array $objects ) {
170
		$lastIndex = count( $objects ) - 1;
171
		$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...
172
		$indexedArray->buildIndex();
173
174
		$removeObject = self::getMethod( 'removeObject' );
175
176
		$removeObject->invokeArgs( $indexedArray, array( $objects[0] ) );
177
		$removeObject->invokeArgs( $indexedArray, array( $objects[$lastIndex] ) );
178
179
		$this->assertFalse(
180
			in_array( $objects[0], $indexedArray->getByPropertyId( $objects[0]->getPropertyId() ) )
181
		);
182
183
		$this->assertFalse( in_array(
184
			$objects[$lastIndex],
185
			$indexedArray->getByPropertyId( $objects[1]->getPropertyId() )
186
		) );
187
188
		$this->assertFalse( in_array( $objects[0], $indexedArray->toFlatArray() ) );
189
		$this->assertFalse( in_array( $objects[$lastIndex], $indexedArray->toFlatArray() ) );
190
	}
191
192
	public function testGetByNotSetIdThrowsException() {
193
		$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...
194
		$indexedArray->buildIndex();
195
196
		$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...
197
198
		$indexedArray->getByPropertyId( PropertyId::newFromNumber( 9000 ) );
199
	}
200
201
	public function testNotBuildExceptionIsThrownForByPropertyId() {
202
		$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...
203
204
		$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...
205
		$indexedArray->getByPropertyId( PropertyId::newFromNumber( 9000 ) );
206
	}
207
208
	public function testNotBuildExceptionIsThrownForGetPropertyIds() {
209
		$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...
210
211
		$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...
212
		$indexedArray->getPropertyIds();
213
	}
214
215
	/**
216
	 * @dataProvider listProvider
217
	 */
218
	public function testGetFlatArrayIndexOfObject( array $objects ) {
219
		$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...
220
		$indexedArray->buildIndex();
221
222
		$indicesSource = array();
223
		$indicesDestination = array();
224
225
		$i = 0;
226
		foreach ( $objects as $object ) {
227
			$indicesSource[$i++] = $object;
228
			$indicesDestination[$indexedArray->getFlatArrayIndexOfObject( $object )] = $object;
229
		}
230
231
		$this->assertEquals( $indicesSource, $indicesDestination );
232
	}
233
234
	/**
235
	 * @dataProvider listProvider
236
	 */
237
	public function testToFlatArray( array $objects ) {
238
		$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...
239
		$indexedArray->buildIndex();
240
241
		$this->assertEquals( $objects, $indexedArray->toFlatArray() );
242
	}
243
244
	public function moveProvider() {
245
		$c = $this->statementsProvider();
246
		$argLists = array();
247
248
		$argLists[] = array( $c, $c[0], 0, $c );
249
		$argLists[] = array( $c, $c[0], 1, array( $c[1], $c[0], $c[2], $c[3], $c[4], $c[5] ) );
250
		$argLists[] = array( $c, $c[0], 2, array( $c[1], $c[0], $c[2], $c[3], $c[4], $c[5] ) );
251
		$argLists[] = array( $c, $c[0], 3, array( $c[2], $c[3], $c[4], $c[1], $c[0], $c[5] ) );
252
		$argLists[] = array( $c, $c[0], 4, array( $c[2], $c[3], $c[4], $c[1], $c[0], $c[5] ) );
253
		$argLists[] = array( $c, $c[0], 5, array( $c[2], $c[3], $c[4], $c[1], $c[0], $c[5] ) );
254
		$argLists[] = array( $c, $c[0], 6, array( $c[2], $c[3], $c[4], $c[5], $c[1], $c[0] ) );
255
256
		$argLists[] = array( $c, $c[1], 0, array( $c[1], $c[0], $c[2], $c[3], $c[4], $c[5] ) );
257
		$argLists[] = array( $c, $c[1], 1, $c );
258
		$argLists[] = array( $c, $c[1], 2, $c );
259
		$argLists[] = array( $c, $c[1], 3, array( $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ) );
260
		$argLists[] = array( $c, $c[1], 4, array( $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ) );
261
		$argLists[] = array( $c, $c[1], 5, array( $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ) );
262
		$argLists[] = array( $c, $c[1], 6, array( $c[2], $c[3], $c[4], $c[5], $c[0], $c[1] ) );
263
264
		$argLists[] = array( $c, $c[2], 0, array( $c[2], $c[3], $c[4], $c[0], $c[1], $c[5] ) );
265
		$argLists[] = array( $c, $c[2], 1, $c );
266
		$argLists[] = array( $c, $c[2], 2, $c );
267
		$argLists[] = array( $c, $c[2], 3, array( $c[0], $c[1], $c[3], $c[2], $c[4], $c[5] ) );
268
		$argLists[] = array( $c, $c[2], 4, array( $c[0], $c[1], $c[3], $c[4], $c[2], $c[5] ) );
269
		$argLists[] = array( $c, $c[2], 5, array( $c[0], $c[1], $c[3], $c[4], $c[2], $c[5] ) );
270
		$argLists[] = array( $c, $c[2], 6, array( $c[0], $c[1], $c[5], $c[3], $c[4], $c[2] ) );
271
272
		$argLists[] = array( $c, $c[3], 0, array( $c[3], $c[2], $c[4], $c[0], $c[1], $c[5] ) );
273
		$argLists[] = array( $c, $c[3], 1, array( $c[0], $c[1], $c[3], $c[2], $c[4], $c[5] ) );
274
		$argLists[] = array( $c, $c[3], 2, array( $c[0], $c[1], $c[3], $c[2], $c[4], $c[5] ) );
275
		$argLists[] = array( $c, $c[3], 3, $c );
276
		$argLists[] = array( $c, $c[3], 4, array( $c[0], $c[1], $c[2], $c[4], $c[3], $c[5] ) );
277
		$argLists[] = array( $c, $c[3], 5, array( $c[0], $c[1], $c[2], $c[4], $c[3], $c[5] ) );
278
		$argLists[] = array( $c, $c[3], 6, array( $c[0], $c[1], $c[5], $c[2], $c[4], $c[3] ) );
279
280
		$argLists[] = array( $c, $c[4], 0, array( $c[4], $c[2], $c[3], $c[0], $c[1], $c[5] ) );
281
		$argLists[] = array( $c, $c[4], 1, array( $c[0], $c[1], $c[4], $c[2], $c[3], $c[5] ) );
282
		$argLists[] = array( $c, $c[4], 2, array( $c[0], $c[1], $c[4], $c[2], $c[3], $c[5] ) );
283
		$argLists[] = array( $c, $c[4], 3, array( $c[0], $c[1], $c[2], $c[4], $c[3], $c[5] ) );
284
		$argLists[] = array( $c, $c[4], 4, $c );
285
		$argLists[] = array( $c, $c[4], 5, $c );
286
		$argLists[] = array( $c, $c[4], 6, array( $c[0], $c[1], $c[5], $c[2], $c[3], $c[4] ) );
287
288
		$argLists[] = array( $c, $c[5], 0, array( $c[5], $c[0], $c[1], $c[2], $c[3], $c[4] ) );
289
		$argLists[] = array( $c, $c[5], 1, array( $c[0], $c[1], $c[5], $c[2], $c[3], $c[4] ) );
290
		$argLists[] = array( $c, $c[5], 2, array( $c[0], $c[1], $c[5], $c[2], $c[3], $c[4] ) );
291
		$argLists[] = array( $c, $c[5], 3, $c );
292
		$argLists[] = array( $c, $c[5], 4, $c );
293
		$argLists[] = array( $c, $c[5], 5, $c );
294
		$argLists[] = array( $c, $c[5], 6, $c );
295
296
		return $argLists;
297
	}
298
299
	/**
300
	 * @dataProvider moveProvider
301
	 * @param object[] $objectsSource
302
	 * @param object $object
303
	 * @param int $toIndex
304
	 * @param object[] $objectsDestination
305
	 */
306
	public function testMoveObjectToIndex(
307
		array $objectsSource,
308
		$object,
309
		$toIndex,
310
		array $objectsDestination
311
	) {
312
		$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...
313
		$indexedArray->buildIndex();
314
315
		$indexedArray->moveObjectToIndex( $object, $toIndex );
316
317
		// Not using $indexedArray->toFlatArray() here to test whether native array has been
318
		// exchanged:
319
		$reindexedArray = array();
320
		foreach ( $indexedArray as $o ) {
321
			$reindexedArray[] = $o;
322
		}
323
324
		$this->assertEquals( $objectsDestination, $reindexedArray );
325
	}
326
327
	public function testMoveThrowingOutOfBoundsExceptionIfObjectNotPresent() {
328
		$statements = $this->statementsProvider();
329
		$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...
330
		$indexedArray->buildIndex();
331
332
		$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...
333
334
		$indexedArray->moveObjectToIndex( new Statement( new PropertyNoValueSnak( new PropertyId( 'P9999' ) ) ), 0 );
335
	}
336
337
	public function testMoveThrowingOutOfBoundsExceptionOnInvalidIndex() {
338
		$statements = $this->statementsProvider();
339
		$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...
340
		$indexedArray->buildIndex();
341
342
		$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...
343
344
		$indexedArray->moveObjectToIndex( $statements[0], 9999 );
345
	}
346
347
	public function addProvider() {
348
		$c = $this->statementsProvider();
349
350
		$argLists = array();
351
352
		$argLists[] = array( array(), $c[0], null, array( $c[0] ) );
353
		$argLists[] = array( array(), $c[0], 1, array( $c[0] ) );
354
		$argLists[] = array( array( $c[0] ), $c[2], 0, array( $c[2], $c[0] ) );
355
		$argLists[] = array( array( $c[2], $c[1] ), $c[0], 0, array( $c[0], $c[1], $c[2] ) );
356
		$argLists[] = array(
357
			array( $c[0], $c[1], $c[3] ),
358
			$c[5],
359
			1,
360
			array( $c[0], $c[1], $c[5], $c[3] )
361
		);
362
		$argLists[] = array(
363
			array( $c[0], $c[1], $c[5], $c[3] ),
364
			$c[2],
365
			2,
366
			array( $c[0], $c[1], $c[2], $c[3], $c[5] )
367
		);
368
		$argLists[] = array(
369
			array( $c[0], $c[1], $c[2], $c[3], $c[5] ),
370
			$c[4],
371
			null,
372
			array( $c[0], $c[1], $c[2], $c[3], $c[4], $c[5] )
373
		);
374
375
		return $argLists;
376
	}
377
378
	/**
379
	 * @dataProvider addProvider
380
	 * @param object[] $objectsSource
381
	 * @param object $object
382
	 * @param int|null $index
383
	 * @param object[] $objectsDestination
384
	 */
385
	public function testAddObjectAtIndex(
386
		array $objectsSource,
387
		$object,
388
		$index,
389
		array $objectsDestination
390
	) {
391
		$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...
392
		$indexedArray->buildIndex();
393
394
		$indexedArray->addObjectAtIndex( $object, $index );
395
396
		$this->assertEquals( $objectsDestination, $indexedArray->toFlatArray() );
397
	}
398
399
}
400