Completed
Pull Request — master (#632)
by Bene
27:38 queued 24:21
created

PropertyTest::testPropertyWithTypeIsEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use InvalidArgumentException;
6
use Wikibase\DataModel\Entity\Property;
7
use Wikibase\DataModel\Entity\PropertyId;
8
use Wikibase\DataModel\Snak\PropertyNoValueSnak;
9
use Wikibase\DataModel\Snak\PropertySomeValueSnak;
10
use Wikibase\DataModel\Statement\Statement;
11
use Wikibase\DataModel\Statement\StatementList;
12
use Wikibase\DataModel\Term\AliasGroup;
13
use Wikibase\DataModel\Term\AliasGroupList;
14
use Wikibase\DataModel\Term\Fingerprint;
15
use Wikibase\DataModel\Term\Term;
16
use Wikibase\DataModel\Term\TermList;
17
18
/**
19
 * @covers Wikibase\DataModel\Entity\Property
20
 *
21
 * @group Wikibase
22
 * @group WikibaseProperty
23
 * @group WikibaseDataModel
24
 * @group PropertyTest
25
 *
26
 * @licence GNU GPL v2+
27
 * @author Jeroen De Dauw < [email protected] >
28
 * @author Bene* < [email protected] >
29
 */
30
class PropertyTest extends \PHPUnit_Framework_TestCase {
31
32
	public function testConstructorWithAllParameters() {
33
		$property = new Property(
34
			new PropertyId( 'P42' ),
35
			new Fingerprint(),
36
			'string',
37
			new StatementList()
38
		);
39
		$this->assertInstanceOf( 'Wikibase\DataModel\Entity\Property', $property );
40
		$this->assertEquals( new PropertyId( 'P42' ), $property->getId() );
41
		$this->assertEquals( new Fingerprint(), $property->getFingerprint() );
42
		$this->assertEquals( 'string', $property->getDataTypeId() );
43
		$this->assertEquals( new StatementList(), $property->getStatements() );
44
	}
45
46
	public function testConstructorWithMinimalParameters() {
47
		$property = new Property( null, null, '' );
48
		$this->assertInstanceOf( 'Wikibase\DataModel\Entity\Property', $property );
49
		$this->assertNull( $property->getId() );
50
		$this->assertEquals( new Fingerprint(), $property->getFingerprint() );
51
		$this->assertEquals( '', $property->getDataTypeId() );
52
		$this->assertEquals( new StatementList(), $property->getStatements() );
53
	}
54
55
	/**
56
	 * @expectedException InvalidArgumentException
57
	 */
58
	public function testGivenInvalidType_ConstructorThrowsException() {
59
		new Property( null, null, null );
60
	}
61
62
	public function testNewFromType() {
63
		$property = Property::newFromType( 'string' );
64
		$this->assertInstanceOf( 'Wikibase\DataModel\Entity\Property', $property );
65
		$this->assertEquals( 'string', $property->getDataTypeId() );
66
	}
67
68
	public function testSetAndGetDataTypeId() {
69
		$property = Property::newFromType( 'string' );
70
71
		foreach ( array( 'string', 'foobar', 'nyan', 'string' ) as $typeId ) {
72
			$property->setDataTypeId( $typeId );
73
			$this->assertEquals( $typeId, $property->getDataTypeId() );
74
		}
75
	}
76
77
	public function testWhenIdSetWithNumber_GetIdReturnsPropertyId() {
78
		$property = Property::newFromType( 'string' );
79
		$property->setId( 42 );
80
81
		$this->assertHasCorrectIdType( $property );
82
	}
83
84
	protected function assertHasCorrectIdType( Property $property ) {
85
		$this->assertInstanceOf( 'Wikibase\DataModel\Entity\PropertyId', $property->getId() );
86
	}
87
88
	public function testWhenIdSetWithPropertyId_GetIdReturnsPropertyId() {
89
		$property = Property::newFromType( 'string' );
90
		$property->setId( new PropertyId( 'P42' ) );
91
92
		$this->assertHasCorrectIdType( $property );
93
	}
94
95
	public function testGetFingerprint_emptyFingerprint() {
96
		$property = Property::newFromType( 'string' );
97
98
		$this->assertEquals(
99
			new Fingerprint(),
100
			$property->getFingerprint()
101
		);
102
	}
103
104
	public function testGetFingerprint_termsAreSet() {
105
		$property = Property::newFromType( 'string' );
106
107
		$property->setLabel( 'en', 'foo' );
108
		$property->setDescription( 'en', 'foo bar' );
109
		$property->setAliases( 'en', array( 'foo', 'bar' ) );
110
111
		$this->assertEquals(
112
			new Fingerprint(
113
				new TermList( array(
114
					new Term( 'en', 'foo' ),
115
				) ),
116
				new TermList( array(
117
					new Term( 'en', 'foo bar' )
118
				) ),
119
				new AliasGroupList( array(
120
					new AliasGroup( 'en', array( 'foo', 'bar' ) )
121
				) )
122
			),
123
			$property->getFingerprint()
124
		);
125
	}
126
127
	public function testSetFingerprint_getFingerprintReturnsIt() {
128
		$property = Property::newFromType( 'string' );
129
		$fingerprint = new Fingerprint();
130
131
		$property->setFingerprint( $fingerprint );
132
		$this->assertSame( $fingerprint, $property->getFingerprint() );
133
	}
134
135
	public function testGetLabels() {
136
		$property = Property::newFromType( 'string' );
137
		$property->setLabel( 'en', 'foo' );
138
139
		$this->assertEquals(
140
			new TermList( array(
141
				new Term( 'en', 'foo' )
142
			) ),
143
			$property->getLabels()
144
		);
145
	}
146
147
	public function testGetDescriptions() {
148
		$property = Property::newFromType( 'string' );
149
		$property->setDescription( 'en', 'foo bar' );
150
151
		$this->assertEquals(
152
			new TermList( array(
153
				new Term( 'en', 'foo bar' )
154
			) ),
155
			$property->getDescriptions()
156
		);
157
	}
158
159
	public function testGetAliases() {
160
		$property = Property::newFromType( 'string' );
161
		$property->setAliases( 'en', array( 'foo', 'bar' ) );
162
163
		$this->assertEquals(
164
			new AliasGroupList( array(
165
				new AliasGroup( 'en', array( 'foo', 'bar' ) )
166
			) ),
167
			$property->getAliasGroups()
168
		);
169
	}
170
171
	public function testGetLabels_sameListAsFingerprint() {
172
		$property = Property::newFromType( 'string' );
173
174
		$this->assertSame(
175
			$property->getFingerprint()->getLabels(),
176
			$property->getLabels()
177
		);
178
	}
179
180
	public function testGetDescriptions_sameListAsFingerprint() {
181
		$property = Property::newFromType( 'string' );
182
183
		$this->assertSame(
184
			$property->getFingerprint()->getDescriptions(),
185
			$property->getDescriptions()
186
		);
187
	}
188
189
	public function testGetAliasGroups_sameListAsFingerprint() {
190
		$property = Property::newFromType( 'string' );
191
192
		$this->assertSame(
193
			$property->getFingerprint()->getAliasGroups(),
194
			$property->getAliasGroups()
195
		);
196
	}
197
198
	public function testPropertyWithTypeIsEmpty() {
199
		$this->assertTrue( Property::newFromType( 'string' )->isEmpty() );
200
	}
201
202
	public function testPropertyWithIdIsEmpty() {
203
		$property = Property::newFromType( 'string' );
204
		$property->setId( 1337 );
205
		$this->assertTrue( $property->isEmpty() );
206
	}
207
208
	public function testPropertyWithFingerprintIsNotEmpty() {
209
		$property = Property::newFromType( 'string' );
210
		$property->getFingerprint()->setAliasGroup( 'en', array( 'foo' ) );
211
		$this->assertFalse( $property->isEmpty() );
212
	}
213
214
	public function testClearRemovesAllButId() {
215
		$property = Property::newFromType( 'string' );
216
		$property->setId( 42 );
217
		$property->getFingerprint()->setLabel( 'en', 'foo' );
218
		$property->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) );
219
220
		$property->clear();
221
222
		$this->assertEquals( new PropertyId( 'P42' ), $property->getId() );
223
		$this->assertTrue( $property->isEmpty() );
224
	}
