Passed
Push — currentLimits ( af2764...193e29 )
by no
03:56
created

EntityIdValueTest::testGetType()   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
c 0
b 0
f 0
rs 10
cc 1
eloc 2
nc 1
nop 1
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 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' => new CustomEntityId( '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
				new CustomEntityId( '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
		$this->assertSame( $expected, $array );
129
	}
130
131
	public function testSerialize() {
132
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
133
134
		$this->assertSame( 'C:32:"Wikibase\DataModel\Entity\ItemId":6:{Q31337}', $id->serialize() );
135
	}
136
137
	public function provideDeserializationCompatibility() {
138
		$local = new EntityIdValue( new ItemId( 'Q31337' ) );
139
		$foreign = new EntityIdValue( new PropertyId( 'foo:P678' ) );
140
		$custom = new EntityIdValue( new CustomEntityId( 'X567' ) );
141
142
		return [
143
			'local: Version 0.5 alpha (f5b8b64)' => [
144
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":14:{["item",31337]}',
145
				$local
146
			],
147
			'local: Version 7.0 (7fcddfc)' => [
148
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":'
149
					. '50:{C:32:"Wikibase\DataModel\Entity\ItemId":6:{Q31337}}',
150
				$local
151
			],
152
			'foreign: Version 7.0 (7fcddfc)' => [
153
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":'
154
					. '56:{C:36:"Wikibase\DataModel\Entity\PropertyId":8:{foo:P678}}',
155
				$foreign
156
			],
157
			'custom: Version 7.0 (7fcddfc): custom' => [
158
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":'
159
					. '58:{C:42:"Wikibase\DataModel\Fixtures\CustomEntityId":4:{X567}}',
160
				$custom
161
			],
162
		];
163
	}
164
165
	/**
166
	 * @dataProvider provideDeserializationCompatibility
167
	 *
168
	 * @param string $serialized
169
	 * @param EntityIdValue $expected
170
	 */
171
	public function testDeserializationCompatibility( $serialized, EntityIdValue $expected ) {
172
		$id = unserialize( $serialized );
173
174
		$this->assertEquals( $expected, $id );
175
	}
176
177
	/**
178
	 * @dataProvider validArrayProvider
179
	 */
180
	public function testNewFromArrayCompatibility( array $array ) {
181
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
182
183
		$this->assertEquals( $id, EntityIdValue::newFromArray( $array ) );
184
	}
185
186
	public function validArrayProvider() {
187
		return [
188
			'Legacy format' => [ [
189
				'entity-type' => 'item',
190
				'numeric-id' => 31337,
191
			] ],
192
			'Maximum compatibility' => [ [
193
				'entity-type' => 'item',
194
				'numeric-id' => 31337,
195
				'id' => 'Q31337',
196
			] ],
197
		];
198
	}
199
200
	/**
201
	 * @dataProvider invalidArrayProvider
202
	 */
203
	public function testCannotDeserializeInvalidSerialization( $invalidArray ) {
204
		$this->setExpectedException( IllegalValueException::class );
205
206
		EntityIdValue::newFromArray( $invalidArray );
207
	}
208
209
	public function invalidArrayProvider() {
210
		return [
211
			[ null ],
212
213
			[ 'foo' ],
214
215
			[ [] ],
216
217
			'newFromArray can not deserialize' => [ [
218
				'id' => 'Q42',
219
			] ],
220
221
			[ [
222
				'entity-type' => 'item',
223
			] ],
224
225
			[ [
226
				'numeric-id' => 42,
227
			] ],
228
229
			[ [
230
				'entity-type' => 'foo',
231
				'numeric-id' => 42,
232
			] ],
233
234
			[ [
235
				'entity-type' => 42,
236
				'numeric-id' => 42,
237
			] ],
238
239
			[ [
240
				'entity-type' => 'item',
241
				'numeric-id' => -1,
242
			] ],
243
244
			[ [
245
				'entity-type' => 'item',
246
				'numeric-id' => 'foo',
247
			] ],
248
		];
249
	}
250
251
}
252