Completed
Push — coverageversiong ( 2761c4 )
by Jeroen De
12:52 queued 07:07
created

EntityIdValueTest::testNewFromArrayCompatibility()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 14
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use Wikibase\DataModel\Entity\EntityIdValue;
6
use Wikibase\DataModel\Entity\ItemId;
7
use Wikibase\DataModel\Entity\PropertyId;
8
9
/**
10
 * @covers Wikibase\DataModel\Entity\EntityIdValue
11
 *
12
 * @group Wikibase
13
 * @group WikibaseDataModel
14
 * @group EntityIdTest
15
 *
16
 * @licence GNU GPL v2+
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class EntityIdValueTest extends \PHPUnit_Framework_TestCase {
20
21
	public function testCanConstruct() {
22
		$entityId = new ItemId( 'Q123' );
23
		$entityIdValue = new EntityIdValue( $entityId );
24
		$this->assertEquals( $entityId, $entityIdValue->getEntityId() );
25
	}
26
27
	/**
28
	 * @dataProvider instanceProvider
29
	 */
30
	public function testSerialzationRoundtrip( EntityIdValue $id ) {
31
		$newId = unserialize( serialize( $id ) );
32
33
		$this->assertEquals( $id, $newId );
34
	}
35
36
	public function instanceProvider() {
37
		$ids = array(
38
			new ItemId( 'Q1' ),
39
			new ItemId( 'Q42' ),
40
			new ItemId( 'Q31337' ),
41
			// Check for 32-bit integer overflow on 32-bit PHP systems.
42
			new ItemId( 'Q2147483648' ),
43
			new PropertyId( 'P1' ),
44
			new PropertyId( 'P42' ),
45
			new PropertyId( 'P31337' ),
46
		);
47
48
		$argLists = array();
49
50
		foreach ( $ids as $id ) {
51
			$argLists[] = array( new EntityIdValue( $id ) );
52
		}
53
54
		return $argLists;
55
	}
56
57
	/**
58
	 * @dataProvider instanceProvider
59
	 */
60
	public function testGetType( EntityIdValue $id ) {
61
		$this->assertEquals( 'wikibase-entityid', $id->getType() );
62
	}
63
64
	/**
65
	 * @dataProvider instanceProvider
66
	 */
67
	public function testGetValue( EntityIdValue $id ) {
68
		$this->assertEquals( $id, $id->getValue() );
69
	}
70
71
	/**
72
	 * @dataProvider instanceProvider
73
	 */
74
	public function testGetSortKey( EntityIdValue $id ) {
75
		$this->assertInternalType( 'string', $id->getSortKey() );
76
	}
77
78
	/**
79
	 * @dataProvider instanceProvider
80
	 */
81
	public function testGetArrayValueRoundtrip( EntityIdValue $id ) {
82
		$newId = EntityIdValue::newFromArray( $id->getArrayValue() );
83
84
		$this->assertEquals( $id, $newId );
85
	}
86
87
	public function testSerializationCompatibility() {
88
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
89
90
		// This is the serialization format from when the EntityIdValue was still together with EntityId.
91
		$this->assertEquals( '["item",31337]', $id->serialize() );
92
	}
93
94
	public function testDeserializationCompatibility() {
95
		$expected = new EntityIdValue( new ItemId( 'Q31337' ) );
96
97
		// This is the serialization format from f5b8b64823ff215c3796a79d916b6eaa65f4be33, version 0.5 alpha.
98
		$id = unserialize( 'C:39:"Wikibase\DataModel\Entity\EntityIdValue":14:{["item",31337]}' );
99
		$this->assertEquals( $expected, $id );
100
	}
101
102
	public function testGetArrayValueCompatibility() {
103
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
104
105
		$this->assertEquals(
106
			// This is the serialization format from when the EntityIdValue was still together with EntityId.
107
			array(
108
				'entity-type' => 'item',
109
				'numeric-id' => 31337,
110
			),
111
			$id->getArrayValue()
112
		);
113
	}
114
115
	public function testNewFromArrayCompatibility() {
116
		$id = new EntityIdValue( new ItemId( 'Q31337' ) );
117
118
		$this->assertEquals(
119
			$id,
120
			EntityIdValue::newFromArray(
121
				// This is the serialization format from when the EntityIdValue was still together with EntityId.
122
				array(
123
					'entity-type' => 'item',
124
					'numeric-id' => 31337,
125
				)
126
			)
127
		);
128
	}
129
130
	/**
131
	 * @dataProvider invalidArrayProvider
132
	 */
133
	public function testCannotDeserializeInvalidSerialization( $invalidArray ) {
134
		$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...
135
136
		EntityIdValue::newFromArray( $invalidArray );
137
	}
138
139
	public function invalidArrayProvider() {
140
		return array(
141
			array( null ),
142
143
			array( 'foo' ),
144
145
			array( array() ),
146
147
			array( array(
148
				'entity-type' => 'item',
149
			) ),
150
151
			array( array(
152
				'numeric-id' => 42,
153
			) ),
154
155
			array( array(
156
				'entity-type' => 'foo',
157
				'numeric-id' => 42,
158
			) ),
159
160
			array( array(
161
				'entity-type' => 42,
162
				'numeric-id' => 42,
163
			) ),
164
165
			array( array(
166
				'entity-type' => 'item',
167
				'numeric-id' => -1,
168
			) ),
169
170
			array( array(
171
				'entity-type' => 'item',
172
				'numeric-id' => 'foo',
173
			) ),
174
		);
175
	}
176
177
}
178