|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Tests\Entity; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionClass; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
|
8
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
|
9
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @covers Wikibase\DataModel\Entity\EntityId |
|
13
|
|
|
* @uses Wikibase\DataModel\Entity\ItemId |
|
14
|
|
|
* @uses Wikibase\DataModel\Entity\PropertyId |
|
15
|
|
|
* |
|
16
|
|
|
* @group Wikibase |
|
17
|
|
|
* @group WikibaseDataModel |
|
18
|
|
|
* |
|
19
|
|
|
* @license GPL-2.0+ |
|
20
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
21
|
|
|
* @author John Erling Blad < [email protected] > |
|
22
|
|
|
*/ |
|
23
|
|
|
class EntityIdTest extends \PHPUnit_Framework_TestCase { |
|
24
|
|
|
|
|
25
|
|
|
public function instanceProvider() { |
|
26
|
|
|
$ids = []; |
|
27
|
|
|
|
|
28
|
|
|
$ids[] = [ new ItemId( 'Q1' ), '' ]; |
|
29
|
|
|
$ids[] = [ new ItemId( 'Q42' ), '' ]; |
|
30
|
|
|
$ids[] = [ new ItemId( 'Q31337' ), '' ]; |
|
31
|
|
|
$ids[] = [ new ItemId( 'Q2147483647' ), '' ]; |
|
32
|
|
|
$ids[] = [ new ItemId( ':Q2147483647' ), '' ]; |
|
33
|
|
|
$ids[] = [ new ItemId( 'foo:Q2147483647' ), 'foo' ]; |
|
34
|
|
|
$ids[] = [ new PropertyId( 'P101010' ), '' ]; |
|
35
|
|
|
$ids[] = [ new PropertyId( 'foo:bar:P101010' ), 'foo' ]; |
|
36
|
|
|
|
|
37
|
|
|
return $ids; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @dataProvider instanceProvider |
|
42
|
|
|
*/ |
|
43
|
|
|
public function testEqualsSimple( EntityId $id ) { |
|
44
|
|
|
$this->assertTrue( $id->equals( $id ) ); |
|
45
|
|
|
$this->assertTrue( $id->equals( unserialize( serialize( $id ) ) ) ); |
|
46
|
|
|
$this->assertFalse( $id->equals( $id->getSerialization() ) ); |
|
47
|
|
|
$this->assertFalse( $id->equals( $id->getEntityType() ) ); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @dataProvider instanceProvider |
|
52
|
|
|
*/ |
|
53
|
|
|
public function testSerializationRoundtrip( EntityId $id ) { |
|
54
|
|
|
$this->assertEquals( $id, unserialize( serialize( $id ) ) ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function testDeserializationCompatibility() { |
|
58
|
|
|
$v05serialization = 'C:32:"Wikibase\DataModel\Entity\ItemId":15:{["item","Q123"]}'; |
|
59
|
|
|
|
|
60
|
|
|
$this->assertEquals( |
|
61
|
|
|
new ItemId( 'q123' ), |
|
62
|
|
|
unserialize( $v05serialization ) |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* This test will change when the serialization format changes. |
|
68
|
|
|
* If it is being changed intentionally, the test should be updated. |
|
69
|
|
|
* It is just here to catch unintentional changes. |
|
70
|
|
|
*/ |
|
71
|
|
|
public function testSerializationStability() { |
|
72
|
|
|
$v05serialization = 'C:32:"Wikibase\DataModel\Entity\ItemId":15:{["item","Q123"]}'; |
|
73
|
|
|
$id = new ItemId( 'q123' ); |
|
74
|
|
|
|
|
75
|
|
|
$this->assertEquals( |
|
76
|
|
|
serialize( $id ), |
|
77
|
|
|
$v05serialization |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @dataProvider instanceProvider |
|
83
|
|
|
*/ |
|
84
|
|
|
public function testReturnTypeOfToString( EntityId $id ) { |
|
85
|
|
|
$this->assertInternalType( 'string', $id->__toString() ); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function testIsForeign() { |
|
89
|
|
|
$this->assertFalse( ( new ItemId( 'Q42' ) )->isForeign() ); |
|
90
|
|
|
$this->assertFalse( ( new ItemId( ':Q42' ) )->isForeign() ); |
|
91
|
|
|
$this->assertTrue( ( new ItemId( 'foo:Q42' ) )->isForeign() ); |
|
92
|
|
|
$this->assertFalse( ( new PropertyId( ':P42' ) )->isForeign() ); |
|
93
|
|
|
$this->assertTrue( ( new PropertyId( 'foo:P42' ) )->isForeign() ); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @dataProvider instanceProvider |
|
98
|
|
|
*/ |
|
99
|
|
|
public function testGetRepositoryName( EntityId $id, $repoName ) { |
|
100
|
|
|
$this->assertSame( $repoName, $id->getRepositoryName() ); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function serializationSplitProvider() { |
|
104
|
|
|
return [ |
|
105
|
|
|
[ 'Q42', [ '', '', 'Q42' ] ], |
|
106
|
|
|
[ 'foo:Q42', [ 'foo', '', 'Q42' ] ], |
|
107
|
|
|
[ '0:Q42', [ '0', '', 'Q42' ] ], |
|
108
|
|
|
[ 'foo:bar:baz:Q42', [ 'foo', 'bar:baz', 'Q42' ] ], |
|
109
|
|
|
]; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @dataProvider serializationSplitProvider |
|
114
|
|
|
*/ |
|
115
|
|
|
public function testSplitSerialization( $serialization, $split ) { |
|
116
|
|
|
$this->assertSame( $split, EntityId::splitSerialization( $serialization ) ); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @dataProvider invalidSerializationProvider |
|
121
|
|
|
*/ |
|
122
|
|
|
public function testSplitSerializationFails_GivenInvalidSerialization( $serialization ) { |
|
123
|
|
|
$this->setExpectedException( InvalidArgumentException::class ); |
|
124
|
|
|
EntityId::splitSerialization( $serialization ); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @dataProvider serializationSplitProvider |
|
129
|
|
|
*/ |
|
130
|
|
|
public function testJoinSerialization( $serialization, $split ) { |
|
131
|
|
|
$this->assertSame( $serialization, EntityId::joinSerialization( $split ) ); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @dataProvider invalidJoinSerializationDataProvider |
|
136
|
|
|
*/ |
|
137
|
|
|
public function testJoinSerializationFails_GivenEmptyId( $parts ) { |
|
138
|
|
|
$this->setExpectedException( InvalidArgumentException::class ); |
|
139
|
|
|
EntityId::joinSerialization( $parts ); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
public function invalidJoinSerializationDataProvider() { |
|
143
|
|
|
return [ |
|
144
|
|
|
[ [ 'Q42', '', '' ] ], |
|
145
|
|
|
[ [ '', 'Q42', '' ] ], |
|
146
|
|
|
[ [ 'foo', 'Q42', '' ] ], |
|
147
|
|
|
]; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
public function testGivenNotNormalizedSerialization_splitSerializationReturnsNormalizedParts() { |
|
151
|
|
|
$this->assertSame( [ '', '', 'Q42' ], EntityId::splitSerialization( ':Q42' ) ); |
|
152
|
|
|
$this->assertSame( [ 'foo', 'bar', 'Q42' ], EntityId::splitSerialization( ':foo:bar:Q42' ) ); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
public function localPartDataProvider() { |
|
156
|
|
|
return [ |
|
157
|
|
|
[ 'Q42', 'Q42' ], |
|
158
|
|
|
[ ':Q42', 'Q42' ], |
|
159
|
|
|
[ 'foo:Q42', 'Q42' ], |
|
160
|
|
|
[ 'foo:bar:Q42', 'bar:Q42' ], |
|
161
|
|
|
]; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @dataProvider localPartDataProvider |
|
166
|
|
|
*/ |
|
167
|
|
|
public function testGetLocalPart( $serialization, $localPart ) { |
|
168
|
|
|
$id = new ItemId( $serialization ); |
|
169
|
|
|
$this->assertSame( $localPart, $id->getLocalPart() ); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
public function invalidSerializationProvider() { |
|
173
|
|
|
return [ |
|
174
|
|
|
[ 's p a c e s:Q42' ], |
|
175
|
|
|
[ '::Q42' ], |
|
176
|
|
|
[ '' ], |
|
177
|
|
|
[ ':' ], |
|
178
|
|
|
[ 42 ], |
|
179
|
|
|
[ null ], |
|
180
|
|
|
]; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
/** |
|
184
|
|
|
* @dataProvider invalidSerializationProvider |
|
185
|
|
|
*/ |
|
186
|
|
|
public function testConstructor( $serialization ) { |
|
187
|
|
|
$this->setExpectedException( InvalidArgumentException::class ); |
|
188
|
|
|
|
|
189
|
|
|
$mock = $this->getMockBuilder( EntityId::class ) |
|
190
|
|
|
->disableOriginalConstructor() |
|
191
|
|
|
->getMockForAbstractClass(); |
|
192
|
|
|
|
|
193
|
|
|
$constructor = ( new ReflectionClass( EntityId::class ) )->getConstructor(); |
|
194
|
|
|
$constructor->invoke( $mock, $serialization ); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
|