Passed
Push — mwCodeSniffer ( 0e2c36...86c36c )
by no
07:54 queued 04:35
created

ItemTest::testEmptyConstructor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use PHPUnit_Framework_TestCase;
6
use Wikibase\DataModel\Entity\Item;
7
use Wikibase\DataModel\Entity\ItemId;
8
use Wikibase\DataModel\SiteLink;
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\Item
21
 *
22
 * @group Wikibase
23
 * @group WikibaseDataModel
24
 *
25
 * @license GPL-2.0+
26
 * @author Jeroen De Dauw < [email protected] >
27
 * @author John Erling Blad < [email protected] >
28
 * @author Michał Łazowik
29
 */
30
class ItemTest extends PHPUnit_Framework_TestCase {
31
32
	private function getNewEmpty() {
33
		return new Item();
34
	}
35
36
	public function testGetId() {
37
		$item = new Item();
38
		$this->assertNull( $item->getId() );
39
40
		$item->setId( new ItemId( 'Q1' ) );
41
		$this->assertEquals( new ItemId( 'Q1' ), $item->getId() );
42
43
		$item->setId( null );
44
		$this->assertNull( $item->getId() );
45
46
		$item = new Item( new ItemId( 'Q2' ) );
47
		$this->assertEquals( new ItemId( 'Q2' ), $item->getId() );
48
	}
49
50
	public function testSetIdUsingNumber() {
51
		$item = new Item();
52
		$item->setId( 42 );
53
		$this->assertEquals( new ItemId( 'Q42' ), $item->getId() );
54
	}
55
56
	public function testGetSiteLinkWithNonSetSiteId() {
57
		$item = new Item();
58
59
		$this->setExpectedException( 'OutOfBoundsException' );
60
		$item->getSiteLinkList()->getBySiteId( 'enwiki' );
61
	}
62
63
	/**
64
	 * @dataProvider simpleSiteLinkProvider
65
	 */
66
	public function testAddSiteLink( SiteLink $siteLink ) {
67
		$item = new Item();
68
69
		$item->getSiteLinkList()->addSiteLink( $siteLink );
70
71
		$this->assertEquals(
72
			$siteLink,
73
			$item->getSiteLinkList()->getBySiteId( $siteLink->getSiteId() )
74
		);
75
	}
76
77
	public function simpleSiteLinkProvider() {
78
		$argLists = [];
79
80
		$argLists[] = [
81
			new SiteLink(
82
				'enwiki',
83
				'Wikidata',
84
				[
85
					new ItemId( 'Q42' )
86
				]
87
			)
88
		];
89
		$argLists[] = [
90
			new SiteLink(
91
				'nlwiki',
92
				'Wikidata'
93
			)
94
		];
95
		$argLists[] = [
96
			new SiteLink(
97
				'enwiki',
98
				'Nyan!',
99
				[
100
					new ItemId( 'Q42' ),
101
					new ItemId( 'Q149' )
102
				]
103
			)
104
		];
105
		$argLists[] = [
106
			new SiteLink(
107
				'foo bar',
108
				'baz bah',
109
				[
110
					new ItemId( 'Q3' ),
111
					new ItemId( 'Q7' )
112
				]
113
			)
114
		];
115
116
		return $argLists;
117
	}
118
119
	/**
120
	 * @dataProvider simpleSiteLinksProvider
121
	 */
122
	public function testGetSiteLinks() {
123
		$siteLinks = func_get_args();
124
		$item = new Item();
125
126
		foreach ( $siteLinks as $siteLink ) {
127
			$item->getSiteLinkList()->addSiteLink( $siteLink );
128
		}
129
130
		$this->assertInternalType( 'array', $item->getSiteLinks() );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Item::getSiteLinks() has been deprecated with message: since 0.8, use getSiteLinkList() instead,

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...
131
		$this->assertEquals( $siteLinks, $item->getSiteLinks() );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Item::getSiteLinks() has been deprecated with message: since 0.8, use getSiteLinkList() instead,

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...
132
	}
133
134
	public function simpleSiteLinksProvider() {
135
		$argLists = [];
136
137
		$argLists[] = [];
138
139
		$argLists[] = [ new SiteLink( 'enwiki', 'Wikidata', [ new ItemId( 'Q42' ) ] ) ];
140
141
		$argLists[] = [
142
			new SiteLink( 'enwiki', 'Wikidata' ),
143
			new SiteLink( 'nlwiki', 'Wikidata', [ new ItemId( 'Q3' ) ] )
144
		];
145
146
		$argLists[] = [
147
			new SiteLink( 'enwiki', 'Wikidata' ),
148
			new SiteLink( 'nlwiki', 'Wikidata' ),
149
			new SiteLink( 'foo bar', 'baz bah', [ new ItemId( 'Q2' ) ] )
150
		];
151
152
		return $argLists;
153
	}
154
155
	public function testHasLinkToSiteForFalse() {
156
		$item = new Item();
157
		$item->getSiteLinkList()->addNewSiteLink( 'ENWIKI', 'Wikidata', [ new ItemId( 'Q42' ) ] );
158
159
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) );
160
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) );
161
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) );
162
	}
