1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Tests\Snak; |
4
|
|
|
|
5
|
|
|
use DataValues\StringValue; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
8
|
|
|
use Wikibase\DataModel\Snak\PropertyNoValueSnak; |
9
|
|
|
use Wikibase\DataModel\Snak\PropertyValueSnak; |
10
|
|
|
use Wikibase\DataModel\Snak\Snak; |
11
|
|
|
use Wikibase\DataModel\Snak\SnakList; |
12
|
|
|
use Wikibase\DataModel\Tests\HashArray\HashArrayTest; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @covers Wikibase\DataModel\Snak\SnakList |
16
|
|
|
* @uses DataValues\StringValue |
17
|
|
|
* @uses Wikibase\DataModel\Entity\PropertyId |
18
|
|
|
* @uses Wikibase\DataModel\Snak\PropertyNoValueSnak |
19
|
|
|
* @uses Wikibase\DataModel\Snak\PropertyValueSnak |
20
|
|
|
* @uses Wikibase\DataModel\Snak\Snak |
21
|
|
|
* @uses Wikibase\DataModel\Snak\SnakList |
22
|
|
|
* @uses Wikibase\DataModel\HashArray |
23
|
|
|
* @uses Wikibase\DataModel\Snak\SnakObject |
24
|
|
|
* @uses Wikibase\DataModel\Internal\MapValueHasher |
25
|
|
|
* @uses Wikibase\DataModel\Entity\EntityId |
26
|
|
|
* |
27
|
|
|
* @license GPL-2.0+ |
28
|
|
|
* @author Jeroen De Dauw < [email protected] > |
29
|
|
|
* @author Addshore |
30
|
|
|
* @author Thiemo Mättig |
31
|
|
|
*/ |
32
|
|
|
class SnakListTest extends HashArrayTest { |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @see HashArrayTest::getInstanceClass |
36
|
|
|
*/ |
37
|
|
|
public function getInstanceClass() { |
38
|
|
|
return 'Wikibase\DataModel\Snak\SnakList'; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function elementInstancesProvider() { |
42
|
|
|
$id42 = new PropertyId( 'P42' ); |
43
|
|
|
|
44
|
|
|
$argLists = array(); |
45
|
|
|
|
46
|
|
|
$argLists[] = array( array( new PropertyNoValueSnak( $id42 ) ) ); |
47
|
|
|
$argLists[] = array( array( new PropertyNoValueSnak( new PropertyId( 'P9001' ) ) ) ); |
48
|
|
|
$argLists[] = array( array( new PropertyValueSnak( $id42, new StringValue( 'a' ) ) ) ); |
49
|
|
|
|
50
|
|
|
return $argLists; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function constructorProvider() { |
54
|
|
|
$id42 = new PropertyId( 'P42' ); |
55
|
|
|
$id9001 = new PropertyId( 'P9001' ); |
56
|
|
|
|
57
|
|
|
return array( |
58
|
|
|
array(), |
59
|
|
|
array( array() ), |
60
|
|
|
array( array( |
61
|
|
|
new PropertyNoValueSnak( $id42 ) |
62
|
|
|
) ), |
63
|
|
|
array( array( |
64
|
|
|
new PropertyNoValueSnak( $id42 ), |
65
|
|
|
new PropertyNoValueSnak( $id9001 ), |
66
|
|
|
) ), |
67
|
|
|
array( array( |
68
|
|
|
new PropertyNoValueSnak( $id42 ), |
69
|
|
|
new PropertyNoValueSnak( $id9001 ), |
70
|
|
|
new PropertyValueSnak( $id42, new StringValue( 'a' ) ), |
71
|
|
|
) ), |
72
|
|
|
); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @dataProvider invalidConstructorArgumentsProvider |
77
|
|
|
* @expectedException InvalidArgumentException |
78
|
|
|
*/ |
79
|
|
|
public function testGivenInvalidConstructorArguments_constructorThrowsException( $input ) { |
80
|
|
|
new SnakList( $input ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
public function invalidConstructorArgumentsProvider() { |
84
|
|
|
$id1 = new PropertyId( 'P1' ); |
85
|
|
|
|
86
|
|
|
return array( |
87
|
|
|
array( false ), |
88
|
|
|
array( 1 ), |
89
|
|
|
array( 0.1 ), |
90
|
|
|
array( 'string' ), |
91
|
|
|
array( $id1 ), |
92
|
|
|
array( new PropertyNoValueSnak( $id1 ) ), |
93
|
|
|
array( new PropertyValueSnak( $id1, new StringValue( 'a' ) ) ), |
94
|
|
|
array( array( null ) ), |
95
|
|
|
array( array( $id1 ) ), |
96
|
|
|
array( array( new SnakList() ) ), |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function testGivenAssociativeArray_constructorPreservesArrayKeys() { |
101
|
|
|
$snakList = new SnakList( array( 'key' => new PropertyNoValueSnak( 1 ) ) ); |
102
|
|
|
$this->assertSame( array( 'key' ), array_keys( iterator_to_array( $snakList ) ) ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @dataProvider instanceProvider |
107
|
|
|
* @param SnakList $array |
108
|
|
|
*/ |
109
|
|
|
public function testHasSnak( SnakList $array ) { |
110
|
|
|
/** |
111
|
|
|
* @var Snak $hashable |
112
|
|
|
*/ |
113
|
|
|
foreach ( iterator_to_array( $array ) as $hashable ) { |
114
|
|
|
$this->assertTrue( $array->hasSnak( $hashable ) ); |
115
|
|
|
$this->assertTrue( $array->hasSnakHash( $hashable->getHash() ) ); |
116
|
|
|
$array->removeSnak( $hashable ); |
117
|
|
|
$this->assertFalse( $array->hasSnak( $hashable ) ); |
118
|
|
|
$this->assertFalse( $array->hasSnakHash( $hashable->getHash() ) ); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->assertTrue( true ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @dataProvider instanceProvider |
126
|
|
|
* @param SnakList $array |
127
|
|
|
*/ |
128
|
|
|
public function testRemoveSnak( SnakList $array ) { |
129
|
|
|
$elementCount = $array->count(); |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @var Snak $element |
133
|
|
|
*/ |
134
|
|
|
foreach ( iterator_to_array( $array ) as $element ) { |
135
|
|
|
$this->assertTrue( $array->hasSnak( $element ) ); |
136
|
|
|
|
137
|
|
|
if ( $elementCount % 2 === 0 ) { |
138
|
|
|
$array->removeSnak( $element ); |
139
|
|
|
} |
140
|
|
|
else { |
141
|
|
|
$array->removeSnakHash( $element->getHash() ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
$this->assertFalse( $array->hasSnak( $element ) ); |
145
|
|
|
$this->assertEquals( --$elementCount, $array->count() ); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$element = new PropertyNoValueSnak( new PropertyId( 'P42' ) ); |
149
|
|
|
|
150
|
|
|
$array->removeSnak( $element ); |
151
|
|
|
$array->removeSnakHash( $element->getHash() ); |
152
|
|
|
|
153
|
|
|
$this->assertTrue( true ); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @dataProvider instanceProvider |
158
|
|
|
* @param SnakList $array |
159
|
|
|
*/ |
160
|
|
|
public function testAddSnak( SnakList $array ) { |
161
|
|
|
$elementCount = $array->count(); |
162
|
|
|
|
163
|
|
|
$elements = $this->elementInstancesProvider(); |
164
|
|
|
$element = array_shift( $elements ); |
165
|
|
|
$element = $element[0][0]; |
166
|
|
|
|
167
|
|
|
if ( !$array->hasSnak( $element ) ) { |
168
|
|
|
++$elementCount; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
$this->assertEquals( !$array->hasSnak( $element ), $array->addSnak( $element ) ); |
172
|
|
|
|
173
|
|
|
$this->assertEquals( $elementCount, $array->count() ); |
174
|
|
|
|
175
|
|
|
$this->assertFalse( $array->addSnak( $element ) ); |
176
|
|
|
|
177
|
|
|
$this->assertEquals( $elementCount, $array->count() ); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function orderByPropertyProvider() { |
181
|
|
|
$class = $this->getInstanceClass(); |
182
|
|
|
|
183
|
|
|
$id1 = new PropertyId( 'P1' ); |
184
|
|
|
$id2 = new PropertyId( 'P2' ); |
185
|
|
|
$id3 = new PropertyId( 'P3' ); |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* List of test data containing snaks to initialize SnakList objects. The first list of |
189
|
|
|
* snaks represents the snak list to be used as test input while the second represents the |
190
|
|
|
* expected result. |
191
|
|
|
* @var array |
192
|
|
|
*/ |
193
|
|
|
$rawArguments = array( |
194
|
|
|
'Default order' => array( |
195
|
|
|
array(), |
196
|
|
|
array(), |
197
|
|
|
), |
198
|
|
|
'Unknown id in order' => array( |
199
|
|
|
array(), |
200
|
|
|
array(), |
201
|
|
|
array( 'P1' ) |
202
|
|
|
), |
203
|
|
|
array( |
204
|
|
|
array( new PropertyNoValueSnak( $id1 ) ), |
205
|
|
|
array( new PropertyNoValueSnak( $id1 ) ), |
206
|
|
|
), |
207
|
|
|
array( |
208
|
|
|
array( |
209
|
|
|
new PropertyNoValueSnak( $id2 ), |
210
|
|
|
new PropertyNoValueSnak( $id1 ), |
211
|
|
|
), |
212
|
|
|
array( |
213
|
|
|
new PropertyNoValueSnak( $id2 ), |
214
|
|
|
new PropertyNoValueSnak( $id1 ), |
215
|
|
|
), |
216
|
|
|
), |
217
|
|
|
array( |
218
|
|
|
array( |
219
|
|
|
new PropertyNoValueSnak( $id1 ), |
220
|
|
|
new PropertyNoValueSnak( $id2 ), |
221
|
|
|
new PropertyValueSnak( $id1, new StringValue( 'a' ) ), |
222
|
|
|
), |
223
|
|
|
array( |
224
|
|
|
new PropertyNoValueSnak( $id1 ), |
225
|
|
|
new PropertyValueSnak( $id1, new StringValue( 'a' ) ), |
226
|
|
|
new PropertyNoValueSnak( $id2 ), |
227
|
|
|
), |
228
|
|
|
), |
229
|
|
|
'With additional order' => array( |
230
|
|
|
array( |
231
|
|
|
new PropertyNoValueSnak( $id3 ), |
232
|
|
|
new PropertyNoValueSnak( $id2 ), |
233
|
|
|
new PropertyValueSnak( $id1, new StringValue( 'a' ) ), |
234
|
|
|
), |
235
|
|
|
array( |
236
|
|
|
new PropertyNoValueSnak( $id2 ), |
237
|
|
|
new PropertyNoValueSnak( $id3 ), |
238
|
|
|
new PropertyValueSnak( $id1, new StringValue( 'a' ) ), |
239
|
|
|
), |
240
|
|
|
array( 'P2' ) |
241
|
|
|
), |
242
|
|
|
array( |
243
|
|
|
array( |
244
|
|
|
new PropertyNoValueSnak( $id3 ), |
245
|
|
|
new PropertyNoValueSnak( $id2 ), |
246
|
|
|
new PropertyNoValueSnak( $id2 ), |
247
|
|
|
new PropertyValueSnak( $id1, new StringValue( 'a' ) ), |
248
|
|
|
new PropertyNoValueSnak( $id1 ), |
249
|
|
|
), |
250
|
|
|
array( |
251
|
|
|
new PropertyValueSnak( $id1, new StringValue( 'a' ) ), |
252
|
|
|
new PropertyNoValueSnak( $id1 ), |
253
|
|
|
new PropertyNoValueSnak( $id3 ), |
254
|
|
|
new PropertyNoValueSnak( $id2 ), |
255
|
|
|
new PropertyNoValueSnak( $id2 ), |
256
|
|
|
), |
257
|
|
|
array( 'P1' ) |
258
|
|
|
), |
259
|
|
|
); |
260
|
|
|
|
261
|
|
|
$arguments = array(); |
262
|
|
|
|
263
|
|
|
foreach ( $rawArguments as $key => $rawArgument ) { |
264
|
|
|
$arguments[$key] = array( |
265
|
|
|
new $class( $rawArgument[0] ), |
266
|
|
|
new $class( $rawArgument[1] ), |
267
|
|
|
array_key_exists( 2, $rawArgument ) ? $rawArgument[2] : array() |
268
|
|
|
); |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return $arguments; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* @dataProvider orderByPropertyProvider |
276
|
|
|
*/ |
277
|
|
|
public function testOrderByProperty( SnakList $snakList, SnakList $expected, array $order = array() ) { |
278
|
|
|
$initialSnakList = new SnakList( array_values( iterator_to_array( $snakList ) ) ); |
279
|
|
|
|
280
|
|
|
$snakList->orderByProperty( $order ); |
281
|
|
|
|
282
|
|
|
// Instantiate new SnakList resetting the snaks' array keys. This allows comparing the |
283
|
|
|
// reordered SnakList to the expected SnakList. |
284
|
|
|
$orderedSnakList = new SnakList( array_values( iterator_to_array( $snakList ) ) ); |
285
|
|
|
|
286
|
|
|
$this->assertEquals( $expected, $orderedSnakList ); |
287
|
|
|
|
288
|
|
|
if ( $orderedSnakList->equals( $initialSnakList ) ) { |
289
|
|
|
$this->assertSame( $initialSnakList->getHash(), $snakList->getHash() ); |
290
|
|
|
} else { |
291
|
|
|
$this->assertNotSame( $initialSnakList->getHash(), $snakList->getHash() ); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
} |
296
|
|
|
|