Passed
Push — deepCloning ( 4428ab...80fdb4 )
by no
05:26
created

ItemTest   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 397
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 8
Bugs 1 Features 2
Metric Value
c 8
b 1
f 2
dl 0
loc 397
wmc 32
lcom 1
cbo 10
rs 9.6

31 Methods

Rating   Name   Duplication   Size   Complexity  
A getNewEmpty() 0 3 1
A testGetId() 0 13 1
A testSetIdUsingNumber() 0 5 1
A testGetSiteLinkWithNonSetSiteId() 0 6 1
A testAddSiteLink() 0 10 1
B simpleSiteLinkProvider() 0 41 1
A testGetSiteLinks() 0 11 2
A simpleSiteLinksProvider() 0 20 1
A testHasLinkToSiteForFalse() 0 8 1
A testHasLinkToSiteForTrue() 0 10 1
A testEmptyItemReturnsEmptySiteLinkList() 0 4 1
A testAddSiteLinkOverridesOldLinks() 0 10 1
A testEmptyItemIsEmpty() 0 4 1
A testItemWithIdIsEmpty() 0 4 1
A testItemWithStuffIsNotEmpty() 0 13 1
A testItemWithSitelinksHasSitelinks() 0 5 1
A testItemWithoutSitelinksHasNoSitelinks() 0 4 1
A newStatement() 0 5 1
A testClearRemovesAllButId() 0 13 1
A testEmptyConstructor() 0 8 1
A testCanConstructWithStatementList() 0 13 1
A testSetStatements() 0 7 1
A testGetStatementsReturnsCorrectTypeAfterClear() 0 6 1
A equalsProvider() 0 20 1
A testEquals() 0 4 1
A getBaseItem() 0 10 1
B notEqualsProvider() 0 29 1
A testNotEquals() 0 4 1
A cloneProvider() 0 11 1
A testCloneIsEqualButNotIdentical() 0 23 1
A testOriginalDoesNotChangeWithClone() 0 14 1
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use Wikibase\DataModel\Entity\Item;
6
use Wikibase\DataModel\Entity\ItemId;
7
use Wikibase\DataModel\SiteLink;
8
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
9
use Wikibase\DataModel\Statement\Statement;
10
use Wikibase\DataModel\Statement\StatementList;
11
12
/**
13
 * @covers Wikibase\DataModel\Entity\Item
14
 * @covers Wikibase\DataModel\Entity\Entity
15
 *
16
 * Some tests for this class are located in ItemMultilangTextsTest,
17
 * ItemNewEmptyTest and ItemNewFromArrayTest.
18
 *
19
 * @since 0.1
20
 *
21
 * @group Wikibase
22
 * @group WikibaseItem
23
 * @group WikibaseDataModel
24
 * @group WikibaseItemTest
25
 *
26
 * @licence GNU GPL v2+
27
 * @author Jeroen De Dauw < [email protected] >
28
 * @author John Erling Blad < [email protected] >
29
 * @author Michał Łazowik
30
 */