163
164
	public function testHasLinkToSiteForTrue() {
165
		$item = new Item();
166
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Wikidata', [ new ItemId( 'Q42' ) ] );
167
		$item->getSiteLinkList()->addNewSiteLink( 'dewiki', 'Wikidata' );
168
		$item->getSiteLinkList()->addNewSiteLink( 'foo bar', 'Wikidata' );
169
170
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) );
171
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) );
172
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) );
173
	}
174
175
	public function testEmptyItemReturnsEmptySiteLinkList() {
176
		$item = new Item();
177
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
178
	}
179
180
	public function testAddSiteLinkOverridesOldLinks() {
181
		$item = new Item();
182
183
		$item->getSiteLinkList()->addNewSiteLink( 'kittens', 'foo' );
184
185
		$newLink = new SiteLink( 'kittens', 'bar' );
186
		$item->addSiteLink( $newLink );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entity\Item::addSiteLink() has been deprecated with message: since 0.8, use getSiteLinkList()->addSiteLink() instead.

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...
187
188
		$this->assertTrue( $item->getSiteLinkList()->getBySiteId( 'kittens' )->equals( $newLink ) );
189
	}
190
191
	public function testEmptyItemIsEmpty() {
192
		$item = new Item();
193
		$this->assertTrue( $item->isEmpty() );
194
	}
195
196
	public function testItemWithIdIsEmpty() {
197
		$item = new Item( new ItemId( 'Q1337' ) );
198
		$this->assertTrue( $item->isEmpty() );
199
	}
200
201
	public function testItemWithStuffIsNotEmpty() {
202
		$item = new Item();
203
		$item->getFingerprint()->setAliasGroup( 'en', [ 'foo' ] );
204
		$this->assertFalse( $item->isEmpty() );
205
206
		$item = new Item();
207
		$item->getSiteLinkList()->addNewSiteLink( 'en', 'o_O' );
208
		$this->assertFalse( $item->isEmpty() );
209
210
		$item = new Item();
211
		$item->getStatements()->addStatement( $this->newStatement() );
212
		$this->assertFalse( $item->isEmpty() );
213
	}
214
215
	public function testItemWithSitelinksHasSitelinks() {
216
		$item = new Item();
217
		$item->getSiteLinkList()->addNewSiteLink( 'en', 'foo' );
218
		$this->assertFalse( $item->getSiteLinkList()->isEmpty() );
219
	}
220
221
	public function testItemWithoutSitelinksHasNoSitelinks() {
222
		$item = new Item();
223
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
224
	}
225
226
	private function newStatement() {
227
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
228
		$statement->setGuid( 'kittens' );
229
		return $statement;
230
	}
