Passed
Push — release500 ( 6f22d3...711bfb )
by no
02:56
created

PropertyTest::testSetClaims()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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