|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Tests\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
|
6
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
|
7
|
|
|
use Wikibase\DataModel\Entity\EntityIdValue; |
|
8
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
|
9
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
|
10
|
|
|
use Wikibase\DataModel\Fixtures\CustomEntityId; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* @covers Wikibase\DataModel\Entity\EntityIdValue |
|
14
|
|
|
* |
|
15
|
|
|
* @group Wikibase |
|
16
|
|
|
* @group WikibaseDataModel |
|
17
|
|
|
* |
|
18
|
|
|
* @license GPL-2.0+ |
|
19
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
20
|
|
|
* @author Thiemo Mättig |
|
21
|
|
|
* @author Daniel Kinzler |
|
22
|
|
|
*/ |
|
23
|
|
|
class EntityIdValueTest extends PHPUnit_Framework_TestCase { |
|
24
|
|
|
|
|
25
|
|
|
public function testCanConstruct() { |
|
26
|
|
|
$entityId = new ItemId( 'Q123' ); |
|
27
|
|
|
$entityIdValue = new EntityIdValue( $entityId ); |
|
28
|
|
|
$this->assertEquals( $entityId, $entityIdValue->getEntityId() ); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @dataProvider instanceProvider |
|
33
|
|
|
*/ |
|
34
|
|
|
public function testSerialzationRoundtrip( EntityIdValue $id ) { |
|
35
|
|
|
$serialized = serialize( $id ); |
|
36
|
|
|
$newId = unserialize( $serialized ); |
|
37
|
|
|
|
|
38
|
|
|
$this->assertEquals( $id, $newId ); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function instanceProvider() { |
|
42
|
|
|
$ids = [ |
|
43
|
|
|
'Q1' => new ItemId( 'Q1' ), |
|
44
|
|
|
'Q42' => new ItemId( 'Q42' ), |
|
45
|
|
|
'Q31337' => new ItemId( 'Q31337' ), |
|
46
|
|
|
'Q2147483647' => new ItemId( 'Q2147483647' ), |
|
47
|
|
|
'P1' => new PropertyId( 'P1' ), |
|
48
|
|
|
'P42' => new PropertyId( 'P42' ), |
|
49
|
|
|
'P31337' => new PropertyId( 'P31337' ), |
|
50
|
|
|
'X567' => new CustomEntityId( 'X567' ), |
|
51
|
|
|
'foo:P678' => new PropertyId( 'foo:P678' ), |
|
52
|
|
|
]; |
|
53
|
|
|
|
|
54
|
|
|
$argLists = []; |
|
55
|
|
|
|
|
56
|
|
|
foreach ( $ids as $k => $id ) { |
|
57
|
|
|
$argLists[$k] = [ new EntityIdValue( $id ) ]; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $argLists; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @dataProvider instanceProvider |
|
65
|
|
|
*/ |
|
66
|
|
|
public function testGetType( EntityIdValue $id ) { |
|
67
|
|
|
$this->assertEquals( 'wikibase-entityid', $id->getType() ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @dataProvider instanceProvider |
|
72
|
|
|
*/ |
|
73
|
|
|
public function testGetValue( EntityIdValue $id ) { |
|
74
|
|
|
$this->assertEquals( $id, $id->getValue() ); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @dataProvider instanceProvider |
|
79
|
|
|
*/ |
|
80
|
|
|
public function testGetSortKey( EntityIdValue $id ) { |
|
81
|
|
|
$this->assertInternalType( 'string', $id->getSortKey() ); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
public function provideGetArrayValue() { |
|
85
|
|
|
return [ |
|
86
|
|
|
'Q2147483647' => [ |
|
87
|
|
|
new ItemId( 'Q2147483647' ), |
|
88
|
|
|
[ |
|
89
|
|
|
'entity-type' => 'item', |
|
90
|
|
|
'numeric-id' => 2147483647, |
|
91
|
|
|
'id' => 'Q2147483647' |
|
92
|
|
|
], |
|
93
|
|
|
], |
|
94
|
|
|
'P31337' => [ |
|
95
|
|
|
new PropertyId( 'P31337' ), |
|
96
|
|
|
[ |
|
97
|
|
|
'entity-type' => 'property', |
|
98
|
|
|
'numeric-id' => 31337, |
|
99
|
|
|
'id' => 'P31337', |
|
100
|
|
|
], |
|
101
|
|
|
], |
|
102
|
|
|
'X567' => [ |
|
103
|
|
|
new CustomEntityId( 'X567' ), |
|
104
|
|
|
[ |
|
105
|
|
|
'entity-type' => 'custom', |
|
106
|
|
|
'id' => 'X567', |
|
107
|
|
|
], |
|
108
|
|
|
], |
|
109
|
|
|
'foo:P678' => [ |
|
110
|
|
|
new PropertyId( 'foo:P678' ), |
|
111
|
|
|
[ |
|
112
|
|
|
'entity-type' => 'property', |
|
113
|
|
|
'numeric-id' => 678, |
|
114
|
|
|
'id' => 'foo:P678', |
|
115
|
|
|
], |
|
116
|
|
|
], |
|
117
|
|
|
]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @dataProvider provideGetArrayValue |
|
122
|
|
|
*/ |
|
123
|
|
|
public function testGetArrayValue( EntityId $id, array $expected ) { |
|
124
|
|
|
$value = new EntityIdValue( $id ); |
|
125
|
|
|
$array = $value->getArrayValue(); |
|
126
|
|
|
|
|
127
|
|
|
$this->assertSame( $expected, $array ); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
public function testSerialize() { |
|
131
|
|
|
$id = new EntityIdValue( new ItemId( 'Q31337' ) ); |
|
132
|
|
|
|
|
133
|
|
|
$this->assertSame( 'C:32:"Wikibase\DataModel\Entity\ItemId":6:{Q31337}', $id->serialize() ); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
public function provideDeserializationCompatibility() { |
|
137
|
|
|
$local = new EntityIdValue( new ItemId( 'Q31337' ) ); |
|
138
|
|
|
$foreign = new EntityIdValue( new PropertyId( 'foo:P678' ) ); |
|
139
|
|
|
$custom = new EntityIdValue( new CustomEntityId( 'X567' ) ); |
|
140
|
|
|
|
|
141
|
|
|
return [ |
|
142
|
|
|
'local: Version 0.5 alpha (f5b8b64)' => [ |
|
143
|
|
|
'C:39:"Wikibase\DataModel\Entity\EntityIdValue":14:{["item",31337]}', |
|
144
|
|
|
$local |
|
145
|
|
|
], |
|
146
|
|
|
'local: Version 7.0 (7fcddfc)' => [ |
|
147
|
|
|
'C:39:"Wikibase\DataModel\Entity\EntityIdValue":' |
|
148
|
|
|
. '50:{C:32:"Wikibase\DataModel\Entity\ItemId":6:{Q31337}}', |
|
149
|
|
|
$local |
|
150
|
|
|
], |
|
151
|
|
|
'foreign: Version 7.0 (7fcddfc)' => [ |
|
152
|
|
|
'C:39:"Wikibase\DataModel\Entity\EntityIdValue":' |
|
153
|
|
|
. '56:{C:36:"Wikibase\DataModel\Entity\PropertyId":8:{foo:P678}}', |
|
154
|
|
|
$foreign |
|
155
|
|
|
], |
|
156
|
|
|
'custom: Version 7.0 (7fcddfc): custom' => [ |
|
157
|
|
|
'C:39:"Wikibase\DataModel\Entity\EntityIdValue":' |
|
158
|
|
|
. '58:{C:42:"Wikibase\DataModel\Fixtures\CustomEntityId":4:{X567}}', |
|
159
|
|
|
$custom |
|
160
|
|
|
], |
|
161
|
|
|
]; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @dataProvider provideDeserializationCompatibility |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $serialized |
|
168
|
|
|
* @param EntityIdValue $expected |
|
169
|
|
|
*/ |
|
170
|
|
|
public function testDeserializationCompatibility( $serialized, EntityIdValue $expected ) { |
|
171
|
|
|
$id = unserialize( $serialized ); |
|
172
|
|
|
|
|
173
|
|
|
$this->assertEquals( $expected, $id ); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* @dataProvider validArrayProvider |
|
178
|
|
|
*/ |
|
179
|
|
|
public function testNewFromArrayCompatibility( array $array ) { |
|
180
|
|
|
$id = new EntityIdValue( new ItemId( 'Q31337' ) ); |
|
181
|
|
|
|
|
182
|
|
|
$this->assertEquals( $id, EntityIdValue::newFromArray( $array ) ); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function validArrayProvider() { |
|
186
|
|
|
return [ |
|
187
|
|
|
'Legacy format' => [ [ |
|
188
|
|
|
'entity-type' => 'item', |
|
189
|
|
|
'numeric-id' => 31337, |
|
190
|
|
|
] ], |
|
191
|
|
|
'Maximum compatibility' => [ [ |
|
192
|
|
|
'entity-type' => 'item', |
|
193
|
|
|
'numeric-id' => 31337, |
|
194
|
|
|
'id' => 'Q31337', |
|
195
|
|
|
] ], |
|
196
|
|
|
]; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @dataProvider invalidArrayProvider |
|
201
|
|
|
*/ |
|
202
|
|
|
public function testCannotDeserializeInvalidSerialization( $invalidArray ) { |
|
203
|
|
|
$this->setExpectedException( 'DataValues\IllegalValueException' ); |
|
204
|
|
|
|
|
205
|
|
|
EntityIdValue::newFromArray( $invalidArray ); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
public function invalidArrayProvider() { |
|
209
|
|
|
return [ |
|
210
|
|
|
[ null ], |
|
211
|
|
|
|
|
212
|
|
|
[ 'foo' ], |
|
213
|
|
|
|
|
214
|
|
|
[ [] ], |
|
215
|
|
|
|
|
216
|
|
|
'newFromArray can not deserialize' => [ [ |
|
217
|
|
|
'id' => 'Q42', |
|
218
|
|
|
] ], |
|
219
|
|
|
|
|
220
|
|
|
[ [ |
|
221
|
|
|
'entity-type' => 'item', |
|
222
|
|
|
] ], |
|
223
|
|
|
|
|
224
|
|
|
[ [ |
|
225
|
|
|
'numeric-id' => 42, |
|
226
|
|
|
] ], |
|
227
|
|
|
|
|
228
|
|
|
[ [ |
|
229
|
|
|
'entity-type' => 'foo', |
|
230
|
|
|
'numeric-id' => 42, |
|
231
|
|
|
] ], |
|
232
|
|
|
|
|
233
|
|
|
[ [ |
|
234
|
|
|
'entity-type' => 42, |
|
235
|
|
|
'numeric-id' => 42, |
|
236
|
|
|
] ], |
|
237
|
|
|
|
|
238
|
|
|
[ [ |
|
239
|
|
|
'entity-type' => 'item', |
|
240
|
|
|
'numeric-id' => -1, |
|
241
|
|
|
] ], |
|
242
|
|
|
|
|
243
|
|
|
[ [ |
|
244
|
|
|
'entity-type' => 'item', |
|
245
|
|
|
'numeric-id' => 'foo', |
|
246
|
|
|
] ], |
|
247
|
|
|
]; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
} |
|
251
|
|
|
|