225
226
	public function testGetStatementsReturnsEmptyListForEmptyProperty() {
227
		$property = Property::newFromType( 'string' );
228
229
		$this->assertEquals( new StatementList(), $property->getStatements() );
230
	}
231
232
	public function testSetAndGetStatements() {
233
		$property = Property::newFromType( 'string' );
234
235
		$statementList = $this->newNonEmptyStatementList();
236
		$property->setStatements( $statementList );
237
238
		$this->assertEquals( $statementList, $property->getStatements() );
239
	}
240
241
	private function newNonEmptyStatementList() {
242
		$statementList = new StatementList();
243
		$statementList->addNewStatement( new PropertyNoValueSnak( 42 ) );
244
		$statementList->addNewStatement( new PropertyNoValueSnak( 1337 ) );
245
246
		return $statementList;
247
	}
248
249
	public function equalsProvider() {
250
		$firstProperty = Property::newFromType( 'string' );
251
		$firstProperty->setStatements( $this->newNonEmptyStatementList() );
252
253
		$secondProperty = Property::newFromType( 'string' );
254
		$secondProperty->setStatements( $this->newNonEmptyStatementList() );
255
256
		$secondPropertyWithId = $secondProperty->copy();
257
		$secondPropertyWithId->setId( 42 );
258
259
		$differentId = $secondPropertyWithId->copy();
260
		$differentId->setId( 43 );
261
262
		return array(
263
			array( Property::newFromType( 'string' ), Property::newFromType( 'string' ) ),
264
			array( $firstProperty, $secondProperty ),
265
			array( $secondProperty, $secondPropertyWithId ),
266
			array( $secondPropertyWithId, $differentId ),
267
		);
268
	}
