Completed
Push — entity-clear ( afee34...e01dde )
by Bene
03:31
created

PropertyTest::newNonEmptyStatementList()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
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 testGetStatementsReturnsEmptyListForEmptyProperty() {
117
		$property = Property::newFromType( 'string' );
118
119
		$this->assertEquals( new StatementList(), $property->getStatements() );
120
	}
121
122
	public function testSetAndGetStatements() {
123
		$property = Property::newFromType( 'string' );
124
125
		$statementList = $this->newNonEmptyStatementList();
126
		$property->setStatements( $statementList );
127
128
		$this->assertEquals( $statementList, $property->getStatements() );
129
	}
130
131
	private function newNonEmptyStatementList() {
132
		$statementList = new StatementList();
133
		$statementList->addNewStatement( new PropertyNoValueSnak( 42 ) );
134
		$statementList->addNewStatement( new PropertyNoValueSnak( 1337 ) );
135
136
		return $statementList;
137
	}
138
139
	public function equalsProvider() {
140
		$firstProperty = Property::newFromType( 'string' );
141
		$firstProperty->setStatements( $this->newNonEmptyStatementList() );
142
143
		$secondProperty = Property::newFromType( 'string' );
144
		$secondProperty->setStatements( $this->newNonEmptyStatementList() );
145
146
		$secondPropertyWithId = $secondProperty->copy();
147
		$secondPropertyWithId->setId( 42 );
148
149
		$differentId = $secondPropertyWithId->copy();
150
		$differentId->setId( 43 );
151
152
		return array(
153
			array( Property::newFromType( 'string' ), Property::newFromType( 'string' ) ),
154
			array( $firstProperty, $secondProperty ),
155
			array( $secondProperty, $secondPropertyWithId ),
156
			array( $secondPropertyWithId, $differentId ),
157
		);
158
	}
159
160
	/**
161
	 * @dataProvider equalsProvider
162
	 */
163
	public function testEquals( Property $firstProperty, Property $secondProperty ) {
164
		$this->assertTrue( $firstProperty->equals( $secondProperty ) );
165
		$this->assertTrue( $secondProperty->equals( $firstProperty ) );
166
	}
167
168
	private function getBaseProperty() {
169
		$property = Property::newFromType( 'string' );
170
171
		$property->setId( 42 );
172
		$property->getFingerprint()->setLabel( 'en', 'Same' );
173
		$property->getFingerprint()->setDescription( 'en', 'Same' );
174
		$property->getFingerprint()->setAliasGroup( 'en', array( 'Same' ) );
175
		$property->setStatements( $this->newNonEmptyStatementList() );
176
177
		return $property;
178
	}
179
180
	public function notEqualsProvider() {
181
		$differentLabel = $this->getBaseProperty();
182
		$differentLabel->getFingerprint()->setLabel( 'en', 'Different' );
183
184
		$differentDescription = $this->getBaseProperty();
185
		$differentDescription->getFingerprint()->setDescription( 'en', 'Different' );
186
187
		$differentAlias = $this->getBaseProperty();
188
		$differentAlias->getFingerprint()->setAliasGroup( 'en', array( 'Different' ) );
189
190
		$differentStatement = $this->getBaseProperty();
191
		$differentStatement->setStatements( new StatementList() );
192
193
		$property = $this->getBaseProperty();
194
195
		return array(
196
			'empty' => array( $property, Property::newFromType( 'string' ) ),
197
			'label' => array( $property, $differentLabel ),
198
			'description' => array( $property, $differentDescription ),
199
			'alias' => array( $property, $differentAlias ),
200
			'dataType' => array( Property::newFromType( 'string' ), Property::newFromType( 'foo' ) ),
201
			'statement' => array( $property, $differentStatement ),
202
		);
203
	}
204
205
	/**
206
	 * @dataProvider notEqualsProvider
207
	 */
208
	public function testNotEquals( Property $firstProperty, Property $secondProperty ) {
209
		$this->assertFalse( $firstProperty->equals( $secondProperty ) );
210
		$this->assertFalse( $secondProperty->equals( $firstProperty ) );
211
	}
212
213
	public function testPropertyWithStatementsIsNotEmpty() {
214
		$property = Property::newFromType( 'string' );
215
		$property->setStatements( $this->newNonEmptyStatementList() );
216
217
		$this->assertFalse( $property->isEmpty() );
218
	}
219
220
}
221