231
232
	public function testEmptyConstructor() {
233
		$item = new Item();
234
235
		$this->assertNull( $item->getId() );
236
		$this->assertTrue( $item->getFingerprint()->isEmpty() );
237
		$this->assertTrue( $item->getLabels()->isEmpty() );
238
		$this->assertTrue( $item->getDescriptions()->isEmpty() );
239
		$this->assertTrue( $item->getAliasGroups()->isEmpty() );
240
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
241
		$this->assertTrue( $item->getStatements()->isEmpty() );
242
	}
243
244
	public function testCanConstructWithStatementList() {
245
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
246
		$statement->setGuid( 'meh' );
247
248
		$statements = new StatementList( $statement );
249
250
		$item = new Item( null, null, null, $statements );
251
252
		$this->assertEquals(
253
			$statements,
254
			$item->getStatements()
255
		);
256
	}
257
258
	public function testSetStatements() {
259
		$item = new Item();
260
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
261
262
		$item->setStatements( new StatementList() );
263
		$this->assertTrue( $item->getStatements()->isEmpty() );
264
	}
265
266
	public function equalsProvider() {
267
		$firstItem = new Item();
268
		$firstItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
269
270
		$secondItem = new Item();
271
		$secondItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
272
273
		$secondItemWithId = $secondItem->copy();
274
		$secondItemWithId->setId( 42 );
275
276
		$differentId = $secondItemWithId->copy();
277
		$differentId->setId( 43 );
278
279
		return [
280
			[ new Item(), new Item() ],
281
			[ $firstItem, $secondItem ],
282
			[ $secondItem, $secondItemWithId ],
283
			[ $secondItemWithId, $differentId ],
284
		];
285
	}
286
287
	/**
288
	 * @dataProvider equalsProvider
289
	 */
290
	public function testEquals( Item $firstItem, Item $secondItem ) {
291
		$this->assertTrue( $firstItem->equals( $secondItem ) );
292
		$this->assertTrue( $secondItem->equals( $firstItem ) );
293
	}
294
295
	private function getBaseItem() {
296
		$item = new Item( new ItemId( 'Q42' ) );
297
		$item->getFingerprint()->setLabel( 'en', 'Same' );
298
		$item->getFingerprint()->setDescription( 'en', 'Same' );
299
		$item->getFingerprint()->setAliasGroup( 'en', [ 'Same' ] );
300
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Same' );
301
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
302
303
		return $item;
304
	}
305
306
	public function notEqualsProvider() {
307
		$differentLabel = $this->getBaseItem();
308
		$differentLabel->getFingerprint()->setLabel( 'en', 'Different' );
309
310
		$differentDescription = $this->getBaseItem();
311
		$differentDescription->getFingerprint()->setDescription( 'en', 'Different' );
312
313
		$differentAlias = $this->getBaseItem();
314
		$differentAlias->getFingerprint()->setAliasGroup( 'en', [ 'Different' ] );
315
316
		$differentSiteLink = $this->getBaseItem();
317
		$differentSiteLink->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' );
318
		$differentSiteLink->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Different' );
319
320
		$differentStatement = $this->getBaseItem();
321
		$differentStatement->setStatements( new StatementList() );
322
		$differentStatement->getStatements()->addNewStatement( new PropertyNoValueSnak( 24 ) );
323
324
		$item = $this->getBaseItem();
325
326
		return [
327
			'empty' => [ $item, new Item() ],
328
			'label' => [ $item, $differentLabel ],
329
			'description' => [ $item, $differentDescription ],
330
			'alias' => [ $item, $differentAlias ],
331
			'siteLink' => [ $item, $differentSiteLink ],
332
			'statement' => [ $item, $differentStatement ],
333
		];
334
	}
335
336
	/**
337
	 * @dataProvider notEqualsProvider
338
	 */
