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
|
|
|
* @licence GNU GPL v2+ |
20
|
|
|
* @author Jeroen De Dauw < [email protected] > |
21
|
|
|
* @author Bene* < [email protected] > |
22
|
|
|
*/ |
23
|
|
|
class PropertyTest extends \PHPUnit_Framework_TestCase { |
24
|
|
|
|
25
|
|
|
public function testConstructorWithAllParameters() { |
26
|
|
|
$property = new Property( |
27
|
|
|
new PropertyId( 'P42' ), |
28
|
|
|
new Fingerprint(), |
29
|
|
|
'string', |
30
|
|
|
new StatementList() |
31
|
|
|
); |
32
|
|
|
$this->assertInstanceOf( 'Wikibase\DataModel\Entity\Property', $property ); |
33
|
|
|
$this->assertEquals( new PropertyId( 'P42' ), $property->getId() ); |
34
|
|
|
$this->assertEquals( new Fingerprint(), $property->getFingerprint() ); |
35
|
|
|
$this->assertEquals( 'string', $property->getDataTypeId() ); |
36
|
|
|
$this->assertEquals( new StatementList(), $property->getStatements() ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function testConstructorWithMinimalParameters() { |
40
|
|
|
$property = new Property( null, null, '' ); |
41
|
|
|
$this->assertInstanceOf( 'Wikibase\DataModel\Entity\Property', $property ); |
42
|
|
|
$this->assertNull( $property->getId() ); |
43
|
|
|
$this->assertEquals( new Fingerprint(), $property->getFingerprint() ); |
44
|
|
|
$this->assertEquals( '', $property->getDataTypeId() ); |
45
|
|
|
$this->assertEquals( new StatementList(), $property->getStatements() ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @expectedException InvalidArgumentException |
50
|
|
|
*/ |
51
|
|
|
public function testGivenInvalidType_ConstructorThrowsException() { |
52
|
|
|
new Property( null, null, null ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function testNewFromType() { |
56
|
|
|
$property = Property::newFromType( 'string' ); |
57
|
|
|
$this->assertInstanceOf( 'Wikibase\DataModel\Entity\Property', $property ); |
58
|
|
|
$this->assertEquals( 'string', $property->getDataTypeId() ); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testSetAndGetDataTypeId() { |
62
|
|
|
$property = Property::newFromType( 'string' ); |
63
|
|
|
|
64
|
|
|
foreach ( array( 'string', 'foobar', 'nyan', 'string' ) as $typeId ) { |
65
|
|
|
$property->setDataTypeId( $typeId ); |
66
|
|
|
$this->assertEquals( $typeId, $property->getDataTypeId() ); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function testWhenIdSetWithNumber_GetIdReturnsPropertyId() { |
71
|
|
|
$property = Property::newFromType( 'string' ); |
72
|
|
|
$property->setId( 42 ); |
73
|
|
|
|
74
|
|
|
$this->assertHasCorrectIdType( $property ); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
protected function assertHasCorrectIdType( Property $property ) { |
78
|
|
|
$this->assertInstanceOf( 'Wikibase\DataModel\Entity\PropertyId', $property->getId() ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testWhenIdSetWithPropertyId_GetIdReturnsPropertyId() { |
82
|
|
|
$property = Property::newFromType( 'string' ); |
83
|
|
|
$property->setId( new PropertyId( 'P42' ) ); |
84
|
|
|
|
85
|
|
|
$this->assertHasCorrectIdType( $property ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function testGetFingerprint_emptyFingerprint() { |
89
|
|
|
$property = Property::newFromType( 'string' ); |
90
|
|
|
|
91
|
|
|
$this->assertEquals( |
92
|
|
|
new Fingerprint(), |
93
|
|
|
$property->getFingerprint() |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testGetFingerprint_termsAreSet() { |
98
|
|
|
$property = Property::newFromType( 'string' ); |
99
|
|
|
|
100
|
|
|
$property->setLabel( 'en', 'foo' ); |
101
|
|
|
$property->setDescription( 'en', 'foo bar' ); |
102
|
|
|
$property->setAliases( 'en', array( 'foo', 'bar' ) ); |
103
|
|
|
|
104
|
|
|
$this->assertEquals( |
105
|
|
|
new Fingerprint( |
106
|
|
|
new TermList( array( |
107
|
|
|
new Term( 'en', 'foo' ), |
108
|
|
|
) ), |
109
|
|
|
new TermList( array( |
110
|
|
|
new Term( 'en', 'foo bar' ) |
111
|
|
|
) ), |
112
|
|
|
new AliasGroupList( array( |
113
|
|
|
new AliasGroup( 'en', array( 'foo', 'bar' ) ) |
114
|
|
|
) ) |
115
|
|
|
), |
116
|
|
|
$property->getFingerprint() |
117
|
|
|
); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function testSetFingerprint_getFingerprintReturnsIt() { |
121
|
|
|
$property = Property::newFromType( 'string' ); |
122
|
|
|
$fingerprint = new Fingerprint(); |
123
|
|
|
|
124
|
|
|
$property->setFingerprint( $fingerprint ); |
125
|
|
|
$this->assertSame( $fingerprint, $property->getFingerprint() ); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testGetLabels() { |
129
|
|
|
$property = Property::newFromType( 'string' ); |
130
|
|
|
$property->setLabel( 'en', 'foo' ); |
131
|
|
|
|
132
|
|
|
$this->assertEquals( |
133
|
|
|
new TermList( array( |
134
|
|
|
new Term( 'en', 'foo' ) |
135
|
|
|
) ), |
136
|
|
|
$property->getLabels() |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function testGetDescriptions() { |
141
|
|
|
$property = Property::newFromType( 'string' ); |
142
|
|
|
$property->setDescription( 'en', 'foo bar' ); |
143
|
|
|
|
144
|
|
|
$this->assertEquals( |
145
|
|
|
new TermList( array( |
146
|
|
|
new Term( 'en', 'foo bar' ) |
147
|
|
|
) ), |
148
|
|
|
$property->getDescriptions() |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function testGetAliases() { |
153
|
|
|
$property = Property::newFromType( 'string' ); |
154
|
|
|
$property->setAliases( 'en', array( 'foo', 'bar' ) ); |
155
|
|
|
|
156
|
|
|
$this->assertEquals( |
157
|
|
|
new AliasGroupList( array( |
158
|
|
|
new AliasGroup( 'en', array( 'foo', 'bar' ) ) |
159
|
|
|
) ), |
160
|
|
|
$property->getAliasGroups() |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function testGetLabels_sameListAsFingerprint() { |
165
|
|
|
$property = Property::newFromType( 'string' ); |
166
|
|
|
|
167
|
|
|
$this->assertSame( |
168
|
|
|
$property->getFingerprint()->getLabels(), |
169
|
|
|
$property->getLabels() |
170
|
|
|
); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function testGetDescriptions_sameListAsFingerprint() { |
174
|
|
|
$property = Property::newFromType( 'string' ); |
175
|
|
|
|
176
|
|
|
$this->assertSame( |
177
|
|
|
$property->getFingerprint()->getDescriptions(), |
178
|
|
|
$property->getDescriptions() |
179
|
|
|
); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
public function testGetAliasGroups_sameListAsFingerprint() { |
183
|
|
|
$property = Property::newFromType( 'string' ); |
184
|
|
|
|
185
|
|
|
$this->assertSame( |
186
|
|
|
$property->getFingerprint()->getAliasGroups(), |
187
|
|
|
$property->getAliasGroups() |
188
|
|
|
); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
public function testPropertyWithTypeIsEmpty() { |
192
|
|
|
$this->assertTrue( Property::newFromType( 'string' )->isEmpty() ); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function testPropertyWithIdIsEmpty() { |
196
|
|
|
$property = Property::newFromType( 'string' ); |
197
|
|
|
$property->setId( 1337 ); |
198
|
|
|
$this->assertTrue( $property->isEmpty() ); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
public function testPropertyWithFingerprintIsNotEmpty() { |
202
|
|
|
$property = Property::newFromType( 'string' ); |
203
|
|
|
$property->getFingerprint()->setAliasGroup( 'en', array( 'foo' ) ); |
204
|
|
|
$this->assertFalse( $property->isEmpty() ); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function testClearRemovesAllButId() { |
208
|
|
|
$property = Property::newFromType( 'string' ); |
209
|
|
|
$property->setId( 42 ); |
210
|
|
|
$property->getFingerprint()->setLabel( 'en', 'foo' ); |
211
|
|
|
$property->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) ); |
212
|
|
|
|
213
|
|
|
$property->clear(); |
214
|
|
|
|
215
|
|
|
$this->assertEquals( new PropertyId( 'P42' ), $property->getId() ); |
216
|
|
|
$this->assertTrue( $property->isEmpty() ); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
public function testGetStatementsReturnsEmptyListForEmptyProperty() { |
220
|
|
|
$property = Property::newFromType( 'string' ); |
221
|
|
|
|
222
|
|
|
$this->assertEquals( new StatementList(), $property->getStatements() ); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function testSetAndGetStatements() { |
226
|
|
|
$property = Property::newFromType( 'string' ); |
227
|
|
|
|
228
|
|
|
$statementList = $this->newNonEmptyStatementList(); |
229
|
|
|
$property->setStatements( $statementList ); |
230
|
|
|
|
231
|
|
|
$this->assertEquals( $statementList, $property->getStatements() ); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
private function newNonEmptyStatementList() { |
235
|
|
|
$statementList = new StatementList(); |
236
|
|
|
$statementList->addNewStatement( new PropertyNoValueSnak( 42 ) ); |
237
|
|
|
$statementList->addNewStatement( new PropertyNoValueSnak( 1337 ) ); |
238
|
|
|
|
239
|
|
|
return $statementList; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
public function equalsProvider() { |
243
|
|
|
$firstProperty = Property::newFromType( 'string' ); |
244
|
|
|
$firstProperty->setStatements( $this->newNonEmptyStatementList() ); |
245
|
|
|
|
246
|
|
|
$secondProperty = Property::newFromType( 'string' ); |
247
|
|
|
$secondProperty->setStatements( $this->newNonEmptyStatementList() ); |
248
|
|
|
|
249
|
|
|
$secondPropertyWithId = $secondProperty->copy(); |
250
|
|
|
$secondPropertyWithId->setId( 42 ); |
251
|
|
|
|
252
|
|
|
$differentId = $secondPropertyWithId->copy(); |
253
|
|
|
$differentId->setId( 43 ); |
254
|
|
|
|
255
|
|
|
return array( |
256
|
|
|
array( Property::newFromType( 'string' ), Property::newFromType( 'string' ) ), |
257
|
|
|
array( $firstProperty, $secondProperty ), |
258
|
|
|
array( $secondProperty, $secondPropertyWithId ), |
259
|
|
|
array( $secondPropertyWithId, $differentId ), |
260
|
|
|
); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
/** |
264
|
|
|
* @dataProvider equalsProvider |
265
|
|
|
*/ |
266
|
|
|
public function testEquals( Property $firstProperty, Property $secondProperty ) { |
267
|
|
|
$this->assertTrue( $firstProperty->equals( $secondProperty ) ); |
268
|
|
|
$this->assertTrue( $secondProperty->equals( $firstProperty ) ); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
private function getBaseProperty() { |
272
|
|
|
$property = Property::newFromType( 'string' ); |
273
|
|
|
|
274
|
|
|
$property->setId( 42 ); |
275
|
|
|
$property->getFingerprint()->setLabel( 'en', 'Same' ); |
276
|
|
|
$property->getFingerprint()->setDescription( 'en', 'Same' ); |
277
|
|
|
$property->getFingerprint()->setAliasGroup( 'en', array( 'Same' ) ); |
278
|
|
|
$property->setStatements( $this->newNonEmptyStatementList() ); |
279
|
|
|
|
280
|
|
|
return $property; |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
public function notEqualsProvider() { |
284
|
|
|
$differentLabel = $this->getBaseProperty(); |
285
|
|
|
$differentLabel->getFingerprint()->setLabel( 'en', 'Different' ); |
286
|
|
|
|
287
|
|
|
$differentDescription = $this->getBaseProperty(); |
288
|
|
|
$differentDescription->getFingerprint()->setDescription( 'en', 'Different' ); |
289
|
|
|
|
290
|
|
|
$differentAlias = $this->getBaseProperty(); |
291
|
|
|
$differentAlias->getFingerprint()->setAliasGroup( 'en', array( 'Different' ) ); |
292
|
|
|
|
293
|
|
|
$differentStatement = $this->getBaseProperty(); |
294
|
|
|
$differentStatement->setStatements( new StatementList() ); |
295
|
|
|
|
296
|
|
|
$property = $this->getBaseProperty(); |
297
|
|
|
|
298
|
|
|
return array( |
299
|
|
|
'empty' => array( $property, Property::newFromType( 'string' ) ), |
300
|
|
|
'label' => array( $property, $differentLabel ), |
301
|
|
|
'description' => array( $property, $differentDescription ), |
302
|
|
|
'alias' => array( $property, $differentAlias ), |
303
|
|
|
'dataType' => array( Property::newFromType( 'string' ), Property::newFromType( 'foo' ) ), |
304
|
|
|
'statement' => array( $property, $differentStatement ), |
305
|
|
|
); |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* @dataProvider notEqualsProvider |
310
|
|
|
*/ |
311
|
|
|
public function testNotEquals( Property $firstProperty, Property $secondProperty ) { |
312
|
|
|
$this->assertFalse( $firstProperty->equals( $secondProperty ) ); |
313
|
|
|
$this->assertFalse( $secondProperty->equals( $firstProperty ) ); |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
public function testPropertyWithStatementsIsNotEmpty() { |
317
|
|
|
$property = Property::newFromType( 'string' ); |
318
|
|
|
$property->setStatements( $this->newNonEmptyStatementList() ); |
319
|
|
|
|
320
|
|
|
$this->assertFalse( $property->isEmpty() ); |
321
|
|
|
} |
322
|
|
|
|
323
|
|
|
public function testCopy() { |
324
|
|
|
$property = Property::newFromType( 'string' ); |
325
|
|
|
$property->setLabel( 'en', 'Foo' ); |
326
|
|
|
|
327
|
|
|
$copy = $property->copy(); |
328
|
|
|
|
329
|
|
|
$this->assertNotSame( $property, $copy ); |
330
|
|
|
$this->assertTrue( $property->equals( $copy ) ); |
331
|
|
|
|
332
|
|
|
$property->setLabel( 'en', 'Bar' ); |
333
|
|
|
|
334
|
|
|
$this->assertEquals( 'Foo', $copy->getFingerprint()->getLabel( 'en' )->getText() ); |
335
|
|
|
} |
336
|
|
|
|
337
|
|
|
} |
338
|
|
|
|