31
class ItemTest extends EntityTest {
0 ignored issues
show
Deprecated Code introduced by
The class Wikibase\DataModel\Tests\Entity\EntityTest has been deprecated with message: This test class is to be phased out, and should not be used from outside of the component!

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

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

Loading history...
32
33
	/**
34
	 * @see EntityTest::getNewEmpty
35
	 *
36
	 * @since 0.1
37
	 *
38
	 * @return Item
39
	 */
40
	protected function getNewEmpty() {
41
		return new Item();
42
	}
43
44
	public function testGetId() {
45
		$item = new Item();
46
		$this->assertNull( $item->getId() );
47
48
		$item->setId( new ItemId( 'Q1' ) );
49
		$this->assertEquals( new ItemId( 'Q1' ), $item->getId() );
50
51
		$item->setId( null );
52
		$this->assertNull( $item->getId() );
53
54
		$item = new Item( new ItemId( 'Q2' ) );
55
		$this->assertEquals( new ItemId( 'Q2' ), $item->getId() );
56
	}
57
58
	public function testSetIdUsingNumber() {
59
		$item = new Item();
60
		$item->setId( 42 );
61
		$this->assertEquals( new ItemId( 'Q42' ), $item->getId() );
62
	}
63
64
	public function testGetSiteLinkWithNonSetSiteId() {
65
		$item = new Item();
66
67
		$this->setExpectedException( 'OutOfBoundsException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

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

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

Loading history...
68
		$item->getSiteLinkList()->getBySiteId( 'enwiki' );
69
	}
70
71
	/**
72
	 * @dataProvider simpleSiteLinkProvider
73
	 */
74
	public function testAddSiteLink( SiteLink $siteLink ) {
75
		$item = new Item();
76
77
		$item->getSiteLinkList()->addSiteLink( $siteLink );
78
79
		$this->assertEquals(
80
			$siteLink,
81
			$item->getSiteLinkList()->getBySiteId( $siteLink->getSiteId() )
82
		);
83
	}
84
85
	public function simpleSiteLinkProvider() {
86
		$argLists = array();
87
88
		$argLists[] = array(
89
			new SiteLink(
90
				'enwiki',
91
				'Wikidata',
92
				array(
93
					new ItemId( 'Q42' )
94
				)
95
			)
96
		);
97
		$argLists[] = array(
98
			new SiteLink(
99
				'nlwiki',
100
				'Wikidata'
101
			)
102
		);
103
		$argLists[] = array(
104
			new SiteLink(
105
				'enwiki',
106
				'Nyan!',
107
				array(
108
					new ItemId( 'Q42' ),
109
					new ItemId( 'Q149' )
110
				)
111
			)
112
		);
113
		$argLists[] = array(
114
			new SiteLink(
115
				'foo bar',
116
				'baz bah',
117
				array(
118
					new ItemId( 'Q3' ),
119
					new ItemId( 'Q7' )
120
				)
121
			)
122
		);
123
124
		return $argLists;
125
	}
126
127
	/**
128
	 * @dataProvider simpleSiteLinksProvider
129
	 */
130
	public function testGetSiteLinks() {
131
		$siteLinks = func_get_args();
132
		$item = new Item();
133
134
		foreach ( $siteLinks as $siteLink ) {
135
			$item->getSiteLinkList()->addSiteLink( $siteLink );
136
		}
137
138
		$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...
139
		$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...
140
	}
141
142
	public function simpleSiteLinksProvider() {
143
		$argLists = array();
144
145
		$argLists[] = array();
146
147
		$argLists[] = array( new SiteLink( 'enwiki', 'Wikidata', array( new ItemId( 'Q42' ) ) ) );
148
149
		$argLists[] = array(
150
			new SiteLink( 'enwiki', 'Wikidata' ),
151
			new SiteLink( 'nlwiki', 'Wikidata', array( new ItemId( 'Q3' ) ) )
152
		);
153
154
		$argLists[] = array(
155
			new SiteLink( 'enwiki', 'Wikidata' ),
156
			new SiteLink( 'nlwiki', 'Wikidata' ),
157
			new SiteLink( 'foo bar', 'baz bah', array( new ItemId( 'Q2' ) ) )
158
		);
159
160
		return $argLists;
161
	}
162
163
	public function testHasLinkToSiteForFalse() {
164
		$item = new Item();
165
		$item->getSiteLinkList()->addNewSiteLink( 'ENWIKI', 'Wikidata', array( new ItemId( 'Q42' ) ) );
166
167
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) );
168
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) );
169
		$this->assertFalse( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) );
170
	}
171
172
	public function testHasLinkToSiteForTrue() {
173
		$item = new Item();
174
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Wikidata', array( new ItemId( 'Q42' ) ) );
175
		$item->getSiteLinkList()->addNewSiteLink( 'dewiki', 'Wikidata' );
176
		$item->getSiteLinkList()->addNewSiteLink( 'foo bar', 'Wikidata' );
177
178
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'enwiki' ) );
179
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'dewiki' ) );
180
		$this->assertTrue( $item->getSiteLinkList()->hasLinkWithSiteId( 'foo bar' ) );
181
	}
182
183
	public function testEmptyItemReturnsEmptySiteLinkList() {
184
		$item = new Item();
185
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
186
	}
187
188
	public function testAddSiteLinkOverridesOldLinks() {
189
		$item = new Item();
190
191
		$item->getSiteLinkList()->addNewSiteLink( 'kittens', 'foo' );
192
193
		$newLink = new SiteLink( 'kittens', 'bar' );
194
		$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...
195
196
		$this->assertTrue( $item->getSiteLinkList()->getBySiteId( 'kittens' )->equals( $newLink ) );
197
	}
198
199
	public function testEmptyItemIsEmpty() {
200
		$item = new Item();
201
		$this->assertTrue( $item->isEmpty() );
202
	}
