Completed
Pull Request — master (#630)
by Bene
07:36 queued 03:44
created

testSetFingerprint_getFingerprintReturnsIt()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
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\AliasGroup;
11
use Wikibase\DataModel\Term\AliasGroupList;
12
use Wikibase\DataModel\Term\Fingerprint;
13
use Wikibase\DataModel\Term\Term;
14
use Wikibase\DataModel\Term\TermList;
15
16
/**
17
 * @covers Wikibase\DataModel\Entity\Property
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
 * @author Bene* < [email protected] >
27
 */
28
class PropertyTest extends \PHPUnit_Framework_TestCase {
29
30
	public function testConstructorWithAllParameters() {
31
		$property = new Property(
32
			new PropertyId( 'P42' ),
33
			new Fingerprint(),
34
			'string',
35
			new StatementList()
36
		);
37
		$this->assertInstanceOf( 'Wikibase\DataModel\Entity\Property', $property );
38
		$this->assertEquals( new PropertyId( 'P42' ), $property->getId() );
39
		$this->assertEquals( new Fingerprint(), $property->getFingerprint() );
40
		$this->assertEquals( 'string', $property->getDataTypeId() );
41
		$this->assertEquals( new StatementList(), $property->getStatements() );
42
	}
43
44
	public function testConstructorWithMinimalParameters() {
45
		$property = new Property( null, null, '' );
46
		$this->assertInstanceOf( 'Wikibase\DataModel\Entity\Property', $property );
47
		$this->assertNull( $property->getId() );
48
		$this->assertEquals( new Fingerprint(), $property->getFingerprint() );
49
		$this->assertEquals( '', $property->getDataTypeId() );
50
		$this->assertEquals( new StatementList(), $property->getStatements() );
51
	}
52
53
	/**
54
	 * @expectedException InvalidArgumentException
55
	 */
56
	public function testGivenInvalidType_ConstructorThrowsException() {
57
		new Property( null, null, null );
58
	}
59
60
	public function testNewFromType() {
61
		$property = Property::newFromType( 'string' );
62
		$this->assertInstanceOf( 'Wikibase\DataModel\Entity\Property', $property );
63
		$this->assertEquals( 'string', $property->getDataTypeId() );
64
	}
65
66
	public function testSetAndGetDataTypeId() {
67
		$property = Property::newFromType( 'string' );
68
69
		foreach ( array( 'string', 'foobar', 'nyan', 'string' ) as $typeId ) {
70
			$property->setDataTypeId( $typeId );
71
			$this->assertEquals( $typeId, $property->getDataTypeId() );
72
		}
73
	}
74
75
	public function testWhenIdSetWithNumber_GetIdReturnsPropertyId() {
76
		$property = Property::newFromType( 'string' );
77
		$property->setId( 42 );
78
79
		$this->assertHasCorrectIdType( $property );
80
	}
81
82
	protected function assertHasCorrectIdType( Property $property ) {
83
		$this->assertInstanceOf( 'Wikibase\DataModel\Entity\PropertyId', $property->getId() );
84
	}
85
86
	public function testWhenIdSetWithPropertyId_GetIdReturnsPropertyId() {
87
		$property = Property::newFromType( 'string' );
88
		$property->setId( new PropertyId( 'P42' ) );
89
90
		$this->assertHasCorrectIdType( $property );
91
	}
92
93
	public function testGetFingerprint_emptyFingerprint() {
94
		$property = Property::newFromType( 'string' );
95
96
		$this->assertEquals(
97
			new Fingerprint(),
98
			$property->getFingerprint()
99
		);
100
	}
101
102
	public function testGetFingerprint_termsAreSet() {
103
		$property = Property::newFromType( 'string' );
104
105
		$property->setLabel( 'en', 'foo' );
106
		$property->setDescription( 'en', 'foo bar' );
107
		$property->setAliases( 'en', array( 'foo', 'bar' ) );
108
109
		$this->assertEquals(
110
			new Fingerprint(
111
				new TermList( array(
112
					new Term( 'en', 'foo' ),
113
				) ),
114
				new TermList( array(
115
					new Term( 'en', 'foo bar' )
116
				) ),
117
				new AliasGroupList( array(
118
					new AliasGroup( 'en', array( 'foo', 'bar' ) )
119
				) )
120
			),
121
			$property->getFingerprint()
122
		);
123
	}
124
125
	public function testSetFingerprint_getFingerprintReturnsIt() {
126
		$property = Property::newFromType( 'string' );
127
		$fingerprint = new Fingerprint();
128
129
		$property->setFingerprint( $fingerprint );
130
		$this->assertSame( $fingerprint, $property->getFingerprint() );
131
	}
132
133
	public function testPropertyWithTypeIsEmpty() {
134
		$this->assertTrue( Property::newFromType( 'string' )->isEmpty() );
135
	}
136
137
	public function testPropertyWithIdIsEmpty() {
138
		$property = Property::newFromType( 'string' );
139
		$property->setId( 1337 );
140
		$this->assertTrue( $property->isEmpty() );
141
	}
142
143
	public function testPropertyWithFingerprintIsNotEmpty() {
144
		$property = Property::newFromType( 'string' );
145
		$property->getFingerprint()->setAliasGroup( 'en', array( 'foo' ) );
146
		$this->assertFalse( $property->isEmpty() );
147
	}
148
149
	public function testClearRemovesAllButId() {
150
		$property = Property::newFromType( 'string' );
151
		$property->setId( 42 );
152
		$property->getFingerprint()->setLabel( 'en', 'foo' );
153
		$property->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) );
154
155
		$property->clear();
156
157
		$this->assertEquals( new PropertyId( 'P42' ), $property->getId() );
158
		$this->assertTrue( $property->isEmpty() );
159
	}
