1 | <?php |
||
24 | class EntityIdValueTest extends PHPUnit_Framework_TestCase { |
||
25 | |||
26 | public function testCanConstruct() { |
||
27 | $entityId = new ItemId( 'Q123' ); |
||
28 | $entityIdValue = new EntityIdValue( $entityId ); |
||
29 | $this->assertEquals( $entityId, $entityIdValue->getEntityId() ); |
||
30 | } |
||
31 | |||
32 | /** |
||
33 | * @dataProvider instanceProvider |
||
34 | */ |
||
35 | public function testSerialzationRoundtrip( EntityIdValue $id ) { |
||
36 | $serialized = serialize( $id ); |
||
37 | $newId = unserialize( $serialized ); |
||
38 | |||
39 | $this->assertEquals( $id, $newId ); |
||
40 | } |
||
41 | |||
42 | public function instanceProvider() { |
||
43 | $ids = [ |
||
44 | 'Q1' => new ItemId( 'Q1' ), |
||
45 | 'Q42' => new ItemId( 'Q42' ), |
||
46 | 'Q31337' => new ItemId( 'Q31337' ), |
||
47 | 'Q2147483647' => new ItemId( 'Q2147483647' ), |
||
48 | 'P1' => new PropertyId( 'P1' ), |
||
49 | 'P42' => new PropertyId( 'P42' ), |
||
50 | 'P31337' => new PropertyId( 'P31337' ), |
||
51 | 'X567' => new CustomEntityId( 'X567' ), |
||
52 | 'foo:P678' => new PropertyId( 'foo:P678' ), |
||
53 | ]; |
||
54 | |||
55 | $argLists = []; |
||
56 | |||
57 | foreach ( $ids as $k => $id ) { |
||
58 | $argLists[$k] = [ new EntityIdValue( $id ) ]; |
||
59 | } |
||
60 | |||
61 | return $argLists; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @dataProvider instanceProvider |
||
66 | */ |
||
67 | public function testGetType( EntityIdValue $id ) { |
||
68 | $this->assertEquals( 'wikibase-entityid', $id->getType() ); |
||
69 | } |
||
70 | |||
71 | /** |
||
72 | * @dataProvider instanceProvider |
||
73 | */ |
||
74 | public function testGetValue( EntityIdValue $id ) { |
||
75 | $this->assertEquals( $id, $id->getValue() ); |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @dataProvider instanceProvider |
||
80 | */ |
||
81 | public function testGetSortKey( EntityIdValue $id ) { |
||
82 | $this->assertInternalType( 'string', $id->getSortKey() ); |
||
83 | } |
||
84 | |||
85 | public function provideGetArrayValue() { |
||
86 | return [ |
||
87 | 'Q2147483647' => [ |
||
88 | new ItemId( 'Q2147483647' ), |
||
89 | [ |
||
90 | 'entity-type' => 'item', |
||
91 | 'numeric-id' => 2147483647, |
||
92 | 'id' => 'Q2147483647' |
||
93 | ], |
||
94 | ], |
||
95 | 'P31337' => [ |
||
96 | new PropertyId( 'P31337' ), |
||
97 | [ |
||
98 | 'entity-type' => 'property', |
||
99 | 'numeric-id' => 31337, |
||
100 | 'id' => 'P31337', |
||
101 | ], |
||
102 | ], |
||
103 | 'X567' => [ |
||
104 | new CustomEntityId( 'X567' ), |
||
105 | [ |
||
106 | 'entity-type' => 'custom', |
||
107 | 'id' => 'X567', |
||
108 | ], |
||
109 | ], |
||
110 | 'foo:P678' => [ |
||
111 | new PropertyId( 'foo:P678' ), |
||
112 | [ |
||
113 | 'entity-type' => 'property', |
||
114 | 'numeric-id' => 678, |
||
115 | 'id' => 'foo:P678', |
||
116 | ], |
||
117 | ], |
||
118 | ]; |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @dataProvider provideGetArrayValue |
||
123 | */ |
||
124 | public function testGetArrayValue( EntityId $id, array $expected ) { |
||
125 | $value = new EntityIdValue( $id ); |
||
126 | $array = $value->getArrayValue(); |
||
127 | |||
128 | $this->assertSame( $expected, $array ); |
||
129 | } |
||
130 | |||
131 | public function testSerialize() { |
||
136 | |||
137 | public function provideDeserializationCompatibility() { |
||
138 | $local = new EntityIdValue( new ItemId( 'Q31337' ) ); |
||
139 | $foreign = new EntityIdValue( new PropertyId( 'foo:P678' ) ); |
||
140 | $custom = new EntityIdValue( new CustomEntityId( 'X567' ) ); |
||
141 | |||
142 | return [ |
||
143 | 'local: Version 0.5 alpha (f5b8b64)' => [ |
||
144 | 'C:39:"Wikibase\DataModel\Entity\EntityIdValue":14:{["item",31337]}', |
||
145 | $local |
||
146 | ], |
||
147 | 'local: Version 7.0 (7fcddfc)' => [ |
||
148 | 'C:39:"Wikibase\DataModel\Entity\EntityIdValue":' |
||
149 | . '50:{C:32:"Wikibase\DataModel\Entity\ItemId":6:{Q31337}}', |
||
150 | $local |
||
151 | ], |
||
152 | 'foreign: Version 7.0 (7fcddfc)' => [ |
||
153 | 'C:39:"Wikibase\DataModel\Entity\EntityIdValue":' |
||
154 | . '56:{C:36:"Wikibase\DataModel\Entity\PropertyId":8:{foo:P678}}', |
||
155 | $foreign |
||
156 | ], |
||
157 | 'custom: Version 7.0 (7fcddfc): custom' => [ |
||
158 | 'C:39:"Wikibase\DataModel\Entity\EntityIdValue":' |
||
159 | . '58:{C:42:"Wikibase\DataModel\Fixtures\CustomEntityId":4:{X567}}', |
||
160 | $custom |
||
161 | ], |
||
162 | ]; |
||
163 | } |
||
164 | |||
165 | /** |
||
166 | * @dataProvider provideDeserializationCompatibility |
||
167 | * |
||
168 | * @param string $serialized |
||
169 | * @param EntityIdValue $expected |
||
170 | */ |
||
171 | public function testDeserializationCompatibility( $serialized, EntityIdValue $expected ) { |
||
172 | $id = unserialize( $serialized ); |
||
173 | |||
174 | $this->assertEquals( $expected, $id ); |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @dataProvider validArrayProvider |
||
179 | */ |
||
180 | public function testNewFromArrayCompatibility( array $array ) { |
||
185 | |||
186 | public function validArrayProvider() { |
||
199 | |||
200 | /** |
||
201 | * @dataProvider invalidArrayProvider |
||
202 | */ |
||
203 | public function testCannotDeserializeInvalidSerialization( $invalidArray ) { |
||
208 | |||
209 | public function invalidArrayProvider() { |
||
250 | |||
251 | } |
||
252 |