339
	public function testNotEquals( Item $firstItem, Item $secondItem ) {
340
		$this->assertFalse( $firstItem->equals( $secondItem ) );
341
		$this->assertFalse( $secondItem->equals( $firstItem ) );
342
	}
343
344
	public function cloneProvider() {
345
		$item = new Item( new ItemId( 'Q1' ) );
346
		$item->setLabel( 'en', 'original' );
347
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) );
348
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Original' );
349
350
		return [
351
			'copy' => [ $item, $item->copy() ],
352
			'native clone' => [ $item, clone $item ],
353
		];
354
	}
355
356
	/**
357
	 * @dataProvider cloneProvider
358
	 */
359
	public function testCloneIsEqualButNotIdentical( Item $original, Item $clone ) {
360
		$this->assertNotSame( $original, $clone );
361
		$this->assertTrue( $original->equals( $clone ) );
362
		$this->assertSame(
363
			$original->getId(),
364
			$clone->getId(),
365
			'id is immutable and must not be cloned'
366
		);
367
368
		// The clone must not reference the same mutable objects
369
		$this->assertNotSame( $original->getFingerprint(), $clone->getFingerprint() );
370
		$this->assertNotSame( $original->getStatements(), $clone->getStatements() );
371
		$this->assertNotSame(
372
			$original->getStatements()->getFirstStatementWithGuid( null ),
373
			$clone->getStatements()->getFirstStatementWithGuid( null )
374
		);
375
		$this->assertNotSame( $original->getSiteLinkList(), $clone->getSiteLinkList() );
376
		$this->assertSame(
377
			$original->getSiteLinkList()->getBySiteId( 'enwiki' ),
378
			$clone->getSiteLinkList()->getBySiteId( 'enwiki' ),
379
			'SiteLink is immutable and must not be cloned'
380
		);
381
	}
382
383
	/**
384
	 * @dataProvider cloneProvider
385
	 */
386
	public function testOriginalDoesNotChangeWithClone( Item $original, Item $clone ) {
387
		$originalStatement = $original->getStatements()->getFirstStatementWithGuid( null );
388
		$clonedStatement = $clone->getStatements()->getFirstStatementWithGuid( null );
389
390
		$clone->setLabel( 'en', 'clone' );
391
		$clone->setDescription( 'en', 'clone' );
392
		$clone->setAliases( 'en', [ 'clone' ] );
393
		$clonedStatement->setGuid( 'clone' );
394
		$clonedStatement->setMainSnak( new PropertySomeValueSnak( 666 ) );
395
		$clonedStatement->setRank( Statement::RANK_DEPRECATED );
396
		$clonedStatement->getQualifiers()->addSnak( new PropertyNoValueSnak( 1 ) );
397
		$clonedStatement->getReferences()->addNewReference( new PropertyNoValueSnak( 1 ) );
398
		$clone->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' );
399
400
		$this->assertSame( 'original', $original->getFingerprint()->getLabel( 'en' )->getText() );
401
		$this->assertFalse( $original->getFingerprint()->hasDescription( 'en' ) );
402
		$this->assertFalse( $original->getFingerprint()->hasAliasGroup( 'en' ) );
403
		$this->assertNull( $originalStatement->getGuid() );
404
		$this->assertSame( 'novalue', $originalStatement->getMainSnak()->getType() );
405
		$this->assertSame( Statement::RANK_NORMAL, $originalStatement->getRank() );
406
		$this->assertTrue( $originalStatement->getQualifiers()->isEmpty() );
407
		$this->assertTrue( $originalStatement->getReferences()->isEmpty() );
408
		$this->assertFalse( $original->getSiteLinkList()->isEmpty() );
409
	}
410
411
	// Below are tests copied from EntityTest
412
413
	public function labelProvider() {
414
		return [
415
			[ 'en', 'spam' ],
416
			[ 'en', 'spam', 'spam' ],
417
			[ 'de', 'foo bar baz' ],
418
		];
419
	}
