Completed
Push — master ( 029489...c23678 )
by Daniel
34s
created

PropertyTest   C

Complexity

Total Complexity 68

Size/Duplication

Total Lines 724
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 68
c 4
b 1
f 1
lcom 1
cbo 11
dl 0
loc 724
rs 5

52 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructorWithAllParameters() 0 13 1
A testConstructorWithMinimalParameters() 0 8 1
A testGivenInvalidType_ConstructorThrowsException() 0 3 1
A testNewFromType() 0 5 1
A testSetAndGetDataTypeId() 0 8 2
A testWhenIdSetWithNumber_GetIdReturnsPropertyId() 0 6 1
A assertHasCorrectIdType() 0 3 1
A testWhenIdSetWithPropertyId_GetIdReturnsPropertyId() 0 6 1
A testPropertyWithTypeIsEmpty() 0 3 1
A testPropertyWithIdIsEmpty() 0 5 1
A testPropertyWithFingerprintIsNotEmpty() 0 5 1
A testGetStatementsReturnsEmptyListForEmptyProperty() 0 5 1
A testSetAndGetStatements() 0 8 1
A newNonEmptyStatementList() 0 7 1
A testEquals() 0 4 1
A getBaseProperty() 0 11 1
B notEqualsProvider() 0 24 1
A testNotEquals() 0 4 1
A testPropertyWithStatementsIsNotEmpty() 0 6 1
A testClearRemovesAllButId() 0 11 1
A equalsProvider() 0 20 1
A getNewEmpty() 0 3 1
A cloneProvider() 0 10 1
A testCloneIsEqualButNotIdentical() 0 17 1
A testOriginalDoesNotChangeWithClone() 0 22 1
A labelProvider() 0 7 1
A testSetLabel() 0 11 1
A testGetLabel() 0 9 1
A testRemoveLabel() 0 6 1
A descriptionProvider() 0 7 1
A testSetDescription() 0 11 1
A testGetDescription() 0 9 1
A testRemoveDescription() 0 6 1
B aliasesProvider() 0 24 1
A testAddAliases() 0 19 4
A testSetAliases() 0 19 4
A testSetEmptyAlias() 0 21 4
B testSetAllAliases() 0 28 5
A testGetAliases() 0 11 1
A duplicateAliasesProvider() 0 21 1
A testRemoveAliases() 0 19 2
B instanceProvider() 0 35 2
A testCopy() 0 9 1
A testCopyRetainsLabels() 0 11 1
A testSerialize() 0 10 1
A testWhenNoStuffIsSet_getFingerprintReturnsEmptyFingerprint() 0 8 1
A testWhenLabelsAreSet_getFingerprintReturnsFingerprintWithLabels() 0 16 1
A testWhenTermsAreSet_getFingerprintReturnsFingerprintWithTerms() 0 22 1
A testGivenEmptyFingerprint_noTermsAreSet() 0 6 1
A assertHasNoTerms() 0 5 1
A testGivenEmptyFingerprint_existingTermsAreRemoved() 0 11 1
A testWhenSettingFingerprint_getFingerprintReturnsIt() 0 19 1

How to fix   Complexity   

Complex Class

Complex classes like PropertyTest often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PropertyTest, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use InvalidArgumentException;
6
use PHPUnit_Framework_TestCase;
7
use Wikibase\DataModel\Entity\Property;
8
use Wikibase\DataModel\Entity\PropertyId;
9
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
10
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
11
use Wikibase\DataModel\Statement\Statement;
12
use Wikibase\DataModel\Statement\StatementList;
13
use Wikibase\DataModel\Term\AliasGroup;
14
use Wikibase\DataModel\Term\AliasGroupList;
15
use Wikibase\DataModel\Term\Fingerprint;
16
use Wikibase\DataModel\Term\Term;
17
use Wikibase\DataModel\Term\TermList;
18
19
/**
20
 * @covers Wikibase\DataModel\Entity\Property
21
 * @covers Wikibase\DataModel\Entity\Entity
22
 *
23
 * @group Wikibase
24
 * @group WikibaseProperty
25
 * @group WikibaseDataModel
26
 * @group PropertyTest
27
 *
28
 * @licence GNU GPL v2+
29
 * @author Jeroen De Dauw < [email protected] >
30
 */
