Completed
Push — 6.x ( 5dcd80...586d48 )
by Leszek
06:19 queued 03:46
created

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