420
421
	/**
422
	 * @dataProvider labelProvider
423
	 * @param string $languageCode
424
	 * @param string $labelText
425
	 * @param string $moarText
426
	 */
427
	public function testSetLabel( $languageCode, $labelText, $moarText = 'ohi there' ) {
428
		$entity = $this->getNewEmpty();
429
430
		$entity->setLabel( $languageCode, $labelText );
431
432
		$this->assertEquals( $labelText, $entity->getFingerprint()->getLabel( $languageCode )->getText() );
433
434
		$entity->setLabel( $languageCode, $moarText );
435
436
		$this->assertEquals( $moarText, $entity->getFingerprint()->getLabel( $languageCode )->getText() );
437
	}
438
439
	public function descriptionProvider() {
440
		return [
441
			[ 'en', 'spam' ],
442
			[ 'en', 'spam', 'spam' ],
443
			[ 'de', 'foo bar baz' ],
444
		];
445
	}
446
447
	/**
448
	 * @dataProvider descriptionProvider
449
	 * @param string $languageCode
450
	 * @param string $description
451
	 * @param string $moarText
452
	 */
453
	public function testSetDescription( $languageCode, $description, $moarText = 'ohi there' ) {
454
		$entity = $this->getNewEmpty();
455
456
		$entity->setDescription( $languageCode, $description );
457
458
		$this->assertEquals( $description, $entity->getFingerprint()->getDescription( $languageCode )->getText() );
459
460
		$entity->setDescription( $languageCode, $moarText );
461
462
		$this->assertEquals( $moarText, $entity->getFingerprint()->getDescription( $languageCode )->getText() );
463
	}
464
465
	public function aliasesProvider() {
466
		return [
467
			[ [
468
				       'en' => [ [ 'spam' ] ]
469
			       ] ],
470
			[ [
471
				       'en' => [ [ 'foo', 'bar', 'baz' ] ]
472
			       ] ],
473
			[ [
474
				       'en' => [ [ 'foo', 'bar' ], [ 'baz', 'spam' ] ]
475
			       ] ],
476
			[ [
477
				       'en' => [ [ 'foo', 'bar', 'baz' ] ],
478
				       'de' => [ [ 'foobar' ], [ 'baz' ] ],
479
			       ] ],
480
			// with duplicates
481
			[ [
482
				       'en' => [ [ 'spam', 'ham', 'ham' ] ]
483
			       ] ],
484
			[ [
485
				       'en' => [ [ 'foo', 'bar' ], [ 'bar', 'spam' ] ]
486
			       ] ],
487
		];
488
	}
489
490
	/**
491
	 * @dataProvider aliasesProvider
492
	 */
493
	public function testSetAliases( array $aliasesLists ) {
494
		$entity = $this->getNewEmpty();
495
496
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
497
			foreach ( $aliasesList as $aliases ) {
498
				$entity->setAliases( $langCode, $aliases );
499
			}
500
		}
501
502
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
503
			$expected = array_values( array_unique( array_pop( $aliasesList ) ) );
504
			asort( $aliasesList );
505
506
			$actual = $entity->getFingerprint()->getAliasGroup( $langCode )->getAliases();
507
			asort( $actual );
508
509
			$this->assertEquals( $expected, $actual );
510
		}
511
	}
512
513
	/**
514
	 * @dataProvider aliasesProvider
515
	 */
516
	public function testSetEmptyAlias( array $aliasesLists ) {
517
		$entity = $this->getNewEmpty();
518
519
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
520
			foreach ( $aliasesList as $aliases ) {
521
				$entity->setAliases( $langCode, $aliases );
522
			}
523
		}
524
		$entity->setAliases( 'zh', [ 'wind', 'air', '', 'fire' ] );
525
		$entity->setAliases( 'zu', [ '', '' ] );
