Completed
Pull Request — master (#630)
by Bene
06:30 queued 03:17
created

PropertyTest   A

Complexity

Total Complexity 28

Size/Duplication

Total Lines 296
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Importance

Changes 4
Bugs 1 Features 1
Metric Value
wmc 28
lcom 1
cbo 11
dl 0
loc 296
rs 10
c 4
b 1
f 1

27 Methods

Rating   Name   Duplication   Size   Complexity  
A testConstructorWithAllParameters() 0 13 1
A testConstructorWithMinimalParameters() 0 8 1
A testGivenInvalidType_ConstructorThrowsException() 0 3 1
A testNewFromType() 0 5 1
A testSetAndGetDataTypeId() 0 8 2
A testWhenIdSetWithNumber_GetIdReturnsPropertyId() 0 6 1
A assertHasCorrectIdType() 0 3 1
A testWhenIdSetWithPropertyId_GetIdReturnsPropertyId() 0 6 1
A testGetFingerprint_emptyFingerprint() 0 8 1
A testGetFingerprint_termsAreSet() 0 22 1
A testSetFingerprint_getFingerprintReturnsIt() 0 7 1
A testPropertyWithTypeIsEmpty() 0 3 1
A testPropertyWithIdIsEmpty() 0 5 1
A testPropertyWithFingerprintIsNotEmpty() 0 5 1
A testClearRemovesAllButId() 0 11 1
A testGetStatementsReturnsEmptyListForEmptyProperty() 0 5 1
A testSetAndGetStatements() 0 8 1
A newNonEmptyStatementList() 0 7 1
A equalsProvider() 0 20 1
A testEquals() 0 4 1
A getBaseProperty() 0 11 1
B notEqualsProvider() 0 24 1
A testNotEquals() 0 4 1
A testPropertyWithStatementsIsNotEmpty() 0 6 1
A cloneProvider() 0 10 1
A testCloneIsEqualButNotIdentical() 0 17 1
A testOriginalDoesNotChangeWithClone() 0 22 1
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 testPropertyWithTypeIsEmpty() {
136
		$this->assertTrue( Property::newFromType( 'string' )->isEmpty() );
137
	}
138
139
	public function testPropertyWithIdIsEmpty() {
140
		$property = Property::newFromType( 'string' );
141
		$property->setId( 1337 );
142
		$this->assertTrue( $property->isEmpty() );
143
	}
144
145
	public function testPropertyWithFingerprintIsNotEmpty() {
146
		$property = Property::newFromType( 'string' );
147
		$property->getFingerprint()->setAliasGroup( 'en', array( 'foo' ) );
148
		$this->assertFalse( $property->isEmpty() );
149
	}
150
151
	public function testClearRemovesAllButId() {
152
		$property = Property::newFromType( 'string' );
153
		$property->setId( 42 );
154
		$property->getFingerprint()->setLabel( 'en', 'foo' );
155
		$property->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) );
156
157
		$property->clear();
158
159
		$this->assertEquals( new PropertyId( 'P42' ), $property->getId() );
160
		$this->assertTrue( $property->isEmpty() );
161
	}
162
163
	public function testGetStatementsReturnsEmptyListForEmptyProperty() {
164
		$property = Property::newFromType( 'string' );
165
166
		$this->assertEquals( new StatementList(), $property->getStatements() );
167
	}
168
169
	public function testSetAndGetStatements() {
170
		$property = Property::newFromType( 'string' );
171
172
		$statementList = $this->newNonEmptyStatementList();
173
		$property->setStatements( $statementList );
174
175
		$this->assertEquals( $statementList, $property->getStatements() );
176
	}
177
178
	private function newNonEmptyStatementList() {
179
		$statementList = new StatementList();
180
		$statementList->addNewStatement( new PropertyNoValueSnak( 42 ) );
181
		$statementList->addNewStatement( new PropertyNoValueSnak( 1337 ) );
182
183
		return $statementList;
184
	}
185
186
	public function equalsProvider() {
187
		$firstProperty = Property::newFromType( 'string' );
188
		$firstProperty->setStatements( $this->newNonEmptyStatementList() );
189
190
		$secondProperty = Property::newFromType( 'string' );
191
		$secondProperty->setStatements( $this->newNonEmptyStatementList() );
192
193
		$secondPropertyWithId = $secondProperty->copy();
194
		$secondPropertyWithId->setId( 42 );
195
196
		$differentId = $secondPropertyWithId->copy();
197
		$differentId->setId( 43 );
198
199
		return array(
200
			array( Property::newFromType( 'string' ), Property::newFromType( 'string' ) ),
201
			array( $firstProperty, $secondProperty ),
202
			array( $secondProperty, $secondPropertyWithId ),
203
			array( $secondPropertyWithId, $differentId ),
204
		);
205
	}
206
207
	/**
208
	 * @dataProvider equalsProvider
209
	 */
210
	public function testEquals( Property $firstProperty, Property $secondProperty ) {
211
		$this->assertTrue( $firstProperty->equals( $secondProperty ) );
212
		$this->assertTrue( $secondProperty->equals( $firstProperty ) );
213
	}