31
class PropertyTest extends PHPUnit_Framework_TestCase {
32
33
	private function getNewEmpty() {
34
		return Property::newFromType( 'string' );
35
	}
36
37
	public function testConstructorWithAllParameters() {
38
		$property = new Property(
39
			new PropertyId( 'P42' ),
40
			new Fingerprint(),
41
			'string',
42
			new StatementList()
43
		);
44
		$this->assertInstanceOf( 'Wikibase\DataModel\Entity\Property', $property );
45
		$this->assertEquals( new PropertyId( 'P42' ), $property->getId() );
46
		$this->assertEquals( new Fingerprint(), $property->getFingerprint() );
47
		$this->assertEquals( 'string', $property->getDataTypeId() );
48
		$this->assertEquals( new StatementList(), $property->getStatements() );
49
	}
50
51
	public function testConstructorWithMinimalParameters() {
52
		$property = new Property( null, null, '' );
53
		$this->assertInstanceOf( 'Wikibase\DataModel\Entity\Property', $property );
54
		$this->assertNull( $property->getId() );
55
		$this->assertEquals( new Fingerprint(), $property->getFingerprint() );
56
		$this->assertEquals( '', $property->getDataTypeId() );
57
		$this->assertEquals( new StatementList(), $property->getStatements() );
58
	}
59
60
	/**
61
	 * @expectedException InvalidArgumentException
62
	 */
63
	public function testGivenInvalidType_ConstructorThrowsException() {
64
		new Property( null, null, null );
65
	}
66
67
	public function testNewFromType() {
68
		$property = Property::newFromType( 'string' );
69
		$this->assertInstanceOf( 'Wikibase\DataModel\Entity\Property', $property );
70
		$this->assertEquals( 'string', $property->getDataTypeId() );
71
	}
72
73
	public function testSetAndGetDataTypeId() {
74
		$property = Property::newFromType( 'string' );
75
76
		foreach ( array( 'string', 'foobar', 'nyan', 'string' ) as $typeId ) {
77
			$property->setDataTypeId( $typeId );
78
			$this->assertEquals( $typeId, $property->getDataTypeId() );
79
		}
80
	}
81
82
	public function testWhenIdSetWithNumber_GetIdReturnsPropertyId() {
83
		$property = Property::newFromType( 'string' );
84
		$property->setId( 42 );
85
86
		$this->assertHasCorrectIdType( $property );
87
	}
88
89
	protected function assertHasCorrectIdType( Property $property ) {
90
		$this->assertInstanceOf( 'Wikibase\DataModel\Entity\PropertyId', $property->getId() );
91
	}
92
93
	public function testWhenIdSetWithPropertyId_GetIdReturnsPropertyId() {
94
		$property = Property::newFromType( 'string' );
95
		$property->setId( new PropertyId( 'P42' ) );
96
97
		$this->assertHasCorrectIdType( $property );
98
	}
99
100
	public function testPropertyWithTypeIsEmpty() {
101
		$this->assertTrue( Property::newFromType( 'string' )->isEmpty() );
102
	}
103
104
	public function testPropertyWithIdIsEmpty() {
105
		$property = Property::newFromType( 'string' );
106
		$property->setId( 1337 );
107
		$this->assertTrue( $property->isEmpty() );
108
	}
109
110
	public function testPropertyWithFingerprintIsNotEmpty() {
111
		$property = Property::newFromType( 'string' );
112
		$property->getFingerprint()->setAliasGroup( 'en', array( 'foo' ) );
113
		$this->assertFalse( $property->isEmpty() );
114
	}
115
116
	public function testClearRemovesAllButId() {
117
		$property = Property::newFromType( 'string' );
118
		$property->setId( 42 );
119
		$property->getFingerprint()->setLabel( 'en', 'foo' );
120
		$property->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) );