203
204
	public function testItemWithIdIsEmpty() {
205
		$item = new Item( new ItemId( 'Q1337' ) );
206
		$this->assertTrue( $item->isEmpty() );
207
	}
208
209
	public function testItemWithStuffIsNotEmpty() {
210
		$item = new Item();
211
		$item->getFingerprint()->setAliasGroup( 'en', array( 'foo' ) );
212
		$this->assertFalse( $item->isEmpty() );
213
214
		$item = new Item();
215
		$item->getSiteLinkList()->addNewSiteLink( 'en', 'o_O' );
216
		$this->assertFalse( $item->isEmpty() );
217
218
		$item = new Item();
219
		$item->getStatements()->addStatement( $this->newStatement() );
220
		$this->assertFalse( $item->isEmpty() );
221
	}
222
223
	public function testItemWithSitelinksHasSitelinks() {
224
		$item = new Item();
225
		$item->getSiteLinkList()->addNewSiteLink( 'en', 'foo' );
226
		$this->assertFalse( $item->getSiteLinkList()->isEmpty() );
227
	}
228
229
	public function testItemWithoutSitelinksHasNoSitelinks() {
230
		$item = new Item();
231
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
232
	}
233
234
	private function newStatement() {
235
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
236
		$statement->setGuid( 'kittens' );
237
		return $statement;
238
	}
239
240
	public function testClearRemovesAllButId() {
241
		$item = new Item( new ItemId( 'Q42' ) );
242
		$item->getFingerprint()->setLabel( 'en', 'foo' );
243
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Foo' );
244
		$item->getStatements()->addStatement( $this->newStatement() );
245
246
		$item->clear();
247
248
		$this->assertEquals( new ItemId( 'Q42' ), $item->getId() );
249
		$this->assertTrue( $item->getFingerprint()->isEmpty() );
250
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
251
		$this->assertTrue( $item->getStatements()->isEmpty() );
252
	}
253
254
	public function testEmptyConstructor() {
255
		$item = new Item();
256
257
		$this->assertNull( $item->getId() );
258
		$this->assertTrue( $item->getFingerprint()->isEmpty() );
259
		$this->assertTrue( $item->getSiteLinkList()->isEmpty() );
260
		$this->assertTrue( $item->getStatements()->isEmpty() );
261
	}
262
263
	public function testCanConstructWithStatementList() {
264
		$statement = new Statement( new PropertyNoValueSnak( 42 ) );
265
		$statement->setGuid( 'meh' );
266
267
		$statements = new StatementList( $statement );
268
269
		$item = new Item( null, null, null, $statements );
270
271
		$this->assertEquals(
272
			$statements,
273
			$item->getStatements()
274
		);
275
	}
276
277
	public function testSetStatements() {
278
		$item = new Item();
279
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
280
281
		$item->setStatements( new StatementList() );
282
		$this->assertTrue( $item->getStatements()->isEmpty() );
283
	}
284
285
	public function testGetStatementsReturnsCorrectTypeAfterClear() {
286
		$item = new Item();
287
		$item->clear();
288
289
		$this->assertTrue( $item->getStatements()->isEmpty() );
290
	}
291
292
	public function equalsProvider() {
293
		$firstItem = new Item();
294
		$firstItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
295
296
		$secondItem = new Item();
297
		$secondItem->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
298
299
		$secondItemWithId = $secondItem->copy();
300
		$secondItemWithId->setId( 42 );
301
302
		$differentId = $secondItemWithId->copy();
303
		$differentId->setId( 43 );
304
305
		return array(
306
			array( new Item(), new Item() ),
307
			array( $firstItem, $secondItem ),
308
			array( $secondItem, $secondItemWithId ),
309
			array( $secondItemWithId, $differentId ),
310
		);
311
	}
312
313
	/**
314
	 * @dataProvider equalsProvider
315
	 */
316
	public function testEquals( Item $firstItem, Item $secondItem ) {
317
		$this->assertTrue( $firstItem->equals( $secondItem ) );
318
		$this->assertTrue( $secondItem->equals( $firstItem ) );
319
	}
320
321
	private function getBaseItem() {
322
		$item = new Item( new ItemId( 'Q42' ) );
323
		$item->getFingerprint()->setLabel( 'en', 'Same' );
324
		$item->getFingerprint()->setDescription( 'en', 'Same' );
325
		$item->getFingerprint()->setAliasGroup( 'en', array( 'Same' ) );
326
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Same' );
327
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 42 ) );
328
329
		return $item;
330
	}
