|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Tests\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
|
6
|
|
|
use Wikibase\DataModel\Entity\EntityIdValue; |
|
7
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
|
8
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* @covers Wikibase\DataModel\Entity\EntityIdValue |
|
12
|
|
|
* |
|
13
|
|
|
* @group Wikibase |
|
14
|
|
|
* @group WikibaseDataModel |
|
15
|
|
|
* |
|
16
|
|
|
* @license GPL-2.0+ |
|
17
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
18
|
|
|
* @author Thiemo Mättig |
|
19
|
|
|
*/ |
|
20
|
|
|
class EntityIdValueTest extends PHPUnit_Framework_TestCase { |
|
21
|
|
|
|
|
22
|
|
|
public function testCanConstruct() { |
|
23
|
|
|
$entityId = new ItemId( 'Q123' ); |
|
24
|
|
|
$entityIdValue = new EntityIdValue( $entityId ); |
|
25
|
|
|
$this->assertEquals( $entityId, $entityIdValue->getEntityId() ); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @dataProvider instanceProvider |
|
30
|
|
|
*/ |
|
31
|
|
|
public function testSerialzationRoundtrip( EntityIdValue $id ) { |
|
32
|
|
|
$newId = unserialize( serialize( $id ) ); |
|
33
|
|
|
|
|
34
|
|
|
$this->assertEquals( $id, $newId ); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function instanceProvider() { |
|
38
|
|
|
$ids = [ |
|
39
|
|
|
new ItemId( 'Q1' ), |
|
40
|
|
|
new ItemId( 'Q42' ), |
|
41
|
|
|
new ItemId( 'Q31337' ), |
|
42
|
|
|
new ItemId( 'Q2147483647' ), |
|
43
|
|
|
new PropertyId( 'P1' ), |
|
44
|
|
|
new PropertyId( 'P42' ), |
|
45
|
|
|
new PropertyId( 'P31337' ), |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
$argLists = []; |
|
49
|
|
|
|
|
50
|
|
|
foreach ( $ids as $id ) { |
|
51
|
|
|
$argLists[] = [ new EntityIdValue( $id ) ]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $argLists; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @dataProvider instanceProvider |
|
59
|
|
|
*/ |
|
60
|
|
|
public function testGetType( EntityIdValue $id ) { |
|
61
|
|
|
$this->assertEquals( 'wikibase-entityid', $id->getType() ); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @dataProvider instanceProvider |
|
66
|
|
|
*/ |
|
67
|
|
|
public function testGetValue( EntityIdValue $id ) { |
|
68
|
|
|
$this->assertEquals( $id, $id->getValue() ); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @dataProvider instanceProvider |
|
73
|
|
|
*/ |
|
74
|
|
|
public function testGetSortKey( EntityIdValue $id ) { |
|
75
|
|
|
$this->assertInternalType( 'string', $id->getSortKey() ); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @dataProvider instanceProvider |
|
80
|
|
|
*/ |
|
81
|
|
|
public function testGetArrayValueRoundtrip( EntityIdValue $id ) { |
|
82
|
|
|
$newId = EntityIdValue::newFromArray( $id->getArrayValue() ); |
|
83
|
|
|
|
|
84
|
|
|
$this->assertEquals( $id, $newId ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function testSerializationCompatibility() { |
|
88
|
|
|
$id = new EntityIdValue( new ItemId( 'Q31337' ) ); |
|
89
|
|
|
|
|
90
|
|
|
// This is the serialization format from when the EntityIdValue was still together with EntityId. |
|
91
|
|
|
$this->assertEquals( '["item",31337]', $id->serialize() ); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function testDeserializationCompatibility() { |
|
95
|
|
|
$expected = new EntityIdValue( new ItemId( 'Q31337' ) ); |
|
96
|
|
|
|
|
97
|
|
|
// This is the serialization format from f5b8b64823ff215c3796a79d916b6eaa65f4be33, version 0.5 alpha. |
|
98
|
|
|
$id = unserialize( 'C:39:"Wikibase\DataModel\Entity\EntityIdValue":14:{["item",31337]}' ); |
|
99
|
|
|
$this->assertEquals( $expected, $id ); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
public function testGetArrayValueCompatibility() { |
|
103
|
|
|
$id = new EntityIdValue( new ItemId( 'Q31337' ) ); |
|
104
|
|
|
|
|
105
|
|
|
$this->assertSame( |
|
106
|
|
|
// This is the serialization format from when the EntityIdValue was still together with EntityId. |
|
107
|
|
|
[ |
|
108
|
|
|
'entity-type' => 'item', |
|
109
|
|
|
'numeric-id' => (float)31337, |
|
110
|
|
|
'id' => 'Q31337', |
|
111
|
|
|
], |
|
112
|
|
|
$id->getArrayValue() |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @dataProvider validArrayProvider |
|
118
|
|
|
*/ |
|
119
|
|
|
public function testNewFromArrayCompatibility( array $array ) { |
|
120
|
|
|
$id = new EntityIdValue( new ItemId( 'Q31337' ) ); |
|
121
|
|
|
|
|
122
|
|
|
$this->assertEquals( $id, EntityIdValue::newFromArray( $array ) ); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
public function validArrayProvider() { |
|
126
|
|
|
return [ |
|
127
|
|
|
'Legacy format' => [ [ |
|
128
|
|
|
'entity-type' => 'item', |
|
129
|
|
|
'numeric-id' => 31337, |
|
130
|
|
|
] ], |
|
131
|
|
|
'Maximum compatibility' => [ [ |
|
132
|
|
|
'entity-type' => 'item', |
|
133
|
|
|
'numeric-id' => 31337, |
|
134
|
|
|
'id' => 'Q31337', |
|
135
|
|
|
] ], |
|
136
|
|
|
]; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @dataProvider invalidArrayProvider |
|
141
|
|
|
*/ |
|
142
|
|
|
public function testCannotDeserializeInvalidSerialization( $invalidArray ) { |
|
143
|
|
|
$this->setExpectedException( 'DataValues\IllegalValueException' ); |
|
144
|
|
|
|
|
145
|
|
|
EntityIdValue::newFromArray( $invalidArray ); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
public function invalidArrayProvider() { |
|
149
|
|
|
return [ |
|
150
|
|
|
[ null ], |
|
151
|
|
|
|
|
152
|
|
|
[ 'foo' ], |
|
153
|
|
|
|
|
154
|
|
|
[ [] ], |
|
155
|
|
|
|
|
156
|
|
|
'newFromArray can not deserialize' => [ [ |
|
157
|
|
|
'id' => 'Q42', |
|
158
|
|
|
] ], |
|
159
|
|
|
|
|
160
|
|
|
[ [ |
|
161
|
|
|
'entity-type' => 'item', |
|
162
|
|
|
] ], |
|
163
|
|
|
|
|
164
|
|
|
[ [ |
|
165
|
|
|
'numeric-id' => 42, |
|
166
|
|
|
] ], |
|
167
|
|
|
|
|
168
|
|
|
[ [ |
|
169
|
|
|
'entity-type' => 'foo', |
|
170
|
|
|
'numeric-id' => 42, |
|
171
|
|
|
] ], |
|
172
|
|
|
|
|
173
|
|
|
[ [ |
|
174
|
|
|
'entity-type' => 42, |
|
175
|
|
|
'numeric-id' => 42, |
|
176
|
|
|
] ], |
|
177
|
|
|
|
|
178
|
|
|
[ [ |
|
179
|
|
|
'entity-type' => 'item', |
|
180
|
|
|
'numeric-id' => -1, |
|
181
|
|
|
] ], |
|
182
|
|
|
|
|
183
|
|
|
[ [ |
|
184
|
|
|
'entity-type' => 'item', |
|
185
|
|
|
'numeric-id' => 'foo', |
|
186
|
|
|
] ], |
|
187
|
|
|
]; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
|