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

ItemIdTest::testGetEntityType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
namespace Wikibase\DataModel\Tests\Entity;
4
5
use PHPUnit_Framework_TestCase;
6
use Wikibase\DataModel\Entity\ItemId;
7
use InvalidArgumentException;
8
9
/**
10
 * @covers Wikibase\DataModel\Entity\ItemId
11
 * @covers Wikibase\DataModel\Entity\EntityId
12
 *
13
 * @group Wikibase
14
 * @group WikibaseDataModel
15
 *
16
 * @license GPL-2.0+
17
 * @author Jeroen De Dauw < [email protected] >
18
 */
19
class ItemIdTest extends PHPUnit_Framework_TestCase {
20
21
	/**
22
	 * @dataProvider idSerializationProvider
23
	 */
24
	public function testCanConstructId( $idSerialization, $normalizedIdSerialization ) {
25
		$id = new ItemId( $idSerialization );
26
27
		$this->assertEquals(
28
			$normalizedIdSerialization,
29
			$id->getSerialization()
30
		);
31
	}
32
33
	public function idSerializationProvider() {
34
		return [
35
			[ 'q1', 'Q1' ],
36
			[ 'q100', 'Q100' ],
37
			[ 'q1337', 'Q1337' ],
38
			[ 'q31337', 'Q31337' ],
39
			[ 'Q31337', 'Q31337' ],
40
			[ 'Q42', 'Q42' ],
41
			[ ':Q42', 'Q42' ],
42
			[ 'foo:Q42', 'foo:Q42' ],
43
			[ 'foo:bar:q42', 'foo:bar:Q42' ],
44
			[ 'Q2147483647', 'Q2147483647' ],
45
		];
46
	}
47
48
	/**
49
	 * @dataProvider invalidIdSerializationProvider
50
	 */
51
	public function testCannotConstructWithInvalidSerialization( $invalidSerialization ) {
52
		$this->setExpectedException( InvalidArgumentException::class );
53
		new ItemId( $invalidSerialization );
54
	}
55
56
	public function invalidIdSerializationProvider() {
57
		return [
58
			[ "Q1\n" ],
59
			[ 'q' ],
60
			[ 'p1' ],
61
			[ 'qq1' ],
62
			[ '1q' ],
63
			[ 'q01' ],
64
			[ 'q 1' ],
65
			[ ' q1' ],
66
			[ 'q1 ' ],
67
			[ '1' ],
68
			[ ' ' ],
69
			[ '' ],
70
			[ '0' ],
71
			[ 0 ],
72
			[ 1 ],
73
			[ 'Q2147483648' ],
74
			[ 'Q99999999999' ],
75
		];
76
	}
77
78
	public function testGetNumericId() {
79
		$id = new ItemId( 'Q1' );
80
		$this->assertSame( 1, $id->getNumericId() );
81
	}
82
83
	public function testGetNumericId_foreignId() {
84
		$id = new ItemId( 'foo:Q1' );
85
		$this->assertSame( 1, $id->getNumericId() );
86
	}
87
88
	public function testGetEntityType() {
89
		$id = new ItemId( 'Q1' );
90
		$this->assertSame( 'item', $id->getEntityType() );
91
	}
92
93
	public function testSerialize() {
94
		$id = new ItemId( 'Q1' );
95
		$this->assertSame( 'Q1', $id->serialize() );
96
	}
97
98
	/**
99
	 * @dataProvider serializationProvider
100
	 */
101
	public function testUnserialize( $json, $expected ) {
102
		$id = new ItemId( 'Q1' );
103
		$id->unserialize( $json );
104
		$this->assertSame( $expected, $id->getSerialization() );
105
	}
106
107
	public function serializationProvider() {
108
		return [
109
			[ 'Q2', 'Q2' ],
110
			[ '["item","Q2"]', 'Q2' ],
111
112
			// All these cases are kind of an injection vector and allow constructing invalid ids.
113
			[ '["string","Q2"]', 'Q2' ],
114
			[ '["","string"]', 'string' ],
115
			[ '["",""]', '' ],
116
			[ '["",2]', 2 ],
117
			[ '["",null]', null ],
118
			[ '', '' ],
119
		];
120
	}
121
122
	/**
123
	 * @dataProvider numericIdProvider
124
	 */
125
	public function testNewFromNumber( $number ) {
126
		$id = ItemId::newFromNumber( $number );
127
		$this->assertEquals( 'Q' . $number, $id->getSerialization() );
128
	}
129
130
	public function numericIdProvider() {
131
		return [
132
			[ 42 ],
133
			[ '42' ],
134
			[ 42.0 ],
135
			// Check for 32-bit integer overflow on 32-bit PHP systems.
136
			[ 2147483647 ],
137
			[ '2147483647' ],
138
		];
139
	}
140
141
	/**
142
	 * @dataProvider invalidNumericIdProvider
143
	 */
144
	public function testNewFromNumberWithInvalidNumericId( $number ) {
145
		$this->setExpectedException( InvalidArgumentException::class );
146
		ItemId::newFromNumber( $number );
147
	}
148
149
	public function invalidNumericIdProvider() {
150
		return [
151
			[ 'Q1' ],
152
			[ '42.1' ],
153
			[ 42.1 ],
154
			[ 2147483648 ],
155
			[ '2147483648' ],
156
		];
157
	}
158
159
	public function testNewFromRepositoryAndNumber() {
160
		$id = ItemId::newFromRepositoryAndNumber( 'foo', 1 );
161
		$this->assertSame( 'foo:Q1', $id->getSerialization() );
162
	}
163
164
	public function testNewFromRepositoryAndNumberWithInvalidNumericId() {
165
		$this->setExpectedException( InvalidArgumentException::class );
166
		ItemId::newFromRepositoryAndNumber( '', 'Q1' );
167
	}
168
169
}
170