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 testPropertyWithTypeIsEmpty() { |
129
|
|
|
$this->assertTrue( Property::newFromType( 'string' )->isEmpty() ); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function testPropertyWithIdIsEmpty() { |
133
|
|
|
$property = Property::newFromType( 'string' ); |
134
|
|
|
$property->setId( 1337 ); |
135
|
|
|
$this->assertTrue( $property->isEmpty() ); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function testPropertyWithFingerprintIsNotEmpty() { |
139
|
|
|
$property = Property::newFromType( 'string' ); |
140
|
|
|
$property->getFingerprint()->setAliasGroup( 'en', array( 'foo' ) ); |
141
|
|
|
$this->assertFalse( $property->isEmpty() ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function testClearRemovesAllButId() { |
145
|
|
|
$property = Property::newFromType( 'string' ); |
146
|
|
|
$property->setId( 42 ); |
147
|
|
|
$property->getFingerprint()->setLabel( 'en', 'foo' ); |
148
|
|
|
$property->getStatements()->addNewStatement( new PropertyNoValueSnak( 1 ) ); |
149
|
|
|
|
150
|
|
|
$property->clear(); |
151
|
|
|
|
152
|
|
|
$this->assertEquals( new PropertyId( 'P42' ), $property->getId() ); |
153
|
|
|
$this->assertTrue( $property->isEmpty() ); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
public function testGetStatementsReturnsEmptyListForEmptyProperty() { |
157
|
|
|
$property = Property::newFromType( 'string' ); |
158
|
|
|
|
159
|
|
|
$this->assertEquals( new StatementList(), $property->getStatements() ); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function testSetAndGetStatements() { |
163
|
|
|
$property = Property::newFromType( 'string' ); |
164
|
|
|
|
165
|
|
|
$statementList = $this->newNonEmptyStatementList(); |
166
|
|
|
$property->setStatements( $statementList ); |
167
|
|
|
|
168
|
|
|
$this->assertEquals( $statementList, $property->getStatements() ); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
private function newNonEmptyStatementList() { |
172
|
|
|
$statementList = new StatementList(); |
173
|
|
|
$statementList->addNewStatement( new PropertyNoValueSnak( 42 ) ); |
174
|
|
|
$statementList->addNewStatement( new PropertyNoValueSnak( 1337 ) ); |
175
|
|
|
|
176
|
|
|
return $statementList; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
public function equalsProvider() { |
180
|
|
|
$firstProperty = Property::newFromType( 'string' ); |
181
|
|
|
$firstProperty->setStatements( $this->newNonEmptyStatementList() ); |
182
|
|
|
|
183
|
|
|
$secondProperty = Property::newFromType( 'string' ); |
184
|
|
|
$secondProperty->setStatements( $this->newNonEmptyStatementList() ); |
185
|
|
|
|
186
|
|
|
$secondPropertyWithId = $secondProperty->copy(); |
187
|
|
|
$secondPropertyWithId->setId( 42 ); |
188
|
|
|
|
189
|
|
|
$differentId = $secondPropertyWithId->copy(); |
190
|
|
|
$differentId->setId( 43 ); |
191
|
|
|
|
192
|
|
|
return array( |
193
|
|
|
array( Property::newFromType( 'string' ), Property::newFromType( 'string' ) ), |
194
|
|
|
array( $firstProperty, $secondProperty ), |
195
|
|
|
array( $secondProperty, $secondPropertyWithId ), |
196
|
|
|
array( $secondPropertyWithId, $differentId ), |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @dataProvider equalsProvider |
202
|
|
|
*/ |
203
|
|
|
public function testEquals( Property $firstProperty, Property $secondProperty ) { |
204
|
|
|
$this->assertTrue( $firstProperty->equals( $secondProperty ) ); |
205
|
|
|
$this->assertTrue( $secondProperty->equals( $firstProperty ) ); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
private function getBaseProperty() { |
209
|
|
|
$property = Property::newFromType( 'string' ); |
210
|
|
|
|
211
|
|
|
$property->setId( 42 ); |
212
|
|
|
$property->getFingerprint()->setLabel( 'en', 'Same' ); |
213
|
|
|
$property->getFingerprint()->setDescription( 'en', 'Same' ); |
214
|
|
|
$property->getFingerprint()->setAliasGroup( 'en', array( 'Same' ) ); |
215
|
|
|
$property->setStatements( $this->newNonEmptyStatementList() ); |
216
|
|
|
|
217
|
|
|
return $property; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function notEqualsProvider() { |
221
|
|
|
$differentLabel = $this->getBaseProperty(); |
222
|
|
|
$differentLabel->getFingerprint()->setLabel( 'en', 'Different' ); |
223
|
|
|
|
224
|
|
|
$differentDescription = $this->getBaseProperty(); |
225
|
|
|
$differentDescription->getFingerprint()->setDescription( 'en', 'Different' ); |
226
|
|
|
|
227
|
|
|
$differentAlias = $this->getBaseProperty(); |
228
|
|
|
$differentAlias->getFingerprint()->setAliasGroup( 'en', array( 'Different' ) ); |
229
|
|
|
|
230
|
|
|
$differentStatement = $this->getBaseProperty(); |
231
|
|
|
$differentStatement->setStatements( new StatementList() ); |
232
|
|
|
|
233
|
|
|
$property = $this->getBaseProperty(); |
234
|
|
|
|
235
|
|
|
return array( |
236
|
|
|
'empty' => array( $property, Property::newFromType( 'string' ) ), |
237
|
|
|
'label' => array( $property, $differentLabel ), |
238
|
|
|
'description' => array( $property, $differentDescription ), |
239
|
|
|
'alias' => array( $property, $differentAlias ), |
240
|
|
|
'dataType' => array( Property::newFromType( 'string' ), Property::newFromType( 'foo' ) ), |
241
|
|
|
'statement' => array( $property, $differentStatement ), |
242
|
|
|
); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* @dataProvider notEqualsProvider |
247
|
|
|
*/ |
248
|
|
|
public function testNotEquals( Property $firstProperty, Property $secondProperty ) { |
249
|
|
|
$this->assertFalse( $firstProperty->equals( $secondProperty ) ); |
250
|
|
|
$this->assertFalse( $secondProperty->equals( $firstProperty ) ); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function testPropertyWithStatementsIsNotEmpty() { |
254
|
|
|
$property = Property::newFromType( 'string' ); |
255
|
|
|
$property->setStatements( $this->newNonEmptyStatementList() ); |
256
|
|
|
|
257
|
|
|
$this->assertFalse( $property->isEmpty() ); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function testCopy() { |
261
|
|
|
$property = Property::newFromType( 'string' ); |
262
|
|
|
$property->setLabel( 'en', 'Foo' ); |
263
|
|
|
|
264
|
|
|
$copy = $property->copy(); |
265
|
|
|
|
266
|
|
|
$this->assertNotSame( $property, $copy ); |
267
|
|
|
$this->assertTrue( $property->equals( $copy ) ); |
268
|
|
|
|
269
|
|
|
$property->setLabel( 'en', 'Bar' ); |
270
|
|
|
|
271
|
|
|
$this->assertEquals( 'Foo', $copy->getFingerprint()->getLabel( 'en' )->getText() ); |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
} |
275
|
|
|
|