1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\DataModel\Tests\Entity; |
4
|
|
|
|
5
|
|
|
use Wikibase\DataModel\Entity\EntityId; |
6
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
7
|
|
|
use Wikibase\DataModel\Entity\PropertyId; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers Wikibase\DataModel\Entity\EntityId |
11
|
|
|
* @uses Wikibase\DataModel\Entity\ItemId |
12
|
|
|
* @uses Wikibase\DataModel\Entity\PropertyId |
13
|
|
|
* |
14
|
|
|
* @group Wikibase |
15
|
|
|
* @group WikibaseDataModel |
16
|
|
|
* @group EntityIdTest |
17
|
|
|
* |
18
|
|
|
* @license GPL-2.0+ |
19
|
|
|
* @author Jeroen De Dauw < [email protected] > |
20
|
|
|
* @author John Erling Blad < [email protected] > |
21
|
|
|
*/ |
22
|
|
|
class EntityIdTest extends \PHPUnit_Framework_TestCase { |
23
|
|
|
|
24
|
|
|
public function instanceProvider() { |
25
|
|
|
$ids = array(); |
26
|
|
|
|
27
|
|
|
$ids[] = array( new ItemId( 'Q1' ), '' ); |
28
|
|
|
$ids[] = array( new ItemId( 'Q42' ), '' ); |
29
|
|
|
$ids[] = array( new ItemId( 'Q31337' ), '' ); |
30
|
|
|
$ids[] = array( new ItemId( 'Q2147483647' ), '' ); |
31
|
|
|
$ids[] = array( new ItemId( ':Q2147483647' ), '' ); |
32
|
|
|
$ids[] = array( new ItemId( 'foo:Q2147483647' ), 'foo' ); |
33
|
|
|
$ids[] = array( new PropertyId( 'P101010' ), '' ); |
34
|
|
|
$ids[] = array( new PropertyId( 'foo:bar:P101010' ), 'foo' ); |
35
|
|
|
|
36
|
|
|
return $ids; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @dataProvider instanceProvider |
41
|
|
|
*/ |
42
|
|
|
public function testEqualsSimple( EntityId $id ) { |
43
|
|
|
$this->assertTrue( $id->equals( $id ) ); |
44
|
|
|
$this->assertTrue( $id->equals( unserialize( serialize( $id ) ) ) ); |
45
|
|
|
$this->assertFalse( $id->equals( $id->getSerialization() ) ); |
46
|
|
|
$this->assertFalse( $id->equals( $id->getEntityType() ) ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @dataProvider instanceProvider |
51
|
|
|
*/ |
52
|
|
|
public function testSerializationRoundtrip( EntityId $id ) { |
53
|
|
|
$this->assertEquals( $id, unserialize( serialize( $id ) ) ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function testDeserializationCompatibility() { |
57
|
|
|
$v05serialization = 'C:32:"Wikibase\DataModel\Entity\ItemId":15:{["item","Q123"]}'; |
58
|
|
|
|
59
|
|
|
$this->assertEquals( |
60
|
|
|
new ItemId( 'q123' ), |
61
|
|
|
unserialize( $v05serialization ) |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* This test will change when the serialization format changes. |
67
|
|
|
* If it is being changed intentionally, the test should be updated. |
68
|
|
|
* It is just here to catch unintentional changes. |
69
|
|
|
*/ |
70
|
|
|
public function testSerializationStability() { |
71
|
|
|
$v05serialization = 'C:32:"Wikibase\DataModel\Entity\ItemId":15:{["item","Q123"]}'; |
72
|
|
|
$id = new ItemId( 'q123' ); |
73
|
|
|
|
74
|
|
|
$this->assertEquals( |
75
|
|
|
serialize( $id ), |
76
|
|
|
$v05serialization |
77
|
|
|
); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @dataProvider instanceProvider |
82
|
|
|
*/ |
83
|
|
|
public function testReturnTypeOfToString( EntityId $id ) { |
84
|
|
|
$this->assertInternalType( 'string', $id->__toString() ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function testIsForeign() { |
88
|
|
|
$this->assertFalse( ( new ItemId( 'Q42' ) )->isForeign() ); |
89
|
|
|
$this->assertFalse( ( new ItemId( ':Q42' ) )->isForeign() ); |
90
|
|
|
$this->assertTrue( ( new ItemId( 'foo:Q42' ) )->isForeign() ); |
91
|
|
|
$this->assertFalse( ( new PropertyId( ':P42' ) )->isForeign() ); |
92
|
|
|
$this->assertTrue( ( new PropertyId( 'foo:P42' ) )->isForeign() ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @dataProvider instanceProvider |
97
|
|
|
*/ |
98
|
|
|
public function testGetRepoName( EntityId $id, $repoName ) { |
99
|
|
|
$this->assertSame( $repoName, $id->getRepoName() ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function serializationSplitProvider() { |
103
|
|
|
return array( |
104
|
|
|
array( 'Q42', array( '', '', 'Q42' ) ), |
105
|
|
|
array( ':Q42', array( '', '', 'Q42' ) ), |
106
|
|
|
array( 'foo:Q42', array( 'foo', '', 'Q42' ) ), |
107
|
|
|
array( 'foo:bar:baz:Q42', array( 'foo', 'bar:baz', 'Q42' ) ), |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @dataProvider serializationSplitProvider |
113
|
|
|
*/ |
114
|
|
|
public function testSplitSerialization( $serialization, $split ) { |
115
|
|
|
$this->assertSame( $split, EntityId::splitSerialization( $serialization ) ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @dataProvider serializationSplitProvider |
120
|
|
|
*/ |
121
|
|
|
public function testJoinSerialization( $serialization, $split ) { |
122
|
|
|
$this->assertSame( ltrim( $serialization, ':' ), EntityId::joinSerialization( $split ) ); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function localPartDataProvider() { |
126
|
|
|
return array( |
127
|
|
|
array( 'Q42', 'Q42' ), |
128
|
|
|
array( ':Q42', 'Q42' ), |
129
|
|
|
array( 'foo:Q42', 'Q42' ), |
130
|
|
|
array( 'foo:bar:Q42', 'bar:Q42' ), |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @dataProvider localPartDataProvider |
136
|
|
|
*/ |
137
|
|
|
public function testGetLocalPart( $serialization, $localPart ) { |
138
|
|
|
$id = new ItemId( $serialization ); |
139
|
|
|
$this->assertSame( $localPart, $id->getLocalPart() ); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
} |
143
|
|
|
|