Completed
Pull Request — master (#614)
by Bene
10:35 queued 07:28
created

PropertyTest::testClearRemovesAllButId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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