214
215
	private function getBaseProperty() {
216
		$property = Property::newFromType( 'string' );
217
218
		$property->setId( 42 );
219
		$property->getFingerprint()->setLabel( 'en', 'Same' );
220
		$property->getFingerprint()->setDescription( 'en', 'Same' );
221
		$property->getFingerprint()->setAliasGroup( 'en', array( 'Same' ) );
222
		$property->setStatements( $this->newNonEmptyStatementList() );
223
224
		return $property;
225
	}
226
227
	public function notEqualsProvider() {
228
		$differentLabel = $this->getBaseProperty();
229
		$differentLabel->getFingerprint()->setLabel( 'en', 'Different' );
230
231
		$differentDescription = $this->getBaseProperty();
232
		$differentDescription->getFingerprint()->setDescription( 'en', 'Different' );
233
234
		$differentAlias = $this->getBaseProperty();
235
		$differentAlias->getFingerprint()->setAliasGroup( 'en', array( 'Different' ) );
236
237
		$differentStatement = $this->getBaseProperty();
238
		$differentStatement->setStatements( new StatementList() );
239
240
		$property = $this->getBaseProperty();
241
242
		return array(
243
			'empty' => array( $property, Property::newFromType( 'string' ) ),
244
			'label' => array( $property, $differentLabel ),
245
			'description' => array( $property, $differentDescription ),
246
			'alias' => array( $property, $differentAlias ),
247
			'dataType' => array( Property::newFromType( 'string' ), Property::newFromType( 'foo' ) ),
248
			'statement' => array( $property, $differentStatement ),
249
		);
250
	}
251
252
	/**
253
	 * @dataProvider notEqualsProvider
254
	 */
255
	public function testNotEquals( Property $firstProperty, Property $secondProperty ) {
256
		$this->assertFalse( $firstProperty->equals( $secondProperty ) );
257
		$this->assertFalse( $secondProperty->equals( $firstProperty ) );
258
	}
259
260
	public function testPropertyWithStatementsIsNotEmpty() {
261
		$property = Property::newFromType( 'string' );
262
		$property->setStatements( $this->newNonEmptyStatementList() );
263
264
		$this->assertFalse( $property->isEmpty() );
265
	}
266
267
	public function cloneProvider() {
268
		$property = new Property( new PropertyId( 'P1' ), null, 'string' );
269
		$property->setLabel( 'en', 'original' );
270
		$property->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) );
271
272
		return array(
273
			'copy' => array( $property, $property->copy() ),
274
			'native clone' => array( $property, clone $property ),
275
		);
276
	}
277
278
	/**
279
	 * @dataProvider cloneProvider
280
	 */
281
	public function testCloneIsEqualButNotIdentical( Property $original, Property $clone ) {
282
		$this->assertNotSame( $original, $clone );
283
		$this->assertTrue( $original->equals( $clone ) );
284
		$this->assertSame(
285
			$original->getId(),
286
			$clone->getId(),
287
			'id is immutable and must not be cloned'
288
		);
289
290
		// The clone must not reference the same mutable objects
291
		$this->assertNotSame( $original->getFingerprint(), $clone->getFingerprint() );
292
		$this->assertNotSame( $original->getStatements(), $clone->getStatements() );
293
		$this->assertNotSame(
294
			$original->getStatements()->getFirstStatementWithGuid( null ),
295
			$clone->getStatements()->getFirstStatementWithGuid( null )
296
		);
297
	}
298
299
	/**
300
	 * @dataProvider cloneProvider
301
	 */
302
	public function testOriginalDoesNotChangeWithClone( Property $original, Property $clone ) {
303
		$originalStatement = $original->getStatements()->getFirstStatementWithGuid( null );
304
		$clonedStatement = $clone->getStatements()->getFirstStatementWithGuid( null );
305
306
		$clone->setLabel( 'en', 'clone' );
307
		$clone->setDescription( 'en', 'clone' );
308
		$clone->setAliases( 'en', array( 'clone' ) );
309
		$clonedStatement->setGuid( 'clone' );
310
		$clonedStatement->setMainSnak( new PropertySomeValueSnak( 666 ) );
311
		$clonedStatement->setRank( Statement::RANK_DEPRECATED );
312
		$clonedStatement->getQualifiers()->addSnak( new PropertyNoValueSnak( 1 ) );
313
		$clonedStatement->getReferences()->addNewReference( new PropertyNoValueSnak( 1 ) );
314
315
		$this->assertSame( 'original', $original->getFingerprint()->getLabel( 'en' )->getText() );
316
		$this->assertFalse( $original->getFingerprint()->hasDescription( 'en' ) );
317
		$this->assertFalse( $original->getFingerprint()->hasAliasGroup( 'en' ) );
318
		$this->assertNull( $originalStatement->getGuid() );
319
		$this->assertSame( 'novalue', $originalStatement->getMainSnak()->getType() );
320
		$this->assertSame( Statement::RANK_NORMAL, $originalStatement->getRank() );
321
		$this->assertTrue( $originalStatement->getQualifiers()->isEmpty() );
322
		$this->assertTrue( $originalStatement->getReferences()->isEmpty() );
323
	}
324
325
}
326