Passed
Push — langAssertions ( 0e6369...680829 )
by no
27:39 queued 24:23
created

PropertyTest::cloneProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 1
eloc 7
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 testClearRemovesAllButId() {
119
		$property = Property::newFromType( 'string' );
120
		$property->setId( 42 );
121
		$property->getFingerprint()->setLabel( 'en', 'foo' );
122
		$property->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) );
123
124
		$property->clear();
125
126
		$this->assertEquals( new PropertyId( 'P42' ), $property->getId() );
127
		$this->assertTrue( $property->isEmpty() );
128
	}
129
130
	public function testGetStatementsReturnsEmptyListForEmptyProperty() {
131
		$property = Property::newFromType( 'string' );
132
133
		$this->assertEquals( new StatementList(), $property->getStatements() );
134
	}
135
136
	public function testSetAndGetStatements() {
137
		$property = Property::newFromType( 'string' );
138
139
		$statementList = $this->newNonEmptyStatementList();
140
		$property->setStatements( $statementList );
141
142
		$this->assertEquals( $statementList, $property->getStatements() );
143
	}
144
145
	private function newNonEmptyStatementList() {
146
		$statementList = new StatementList();
147
		$statementList->addNewStatement( new PropertyNoValueSnak( 42 ) );
148
		$statementList->addNewStatement( new PropertyNoValueSnak( 1337 ) );
149
150
		return $statementList;
151
	}
152
153
	public function equalsProvider() {
154
		$firstProperty = Property::newFromType( 'string' );
155
		$firstProperty->setStatements( $this->newNonEmptyStatementList() );
156
157
		$secondProperty = Property::newFromType( 'string' );
158
		$secondProperty->setStatements( $this->newNonEmptyStatementList() );
159
160
		$secondPropertyWithId = $secondProperty->copy();
161
		$secondPropertyWithId->setId( 42 );
162
163
		$differentId = $secondPropertyWithId->copy();
164
		$differentId->setId( 43 );
165
166
		return array(
167
			array( Property::newFromType( 'string' ), Property::newFromType( 'string' ) ),
168
			array( $firstProperty, $secondProperty ),
169
			array( $secondProperty, $secondPropertyWithId ),
170
			array( $secondPropertyWithId, $differentId ),
171
		);
172
	}
173
174
	/**
175
	 * @dataProvider equalsProvider
176
	 */
177
	public function testEquals( Property $firstProperty, Property $secondProperty ) {
178
		$this->assertTrue( $firstProperty->equals( $secondProperty ) );
179
		$this->assertTrue( $secondProperty->equals( $firstProperty ) );
180
	}
181
182
	private function getBaseProperty() {
183
		$property = Property::newFromType( 'string' );
184
185
		$property->setId( 42 );
186
		$property->getFingerprint()->setLabel( 'en', 'Same' );
187
		$property->getFingerprint()->setDescription( 'en', 'Same' );
188
		$property->getFingerprint()->setAliasGroup( 'en', array( 'Same' ) );
189
		$property->setStatements( $this->newNonEmptyStatementList() );
190
191
		return $property;
192
	}
193
194
	public function notEqualsProvider() {
195
		$differentLabel = $this->getBaseProperty();
196
		$differentLabel->getFingerprint()->setLabel( 'en', 'Different' );
197
198
		$differentDescription = $this->getBaseProperty();
199
		$differentDescription->getFingerprint()->setDescription( 'en', 'Different' );
200
201
		$differentAlias = $this->getBaseProperty();
202
		$differentAlias->getFingerprint()->setAliasGroup( 'en', array( 'Different' ) );
203
204
		$differentStatement = $this->getBaseProperty();
205
		$differentStatement->setStatements( new StatementList() );
206
207
		$property = $this->getBaseProperty();
208
209
		return array(
210
			'empty' => array( $property, Property::newFromType( 'string' ) ),
211
			'label' => array( $property, $differentLabel ),
212
			'description' => array( $property, $differentDescription ),
213
			'alias' => array( $property, $differentAlias ),
214
			'dataType' => array( Property::newFromType( 'string' ), Property::newFromType( 'foo' ) ),
215
			'statement' => array( $property, $differentStatement ),
216
		);
217
	}
