Completed
Pull Request — master (#728)
by Daniel
05:40 queued 02:59
created

provideDeserializationCompatibility()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 28
rs 8.8571
cc 1
eloc 20
nc 1
nop 0
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
use Wikibase\DataModel\Fixtures\CustomEntityId;
12
13
/**
14
 * @covers Wikibase\DataModel\Entity\EntityIdValue
15
 *
16
 * @group Wikibase
17
 * @group WikibaseDataModel
18
 *
19
 * @license GPL-2.0+
20
 * @author Jeroen De Dauw < [email protected] >
21
 * @author Thiemo Mättig
22
 * @author Daniel Kinzler
23
 */
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' => $this->newCustomId( '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
				$this->newCustomId( '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
		ksort( $expected );
129
		ksort( $array );
130
		$this->assertSame( $expected, $array );
131
	}
132
133
	public function testSerialize() {
134
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
135
136
		$this->assertSame( 'C:32:"Wikibase\DataModel\Entity\ItemId":6:{Q31337}', $id->serialize() );
137
	}
138
139
	public function provideDeserializationCompatibility() {
140
141
		$local = new EntityIdValue( new ItemId( 'Q31337' ) );
142
		$foreign = new EntityIdValue( new PropertyId( 'foo:P678' ) );
143
		$custom = new EntityIdValue( $this->newCustomId( 'X567' ) );
144
145
		return [
146
			'local: Version 0.5 alpha (f5b8b64)' => [
147
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":14:{["item",31337]}',
148
				$local
149
			],
150
			'local: Version 7.0 (7fcddfc)' => [
151
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":'
152
					. '50:{C:32:"Wikibase\DataModel\Entity\ItemId":6:{Q31337}}',
153
				$local
154
			],
155
			'foreign: Version 7.0 (7fcddfc)' => [
156
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":'
157
					. '56:{C:36:"Wikibase\DataModel\Entity\PropertyId":8:{foo:P678}}',
158
				$foreign
159
			],
160
			'custom: Version 7.0 (7fcddfc): custom' => [
161
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":'
162
					. '58:{C:42:"Wikibase\DataModel\Fixtures\CustomEntityId":4:{X567}}',
163
				$custom
164
			],
165
		];
166
	}
167
168
	/**
169
	 * @dataProvider provideDeserializationCompatibility
170
	 *
171
	 * @param string $serialized
172
	 * @param EntityId $expected
173
	 */
174
	public function testDeserializationCompatibility( $serialized, EntityIdValue $expected ) {
175
		$id = unserialize( $serialized );
176
		$this->assertEquals( $expected, $id );
177
	}
178
179
	/**
180
	 * @dataProvider validArrayProvider
181
	 */
182
	public function testNewFromArrayCompatibility( array $array ) {
183
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
184
185
		$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...
186
	}
187
188
	public function validArrayProvider() {
189
		return [
190
			'Legacy format' => [ [
191
				'entity-type' => 'item',
192
				'numeric-id' => 31337,
193
			] ],
194
			'Maximum compatibility' => [ [
195
				'entity-type' => 'item',
196
				'numeric-id' => 31337,
197
				'id' => 'Q31337',
198
			] ],
199
		];
200
	}
201
202
	/**
203
	 * @dataProvider invalidArrayProvider
204
	 */
205
	public function testCannotDeserializeInvalidSerialization( $invalidArray ) {
206
		$this->setExpectedException( 'DataValues\IllegalValueException' );
207
208
		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...
209
	}
210
211
	public function invalidArrayProvider() {
212
		return [
213
			[ null ],
214
215
			[ 'foo' ],
216
217
			[ [] ],
218
219
			'newFromArray can not deserialize' => [ [
220
				'id' => 'Q42',
221
			] ],
222
223
			[ [
224
				'entity-type' => 'item',
225
			] ],
226
227
			[ [
228
				'numeric-id' => 42,
229
			] ],
230
231
			[ [
232
				'entity-type' => 'foo',
233
				'numeric-id' => 42,
234
			] ],
235
236
			[ [
237
				'entity-type' => 42,
238
				'numeric-id' => 42,
239
			] ],
240
241
			[ [
242
				'entity-type' => 'item',
243
				'numeric-id' => -1,
244
			] ],
245
246
			[ [
247
				'entity-type' => 'item',
248
				'numeric-id' => 'foo',
249
			] ],
250
		];
251
	}
252
253
	/**
254
	 * @param string $string
255
	 *
256
	 * @return EntityId
257
	 */
258
	private function newCustomId( $string ) {
259
		return new CustomEntityId( $string );
260
	}
261
262
}
263