EntityIdValueTest::instanceProvider()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use DataValues\IllegalValueException;
6
use Wikibase\DataModel\Entity\EntityId;
7
use Wikibase\DataModel\Entity\EntityIdValue;
8
use Wikibase\DataModel\Entity\ItemId;
9
use Wikibase\DataModel\Entity\PropertyId;
10
use Wikibase\DataModel\Fixtures\CustomEntityId;
11
12
/**
13
 * @covers \Wikibase\DataModel\Entity\EntityIdValue
14
 *
15
 * @group Wikibase
16
 * @group WikibaseDataModel
17
 *
18
 * @license GPL-2.0-or-later
19
 * @author Jeroen De Dauw < [email protected] >
20
 * @author Thiemo Kreuz
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
		/** @var EntityId[] $ids */
43
		$ids = [
44
			new ItemId( 'Q1' ),
45
			new ItemId( 'Q2147483647' ),
46
			new PropertyId( 'P1' ),
47
			new PropertyId( 'P31337' ),
48
			new CustomEntityId( 'X567' ),
49
			new PropertyId( 'foo:P678' ),
50
		];
51
52
		$argLists = [];
53
54
		foreach ( $ids as $id ) {
55
			$argLists[$id->getSerialization()] = [ new EntityIdValue( $id ) ];
56
		}
57
58
		return $argLists;
59
	}
60
61
	/**
62
	 * @dataProvider instanceProvider
63
	 */
64
	public function testGetType( EntityIdValue $id ) {
65
		$this->assertSame( 'wikibase-entityid', $id->getType() );
66
	}
67
68
	/**
69
	 * @dataProvider instanceProvider
70
	 */
71
	public function testGetValue( EntityIdValue $id ) {
72
		$this->assertEquals( $id, $id->getValue() );
73
	}
74
75
	/**
76
	 * @dataProvider instanceProvider
77
	 */
78
	public function testGetSortKey( EntityIdValue $id ) {
79
		$this->assertIsString( $id->getSortKey() );
0 ignored issues
show
Deprecated Code introduced by
The method Wikibase\DataModel\Entit...tyIdValue::getSortKey() has been deprecated with message: Kept for compatibility with older DataValues versions.
Do not use.

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...
80
	}
81
82
	public function provideGetArrayValue() {
83
		return [
84
			'Q2147483647' => [
85
				new ItemId( 'Q2147483647' ),
86
				[
87
					'entity-type' => 'item',
88
					'numeric-id' => 2147483647,
89
					'id' => 'Q2147483647'
90
				],
91
			],
92
			'P31337' => [
93
				new PropertyId( 'P31337' ),
94
				[
95
					'entity-type' => 'property',
96
					'numeric-id' => 31337,
97
					'id' => 'P31337',
98
				],
99
			],
100
			'X567' => [
101
				new CustomEntityId( 'X567' ),
102
				[
103
					'entity-type' => 'custom',
104
					'id' => 'X567',
105
				],
106
			],
107
			'foo:P678' => [
108
				new PropertyId( 'foo:P678' ),
109
				[
110
					'entity-type' => 'property',
111
					'numeric-id' => 678,
112
					'id' => 'foo:P678',
113
				],
114
			],
115
		];
116
	}
117
118
	/**
119
	 * @dataProvider provideGetArrayValue
120
	 */
121
	public function testGetArrayValue( EntityId $id, array $expected ) {
122
		$value = new EntityIdValue( $id );
123
		$array = $value->getArrayValue();
124
125
		$this->assertSame( $expected, $array );
126
	}
127
128
	public function testSerialize() {
129
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
130
131
		$this->assertSame( 'C:32:"Wikibase\DataModel\Entity\ItemId":6:{Q31337}', $id->serialize() );
132
	}
133
134
	public function provideDeserializationCompatibility() {
135
		$local = new EntityIdValue( new ItemId( 'Q31337' ) );
136
		$foreign = new EntityIdValue( new PropertyId( 'foo:P678' ) );
137
		$custom = new EntityIdValue( new CustomEntityId( 'X567' ) );
138
139
		return [
140
			'local: Version 0.5 alpha (f5b8b64)' => [
141
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":14:{["item",31337]}',
142
				$local
143
			],
144
			'local: Version 7.0 (7fcddfc)' => [
145
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":'
146
					. '50:{C:32:"Wikibase\DataModel\Entity\ItemId":6:{Q31337}}',
147
				$local
148
			],
149
			'foreign: Version 7.0 (7fcddfc)' => [
150
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":'
151
					. '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":'
156
					. '58:{C:42:"Wikibase\DataModel\Fixtures\CustomEntityId":4:{X567}}',
157
				$custom
158
			],
159
		];
160
	}
161
162
	/**
163
	 * @dataProvider provideDeserializationCompatibility
164
	 *
165
	 * @param string $serialized
166
	 * @param EntityIdValue $expected
167
	 */
168
	public function testDeserializationCompatibility( $serialized, EntityIdValue $expected ) {
169
		$id = unserialize( $serialized );
170
171
		$this->assertEquals( $expected, $id );
172
	}
173
174
	/**
175
	 * @dataProvider validArrayProvider
176
	 */
177
	public function testNewFromArrayCompatibility( array $array ) {
178
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
179
180
		$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. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

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...
181
	}
182
183
	public function validArrayProvider() {
184
		return [
185
			'Legacy format' => [ [
186
				'entity-type' => 'item',
187
				'numeric-id' => 31337,
188
			] ],
189
			'Maximum compatibility' => [ [
190
				'entity-type' => 'item',
191
				'numeric-id' => 31337,
192
				'id' => 'Q31337',
193
			] ],
194
		];
195
	}
196
197
	/**
198
	 * @dataProvider invalidArrayProvider
199
	 */
200
	public function testCannotDeserializeInvalidSerialization( $invalidArray ) {
201
		$this->expectException( IllegalValueException::class );
202
203
		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. Static DataValue::newFromArray constructors like this are underspecified (not in the DataValue interface), and misleadingly named (should be named newFromArrayValue). Instead, use DataValue builder callbacks in @see DataValueDeserializer.

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...
204
	}
205
206
	public function invalidArrayProvider() {
207
		return [
208
			[ null ],
209
210
			[ 'foo' ],
211
212
			[ [] ],
213
214
			'newFromArray can not deserialize' => [ [
215
				'id' => 'Q42',
216
			] ],
217
218
			[ [
219
				'entity-type' => 'item',
220
			] ],
221
222
			[ [
223
				'numeric-id' => 42,
224
			] ],
225
226
			[ [
227
				'entity-type' => 'foo',
228
				'numeric-id' => 42,
229
			] ],
230
231
			[ [
232
				'entity-type' => 42,
233
				'numeric-id' => 42,
234
			] ],
235
236
			[ [
237
				'entity-type' => 'item',
238
				'numeric-id' => -1,
239
			] ],
240
241
			[ [
242
				'entity-type' => 'item',
243
				'numeric-id' => 'foo',
244
			] ],
245
		];
246
	}
247
248
}
249