Test Failed
Push — T146030 ( 69731c...ad7786 )
by Leszek
05:52
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
8
/**
9
 * @covers Wikibase\DataModel\Entity\ItemId
10
 * @covers Wikibase\DataModel\Entity\EntityId
11
 *
12
 * @group Wikibase
13
 * @group WikibaseDataModel
14
 * @group EntityIdTest
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 array(
35
			array( 'q1', 'Q1' ),
36
			array( 'q100', 'Q100' ),
37
			array( 'q1337', 'Q1337' ),
38
			array( 'q31337', 'Q31337' ),
39
			array( 'Q31337', 'Q31337' ),
40
			array( 'Q42', 'Q42' ),
41
			array( ':Q42', 'Q42' ),
42
			array( 'foo:Q42', 'foo:Q42' ),
43
			array( 'foo:bar:q42', 'foo:bar:Q42' ),
44
			array( 'Q42', 'Q42' ),
45
			array( 'Q2147483647', 'Q2147483647' ),
46
		);
47
	}
48
49
	/**
50
	 * @dataProvider invalidIdSerializationProvider
51
	 */
52
	public function testCannotConstructWithInvalidSerialization( $invalidSerialization ) {
53
		$this->setExpectedException( 'InvalidArgumentException' );
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 array(
59
			array( "Q1\n" ),
60
			array( 'q' ),
61
			array( 'p1' ),
62
			array( 'qq1' ),
63
			array( '1q' ),
64
			array( 'q01' ),
65
			array( 'q 1' ),
66
			array( ' q1' ),
67
			array( 'q1 ' ),
68
			array( '1' ),
69
			array( ' ' ),
70
			array( '' ),
71
			array( '0' ),
72
			array( 0 ),
73
			array( 1 ),
74
			array( 'Q2147483648' ),
75
			array( '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 array(
105
			array( '["item","Q2"]', 'Q2' ),
106
107
			// All these cases are kind of an injection vector and allow constructing invalid ids.
108
			array( '["string","Q2"]', 'Q2' ),
109
			array( '["","string"]', 'string' ),
110
			array( '["",""]', '' ),
111
			array( '["",2]', 2 ),
112
			array( '["",null]', null ),
113
			array( '', 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 array(
127
			array( 42 ),
128
			array( '42' ),
129
			array( 42.0 ),
130
			// Check for 32-bit integer overflow on 32-bit PHP systems.
131
			array( 2147483647 ),
132
			array( '2147483647' ),
133
		);
134
	}
135
136
	/**
137
	 * @dataProvider invalidNumericIdProvider
138
	 */
139
	public function testNewFromNumberWithInvalidNumericId( $number ) {
140
		$this->setExpectedException( 'InvalidArgumentException' );
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 array(
146
			array( 'Q1' ),
147
			array( '42.1' ),
148
			array( 42.1 ),
149
			array( 2147483648 ),
150
			array( '2147483648' ),
151
		);
152
	}
153
154
	public function testGetNumericIdThrowsExceptionOnForeignIds() {
155
		$this->setExpectedException( 'RuntimeException' );
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