Completed
Pull Request — master (#728)
by Daniel
03:30
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
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
			'Q2147483647' => new ItemId( 'Q2147483647' ),
46
			'P31337' => new PropertyId( 'P31337' ),
47
			'X567' => $this->newCustomId( 'X567' ),
48
			'foo:P678' => new PropertyId( 'foo:P678' ),
49
		];
50
51
		$argLists = [];
52
53
		foreach ( $ids as $k => $id ) {
54
			$argLists[$k] = [ new EntityIdValue( $id ) ];
55
		}
56
57
		return $argLists;
58
	}
59
60
	/**
61
	 * @dataProvider instanceProvider
62
	 */
63
	public function testGetType( EntityIdValue $id ) {
64
		$this->assertEquals( 'wikibase-entityid', $id->getType() );
65
	}
66
67
	/**
68
	 * @dataProvider instanceProvider
69
	 */
70
	public function testGetValue( EntityIdValue $id ) {
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->assertSame( $expected, $array );
127
	}
128
129
	public function testSerialize() {
130
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
131
132
		$this->assertSame( '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":'
148
					. '50:{C:32:"Wikibase\DataModel\Entity\ItemId":6:{Q31337}}',
149
				$local
150
			],
151
			'foreign: Version 7.0 (7fcddfc)' => [
152
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":'
153
					. '56:{C:36:"Wikibase\DataModel\Entity\PropertyId":8:{foo:P678}}',
154
				$foreign
155
			],
156
			'custom: Version 7.0 (7fcddfc): custom' => [
157
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":'
158
					. '58:{C:42:"Wikibase\DataModel\Fixtures\CustomEntityId":4:{X567}}',
159
				$custom
160
			],
161
		];
162
	}
163
164
	/**
165
	 * @dataProvider provideDeserializationCompatibility
166
	 *
167
	 * @param string $serialized
168
	 * @param EntityId $expected
169
	 */
170
	public function testDeserializationCompatibility( $serialized, EntityIdValue $expected ) {
171
		$id = unserialize( $serialized );
172
		$this->assertEquals( $expected, $id );
173
	}
174
175
	/**
176
	 * @dataProvider validArrayProvider
177
	 */
178
	public function testNewFromArrayCompatibility( array $array ) {
179
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
180
181
		$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...
182
	}
183
184
	public function validArrayProvider() {
185
		return [
186
			'Legacy format' => [ [
187
				'entity-type' => 'item',
188
				'numeric-id' => 31337,
189
			] ],
190
			'Maximum compatibility' => [ [
191
				'entity-type' => 'item',
192
				'numeric-id' => 31337,
193
				'id' => 'Q31337',
194
			] ],
195
		];
196
	}
197
198
	/**
199
	 * @dataProvider invalidArrayProvider
200
	 */
201
	public function testCannotDeserializeInvalidSerialization( $invalidArray ) {
202
		$this->setExpectedException( 'DataValues\IllegalValueException' );
203
204
		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...
205
	}
206
207
	public function invalidArrayProvider() {
208
		return [
209
			[ null ],
210
211
			[ 'foo' ],
212
213
			[ [] ],
214
215
			'newFromArray can not deserialize' => [ [
216
				'id' => 'Q42',
217
			] ],
218
219
			[ [
220
				'entity-type' => 'item',
221
			] ],
222
223
			[ [
224
				'numeric-id' => 42,
225
			] ],
226
227
			[ [
228
				'entity-type' => 'foo',
229
				'numeric-id' => 42,
230
			] ],
231
232
			[ [
233
				'entity-type' => 42,
234
				'numeric-id' => 42,
235
			] ],
236
237
			[ [
238
				'entity-type' => 'item',
239
				'numeric-id' => -1,
240
			] ],
241
242
			[ [
243
				'entity-type' => 'item',
244
				'numeric-id' => 'foo',
245
			] ],
246
		];
247
	}
248
249
	/**
250
	 * @param string $string
251
	 *
252
	 * @return EntityId
253
	 */
254
	private function newCustomId( $string ) {
255
		return new CustomEntityId( $string );
256
	}
257
258
}
259