331
332
	public function notEqualsProvider() {
333
		$differentLabel = $this->getBaseItem();
334
		$differentLabel->getFingerprint()->setLabel( 'en', 'Different' );
335
336
		$differentDescription = $this->getBaseItem();
337
		$differentDescription->getFingerprint()->setDescription( 'en', 'Different' );
338
339
		$differentAlias = $this->getBaseItem();
340
		$differentAlias->getFingerprint()->setAliasGroup( 'en', array( 'Different' ) );
341
342
		$differentSiteLink = $this->getBaseItem();
343
		$differentSiteLink->getSiteLinkList()->removeLinkWithSiteId( 'enwiki' );
344
		$differentSiteLink->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Different' );
345
346
		$differentStatement = $this->getBaseItem();
347
		$differentStatement->setStatements( new StatementList() );
348
		$differentStatement->getStatements()->addNewStatement( new PropertyNoValueSnak( 24 ) );
349
350
		$item = $this->getBaseItem();
351
352
		return array(
353
			'empty' => array( $item, new Item() ),
354
			'label' => array( $item, $differentLabel ),
355
			'description' => array( $item, $differentDescription ),
356
			'alias' => array( $item, $differentAlias ),
357
			'siteLink' => array( $item, $differentSiteLink ),
358
			'statement' => array( $item, $differentStatement ),
359
		);
360
	}
361
362
	/**
363
	 * @dataProvider notEqualsProvider
364
	 */
365
	public function testNotEquals( Item $firstItem, Item $secondItem ) {
366
		$this->assertFalse( $firstItem->equals( $secondItem ) );
367
		$this->assertFalse( $secondItem->equals( $firstItem ) );
368
	}
369
370
	public function cloneProvider() {
371
		$item = new Item( new ItemId( 'Q1' ) );
372
		$item->setLabel( 'en', 'original' );
373
		$item->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) );
374
		$item->getSiteLinkList()->addNewSiteLink( 'enwiki', 'Original' );
375
376
		return array(
377
			'copy' => array( $item, $item->copy() ),
378
			'native clone' => array( $item, clone $item ),
379
		);
380
	}
381
382
	/**
383
	 * @dataProvider cloneProvider
384
	 */
385
	public function testCloneIsEqualButNotIdentical( Item $original, Item $clone ) {
386
		$this->assertNotSame( $original, $clone );
387
		$this->assertTrue( $original->equals( $clone ) );
388
		$this->assertSame(
389
			$original->getId(),
390
			$clone->getId(),
391
			'id is immutable and must not be cloned'
392
		);
393
394
		// The clone must not reference the same mutable objects
395
		$this->assertNotSame( $original->getFingerprint(), $clone->getFingerprint() );
396
		$this->assertNotSame( $original->getStatements(), $clone->getStatements() );
397
		$this->assertNotSame(
398
			$original->getStatements()->getFirstStatementWithGuid( null ),
399
			$clone->getStatements()->getFirstStatementWithGuid( null )
400
		);
401
		$this->assertNotSame( $original->getSiteLinkList(), $clone->getSiteLinkList() );
402
		$this->assertSame(
403
			$original->getSiteLinkList()->getBySiteId( 'enwiki' ),
404
			$clone->getSiteLinkList()->getBySiteId( 'enwiki' ),
405
			'SiteLink is immutable and must not be cloned'
406
		);
407
	}
408
409
	/**
410
	 * @dataProvider cloneProvider
411
	 */
412
	public function testOriginalDoesNotChangeWithClone( Item $original, Item $clone ) {
413
		$originalStatement = $original->getStatements()->getFirstStatementWithGuid( null );
414
		$clonedStatement = $clone->getStatements()->getFirstStatementWithGuid( null );
415
416
		$clone->setLabel( 'en', 'clone' );
417
		$clonedStatement->setRank( Statement::RANK_DEPRECATED );
418
		$clonedStatement->getQualifiers()->addSnak( new PropertyNoValueSnak( 1 ) );
419
		$clonedStatement->getReferences()->addNewReference( new PropertyNoValueSnak( 1 ) );
420
421
		$this->assertSame( 'original', $original->getFingerprint()->getLabel( 'en' )->getText() );
422
		$this->assertSame( Statement::RANK_NORMAL, $originalStatement->getRank() );
423
		$this->assertTrue( $originalStatement->getQualifiers()->isEmpty() );
424
		$this->assertTrue( $originalStatement->getReferences()->isEmpty() );
425
	}
426
427
}
428