Completed
Pull Request — master (#728)
by Daniel
06:09 queued 03:08
created

EntityIdValueTest::testGetArrayValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 2
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
use Wikibase\DataModel\Fixtures\HashArrayElement;
13
14
/**
15
 * @covers Wikibase\DataModel\Entity\EntityIdValue
16
 *
17
 * @group Wikibase
18
 * @group WikibaseDataModel
19
 *
20
 * @license GPL-2.0+
21
 * @author Jeroen De Dauw < [email protected] >
22
 * @author Thiemo Mättig
23
 * @author Daniel Kinzler
24
 */
25
class EntityIdValueTest extends PHPUnit_Framework_TestCase {
26
27
	public function testCanConstruct() {
28
		$entityId = new ItemId( 'Q123' );
29
		$entityIdValue = new EntityIdValue( $entityId );
30
		$this->assertEquals( $entityId, $entityIdValue->getEntityId() );
31
	}
32
33
	/**
34
	 * @dataProvider instanceProvider
35
	 */
36
	public function testSerialzationRoundtrip( EntityIdValue $id ) {
37
		$serialized = serialize( $id );
38
		$newId = unserialize( $serialized );
39
40
		$this->assertEquals( $id, $newId );
41
	}
42
43
	public function instanceProvider() {
44
		$ids = [
45
			'Q1' => new ItemId( 'Q1' ),
46
			'Q2147483647' => new ItemId( 'Q2147483647' ),
47
			'P31337' => new PropertyId( 'P31337' ),
48
			'X567' => $this->newCustomId( 'X567' ),
49
			'foo:P678' => new PropertyId( 'foo:P678' ),
50
		];
51
52
		$argLists = [];
53
54
		foreach ( $ids as $k => $id ) {
55
			$argLists[$k] = [ new EntityIdValue( $id ) ];
56
		}
57
58
		return $argLists;
59
	}
60
61
	/**
62
	 * @dataProvider instanceProvider
63
	 */
64
	public function testGetType( EntityIdValue $id ) {
65
		$this->assertEquals( 'wikibase-entityid', $id->getType() );
66
	}
67
68
	/**
69
	 * @dataProvider instanceProvider
70
	 */
71
	public function testGetValue( EntityIdValue $id ) {
72
		// TODO: this should be changed to return the EntityId.
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
				$this->newCustomId( '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
		ksort( $expected );
127
		ksort( $array );
128
		$this->assertEquals( $expected, $array );
129
	}
130
131
	public function testSerialize() {
132
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
133
134
		$this->assertEquals( 'C:32:"Wikibase\DataModel\Entity\ItemId":6:{Q31337}', $id->serialize() );
135
	}
136
137
	public function provideDeserializationCompatibility() {
138
139
		$local = new EntityIdValue( new ItemId( 'Q31337' ) );
140
		$foreign = new EntityIdValue( new PropertyId( 'foo:P678' ) );
141
		$custom = new EntityIdValue( $this->newCustomId( 'X567' ) );
142
143
		return [
144
			'local: Version 0.5 alpha (f5b8b64)' => [
145
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":14:{["item",31337]}',
146
				$local
147
			],
148
			'local: Version 7.0 (7fcddfc)' => [
149
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":'
150
					. '50:{C:32:"Wikibase\DataModel\Entity\ItemId":6:{Q31337}}',
151
				$local
152
			],
153
			'foreign: Version 7.0 (7fcddfc)' => [
154
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":'
155
					. '56:{C:36:"Wikibase\DataModel\Entity\PropertyId":8:{foo:P678}}',
156
				$foreign
157
			],
158
			'custom: Version 7.0 (7fcddfc): custom' => [
159
				'C:39:"Wikibase\DataModel\Entity\EntityIdValue":'
160
					. '58:{C:42:"Wikibase\DataModel\Fixtures\CustomEntityId":4:{X567}}',
161
				$custom
162
			],
163
		];
164
	}
165
166
	/**
167
	 * @dataProvider provideDeserializationCompatibility
168
	 *
169
	 * @param string $serialized
170
	 * @param EntityId $expected
171
	 */
172
	public function testDeserializationCompatibility( $serialized, EntityIdValue $expected ) {
173
		$id = unserialize( $serialized );
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 ) );
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...
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( 'DataValues\IllegalValueException' );
205
206
		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...
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
	 * @param string $string
253
	 *
254
	 * @return EntityId
255
	 */
256
	private function newCustomId( $string ) {
257
		new HashArrayElement( 'Bla ');
258
		return new CustomEntityId( $string );
259
	}
260
261
}
262