|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tests\Wikibase\DataModel\Deserializers; |
|
4
|
|
|
|
|
5
|
|
|
use Deserializers\Deserializer; |
|
6
|
|
|
use Deserializers\Exceptions\DeserializationException; |
|
7
|
|
|
use Deserializers\Exceptions\InvalidAttributeException; |
|
8
|
|
|
use Wikibase\DataModel\Deserializers\StatementDeserializer; |
|
9
|
|
|
use Wikibase\DataModel\ReferenceList; |
|
10
|
|
|
use Wikibase\DataModel\Snak\PropertyNoValueSnak; |
|
11
|
|
|
use Wikibase\DataModel\Snak\PropertySomeValueSnak; |
|
12
|
|
|
use Wikibase\DataModel\Snak\SnakList; |
|
13
|
|
|
use Wikibase\DataModel\Statement\Statement; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @covers Wikibase\DataModel\Deserializers\StatementDeserializer |
|
17
|
|
|
* |
|
18
|
|
|
* @license GPL-2.0-or-later |
|
19
|
|
|
* @author Thomas Pellissier Tanon |
|
20
|
|
|
*/ |
|
21
|
|
|
class StatementDeserializerTest extends DispatchableDeserializerTest { |
|
22
|
|
|
|
|
23
|
|
|
protected function buildDeserializer() { |
|
24
|
|
|
$snakDeserializer = $this->getMockBuilder( Deserializer::class )->getMock(); |
|
25
|
|
|
$snakDeserializer->expects( $this->any() ) |
|
26
|
|
|
->method( 'deserialize' ) |
|
27
|
|
|
->with( $this->equalTo( [ |
|
28
|
|
|
'snaktype' => 'novalue', |
|
29
|
|
|
'property' => 'P42' |
|
30
|
|
|
] ) ) |
|
31
|
|
|
->will( $this->returnValue( new PropertyNoValueSnak( 42 ) ) ); |
|
32
|
|
|
|
|
33
|
|
|
$snakListDeserializer = $this->getMockBuilder( Deserializer::class )->getMock(); |
|
34
|
|
|
$snakListDeserializer->expects( $this->any() ) |
|
35
|
|
|
->method( 'deserialize' ) |
|
36
|
|
|
->with( $this->equalTo( [ |
|
37
|
|
|
'P42' => [ |
|
38
|
|
|
[ |
|
39
|
|
|
'snaktype' => 'novalue', |
|
40
|
|
|
'property' => 'P42' |
|
41
|
|
|
] |
|
42
|
|
|
] |
|
43
|
|
|
] ) ) |
|
44
|
|
|
->will( $this->returnValue( new SnakList( [ |
|
45
|
|
|
new PropertyNoValueSnak( 42 ) |
|
46
|
|
|
] ) ) ); |
|
47
|
|
|
|
|
48
|
|
|
$referenceListDeserializer = $this->getMockBuilder( Deserializer::class )->getMock(); |
|
49
|
|
|
$referenceListDeserializer->expects( $this->any() ) |
|
50
|
|
|
->method( 'deserialize' ) |
|
51
|
|
|
->with( $this->equalTo( [] ) ) |
|
52
|
|
|
->will( $this->returnValue( new ReferenceList() ) ); |
|
53
|
|
|
|
|
54
|
|
|
return new StatementDeserializer( |
|
55
|
|
|
$snakDeserializer, |
|
|
|
|
|
|
56
|
|
|
$snakListDeserializer, |
|
|
|
|
|
|
57
|
|
|
$referenceListDeserializer |
|
|
|
|
|
|
58
|
|
|
); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function deserializableProvider() { |
|
62
|
|
|
return [ |
|
63
|
|
|
[ |
|
64
|
|
|
[ |
|
65
|
|
|
'mainsnak' => [ |
|
66
|
|
|
'snaktype' => 'novalue', |
|
67
|
|
|
'property' => 'P42' |
|
68
|
|
|
], |
|
69
|
|
|
'type' => 'claim' |
|
70
|
|
|
] |
|
71
|
|
|
], |
|
72
|
|
|
[ |
|
73
|
|
|
[ |
|
74
|
|
|
'mainsnak' => [ |
|
75
|
|
|
'snaktype' => 'novalue', |
|
76
|
|
|
'property' => 'P42' |
|
77
|
|
|
], |
|
78
|
|
|
'type' => 'statement', |
|
79
|
|
|
'rank' => 'normal' |
|
80
|
|
|
] |
|
81
|
|
|
], |
|
82
|
|
|
]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function nonDeserializableProvider() { |
|
86
|
|
|
return [ |
|
87
|
|
|
[ |
|
88
|
|
|
42 |
|
89
|
|
|
], |
|
90
|
|
|
[ |
|
91
|
|
|
[ |
|
92
|
|
|
'id' => 'P10' |
|
93
|
|
|
] |
|
94
|
|
|
], |
|
95
|
|
|
[ |
|
96
|
|
|
[ |
|
97
|
|
|
'type' => '42' |
|
98
|
|
|
] |
|
99
|
|
|
], |
|
100
|
|
|
]; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function deserializationProvider() { |
|
104
|
|
|
$serializations = []; |
|
105
|
|
|
|
|
106
|
|
|
$serializations[] = [ |
|
107
|
|
|
new Statement( new PropertyNoValueSnak( 42 ) ), |
|
108
|
|
|
[ |
|
109
|
|
|
'mainsnak' => [ |
|
110
|
|
|
'snaktype' => 'novalue', |
|
111
|
|
|
'property' => 'P42' |
|
112
|
|
|
], |
|
113
|
|
|
'type' => 'claim' |
|
114
|
|
|
] |
|
115
|
|
|
]; |
|
116
|
|
|
|
|
117
|
|
|
$serializations[] = [ |
|
118
|
|
|
new Statement( new PropertyNoValueSnak( 42 ) ), |
|
119
|
|
|
[ |
|
120
|
|
|
'mainsnak' => [ |
|
121
|
|
|
'snaktype' => 'novalue', |
|
122
|
|
|
'property' => 'P42' |
|
123
|
|
|
], |
|
124
|
|
|
'type' => 'statement' |
|
125
|
|
|
] |
|
126
|
|
|
]; |
|
127
|
|
|
|
|
128
|
|
|
$statement = new Statement( new PropertyNoValueSnak( 42 ) ); |
|
129
|
|
|
$statement->setGuid( 'q42' ); |
|
130
|
|
|
$serializations[] = [ |
|
131
|
|
|
$statement, |
|
132
|
|
|
[ |
|
133
|
|
|
'id' => 'q42', |
|
134
|
|
|
'mainsnak' => [ |
|
135
|
|
|
'snaktype' => 'novalue', |
|
136
|
|
|
'property' => 'P42' |
|
137
|
|
|
], |
|
138
|
|
|
'type' => 'claim' |
|
139
|
|
|
] |
|
140
|
|
|
]; |
|
141
|
|
|
|
|
142
|
|
|
$statement = new Statement( new PropertyNoValueSnak( 42 ) ); |
|
143
|
|
|
$statement->setRank( Statement::RANK_PREFERRED ); |
|
144
|
|
|
$serializations[] = [ |
|
145
|
|
|
$statement, |
|
146
|
|
|
[ |
|
147
|
|
|
'mainsnak' => [ |
|
148
|
|
|
'snaktype' => 'novalue', |
|
149
|
|
|
'property' => 'P42' |
|
150
|
|
|
], |
|
151
|
|
|
'type' => 'statement', |
|
152
|
|
|
'rank' => 'preferred' |
|
153
|
|
|
] |
|
154
|
|
|
]; |
|
155
|
|
|
|
|
156
|
|
|
$statement = new Statement( new PropertyNoValueSnak( 42 ) ); |
|
157
|
|
|
$statement->setRank( Statement::RANK_NORMAL ); |
|
158
|
|
|
$serializations[] = [ |
|
159
|
|
|
$statement, |
|
160
|
|
|
[ |
|
161
|
|
|
'mainsnak' => [ |
|
162
|
|
|
'snaktype' => 'novalue', |
|
163
|
|
|
'property' => 'P42' |
|
164
|
|
|
], |
|
165
|
|
|
'type' => 'statement', |
|
166
|
|
|
'rank' => 'normal' |
|
167
|
|
|
] |
|
168
|
|
|
]; |
|
169
|
|
|
|
|
170
|
|
|
$statement = new Statement( new PropertyNoValueSnak( 42 ) ); |
|
171
|
|
|
$statement->setRank( Statement::RANK_DEPRECATED ); |
|
172
|
|
|
$serializations[] = [ |
|
173
|
|
|
$statement, |
|
174
|
|
|
[ |
|
175
|
|
|
'mainsnak' => [ |
|
176
|
|
|
'snaktype' => 'novalue', |
|
177
|
|
|
'property' => 'P42' |
|
178
|
|
|
], |
|
179
|
|
|
'type' => 'statement', |
|
180
|
|
|
'rank' => 'deprecated' |
|
181
|
|
|
] |
|
182
|
|
|
]; |
|
183
|
|
|
|
|
184
|
|
|
$statement = new Statement( new PropertyNoValueSnak( 42 ) ); |
|
185
|
|
|
$statement->setQualifiers( new SnakList( [] ) ); |
|
186
|
|
|
$serializations[] = [ |
|
187
|
|
|
$statement, |
|
188
|
|
|
[ |
|
189
|
|
|
'mainsnak' => [ |
|
190
|
|
|
'snaktype' => 'novalue', |
|
191
|
|
|
'property' => 'P42' |
|
192
|
|
|
], |
|
193
|
|
|
'type' => 'statement', |
|
194
|
|
|
'rank' => 'normal' |
|
195
|
|
|
] |
|
196
|
|
|
]; |
|
197
|
|
|
|
|
198
|
|
|
$statement = new Statement( new PropertyNoValueSnak( 42 ) ); |
|
199
|
|
|
$statement->setQualifiers( new SnakList( [ |
|
200
|
|
|
new PropertyNoValueSnak( 42 ) |
|
201
|
|
|
] ) ); |
|
202
|
|
|
$serializations[] = [ |
|
203
|
|
|
$statement, |
|
204
|
|
|
[ |
|
205
|
|
|
'mainsnak' => [ |
|
206
|
|
|
'snaktype' => 'novalue', |
|
207
|
|
|
'property' => 'P42' |
|
208
|
|
|
], |
|
209
|
|
|
'qualifiers' => [ |
|
210
|
|
|
'P42' => [ |
|
211
|
|
|
[ |
|
212
|
|
|
'snaktype' => 'novalue', |
|
213
|
|
|
'property' => 'P42' |
|
214
|
|
|
] |
|
215
|
|
|
] |
|
216
|
|
|
], |
|
217
|
|
|
'type' => 'statement', |
|
218
|
|
|
'rank' => 'normal' |
|
219
|
|
|
] |
|
220
|
|
|
]; |
|
221
|
|
|
|
|
222
|
|
|
$statement = new Statement( new PropertyNoValueSnak( 42 ) ); |
|
223
|
|
|
$statement->setReferences( new ReferenceList() ); |
|
224
|
|
|
$serializations[] = [ |
|
225
|
|
|
$statement, |
|
226
|
|
|
[ |
|
227
|
|
|
'mainsnak' => [ |
|
228
|
|
|
'snaktype' => 'novalue', |
|
229
|
|
|
'property' => "P42" |
|
230
|
|
|
], |
|
231
|
|
|
'references' => [], |
|
232
|
|
|
'type' => 'statement', |
|
233
|
|
|
'rank' => 'normal' |
|
234
|
|
|
] |
|
235
|
|
|
]; |
|
236
|
|
|
|
|
237
|
|
|
return $serializations; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* @dataProvider invalidDeserializationProvider |
|
242
|
|
|
*/ |
|
243
|
|
|
public function testInvalidSerialization( $serialization ) { |
|
244
|
|
|
$this->expectException( DeserializationException::class ); |
|
245
|
|
|
$this->buildDeserializer()->deserialize( $serialization ); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
public function invalidDeserializationProvider() { |
|
249
|
|
|
return [ |
|
250
|
|
|
[ |
|
251
|
|
|
[ |
|
252
|
|
|
'type' => 'claim' |
|
253
|
|
|
] |
|
254
|
|
|
], |
|
255
|
|
|
[ |
|
256
|
|
|
[ |
|
257
|
|
|
'id' => 42, |
|
258
|
|
|
'mainsnak' => [ |
|
259
|
|
|
'snaktype' => 'novalue', |
|
260
|
|
|
'property' => 'P42' |
|
261
|
|
|
], |
|
262
|
|
|
'type' => 'claim' |
|
263
|
|
|
] |
|
264
|
|
|
], |
|
265
|
|
|
[ |
|
266
|
|
|
[ |
|
267
|
|
|
'mainsnak' => [ |
|
268
|
|
|
'snaktype' => 'novalue', |
|
269
|
|
|
'property' => 'P42' |
|
270
|
|
|
], |
|
271
|
|
|
'type' => 'statement', |
|
272
|
|
|
'rank' => 'nyan-cat' |
|
273
|
|
|
] |
|
274
|
|
|
], |
|
275
|
|
|
]; |
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
public function testQualifiersOrderDeserialization() { |
|
279
|
|
|
$snakDeserializerMock = $this->getMockBuilder( Deserializer::class )->getMock(); |
|
280
|
|
|
$snakDeserializerMock->expects( $this->any() ) |
|
281
|
|
|
->method( 'deserialize' ) |
|
282
|
|
|
->with( $this->equalTo( [ |
|
283
|
|
|
'snaktype' => 'novalue', |
|
284
|
|
|
'property' => 'P42' |
|
285
|
|
|
] ) ) |
|
286
|
|
|
->will( $this->returnValue( new PropertyNoValueSnak( 42 ) ) ); |
|
287
|
|
|
|
|
288
|
|
|
$snaksDeserializerMock = $this->getMockBuilder( Deserializer::class )->getMock(); |
|
289
|
|
|
$snaksDeserializerMock->expects( $this->any() ) |
|
290
|
|
|
->method( 'deserialize' ) |
|
291
|
|
|
->with( $this->equalTo( [ |
|
292
|
|
|
'P24' => [ |
|
293
|
|
|
[ |
|
294
|
|
|
'snaktype' => 'novalue', |
|
295
|
|
|
'property' => 'P24' |
|
296
|
|
|
] |
|
297
|
|
|
], |
|
298
|
|
|
'P42' => [ |
|
299
|
|
|
[ |
|
300
|
|
|
'snaktype' => 'somevalue', |
|
301
|
|
|
'property' => 'P42' |
|
302
|
|
|
], |
|
303
|
|
|
[ |
|
304
|
|
|
'snaktype' => 'novalue', |
|
305
|
|
|
'property' => 'P42' |
|
306
|
|
|
] |
|
307
|
|
|
] |
|
308
|
|
|
] |
|
309
|
|
|
) ) |
|
310
|
|
|
->will( $this->returnValue( new SnakList( [ |
|
311
|
|
|
new PropertyNoValueSnak( 24 ), |
|
312
|
|
|
new PropertySomeValueSnak( 42 ), |
|
313
|
|
|
new PropertyNoValueSnak( 42 ) |
|
314
|
|
|
] ) ) ); |
|
315
|
|
|
|
|
316
|
|
|
$referencesDeserializerMock = $this->getMockBuilder( Deserializer::class )->getMock(); |
|
317
|
|
|
$statementDeserializer = new StatementDeserializer( |
|
318
|
|
|
$snakDeserializerMock, |
|
|
|
|
|
|
319
|
|
|
$snaksDeserializerMock, |
|
|
|
|
|
|
320
|
|
|
$referencesDeserializerMock |
|
|
|
|
|
|
321
|
|
|
); |
|
322
|
|
|
|
|
323
|
|
|
$statement = new Statement( new PropertyNoValueSnak( 42 ) ); |
|
324
|
|
|
$statement->setQualifiers( new SnakList( [ |
|
325
|
|
|
new PropertySomeValueSnak( 42 ), |
|
326
|
|
|
new PropertyNoValueSnak( 42 ), |
|
327
|
|
|
new PropertyNoValueSnak( 24 ) |
|
328
|
|
|
] ) ); |
|
329
|
|
|
|
|
330
|
|
|
$serialization = [ |
|
331
|
|
|
'mainsnak' => [ |
|
332
|
|
|
'snaktype' => 'novalue', |
|
333
|
|
|
'property' => 'P42' |
|
334
|
|
|
], |
|
335
|
|
|
'qualifiers' => [ |
|
336
|
|
|
'P24' => [ |
|
337
|
|
|
[ |
|
338
|
|
|
'snaktype' => 'novalue', |
|
339
|
|
|
'property' => 'P24' |
|
340
|
|
|
] |
|
341
|
|
|
], |
|
342
|
|
|
'P42' => [ |
|
343
|
|
|
[ |
|
344
|
|
|
'snaktype' => 'somevalue', |
|
345
|
|
|
'property' => 'P42' |
|
346
|
|
|
], |
|
347
|
|
|
[ |
|
348
|
|
|
'snaktype' => 'novalue', |
|
349
|
|
|
'property' => 'P42' |
|
350
|
|
|
] |
|
351
|
|
|
] |
|
352
|
|
|
], |
|
353
|
|
|
'qualifiers-order' => [ |
|
354
|
|
|
'P42', |
|
355
|
|
|
'P24' |
|
356
|
|
|
], |
|
357
|
|
|
'type' => 'claim' |
|
358
|
|
|
]; |
|
359
|
|
|
|
|
360
|
|
|
$this->assertSame( |
|
361
|
|
|
$statement->getHash(), |
|
362
|
|
|
$statementDeserializer->deserialize( $serialization )->getHash() |
|
363
|
|
|
); |
|
364
|
|
|
} |
|
365
|
|
|
|
|
366
|
|
|
public function testQualifiersOrderDeserializationWithTypeError() { |
|
367
|
|
|
$serialization = [ |
|
368
|
|
|
'mainsnak' => [ |
|
369
|
|
|
'snaktype' => 'novalue', |
|
370
|
|
|
'property' => 'P42' |
|
371
|
|
|
], |
|
372
|
|
|
'qualifiers' => [ |
|
373
|
|
|
'P42' => [ |
|
374
|
|
|
[ |
|
375
|
|
|
'snaktype' => 'novalue', |
|
376
|
|
|
'property' => 'P42' |
|
377
|
|
|
] |
|
378
|
|
|
] |
|
379
|
|
|
], |
|
380
|
|
|
'qualifiers-order' => 'stringInsteadOfArray', |
|
381
|
|
|
'type' => 'claim' |
|
382
|
|
|
]; |
|
383
|
|
|
|
|
384
|
|
|
$deserializer = $this->buildDeserializer(); |
|
385
|
|
|
|
|
386
|
|
|
$this->expectException( InvalidAttributeException::class ); |
|
387
|
|
|
$deserializer->deserialize( $serialization ); |
|
388
|
|
|
} |
|
389
|
|
|
|
|
390
|
|
|
} |
|
391
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: