Completed
Pull Request — master (#631)
by Bene
06:01 queued 03:03
created

PropertyTest::testSetAndGetDataTypeId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

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

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
311
312
		$entity->setLabel( $languageCode, $moarText );
313
314
		$this->assertEquals( $moarText, $entity->getLabel( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getLabel() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
315
	}
316
317
	/**
318
	 * @dataProvider labelProvider
319
	 * @param string $languageCode
320
	 * @param string $labelText
321
	 */
322
	public function testGetLabel( $languageCode, $labelText ) {
323
		$entity = $this->getNewEmpty();
324
325
		$this->assertFalse( $entity->getLabel( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getLabel() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
326
327
		$entity->setLabel( $languageCode, $labelText );
328
329
		$this->assertEquals( $labelText, $entity->getLabel( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getLabel() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
330
	}
331
332
	/**
333
	 * @dataProvider labelProvider
334
	 * @param string $languageCode
335
	 * @param string $labelText
336
	 */
337
	public function testRemoveLabel( $languageCode, $labelText ) {
338
		$entity = $this->getNewEmpty();
339
		$entity->setLabel( $languageCode, $labelText );
340
		$entity->removeLabel( $languageCode );
0 ignored issues
show
Bug introduced by
The method removeLabel() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
341
		$this->assertFalse( $entity->getLabel( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getLabel() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
342
	}
343
344
	public function descriptionProvider() {
345
		return array(
346
			array( 'en', 'spam' ),
347
			array( 'en', 'spam', 'spam' ),
348
			array( 'de', 'foo bar baz' ),
349
		);
350
	}
351
352
	/**
353
	 * @dataProvider descriptionProvider
354
	 * @param string $languageCode
355
	 * @param string $description
356
	 * @param string $moarText
357
	 */
358
	public function testSetDescription( $languageCode, $description, $moarText = 'ohi there' ) {
359
		$entity = $this->getNewEmpty();
360
361
		$entity->setDescription( $languageCode, $description );
362
363
		$this->assertEquals( $description, $entity->getDescription( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getDescription() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
364
365
		$entity->setDescription( $languageCode, $moarText );
366
367
		$this->assertEquals( $moarText, $entity->getDescription( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getDescription() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
368
	}
369
370
	/**
371
	 * @dataProvider descriptionProvider
372
	 * @param string $languageCode
373
	 * @param string $description
374
	 */
375
	public function testGetDescription( $languageCode, $description ) {
376
		$entity = $this->getNewEmpty();
377
378
		$this->assertFalse( $entity->getDescription( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getDescription() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
379
380
		$entity->setDescription( $languageCode, $description );
381
382
		$this->assertEquals( $description, $entity->getDescription( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getDescription() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
383
	}
384
385
	/**
386
	 * @dataProvider descriptionProvider
387
	 * @param string $languageCode
388
	 * @param string $description
389
	 */
390
	public function testRemoveDescription( $languageCode, $description ) {
391
		$entity = $this->getNewEmpty();
392
		$entity->setDescription( $languageCode, $description );
393
		$entity->removeDescription( $languageCode );
0 ignored issues
show
Bug introduced by
The method removeDescription() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
394
		$this->assertFalse( $entity->getDescription( $languageCode ) );
0 ignored issues
show
Bug introduced by
The method getDescription() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
395
	}
396
397
	public function aliasesProvider() {
398
		return array(
399
			array( array(
400
				       'en' => array( array( 'spam' ) )
401
			       ) ),
402
			array( array(
403
				       'en' => array( array( 'foo', 'bar', 'baz' ) )
404
			       ) ),
405
			array( array(
406
				       'en' => array( array( 'foo', 'bar' ), array( 'baz', 'spam' ) )
407
			       ) ),
408
			array( array(
409
				       'en' => array( array( 'foo', 'bar', 'baz' ) ),
410
				       'de' => array( array( 'foobar' ), array( 'baz' ) ),
411
			       ) ),
412
			// with duplicates
413
			array( array(
414
				       'en' => array( array( 'spam', 'ham', 'ham' ) )
415
			       ) ),
416
			array( array(
417
				       'en' => array( array( 'foo', 'bar' ), array( 'bar', 'spam' ) )
418
			       ) ),
419
		);
420
	}
421
422
	/**
423
	 * @dataProvider aliasesProvider
424
	 */
425
	public function testAddAliases( array $aliasesLists ) {
426
		$entity = $this->getNewEmpty();
427
428
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
429
			foreach ( $aliasesList as $aliases ) {
430
				$entity->addAliases( $langCode, $aliases );
0 ignored issues
show
Bug introduced by
The method addAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
431
			}
432
		}
433
434
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
435
			$expected = array_values( array_unique( call_user_func_array( 'array_merge', $aliasesList ) ) );
436
			asort( $expected );
437
438
			$actual = $entity->getAliases( $langCode );
0 ignored issues
show
Bug introduced by
The method getAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
439
			asort( $actual );
440
441
			$this->assertEquals( $expected, $actual );
442
		}
443
	}
444
445
	/**
446
	 * @dataProvider aliasesProvider
447
	 */
448
	public function testSetAliases( array $aliasesLists ) {
449
		$entity = $this->getNewEmpty();
450
451
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
452
			foreach ( $aliasesList as $aliases ) {
453
				$entity->setAliases( $langCode, $aliases );
454
			}
455
		}
456
457
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
458
			$expected = array_values( array_unique( array_pop( $aliasesList ) ) );
459
			asort( $aliasesList );
460
461
			$actual = $entity->getAliases( $langCode );
0 ignored issues
show
Bug introduced by
The method getAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
462
			asort( $actual );
463
464
			$this->assertEquals( $expected, $actual );
465
		}
466
	}
467
468
	/**
469
	 * @dataProvider aliasesProvider
470
	 */
471
	public function testSetEmptyAlias( array $aliasesLists ) {
472
		$entity = $this->getNewEmpty();
473
474
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
475
			foreach ( $aliasesList as $aliases ) {
476
				$entity->setAliases( $langCode, $aliases );
477
			}
478
		}
479
		$entity->setAliases( 'zh', array( 'wind', 'air', '', 'fire' ) );
480
		$entity->setAliases( 'zu', array( '', '' ) );
481
482
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
483
			$expected = array_values( array_unique( array_pop( $aliasesList ) ) );
484
			asort( $aliasesList );
485
486
			$actual = $entity->getAliases( $langCode );
0 ignored issues
show
Bug introduced by
The method getAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
487
			asort( $actual );
488
489
			$this->assertEquals( $expected, $actual );
490
		}
491
	}
492
493
	/**
494
	 * @dataProvider aliasesProvider
495
	 */
496
	public function testSetAllAliases( array $aliasGroups ) {
497
		$entity = $this->getNewEmpty();
498
		$entity->addAliases( 'zh', array( 'qwertyuiop123', '321poiuytrewq' ) );
0 ignored issues
show
Bug introduced by
The method addAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
499
500
		$aliasesToSet = array();
501
		foreach ( $aliasGroups as $langCode => $aliasGroup ) {
502
			foreach ( $aliasGroup as $aliases ) {
503
				$aliasesToSet[$langCode] = $aliases;
504
			}
505
		}
506
507
		$entity->setAllAliases( $aliasesToSet );
0 ignored issues
show
Bug introduced by
The method setAllAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
508
509
		foreach ( $aliasGroups as $langCode => $aliasGroup ) {
510
			$expected = array_values( array_unique( array_pop( $aliasGroup ) ) );
511
			asort( $aliasGroup );
512
513
			$actual = $entity->getFingerprint()->getAliasGroups()->getByLanguage( $langCode )->getAliases();
514
			asort( $actual );
515
516
			$this->assertEquals( $expected, $actual );
517
		}
518
519
		/** @var AliasGroup $aliasGroup */
520
		foreach ( $entity->getFingerprint()->getAliasGroups() as $langCode => $aliasGroup ) {
521
			$this->assertEquals( $aliasGroup->getAliases(), array_unique( $aliasesToSet[$langCode] ) );
522
		}
523
	}
524
525
	public function testGetAliases() {
526
		$entity = $this->getNewEmpty();
527
		$aliases = array( 'a', 'b' );
528
529
		$entity->getFingerprint()->setAliasGroup( 'en', $aliases );
530
531
		$this->assertEquals(
532
			$aliases,
533
			$entity->getAliases( 'en' )
0 ignored issues
show
Bug introduced by
The method getAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
534
		);
535
	}
536
537
	public function duplicateAliasesProvider() {
538
		return array(
539
			array( array(
540
				       'en' => array( array( 'foo', 'bar', 'baz' ), array( 'foo', 'bar', 'baz' ) )
541
			       ) ),
542
			array( array(
543
				       'en' => array( array( 'foo', 'bar', 'baz' ), array( 'foo', 'bar' ) )
544
			       ) ),
545
			array( array(
546
				       'en' => array( array( 'foo', 'bar' ), array( 'foo', 'bar', 'baz' ) )
547
			       ) ),
548
			array( array(
549
				       'en' => array( array( 'foo', 'bar' ), array( 'bar', 'baz' ) ),
550
				       'de' => array( array(), array( 'foo' ) ),
551
				       'nl' => array( array( 'foo' ), array() ),
552
			       ) ),
553
			array( array(
554
				       'en' => array( array( 'foo', 'bar', 'baz' ), array( 'foo', 'bar', 'baz', 'foo', 'bar' ) )
555
			       ) ),
556
		);
557
	}
558
559
	/**
560
	 * @dataProvider duplicateAliasesProvider
561
	 */
562
	public function testRemoveAliases( array $aliasesLists ) {
563
		$entity = $this->getNewEmpty();
564
565
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
566
			$aliases = array_shift( $aliasesList );
567
			$removedAliases = array_shift( $aliasesList );
568
569
			$entity->setAliases( $langCode, $aliases );
570
			$entity->removeAliases( $langCode, $removedAliases );
0 ignored issues
show
Bug introduced by
The method removeAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
571
572
			$expected = array_values( array_diff( $aliases, $removedAliases ) );
573
			$actual = $entity->getAliases( $langCode );
0 ignored issues
show
Bug introduced by
The method getAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
717
		$this->assertEquals( array(), $entity->getDescriptions() );
0 ignored issues
show
Bug introduced by
The method getDescriptions() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
718
		$this->assertEquals( array(), $entity->getAllAliases() );
0 ignored issues
show
Bug introduced by
The method getAllAliases() does not seem to exist on object<Wikibase\DataModel\Entity\Property>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

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