1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Wikibase\InternalSerialization\Deserializers; |
4
|
|
|
|
5
|
|
|
use Deserializers\Deserializer; |
6
|
|
|
use Deserializers\Exceptions\DeserializationException; |
7
|
|
|
use Wikibase\DataModel\Entity\BasicEntityIdParser; |
8
|
|
|
use Wikibase\DataModel\Entity\Item; |
9
|
|
|
use Wikibase\DataModel\Snak\PropertyNoValueSnak; |
10
|
|
|
use Wikibase\DataModel\Statement\Statement; |
11
|
|
|
use Wikibase\InternalSerialization\Deserializers\LegacyEntityIdDeserializer; |
12
|
|
|
use Wikibase\InternalSerialization\Deserializers\LegacyFingerprintDeserializer; |
13
|
|
|
use Wikibase\InternalSerialization\Deserializers\LegacyItemDeserializer; |
14
|
|
|
use Wikibase\InternalSerialization\Deserializers\LegacySiteLinkListDeserializer; |
15
|
|
|
use Wikibase\InternalSerialization\Deserializers\LegacySnakDeserializer; |
16
|
|
|
use Wikibase\InternalSerialization\Deserializers\LegacySnakListDeserializer; |
17
|
|
|
use Wikibase\InternalSerialization\Deserializers\LegacyStatementDeserializer; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @covers Wikibase\InternalSerialization\Deserializers\LegacyItemDeserializer |
21
|
|
|
* |
22
|
|
|
* @license GPL-2.0-or-later |
23
|
|
|
* @author Jeroen De Dauw < [email protected] > |
24
|
|
|
*/ |
25
|
|
|
class LegacyItemDeserializerTest extends \PHPUnit\Framework\TestCase { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Deserializer |
29
|
|
|
*/ |
30
|
|
|
private $deserializer; |
31
|
|
|
|
32
|
|
|
protected function setUp() : void { |
33
|
|
|
$idDeserializer = new LegacyEntityIdDeserializer( new BasicEntityIdParser() ); |
34
|
|
|
|
35
|
|
|
$snakDeserializer = new LegacySnakDeserializer( $this->createMock( Deserializer::class ) ); |
|
|
|
|
36
|
|
|
|
37
|
|
|
$statementDeserializer = new LegacyStatementDeserializer( |
38
|
|
|
$snakDeserializer, |
39
|
|
|
new LegacySnakListDeserializer( $snakDeserializer ) |
40
|
|
|
); |
41
|
|
|
|
42
|
|
|
$this->deserializer = new LegacyItemDeserializer( |
43
|
|
|
$idDeserializer, |
44
|
|
|
new LegacySiteLinkListDeserializer(), |
45
|
|
|
$statementDeserializer, |
46
|
|
|
new LegacyFingerprintDeserializer() |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function invalidSerializationProvider() { |
51
|
|
|
return array( |
52
|
|
|
array( null ), |
53
|
|
|
|
54
|
|
|
array( array( |
55
|
|
|
'links' => array( null ) |
56
|
|
|
) ), |
57
|
|
|
|
58
|
|
|
array( array( |
59
|
|
|
'claims' => null |
60
|
|
|
) ), |
61
|
|
|
|
62
|
|
|
array( array( |
63
|
|
|
'claims' => array( null ) |
64
|
|
|
) ), |
65
|
|
|
|
66
|
|
|
array( array( |
67
|
|
|
'entity' => 42 |
68
|
|
|
) ), |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @dataProvider invalidSerializationProvider |
74
|
|
|
*/ |
75
|
|
|
public function testGivenInvalidSerialization_deserializeThrowsException( $serialization ) { |
76
|
|
|
$this->expectDeserializationException(); |
77
|
|
|
$this->deserializer->deserialize( $serialization ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
private function expectDeserializationException() { |
81
|
|
|
$this->expectException( DeserializationException::class ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function testGivenEmptyArray_emptyItemIsReturned() { |
85
|
|
|
$this->assertEquals( |
86
|
|
|
new Item(), |
87
|
|
|
$this->deserializer->deserialize( array() ) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function testGivenLinks_itemHasSiteLinks() { |
92
|
|
|
$item = new Item(); |
93
|
|
|
|
94
|
|
|
$item->getSiteLinkList()->addNewSiteLink( 'foo', 'bar' ); |
95
|
|
|
$item->getSiteLinkList()->addNewSiteLink( 'baz', 'bah' ); |
96
|
|
|
|
97
|
|
|
$this->assertDeserialization( |
98
|
|
|
array( |
99
|
|
|
'links' => array( |
100
|
|
|
'foo' => 'bar', |
101
|
|
|
'baz' => 'bah', |
102
|
|
|
) |
103
|
|
|
), |
104
|
|
|
$item |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
private function assertDeserialization( $serialization, Item $expectedItem ) { |
109
|
|
|
$newItem = $this->itemFromSerialization( $serialization ); |
110
|
|
|
|
111
|
|
|
$this->assertTrue( |
112
|
|
|
$expectedItem->equals( $newItem ), |
113
|
|
|
'Deserialized Item should match expected Item' |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param string $serialization |
119
|
|
|
* |
120
|
|
|
* @return Item |
121
|
|
|
*/ |
122
|
|
|
private function itemFromSerialization( $serialization ) { |
123
|
|
|
$item = $this->deserializer->deserialize( $serialization ); |
124
|
|
|
$this->assertInstanceOf( Item::class, $item ); |
125
|
|
|
return $item; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function testGivenStatement_itemHasStatement() { |
129
|
|
|
$item = new Item(); |
130
|
|
|
$item->getStatements()->addStatement( $this->newStatement() ); |
131
|
|
|
|
132
|
|
|
$this->assertDeserialization( |
133
|
|
|
array( |
134
|
|
|
'claims' => array( |
135
|
|
|
$this->newStatementSerialization() |
136
|
|
|
) |
137
|
|
|
), |
138
|
|
|
$item |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function newStatement() { |
143
|
|
|
$statement = new Statement( new PropertyNoValueSnak( 42 ) ); |
144
|
|
|
$statement->setGuid( 'foo' ); |
145
|
|
|
return $statement; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private function newStatementSerialization() { |
149
|
|
|
return array( |
150
|
|
|
'm' => array( 'novalue', 42 ), |
151
|
|
|
'q' => array(), |
152
|
|
|
'g' => 'foo', |
153
|
|
|
'rank' => Statement::RANK_NORMAL, |
154
|
|
|
'refs' => array() |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testGivenStatementWithLegacyKey_itemHasStatement() { |
159
|
|
|
$item = new Item(); |
160
|
|
|
$item->getStatements()->addStatement( $this->newStatement() ); |
161
|
|
|
|
162
|
|
|
$this->assertDeserialization( |
163
|
|
|
array( |
164
|
|
|
'statements' => array( |
165
|
|
|
$this->newStatementSerialization() |
166
|
|
|
) |
167
|
|
|
), |
168
|
|
|
$item |
169
|
|
|
); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @dataProvider TermListProvider |
174
|
|
|
*/ |
175
|
|
|
public function testGivenLabels_getLabelsReturnsThem( array $labels ) { |
176
|
|
|
$item = $this->itemFromSerialization( array( 'label' => $labels ) ); |
|
|
|
|
177
|
|
|
|
178
|
|
|
$this->assertEquals( $labels, $item->getFingerprint()->getLabels()->toTextArray() ); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
public function TermListProvider() { |
182
|
|
|
return array( |
183
|
|
|
array( array() ), |
184
|
|
|
|
185
|
|
|
array( array( |
186
|
|
|
'en' => 'foo', |
187
|
|
|
'de' => 'bar', |
188
|
|
|
) ), |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function testGivenInvalidLabels_exceptionIsThrown() { |
193
|
|
|
$this->expectDeserializationException(); |
194
|
|
|
$this->deserializer->deserialize( array( 'label' => null ) ); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @dataProvider TermListProvider |
199
|
|
|
*/ |
200
|
|
|
public function testGivenDescriptions_getDescriptionsReturnsThem( array $descriptions ) { |
201
|
|
|
$item = $this->itemFromSerialization( array( 'description' => $descriptions ) ); |
|
|
|
|
202
|
|
|
|
203
|
|
|
$this->assertEquals( $descriptions, $item->getFingerprint()->getDescriptions()->toTextArray() ); |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
public function testGivenInvalidAliases_exceptionIsThrown() { |
207
|
|
|
$this->expectDeserializationException(); |
208
|
|
|
$this->deserializer->deserialize( array( 'aliases' => null ) ); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* @dataProvider aliasesListProvider |
213
|
|
|
*/ |
214
|
|
|
public function testGivenAliases_getAliasesReturnsThem( array $aliases ) { |
215
|
|
|
$item = $this->itemFromSerialization( array( 'aliases' => $aliases ) ); |
|
|
|
|
216
|
|
|
|
217
|
|
|
$this->assertEquals( $aliases, $item->getFingerprint()->getAliasGroups()->toTextArray() ); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function aliasesListProvider() { |
221
|
|
|
return array( |
222
|
|
|
array( array() ), |
223
|
|
|
|
224
|
|
|
array( array( |
225
|
|
|
'en' => array( 'foo', 'bar' ), |
226
|
|
|
'de' => array( 'foo', 'bar', 'baz' ), |
227
|
|
|
'nl' => array( 'bah' ), |
228
|
|
|
) ), |
229
|
|
|
); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
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: