Passed
Push — rankClass ( 1b9cf4...7f76c7 )
by no
04:15 queued 16s
created

EntityIdValueTest::testCanConstruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use DataValues\IllegalValueException;
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 Kreuz
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
		/** @var EntityId[] $ids */
44
		$ids = [
45
			new ItemId( 'Q1' ),
46
			new ItemId( 'Q2147483647' ),
47
			new PropertyId( 'P1' ),
48
			new PropertyId( 'P31337' ),
49
			new CustomEntityId( 'X567' ),
50
			new PropertyId( 'foo:P678' ),
51
		];
52
53
		$argLists = [];
54
55
		foreach ( $ids as $id ) {
56
			$argLists[$id->getSerialization()] = [ new EntityIdValue( $id ) ];
57
		}
58
59
		return $argLists;
60
	}
61
62
	/**
63
	 * @dataProvider instanceProvider
64
	 */
65
	public function testGetType( EntityIdValue $id ) {
66
		$this->assertEquals( 'wikibase-entityid', $id->getType() );
67
	}
68
69
	/**
70
	 * @dataProvider instanceProvider
71
	 */
72
	public function testGetValue( EntityIdValue $id ) {
73
		$this->assertEquals( $id, $id->getValue() );
74
	}
75
76
	/**
77
	 * @dataProvider instanceProvider
78
	 */
79
	public function testGetSortKey( EntityIdValue $id ) {
80
		$this->assertInternalType( 'string', $id->getSortKey() );
81
	}
82
83
	public function provideGetArrayValue() {
84
		return [
85
			'Q2147483647' => [
86
				new ItemId( 'Q2147483647' ),
87
				[
88
					'entity-type' => 'item',
89
					'numeric-id' => 2147483647,
90
					'id' => 'Q2147483647'
91
				],
92
			],
93
			'P31337' => [
94
				new PropertyId( 'P31337' ),
95
				[
96
					'entity-type' => 'property',
97
					'numeric-id' => 31337,
98
					'id' => 'P31337',
99
				],
100
			],
101
			'X567' => [
102
				new CustomEntityId( 'X567' ),
103
				[
104
					'entity-type' => 'custom',
105
					'id' => 'X567',
106
				],
107
			],
108
			'foo:P678' => [
109
				new PropertyId( 'foo:P678' ),
110
				[
111
					'entity-type' => 'property',
112
					'numeric-id' => 678,
113
					'id' => 'foo:P678',
114
				],
115
			],
116
		];
117
	}
118
119
	/**
120
	 * @dataProvider provideGetArrayValue
121
	 */
122
	public function testGetArrayValue( EntityId $id, array $expected ) {
123
		$value = new EntityIdValue( $id );
124
		$array = $value->getArrayValue();
125
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
		$local = new EntityIdValue( new ItemId( 'Q31337' ) );
137
		$foreign = new EntityIdValue( new PropertyId( 'foo:P678' ) );
138
		$custom = new EntityIdValue( new CustomEntityId( 'X567' ) );
139
140
		return [
141
			'local: Version 0.5 alpha (f5b8b64)' => [
142
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":14:{["item",31337]}',
143
				$local
144
			],
145
			'local: Version 7.0 (7fcddfc)' => [
146
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":'
147
					. '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":'
152
					. '56:{C:36:"Wikibase\DataModel\Entity\PropertyId":8:{foo:P678}}',
153
				$foreign
154
			],
155
			'custom: Version 7.0 (7fcddfc): custom' => [
156
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":'
157
					. '58:{C:42:"Wikibase\DataModel\Fixtures\CustomEntityId":4:{X567}}',
158
				$custom
159
			],
160
		];
161
	}
162
163
	/**
164
	 * @dataProvider provideDeserializationCompatibility
165
	 *
166
	 * @param string $serialized
167
	 * @param EntityIdValue $expected
168
	 */
169
	public function testDeserializationCompatibility( $serialized, EntityIdValue $expected ) {
170
		$id = unserialize( $serialized );
171
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. 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...
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->expectException( IllegalValueException::class );
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. 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...
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