218
219
	/**
220
	 * @dataProvider notEqualsProvider
221
	 */
222
	public function testNotEquals( Property $firstProperty, Property $secondProperty ) {
223
		$this->assertFalse( $firstProperty->equals( $secondProperty ) );
224
		$this->assertFalse( $secondProperty->equals( $firstProperty ) );
225
	}
226
227
	public function testPropertyWithStatementsIsNotEmpty() {
228
		$property = Property::newFromType( 'string' );
229
		$property->setStatements( $this->newNonEmptyStatementList() );
230
231
		$this->assertFalse( $property->isEmpty() );
232
	}
233
234
	public function cloneProvider() {
235
		$property = new Property( new PropertyId( 'P1' ), null, 'string' );
236
		$property->setLabel( 'en', 'original' );
237
		$property->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) );
238
239
		return array(
240
			'copy' => array( $property, $property->copy() ),
241
			'native clone' => array( $property, clone $property ),
242
		);
243
	}
244
245
	/**
246
	 * @dataProvider cloneProvider
247
	 */
248
	public function testCloneIsEqualButNotIdentical( Property $original, Property $clone ) {
249
		$this->assertNotSame( $original, $clone );
250
		$this->assertTrue( $original->equals( $clone ) );
251
		$this->assertSame(
252
			$original->getId(),
253
			$clone->getId(),
254
			'id is immutable and must not be cloned'
255
		);
256
257
		// The clone must not reference the same mutable objects
258
		$this->assertNotSame( $original->getFingerprint(), $clone->getFingerprint() );
259
		$this->assertNotSame( $original->getStatements(), $clone->getStatements() );
260
		$this->assertNotSame(
261
			$original->getStatements()->getFirstStatementWithGuid( null ),
262
			$clone->getStatements()->getFirstStatementWithGuid( null )
263
		);
264
	}
265
266
	/**
267
	 * @dataProvider cloneProvider
268
	 */
269
	public function testOriginalDoesNotChangeWithClone( Property $original, Property $clone ) {
270
		$originalStatement = $original->getStatements()->getFirstStatementWithGuid( null );
271
		$clonedStatement = $clone->getStatements()->getFirstStatementWithGuid( null );
272
273
		$clone->setLabel( 'en', 'clone' );
274
		$clone->setDescription( 'en', 'clone' );
275
		$clone->setAliases( 'en', array( 'clone' ) );
276
		$clonedStatement->setGuid( 'clone' );
277
		$clonedStatement->setMainSnak( new PropertySomeValueSnak( 666 ) );
278
		$clonedStatement->setRank( Statement::RANK_DEPRECATED );
279
		$clonedStatement->getQualifiers()->addSnak( new PropertyNoValueSnak( 1 ) );
280
		$clonedStatement->getReferences()->addNewReference( new PropertyNoValueSnak( 1 ) );
281
282
		$this->assertSame( 'original', $original->getFingerprint()->getLabel( 'en' )->getText() );
283
		$this->assertFalse( $original->getFingerprint()->hasDescription( 'en' ) );
284
		$this->assertFalse( $original->getFingerprint()->hasAliasGroup( 'en' ) );
285
		$this->assertNull( $originalStatement->getGuid() );
286
		$this->assertSame( 'novalue', $originalStatement->getMainSnak()->getType() );
287
		$this->assertSame( Statement::RANK_NORMAL, $originalStatement->getRank() );
288
		$this->assertTrue( $originalStatement->getQualifiers()->isEmpty() );
289
		$this->assertTrue( $originalStatement->getReferences()->isEmpty() );
290
	}
291
292
}
293