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