Passed
Push — int32EntityId ( f29bef...7aefb4 )
by no
03:45
created

EntityIdValueTest::validArrayProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 13
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use PHPUnit_Framework_TestCase;
6
use Wikibase\DataModel\Entity\EntityIdValue;
7
use Wikibase\DataModel\Entity\ItemId;
8
use Wikibase\DataModel\Entity\PropertyId;
9
10
/**
11
 * @covers Wikibase\DataModel\Entity\EntityIdValue
12
 *
13
 * @group Wikibase
14
 * @group WikibaseDataModel
15
 * @group EntityIdTest
16
 *
17
 * @license GPL-2.0+
18
 * @author Jeroen De Dauw < [email protected] >
19
 * @author Thiemo Mättig
20
 */
21
class EntityIdValueTest extends PHPUnit_Framework_TestCase {
22
23
	public function testCanConstruct() {
24
		$entityId = new ItemId( 'Q123' );
25
		$entityIdValue = new EntityIdValue( $entityId );
26
		$this->assertEquals( $entityId, $entityIdValue->getEntityId() );
27
	}
28
29
	/**
30
	 * @dataProvider instanceProvider
31
	 */
32
	public function testSerialzationRoundtrip( EntityIdValue $id ) {
33
		$newId = unserialize( serialize( $id ) );
34
35
		$this->assertEquals( $id, $newId );
36
	}
37
38
	public function instanceProvider() {
39
		$ids = array(
40
			new ItemId( 'Q1' ),
41
			new ItemId( 'Q42' ),
42
			new ItemId( 'Q31337' ),
43
			new ItemId( 'Q2147483647' ),
44
			new PropertyId( 'P1' ),
45
			new PropertyId( 'P42' ),
46
			new PropertyId( 'P31337' ),
47
		);
48
49
		$argLists = array();
50
51
		foreach ( $ids as $id ) {
52
			$argLists[] = array( new EntityIdValue( $id ) );
53
		}
54
55
		return $argLists;
56
	}
57
58
	/**
59
	 * @dataProvider instanceProvider
60
	 */
61
	public function testGetType( EntityIdValue $id ) {
62
		$this->assertEquals( 'wikibase-entityid', $id->getType() );
63
	}
64
65
	/**
66
	 * @dataProvider instanceProvider
67
	 */
68
	public function testGetValue( EntityIdValue $id ) {
69
		$this->assertEquals( $id, $id->getValue() );
70
	}
71
72
	/**
73
	 * @dataProvider instanceProvider
74
	 */
75
	public function testGetSortKey( EntityIdValue $id ) {
76
		$this->assertInternalType( 'string', $id->getSortKey() );
77
	}
78
79
	/**
80
	 * @dataProvider instanceProvider
81
	 */
82
	public function testGetArrayValueRoundtrip( EntityIdValue $id ) {
83
		$newId = EntityIdValue::newFromArray( $id->getArrayValue() );
84
85
		$this->assertEquals( $id, $newId );
86
	}
87
88
	public function testSerializationCompatibility() {
89
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
90
91
		// This is the serialization format from when the EntityIdValue was still together with EntityId.
92
		$this->assertEquals( '["item",31337]', $id->serialize() );
93
	}
94
95
	public function testDeserializationCompatibility() {
96
		$expected = new EntityIdValue( new ItemId( 'Q31337' ) );
97
98
		// This is the serialization format from f5b8b64823ff215c3796a79d916b6eaa65f4be33, version 0.5 alpha.
99
		$id = unserialize( 'C:39:"Wikibase\DataModel\Entity\EntityIdValue":14:{["item",31337]}' );
100
		$this->assertEquals( $expected, $id );
101
	}
102
103
	public function testGetArrayValueCompatibility() {
104
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
105
106
		$this->assertSame(
107
			// This is the serialization format from when the EntityIdValue was still together with EntityId.
108
			array(
109
				'entity-type' => 'item',
110
				'numeric-id' => (float)31337,
111
				'id' => 'Q31337',
112
			),
113
			$id->getArrayValue()
114
		);
115
	}
116
117
	/**
118
	 * @dataProvider validArrayProvider
119
	 */
120
	public function testNewFromArrayCompatibility( array $array ) {
121
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
122
123
		$this->assertEquals( $id, EntityIdValue::newFromArray( $array ) );
124
	}
125
126
	public function validArrayProvider() {
127
		return array(
128
			'Legacy format' => array( array(
129
				'entity-type' => 'item',
130
				'numeric-id' => 31337,
131
			) ),
132
			'Maximum compatibility' => array( array(
133
				'entity-type' => 'item',
134
				'numeric-id' => 31337,
135
				'id' => 'Q31337',
136
			) ),
137
		);
138
	}
139
140
	/**
141
	 * @dataProvider invalidArrayProvider
142
	 */
143
	public function testCannotDeserializeInvalidSerialization( $invalidArray ) {
144
		$this->setExpectedException( 'DataValues\IllegalValueException' );
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

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...
145
146
		EntityIdValue::newFromArray( $invalidArray );
147
	}
148
149
	public function invalidArrayProvider() {
150
		return array(
151
			array( null ),
152
153
			array( 'foo' ),
154
155
			array( array() ),
156
157
			'newFromArray can not deserialize' => array( array(
158
				'id' => 'Q42',
159
			) ),
160
161
			array( array(
162
				'entity-type' => 'item',
163
			) ),
164
165
			array( array(
166
				'numeric-id' => 42,
167
			) ),
168
169
			array( array(
170
				'entity-type' => 'foo',
171
				'numeric-id' => 42,
172
			) ),
173
174
			array( array(
175
				'entity-type' => 42,
176
				'numeric-id' => 42,
177
			) ),
178
179
			array( array(
180
				'entity-type' => 'item',
181
				'numeric-id' => -1,
182
			) ),
183
184
			array( array(
185
				'entity-type' => 'item',
186
				'numeric-id' => 'foo',
187
			) ),
188
		);
189
	}
190
191
}
192