Completed
Pull Request — master (#728)
by Daniel
04:05 queued 01:24
created

EntityIdValueTest::newCustomId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use InvalidArgumentException;
6
use PHPUnit_Framework_TestCase;
7
use Wikibase\DataModel\Entity\EntityId;
8
use Wikibase\DataModel\Entity\EntityIdValue;
9
use Wikibase\DataModel\Entity\ItemId;
10
use Wikibase\DataModel\Entity\PropertyId;
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
			'Q2147483647' => new ItemId( 'Q2147483647' ),
45
			'P31337' => new PropertyId( 'P31337' ),
46
			'X567' => $this->newCustomId( 'X567' ),
47
			'foo:P678' => new PropertyId( 'foo:P678' ),
48
		];
49
50
		$argLists = [];
51
52
		foreach ( $ids as $k => $id ) {
53
			$argLists[$k] = [ new EntityIdValue( $id ) ];
54
		}
55
56
		return $argLists;
57
	}
58
59
	/**
60
	 * @dataProvider instanceProvider
61
	 */
62
	public function testGetType( EntityIdValue $id ) {
63
		$this->assertEquals( 'wikibase-entityid', $id->getType() );
64
	}
65
66
	/**
67
	 * @dataProvider instanceProvider
68
	 */
69
	public function testGetValue( EntityIdValue $id ) {
70
		// TODO: this should be changed to return the EntityId.
71
		$this->assertEquals( $id, $id->getValue() );
72
	}
73
74
	/**
75
	 * @dataProvider instanceProvider
76
	 */
77
	public function testGetSortKey( EntityIdValue $id ) {
78
		$this->assertInternalType( 'string', $id->getSortKey() );
79
	}
80
81
	public function provideGetArrayValue() {
82
		return [
83
			'Q2147483647' => [
84
				new ItemId( 'Q2147483647' ),
85
				[
86
					'entity-type' => 'item',
87
					'numeric-id' => 2147483647,
88
					'id' => 'Q2147483647'
89
				],
90
			],
91
			'P31337' => [
92
				new PropertyId( 'P31337' ),
93
				[
94
					'entity-type' => 'property',
95
					'numeric-id' => 31337,
96
					'id' => 'P31337',
97
				],
98
			],
99
			'X567' => [
100
				$this->newCustomId( 'X567' ),
101
				[
102
					'entity-type' => 'custom',
103
					'id' => 'X567',
104
				],
105
			],
106
			'foo:P678' => [
107
				new PropertyId( 'foo:P678' ),
108
				[
109
					'entity-type' => 'property',
110
					'numeric-id' => 678,
111
					'id' => 'foo:P678',
112
				],
113
			],
114
		];
115
	}
116
117
	/**
118
	 * @dataProvider provideGetArrayValue
119
	 */
120
	public function testGetArrayValue( EntityId $id, array $expected ) {
121
		$value = new EntityIdValue( $id );
122
		$array = $value->getArrayValue();
123
124
		ksort( $expected );
125
		ksort( $array );
126
		$this->assertEquals( $expected, $array );
127
	}
128
129
	public function testSerialize() {
130
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
131
132
		$this->assertEquals( 'C:32:"Wikibase\DataModel\Entity\ItemId":6:{Q31337}', $id->serialize() );
133
	}
134
135
	public function provideDeserializationCompatibility() {
136
137
		$local = new EntityIdValue( new ItemId( 'Q31337' ) );
138
		$foreign = new EntityIdValue( new PropertyId( 'foo:P678' ) );
139
		$custom = new EntityIdValue( $this->newCustomId( '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":50:{C:32:"Wikibase\DataModel\Entity\ItemId":6:{Q31337}}',
148
				$local
149
			],
150
			'foreign: Version 7.0 (7fcddfc)' => [
151
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":56:{C:36:"Wikibase\DataModel\Entity\PropertyId":8:{foo:P678}}',
152
				$foreign
153
			],
154
			'custom: Version 7.0 (7fcddfc): custom' => [
155
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":80:{C:64:"Wikibase\DataModel\Tests\Entity\EntityIdValueTest_CustomEntityId":4:{X567}}',
156
				$custom
157
			],
158
		];
159
	}