526
527
		foreach ( $aliasesLists as $langCode => $aliasesList ) {
528
			$expected = array_values( array_unique( array_pop( $aliasesList ) ) );
529
			asort( $aliasesList );
530
531
			$actual = $entity->getFingerprint()->getAliasGroup( $langCode )->getAliases();
532
			asort( $actual );
533
534
			$this->assertEquals( $expected, $actual );
535
		}
536
	}
537
538
	public function instanceProvider() {
539
		$entities = [];
540
541
		// empty
542
		$entity = $this->getNewEmpty();
543
		$entities[] = $entity;
544
545
		// ID only
546
		$entity = clone $entity;
547
		$entity->setId( 44 );
548
549
		$entities[] = $entity;
550
551
		// with labels and stuff
552
		$entity = $this->getNewEmpty();
553
		$entity->setAliases( 'en', [ 'o', 'noez' ] );
554
		$entity->setLabel( 'de', 'spam' );
555
		$entity->setDescription( 'en', 'foo bar baz' );
556
557
		$entities[] = $entity;
558
559
		// with labels etc and ID
560
		$entity = clone $entity;
561
		$entity->setId( 42 );
562
563
		$entities[] = $entity;
564
565
		$argLists = [];
566
567
		foreach ( $entities as $entity ) {
568
			$argLists[] = [ $entity ];
569
		}
570
571
		return $argLists;
572
	}
573
574
	/**
575
	 * @dataProvider instanceProvider
576
	 * @param Item $entity
577
	 */
578
	public function testCopy( Item $entity ) {
579
		$copy = $entity->copy();
580
581
		// The equality method alone is not enough since it does not check the IDs.
582
		$this->assertTrue( $entity->equals( $copy ) );
583
		$this->assertEquals( $entity->getId(), $copy->getId() );
584
585
		$this->assertNotSame( $entity, $copy );
586
	}
587
588
	public function testCopyRetainsLabels() {
589
		$item = new Item();
590
591
		$item->getFingerprint()->setLabel( 'en', 'foo' );
592
		$item->getFingerprint()->setLabel( 'de', 'bar' );
593
594
		$newItem = $item->copy();
595
596
		$this->assertTrue( $newItem->getFingerprint()->getLabels()->hasTermForLanguage( 'en' ) );
597
		$this->assertTrue( $newItem->getFingerprint()->getLabels()->hasTermForLanguage( 'de' ) );
598
	}
599
600
	/**
601
	 * @dataProvider instanceProvider
602
	 * @param Item $entity
603
	 */
604
	public function testSerialize( Item $entity ) {
605
		$string = serialize( $entity );
606
607
		$this->assertInternalType( 'string', $string );
608
609
		$instance = unserialize( $string );
610
611
		$this->assertTrue( $entity->equals( $instance ) );
612
		$this->assertEquals( $entity->getId(), $instance->getId() );
613
	}
614
615
	public function testWhenNoStuffIsSet_getFingerprintReturnsEmptyFingerprint() {
616
		$entity = $this->getNewEmpty();
617
618
		$this->assertEquals(
619
			new Fingerprint(),
620
			$entity->getFingerprint()
621
		);
622
	}
623
624
	public function testWhenLabelsAreSet_getFingerprintReturnsFingerprintWithLabels() {
625
		$entity = $this->getNewEmpty();
626
627
		$entity->setLabel( 'en', 'foo' );
628
		$entity->setLabel( 'de', 'bar' );
629
630
		$this->assertEquals(
631
			new Fingerprint(
632
				new TermList( [
633
					new Term( 'en', 'foo' ),
634
					new Term( 'de', 'bar' ),
635
				] )
636
			),
637
			$entity->getFingerprint()
638
		);
639
	}