269
270
	/**
271
	 * @dataProvider equalsProvider
272
	 */
273
	public function testEquals( Property $firstProperty, Property $secondProperty ) {
274
		$this->assertTrue( $firstProperty->equals( $secondProperty ) );
275
		$this->assertTrue( $secondProperty->equals( $firstProperty ) );
276
	}
277
278
	private function getBaseProperty() {
279
		$property = Property::newFromType( 'string' );
280
281
		$property->setId( 42 );
282
		$property->getFingerprint()->setLabel( 'en', 'Same' );
283
		$property->getFingerprint()->setDescription( 'en', 'Same' );
284
		$property->getFingerprint()->setAliasGroup( 'en', array( 'Same' ) );
285
		$property->setStatements( $this->newNonEmptyStatementList() );
286
287
		return $property;
288
	}
289
290
	public function notEqualsProvider() {
291
		$differentLabel = $this->getBaseProperty();
292
		$differentLabel->getFingerprint()->setLabel( 'en', 'Different' );
293
294
		$differentDescription = $this->getBaseProperty();
295
		$differentDescription->getFingerprint()->setDescription( 'en', 'Different' );
296
297
		$differentAlias = $this->getBaseProperty();
298
		$differentAlias->getFingerprint()->setAliasGroup( 'en', array( 'Different' ) );
299
300
		$differentStatement = $this->getBaseProperty();
301
		$differentStatement->setStatements( new StatementList() );
302
303
		$property = $this->getBaseProperty();
304
305
		return array(
306
			'empty' => array( $property, Property::newFromType( 'string' ) ),
307
			'label' => array( $property, $differentLabel ),
308
			'description' => array( $property, $differentDescription ),
309
			'alias' => array( $property, $differentAlias ),
310
			'dataType' => array( Property::newFromType( 'string' ), Property::newFromType( 'foo' ) ),
311
			'statement' => array( $property, $differentStatement ),
312
		);
313
	}