121
122
		$property->clear();
123
124
		$this->assertEquals( new PropertyId( 'P42' ), $property->getId() );
125
		$this->assertTrue( $property->isEmpty() );
126
	}
127
128
	public function testGetStatementsReturnsEmptyListForEmptyProperty() {
129
		$property = Property::newFromType( 'string' );
130
131
		$this->assertEquals( new StatementList(), $property->getStatements() );
132
	}
133
134
	public function testSetAndGetStatements() {
135
		$property = Property::newFromType( 'string' );
136
137
		$statementList = $this->newNonEmptyStatementList();
138
		$property->setStatements( $statementList );
139
140
		$this->assertEquals( $statementList, $property->getStatements() );
141
	}
142
143
	private function newNonEmptyStatementList() {
144
		$statementList = new StatementList();
145
		$statementList->addNewStatement( new PropertyNoValueSnak( 42 ) );
146
		$statementList->addNewStatement( new PropertyNoValueSnak( 1337 ) );
147
148
		return $statementList;
149
	}
150
151
	public function equalsProvider() {
152
		$firstProperty = Property::newFromType( 'string' );
153
		$firstProperty->setStatements( $this->newNonEmptyStatementList() );
154
155
		$secondProperty = Property::newFromType( 'string' );
156
		$secondProperty->setStatements( $this->newNonEmptyStatementList() );
157
158
		$secondPropertyWithId = $secondProperty->copy();
159
		$secondPropertyWithId->setId( 42 );
160
161
		$differentId = $secondPropertyWithId->copy();
162
		$differentId->setId( 43 );
163
164
		return array(
165
			array( Property::newFromType( 'string' ), Property::newFromType( 'string' ) ),
166
			array( $firstProperty, $secondProperty ),
167
			array( $secondProperty, $secondPropertyWithId ),
168
			array( $secondPropertyWithId, $differentId ),
169
		);
170
	}
171
172
	/**
173
	 * @dataProvider equalsProvider
174
	 */
175
	public function testEquals( Property $firstProperty, Property $secondProperty ) {
176
		$this->assertTrue( $firstProperty->equals( $secondProperty ) );
177
		$this->assertTrue( $secondProperty->equals( $firstProperty ) );
178
	}
179
180
	private function getBaseProperty() {
181
		$property = Property::newFromType( 'string' );
182
183
		$property->setId( 42 );
184
		$property->getFingerprint()->setLabel( 'en', 'Same' );
185
		$property->getFingerprint()->setDescription( 'en', 'Same' );
186
		$property->getFingerprint()->setAliasGroup( 'en', array( 'Same' ) );
187
		$property->setStatements( $this->newNonEmptyStatementList() );
188
189
		return $property;
190
	}
191
192
	public function notEqualsProvider() {
193
		$differentLabel = $this->getBaseProperty();
194
		$differentLabel->getFingerprint()->setLabel( 'en', 'Different' );
195
196
		$differentDescription = $this->getBaseProperty();
197
		$differentDescription->getFingerprint()->setDescription( 'en', 'Different' );
198
199
		$differentAlias = $this->getBaseProperty();
200
		$differentAlias->getFingerprint()->setAliasGroup( 'en', array( 'Different' ) );
201
202
		$differentStatement = $this->getBaseProperty();
203
		$differentStatement->setStatements( new StatementList() );
204
205
		$property = $this->getBaseProperty();
206
207
		return array(
208
			'empty' => array( $property, Property::newFromType( 'string' ) ),
209
			'label' => array( $property, $differentLabel ),
210
			'description' => array( $property, $differentDescription ),
211
			'alias' => array( $property, $differentAlias ),
212
			'dataType' => array( Property::newFromType( 'string' ), Property::newFromType( 'foo' ) ),
213
			'statement' => array( $property, $differentStatement ),
214
		);
215
	}
216
217
	/**
218
	 * @dataProvider notEqualsProvider
219
	 */