160
161
	/**
162
	 * @dataProvider provideDeserializationCompatibility
163
	 *
164
	 * @param string $serialized
165
	 * @param EntityId $expected
166
	 */
167
	public function testDeserializationCompatibility( $serialized, EntityIdValue $expected ) {
168
		$id = unserialize( $serialized );
169
		$this->assertEquals( $expected, $id );
170
	}
171
172
	/**
173
	 * @dataProvider validArrayProvider
174
	 */
175
	public function testNewFromArrayCompatibility( array $array ) {
176
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
177
178
		$this->assertEquals( $id, EntityIdValue::newFromArray( $array ) );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entit...IdValue::newFromArray() has been deprecated with message: since 7.1, use callbacks in DataValueDeserializer instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
179
	}
180
181
	public function validArrayProvider() {
182
		return [
183
			'Legacy format' => [ [
184
				'entity-type' => 'item',
185
				'numeric-id' => 31337,
186
			] ],
187
			'Maximum compatibility' => [ [
188
				'entity-type' => 'item',
189
				'numeric-id' => 31337,
190
				'id' => 'Q31337',
191
			] ],
192
		];
193
	}
194
195
	/**
196
	 * @dataProvider invalidArrayProvider
197
	 */
198
	public function testCannotDeserializeInvalidSerialization( $invalidArray ) {
199
		$this->setExpectedException( 'DataValues\IllegalValueException' );
200
201
		EntityIdValue::newFromArray( $invalidArray );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entit...IdValue::newFromArray() has been deprecated with message: since 7.1, use callbacks in DataValueDeserializer instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
202
	}
203
204
	public function invalidArrayProvider() {
205
		return [
206
			[ null ],
207
208
			[ 'foo' ],
209
210
			[ [] ],
211
212
			'newFromArray can not deserialize' => [ [
213
				'id' => 'Q42',
214
			] ],
215
216
			[ [
217
				'entity-type' => 'item',
218
			] ],
219
220
			[ [
221
				'numeric-id' => 42,
222
			] ],
223
224
			[ [
225
				'entity-type' => 'foo',
226
				'numeric-id' => 42,
227
			] ],
228
229
			[ [
230
				'entity-type' => 42,
231
				'numeric-id' => 42,
232
			] ],
233
234
			[ [
235
				'entity-type' => 'item',
236
				'numeric-id' => -1,
237
			] ],
238
239
			[ [
240
				'entity-type' => 'item',
241
				'numeric-id' => 'foo',
242
			] ],
243
		];
244
	}
245
246
	/**
247
	 * @param string $string
248
	 *
249
	 * @return EntityId
250
	 */
251
	private function newCustomId( $string ) {
252
		return new EntityIdValueTest_CustomEntityId( $string );
253
	}
254
255
}
256
257
class EntityIdValueTest_CustomEntityId extends EntityId {
258
259
	/**
260
	 * @param string $idSerialization
261
	 *
262
	 * @throws InvalidArgumentException
263
	 */
264
	public function __construct( $idSerialization ) {
265
		$serializationParts = self::splitSerialization( $idSerialization );
266
		$localId = strtoupper( $serializationParts[2] );
267
		parent::__construct( self::joinSerialization(
268
			[ $serializationParts[0], $serializationParts[1], $localId ] )
269
		);
270
	}
271
272
	/**
273
	 * @see Serializable::serialize
274
	 *
275
	 * @since 7.0 serialization format changed in an incompatible way
276
	 *
277
	 * @return string
278
	 */
279
	public function serialize() {
280
		return $this->serialization;
281
	}
282
283
	/**
284
	 * @see Serializable::unserialize
285
	 *
286
	 * @param string $serialized
287
	 */
288
	public function unserialize( $serialized ) {
289
		$array = json_decode( $serialized );
290
		$this->serialization = is_array( $array ) ? $array[1] : $serialized;
291
	}
292
293
	/**
294
	 * @return string
295
	 */
296
	public function getEntityType() {
297
		return 'custom';
298
	}
299
300
}