314
315
	/**
316
	 * @dataProvider notEqualsProvider
317
	 */
318
	public function testNotEquals( Property $firstProperty, Property $secondProperty ) {
319
		$this->assertFalse( $firstProperty->equals( $secondProperty ) );
320
		$this->assertFalse( $secondProperty->equals( $firstProperty ) );
321
	}
322
323
	public function testPropertyWithStatementsIsNotEmpty() {
324
		$property = Property::newFromType( 'string' );
325
		$property->setStatements( $this->newNonEmptyStatementList() );
326
327
		$this->assertFalse( $property->isEmpty() );
328
	}
329
330
	public function cloneProvider() {
331
		$property = new Property( new PropertyId( 'P1' ), null, 'string' );
332
		$property->setLabel( 'en', 'original' );
333
		$property->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) );
334
335
		return array(
336
			'copy' => array( $property, $property->copy() ),
337
			'native clone' => array( $property, clone $property ),
338
		);
339
	}
340
341
	/**
342
	 * @dataProvider cloneProvider
343
	 */
344
	public function testCloneIsEqualButNotIdentical( Property $original, Property $clone ) {
345
		$this->assertNotSame( $original, $clone );
346
		$this->assertTrue( $original->equals( $clone ) );
347
		$this->assertSame(
348
			$original->getId(),
349
			$clone->getId(),
350
			'id is immutable and must not be cloned'
351
		);
352
353
		// The clone must not reference the same mutable objects
354
		$this->assertNotSame( $original->getFingerprint(), $clone->getFingerprint() );
355
		$this->assertNotSame( $original->getStatements(), $clone->getStatements() );
356
		$this->assertNotSame(
357
			$original->getStatements()->getFirstStatementWithGuid( null ),
358
			$clone->getStatements()->getFirstStatementWithGuid( null )
359
		);
360
	}
361
362
	/**
363
	 * @dataProvider cloneProvider
364
	 */
365
	public function testOriginalDoesNotChangeWithClone( Property $original, Property $clone ) {
366
		$originalStatement = $original->getStatements()->getFirstStatementWithGuid( null );
367
		$clonedStatement = $clone->getStatements()->getFirstStatementWithGuid( null );
368
369
		$clone->setLabel( 'en', 'clone' );
370
		$clone->setDescription( 'en', 'clone' );
371
		$clone->setAliases( 'en', array( 'clone' ) );
372
		$clonedStatement->setGuid( 'clone' );
373
		$clonedStatement->setMainSnak( new PropertySomeValueSnak( 666 ) );
374
		$clonedStatement->setRank( Statement::RANK_DEPRECATED );
375
		$clonedStatement->getQualifiers()->addSnak( new PropertyNoValueSnak( 1 ) );
376
		$clonedStatement->getReferences()->addNewReference( new PropertyNoValueSnak( 1 ) );
377
378
		$this->assertSame( 'original', $original->getFingerprint()->getLabel( 'en' )->getText() );
379
		$this->assertFalse( $original->getFingerprint()->hasDescription( 'en' ) );
380
		$this->assertFalse( $original->getFingerprint()->hasAliasGroup( 'en' ) );
381
		$this->assertNull( $originalStatement->getGuid() );
382
		$this->assertSame( 'novalue', $originalStatement->getMainSnak()->getType() );
383
		$this->assertSame( Statement::RANK_NORMAL, $originalStatement->getRank() );
384
		$this->assertTrue( $originalStatement->getQualifiers()->isEmpty() );
385
		$this->assertTrue( $originalStatement->getReferences()->isEmpty() );
386
	}
387
388
}
389