640
641
	public function testWhenTermsAreSet_getFingerprintReturnsFingerprintWithTerms() {
642
		$entity = $this->getNewEmpty();
643
644
		$entity->setLabel( 'en', 'foo' );
645
		$entity->setDescription( 'en', 'foo bar' );
646
		$entity->setAliases( 'en', [ 'foo', 'bar' ] );
647
648
		$this->assertEquals(
649
			new Fingerprint(
650
				new TermList( [
651
					new Term( 'en', 'foo' ),
652
				] ),
653
				new TermList( [
654
					new Term( 'en', 'foo bar' )
655
				] ),
656
				new AliasGroupList( [
657
					new AliasGroup( 'en', [ 'foo', 'bar' ] )
658
				] )
659
			),
660
			$entity->getFingerprint()
661
		);
662
	}
663
664
	public function testGivenEmptyFingerprint_noTermsAreSet() {
665
		$entity = $this->getNewEmpty();
666
		$entity->setFingerprint( new Fingerprint() );
667
668
		$this->assertTrue( $entity->getFingerprint()->isEmpty() );
669
	}
670
671
	public function testGivenEmptyFingerprint_existingTermsAreRemoved() {
672
		$entity = $this->getNewEmpty();
673
674
		$entity->setLabel( 'en', 'foo' );
675
		$entity->setDescription( 'en', 'foo bar' );
676
		$entity->setAliases( 'en', [ 'foo', 'bar' ] );
677
678
		$entity->setFingerprint( new Fingerprint() );
679
680
		$this->assertTrue( $entity->getFingerprint()->isEmpty() );
681
	}
682
683
	public function testWhenSettingFingerprint_getFingerprintReturnsIt() {
684
		$fingerprint = new Fingerprint(
685
			new TermList( [
686
				new Term( 'en', 'english label' ),
687
			] ),
688
			new TermList( [
689
				new Term( 'en', 'english description' )
690
			] ),
691
			new AliasGroupList( [
692
				new AliasGroup( 'en', [ 'first en alias', 'second en alias' ] )
693
			] )
694
		);
695
696
		$entity = $this->getNewEmpty();
697
		$entity->setFingerprint( $fingerprint );
698
		$newFingerprint = $entity->getFingerprint();
699
700
		$this->assertSame( $fingerprint, $newFingerprint );
701
	}
702
703
	public function testGetLabels() {
704
		$item = new Item();
705
		$item->setLabel( 'en', 'foo' );
706
707
		$this->assertEquals(
708
			new TermList( [
709
				new Term( 'en', 'foo' )
710
			] ),
711
			$item->getLabels()
712
		);
713
	}
714
715
	public function testGetDescriptions() {
716
		$item = new Item();
717
		$item->setDescription( 'en', 'foo bar' );
718
719
		$this->assertEquals(
720
			new TermList( [
721
				new Term( 'en', 'foo bar' )
722
			] ),
723
			$item->getDescriptions()
724
		);
725
	}
726
727
	public function testGetAliasGroups() {
728
		$item = new Item();
729
		$item->setAliases( 'en', [ 'foo', 'bar' ] );
730
731
		$this->assertEquals(
732
			new AliasGroupList( [
733
				new AliasGroup( 'en', [ 'foo', 'bar' ] )
734
			] ),
735
			$item->getAliasGroups()
736
		);
737
	}
738
739
	public function testGetLabels_sameListAsFingerprint() {
740
		$item = new Item();
741
742
		$this->assertSame(
743
			$item->getFingerprint()->getLabels(),
744
			$item->getLabels()
745
		);
746
	}
747
748
	public function testGetDescriptions_sameListAsFingerprint() {
749
		$item = new Item();
750
751
		$this->assertSame(
752
			$item->getFingerprint()->getDescriptions(),
753
			$item->getDescriptions()
754
		);
755
	}
756
757
	public function testGetAliasGroups_sameListAsFingerprint() {
758
		$item = new Item();
759
760
		$this->assertSame(
761
			$item->getFingerprint()->getAliasGroups(),
762
			$item->getAliasGroups()
763
		);
764
	}
765
766
}
767