220
	public function testNotEquals( Property $firstProperty, Property $secondProperty ) {
221
		$this->assertFalse( $firstProperty->equals( $secondProperty ) );
222
		$this->assertFalse( $secondProperty->equals( $firstProperty ) );
223
	}
224
225
	public function testPropertyWithStatementsIsNotEmpty() {
226
		$property = Property::newFromType( 'string' );
227
		$property->setStatements( $this->newNonEmptyStatementList() );
228
229
		$this->assertFalse( $property->isEmpty() );
230
	}
231
232
	public function cloneProvider() {
233
		$property = new Property( new PropertyId( 'P1' ), null, 'string' );
234
		$property->setLabel( 'en', 'original' );
235
		$property->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) );
236
237
		return array(
238
			'copy' => array( $property, $property->copy() ),
239
			'native clone' => array( $property, clone $property ),
240
		);
241
	}
242
243
	/**
244
	 * @dataProvider cloneProvider
245
	 */
246
	public function testCloneIsEqualButNotIdentical( Property $original, Property $clone ) {
247
		$this->assertNotSame( $original, $clone );
248
		$this->assertTrue( $original->equals( $clone ) );
249
		$this->assertSame(
250
			$original->getId(),
251
			$clone->getId(),
252
			'id is immutable and must not be cloned'
253
		);
254
255
		// The clone must not reference the same mutable objects
256
		$this->assertNotSame( $original->getFingerprint(), $clone->getFingerprint() );
257
		$this->assertNotSame( $original->getStatements(), $clone->getStatements() );
258
		$this->assertNotSame(
259
			$original->getStatements()->getFirstStatementWithGuid( null ),
260
			$clone->getStatements()->getFirstStatementWithGuid( null )
261
		);
262
	}
263
264
	/**
265
	 * @dataProvider cloneProvider
266
	 */
267
	public function testOriginalDoesNotChangeWithClone( Property $original, Property $clone ) {
268
		$originalStatement = $original->getStatements()->getFirstStatementWithGuid( null );
269
		$clonedStatement = $clone->getStatements()->getFirstStatementWithGuid( null );
270
271
		$clone->setLabel( 'en', 'clone' );
272
		$clone->setDescription( 'en', 'clone' );
273
		$clone->setAliases( 'en', array( 'clone' ) );
274
		$clonedStatement->setGuid( 'clone' );
275
		$clonedStatement->setMainSnak( new PropertySomeValueSnak( 666 ) );
276
		$clonedStatement->setRank( Statement::RANK_DEPRECATED );
277
		$clonedStatement->getQualifiers()->addSnak( new PropertyNoValueSnak( 1 ) );
278
		$clonedStatement->getReferences()->addNewReference( new PropertyNoValueSnak( 1 ) );
279
280
		$this->assertSame( 'original', $original->getFingerprint()->getLabel( 'en' )->getText() );
281
		$this->assertFalse( $original->getFingerprint()->hasDescription( 'en' ) );
282
		$this->assertFalse( $original->getFingerprint()->hasAliasGroup( 'en' ) );
283
		$this->assertNull( $originalStatement->getGuid() );
284
		$this->assertSame( 'novalue', $originalStatement->getMainSnak()->getType() );
285
		$this->assertSame( Statement::RANK_NORMAL, $originalStatement->getRank() );
286
		$this->assertTrue( $originalStatement->getQualifiers()->isEmpty() );
287
		$this->assertTrue( $originalStatement->getReferences()->isEmpty() );
288
	}
289
290
	// Below are tests copied from EntityTest
291
292
	public function labelProvider() {
293
		return array(
294
			array( 'en', 'spam' ),
295
			array( 'en', 'spam', 'spam' ),
296
			array( 'de', 'foo bar baz' ),
297
		);
298
	}
299
300
	/**
301
	 * @dataProvider labelProvider
302
	 * @param string $languageCode
303
	 * @param string $labelText
304
	 * @param string $moarText
305
	 */