160
161
	public function testGetStatementsReturnsEmptyListForEmptyProperty() {
162
		$property = Property::newFromType( 'string' );
163
164
		$this->assertEquals( new StatementList(), $property->getStatements() );
165
	}
166
167
	public function testSetAndGetStatements() {
168
		$property = Property::newFromType( 'string' );
169
170
		$statementList = $this->newNonEmptyStatementList();
171
		$property->setStatements( $statementList );
172
173
		$this->assertEquals( $statementList, $property->getStatements() );
174
	}
175
176
	private function newNonEmptyStatementList() {
177
		$statementList = new StatementList();
178
		$statementList->addNewStatement( new PropertyNoValueSnak( 42 ) );
179
		$statementList->addNewStatement( new PropertyNoValueSnak( 1337 ) );
180
181
		return $statementList;
182
	}
183
184
	public function equalsProvider() {
185
		$firstProperty = Property::newFromType( 'string' );
186
		$firstProperty->setStatements( $this->newNonEmptyStatementList() );
187
188
		$secondProperty = Property::newFromType( 'string' );
189
		$secondProperty->setStatements( $this->newNonEmptyStatementList() );
190
191
		$secondPropertyWithId = $secondProperty->copy();
192
		$secondPropertyWithId->setId( 42 );
193
194
		$differentId = $secondPropertyWithId->copy();
195
		$differentId->setId( 43 );
196
197
		return array(
198
			array( Property::newFromType( 'string' ), Property::newFromType( 'string' ) ),
199
			array( $firstProperty, $secondProperty ),
200
			array( $secondProperty, $secondPropertyWithId ),
201
			array( $secondPropertyWithId, $differentId ),
202
		);
203
	}
204
205
	/**
206
	 * @dataProvider equalsProvider
207
	 */
208
	public function testEquals( Property $firstProperty, Property $secondProperty ) {
209
		$this->assertTrue( $firstProperty->equals( $secondProperty ) );
210
		$this->assertTrue( $secondProperty->equals( $firstProperty ) );
211
	}
212
213
	private function getBaseProperty() {
214
		$property = Property::newFromType( 'string' );
215
216
		$property->setId( 42 );
217
		$property->getFingerprint()->setLabel( 'en', 'Same' );
218
		$property->getFingerprint()->setDescription( 'en', 'Same' );
219
		$property->getFingerprint()->setAliasGroup( 'en', array( 'Same' ) );
220
		$property->setStatements( $this->newNonEmptyStatementList() );
221
222
		return $property;
223
	}
224
225
	public function notEqualsProvider() {
226
		$differentLabel = $this->getBaseProperty();
227
		$differentLabel->getFingerprint()->setLabel( 'en', 'Different' );
228
229
		$differentDescription = $this->getBaseProperty();
230
		$differentDescription->getFingerprint()->setDescription( 'en', 'Different' );
231
232
		$differentAlias = $this->getBaseProperty();
233
		$differentAlias->getFingerprint()->setAliasGroup( 'en', array( 'Different' ) );
234
235
		$differentStatement = $this->getBaseProperty();
236
		$differentStatement->setStatements( new StatementList() );
237
238
		$property = $this->getBaseProperty();
239
240
		return array(
241
			'empty' => array( $property, Property::newFromType( 'string' ) ),
242
			'label' => array( $property, $differentLabel ),
243
			'description' => array( $property, $differentDescription ),
244
			'alias' => array( $property, $differentAlias ),
245
			'dataType' => array( Property::newFromType( 'string' ), Property::newFromType( 'foo' ) ),
246
			'statement' => array( $property, $differentStatement ),
247
		);
248
	}
249
250
	/**
251
	 * @dataProvider notEqualsProvider
252
	 */
253
	public function testNotEquals( Property $firstProperty, Property $secondProperty ) {
254
		$this->assertFalse( $firstProperty->equals( $secondProperty ) );
255
		$this->assertFalse( $secondProperty->equals( $firstProperty ) );
256
	}
257
258
	public function testPropertyWithStatementsIsNotEmpty() {
259
		$property = Property::newFromType( 'string' );
260
		$property->setStatements( $this->newNonEmptyStatementList() );
261
262
		$this->assertFalse( $property->isEmpty() );
263
	}
264
265
	public function testCopy() {
266
		$property = Property::newFromType( 'string' );
267
		$property->setLabel( 'en', 'Foo' );
268
269
		$copy = $property->copy();
270
271
		$this->assertNotSame( $property, $copy );
272
		$this->assertTrue( $property->equals( $copy ) );
273
274
		$property->setLabel( 'en', 'Bar' );
275
276
		$this->assertEquals( 'Foo', $copy->getFingerprint()->getLabel( 'en' )->getText() );
277
	}
278
279
}
280