Passed
Push — deepCloning ( b0de0a...4428ab )
by no
03:40
created

PropertyTest::cloneProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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