306
	public function testSetLabel( $languageCode, $labelText, $moarText = 'ohi there' ) {
307
		$entity = $this->getNewEmpty();
308
309
		$entity->setLabel( $languageCode, $labelText );
310
311
		$this->assertEquals( $labelText, $entity->getLabel( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getLabel() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
312
313
		$entity->setLabel( $languageCode, $moarText );
314
315
		$this->assertEquals( $moarText, $entity->getLabel( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getLabel() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
316
	}
317
318
	/**
319
	 * @dataProvider labelProvider
320
	 * @param string $languageCode
321
	 * @param string $labelText
322
	 */
323
	public function testGetLabel( $languageCode, $labelText ) {
324
		$entity = $this->getNewEmpty();
325
326
		$this->assertFalse( $entity->getLabel( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getLabel() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
327
328
		$entity->setLabel( $languageCode, $labelText );
329
330
		$this->assertEquals( $labelText, $entity->getLabel( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getLabel() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
331
	}
332
333
	/**
334
	 * @dataProvider labelProvider
335
	 * @param string $languageCode
336
	 * @param string $labelText
337
	 */
338
	public function testRemoveLabel( $languageCode, $labelText ) {
339
		$entity = $this->getNewEmpty();
340
		$entity->setLabel( $languageCode, $labelText );
341
		$entity->removeLabel( $languageCode );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::removeLabel() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
342
		$this->assertFalse( $entity->getLabel( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getLabel() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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
345
	public function descriptionProvider() {
346
		return array(
347
			array( 'en', 'spam' ),
348
			array( 'en', 'spam', 'spam' ),
349
			array( 'de', 'foo bar baz' ),
350
		);
351
	}
352
353
	/**
354
	 * @dataProvider descriptionProvider
355
	 * @param string $languageCode
356
	 * @param string $description
357
	 * @param string $moarText
358
	 */
359
	public function testSetDescription( $languageCode, $description, $moarText = 'ohi there' ) {
360
		$entity = $this->getNewEmpty();
361
362
		$entity->setDescription( $languageCode, $description );
363
364
		$this->assertEquals( $description, $entity->getDescription( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getDescription() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
365
366
		$entity->setDescription( $languageCode, $moarText );
367
368
		$this->assertEquals( $moarText, $entity->getDescription( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getDescription() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
369
	}
370
371
	/**
372
	 * @dataProvider descriptionProvider
373
	 * @param string $languageCode
374
	 * @param string $description
375
	 */
376
	public function testGetDescription( $languageCode, $description ) {
377
		$entity = $this->getNewEmpty();
378
379
		$this->assertFalse( $entity->getDescription( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getDescription() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
380
381
		$entity->setDescription( $languageCode, $description );
382
383
		$this->assertEquals( $description, $entity->getDescription( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getDescription() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
384
	}
385
386
	/**
387
	 * @dataProvider descriptionProvider
388
	 * @param string $languageCode
389
	 * @param string $description
390
	 */
391
	public function testRemoveDescription( $languageCode, $description ) {
392
		$entity = $this->getNewEmpty();
393
		$entity->setDescription( $languageCode, $description );
394
		$entity->removeDescription( $languageCode );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entit...ty::removeDescription() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
395
		$this->assertFalse( $entity->getDescription( $languageCode ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getDescription() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
396
	}
397
398
	public function aliasesProvider() {
399
		return array(
400
			array( array(
401
				       'en' => array( array( 'spam' ) )
402
			       ) ),
403
			array( array(
404
				       'en' => array( array( 'foo', 'bar', 'baz' ) )
405
			       ) ),
406
			array( array(
407
				       'en' => array( array( 'foo', 'bar' ), array( 'baz', 'spam' ) )
408
			       ) ),
409
			array( array(
410
				       'en' => array( array( 'foo', 'bar', 'baz' ) ),
411
				       'de' => array( array( 'foobar' ), array( 'baz' ) ),
412
			       ) ),
413
			// with duplicates
414
			array( array(
415
				       'en' => array( array( 'spam', 'ham', 'ham' ) )
416
			       ) ),
417
			array( array(
418
				       'en' => array( array( 'foo', 'bar' ), array( 'bar', 'spam' ) )
419
			       ) ),
420
		);
421
	}
422
423
	/**
424
	 * @dataProvider aliasesProvider
425
	 */
426
	public function testAddAliases( array $aliasesLists ) {
427
		$entity = $this->getNewEmpty();
428
429
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
430
			foreach ( $aliasesList as $aliases ) {
431
				$entity->addAliases( $langCode, $aliases );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::addAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
432
			}
433
		}
434
435
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
436
			$expected = array_values( array_unique( call_user_func_array( 'array_merge', $aliasesList ) ) );
437
			asort( $expected );
438
439
			$actual = $entity->getAliases( $langCode );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
440
			asort( $actual );
441
442
			$this->assertEquals( $expected, $actual );
443
		}
444
	}
445
446
	/**
447
	 * @dataProvider aliasesProvider
448
	 */
449
	public function testSetAliases( array $aliasesLists ) {
450
		$entity = $this->getNewEmpty();
451
452
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
453
			foreach ( $aliasesList as $aliases ) {
454
				$entity->setAliases( $langCode, $aliases );
455
			}
456
		}
457
458
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
459
			$expected = array_values( array_unique( array_pop( $aliasesList ) ) );
460
			asort( $aliasesList );
461
462
			$actual = $entity->getAliases( $langCode );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
463
			asort( $actual );
464
465
			$this->assertEquals( $expected, $actual );
466
		}
467
	}
468
469
	/**
470
	 * @dataProvider aliasesProvider
471
	 */
472
	public function testSetEmptyAlias( array $aliasesLists ) {
473
		$entity = $this->getNewEmpty();
474
475
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
476
			foreach ( $aliasesList as $aliases ) {
477
				$entity->setAliases( $langCode, $aliases );
478
			}
479
		}
480
		$entity->setAliases( 'zh', array( 'wind', 'air', '', 'fire' ) );
481
		$entity->setAliases( 'zu', array( '', '' ) );
482
483
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
484
			$expected = array_values( array_unique( array_pop( $aliasesList ) ) );
485
			asort( $aliasesList );
486
487
			$actual = $entity->getAliases( $langCode );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
488
			asort( $actual );
489
490
			$this->assertEquals( $expected, $actual );
491
		}
492
	}
493
494
	/**
495
	 * @dataProvider aliasesProvider
496
	 */
497
	public function testSetAllAliases( array $aliasGroups ) {
498
		$entity = $this->getNewEmpty();
499
		$entity->addAliases( 'zh', array( 'qwertyuiop123', '321poiuytrewq' ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::addAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
500
501
		$aliasesToSet = array();
502
		foreach ( $aliasGroups as $langCode => $aliasGroup ) {
503
			foreach ( $aliasGroup as $aliases ) {
504
				$aliasesToSet[$langCode] = $aliases;
505
			}
506
		}
507
508
		$entity->setAllAliases( $aliasesToSet );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::setAllAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
509
510
		foreach ( $aliasGroups as $langCode => $aliasGroup ) {
511
			$expected = array_values( array_unique( array_pop( $aliasGroup ) ) );
512
			asort( $aliasGroup );
513
514
			$actual = $entity->getFingerprint()->getAliasGroups()->getByLanguage( $langCode )->getAliases();
515
			asort( $actual );
516
517
			$this->assertEquals( $expected, $actual );
518
		}
519
520
		/** @var AliasGroup $aliasGroup */
521
		foreach ( $entity->getFingerprint()->getAliasGroups() as $langCode => $aliasGroup ) {
522
			$this->assertEquals( $aliasGroup->getAliases(), array_unique( $aliasesToSet[$langCode] ) );
523
		}
524
	}
525
526
	public function testGetAliases() {
527
		$entity = $this->getNewEmpty();
528
		$aliases = array( 'a', 'b' );
529
530
		$entity->getFingerprint()->setAliasGroup( 'en', $aliases );
531
532
		$this->assertEquals(
533
			$aliases,
534
			$entity->getAliases( 'en' )
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
535
		);
536
	}
537
538
	public function duplicateAliasesProvider() {
539
		return array(
540
			array( array(
541
				       'en' => array( array( 'foo', 'bar', 'baz' ), array( 'foo', 'bar', 'baz' ) )
542
			       ) ),
543
			array( array(
544
				       'en' => array( array( 'foo', 'bar', 'baz' ), array( 'foo', 'bar' ) )
545
			       ) ),
546
			array( array(
547
				       'en' => array( array( 'foo', 'bar' ), array( 'foo', 'bar', 'baz' ) )
548
			       ) ),
549
			array( array(
550
				       'en' => array( array( 'foo', 'bar' ), array( 'bar', 'baz' ) ),
551
				       'de' => array( array(), array( 'foo' ) ),
552
				       'nl' => array( array( 'foo' ), array() ),
553
			       ) ),
554
			array( array(
555
				       'en' => array( array( 'foo', 'bar', 'baz' ), array( 'foo', 'bar', 'baz', 'foo', 'bar' ) )
556
			       ) ),
557
		);
558
	}
559
560
	/**
561
	 * @dataProvider duplicateAliasesProvider
562
	 */
563
	public function testRemoveAliases( array $aliasesLists ) {
564
		$entity = $this->getNewEmpty();
565
566
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
567
			$aliases = array_shift( $aliasesList );
568
			$removedAliases = array_shift( $aliasesList );
569
570
			$entity->setAliases( $langCode, $aliases );
571
			$entity->removeAliases( $langCode, $removedAliases );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::removeAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
572
573
			$expected = array_values( array_diff( $aliases, $removedAliases ) );
574
			$actual = $entity->getAliases( $langCode );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
575
576
			asort( $expected );
577
			asort( $actual );
578
579
			$this->assertEquals( $expected, $actual );
580
		}
581
	}
582
583
	public function instanceProvider() {
584
		$entities = array();
585
586
		// empty
587
		$entity = $this->getNewEmpty();
588
		$entities[] = $entity;
589
590
		// ID only
591
		$entity = clone $entity;
592
		$entity->setId( 44 );
593
594
		$entities[] = $entity;
595
596
		// with labels and stuff
597
		$entity = $this->getNewEmpty();
598
		$entity->setAliases( 'en', array( 'o', 'noez' ) );
599
		$entity->setLabel( 'de', 'spam' );
600
		$entity->setDescription( 'en', 'foo bar baz' );
601
602
		$entities[] = $entity;
603
604
		// with labels etc and ID
605
		$entity = clone $entity;
606
		$entity->setId( 42 );
607
608
		$entities[] = $entity;
609
610
		$argLists = array();
611
612
		foreach ( $entities as $entity ) {
613
			$argLists[] = array( $entity );
614
		}
615
616
		return $argLists;
617
	}
618
619
	/**
620
	 * @dataProvider instanceProvider
621
	 * @param Property $entity
622
	 */
623
	public function testCopy( Property $entity ) {
624
		$copy = $entity->copy();
625
626
		// The equality method alone is not enough since it does not check the IDs.
627
		$this->assertTrue( $entity->equals( $copy ) );
628
		$this->assertEquals( $entity->getId(), $copy->getId() );
629
630
		$this->assertNotSame( $entity, $copy );
631
	}
632
633
	public function testCopyRetainsLabels() {
634
		$property = Property::newFromType( 'string' );
635
636
		$property->getFingerprint()->setLabel( 'en', 'foo' );
637
		$property->getFingerprint()->setLabel( 'de', 'bar' );
638
639
		$newProperty = $property->copy();
640
641
		$this->assertTrue( $newProperty->getFingerprint()->getLabels()->hasTermForLanguage( 'en' ) );
642
		$this->assertTrue( $newProperty->getFingerprint()->getLabels()->hasTermForLanguage( 'de' ) );
643
	}
644
645
	/**
646
	 * @dataProvider instanceProvider
647
	 * @param Property $entity
648
	 */
649
	public function testSerialize( Property $entity ) {
650
		$string = serialize( $entity );
651
652
		$this->assertInternalType( 'string', $string );
653
654
		$instance = unserialize( $string );
655
656
		$this->assertTrue( $entity->equals( $instance ) );
657
		$this->assertEquals( $entity->getId(), $instance->getId() );
658
	}
659
660
	public function testWhenNoStuffIsSet_getFingerprintReturnsEmptyFingerprint() {
661
		$entity = $this->getNewEmpty();
662
663
		$this->assertEquals(
664
			new Fingerprint(),
665
			$entity->getFingerprint()
666
		);
667
	}
668
669
	public function testWhenLabelsAreSet_getFingerprintReturnsFingerprintWithLabels() {
670
		$entity = $this->getNewEmpty();
671
672
		$entity->setLabel( 'en', 'foo' );
673
		$entity->setLabel( 'de', 'bar' );
674
675
		$this->assertEquals(
676
			new Fingerprint(
677
				new TermList( array(
678
					new Term( 'en', 'foo' ),
679
					new Term( 'de', 'bar' ),
680
				) )
681
			),
682
			$entity->getFingerprint()
683
		);
684
	}
685
686
	public function testWhenTermsAreSet_getFingerprintReturnsFingerprintWithTerms() {
687
		$entity = $this->getNewEmpty();
688
689
		$entity->setLabel( 'en', 'foo' );
690
		$entity->setDescription( 'en', 'foo bar' );
691
		$entity->setAliases( 'en', array( 'foo', 'bar' ) );
692
693
		$this->assertEquals(
694
			new Fingerprint(
695
				new TermList( array(
696
					new Term( 'en', 'foo' ),
697
				) ),
698
				new TermList( array(
699
					new Term( 'en', 'foo bar' )
700
				) ),
701
				new AliasGroupList( array(
702
					new AliasGroup( 'en', array( 'foo', 'bar' ) )
703
				) )
704
			),
705
			$entity->getFingerprint()
706
		);
707
	}
708
709
	public function testGivenEmptyFingerprint_noTermsAreSet() {
710
		$entity = $this->getNewEmpty();
711
		$entity->setFingerprint( new Fingerprint() );
712
713
		$this->assertHasNoTerms( $entity );
714
	}
715
716
	private function assertHasNoTerms( Property $entity ) {
717
		$this->assertEquals( array(), $entity->getLabels() );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getLabels() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
718
		$this->assertEquals( array(), $entity->getDescriptions() );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entit...tity::getDescriptions() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
719
		$this->assertEquals( array(), $entity->getAllAliases() );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Entity::getAllAliases() has been deprecated with message: since 0.7.3 - use getFingerprint and setFingerprint

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...
720
	}
721
722
	public function testGivenEmptyFingerprint_existingTermsAreRemoved() {
723
		$entity = $this->getNewEmpty();
724
725
		$entity->setLabel( 'en', 'foo' );
726
		$entity->setDescription( 'en', 'foo bar' );
727
		$entity->setAliases( 'en', array( 'foo', 'bar' ) );
728
729
		$entity->setFingerprint( new Fingerprint() );
730
731
		$this->assertHasNoTerms( $entity );
732
	}
733
734
	public function testWhenSettingFingerprint_getFingerprintReturnsIt() {
735
		$fingerprint = new Fingerprint(
736
			new TermList( array(
737
				new Term( 'en', 'english label' ),
738
			) ),
739
			new TermList( array(
740
				new Term( 'en', 'english description' )
741
			) ),
742
			new AliasGroupList( array(
743
				new AliasGroup( 'en', array( 'first en alias', 'second en alias' ) )
744
			) )
745
		);
746
747
		$entity = $this->getNewEmpty();
748
		$entity->setFingerprint( $fingerprint );
749
		$newFingerprint = $entity->getFingerprint();
750
751
		$this->assertEquals( $fingerprint, $newFingerprint );
752
	}
753
754
}
755