Completed
Push — suchids ( 874a83 )
by Jeroen De
03:40
created

ItemIdTest::serializationProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
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( 'Q2147483647', 'Q2147483647' ),
42
		);
43
	}
44
45
	/**
46
	 * @dataProvider invalidIdSerializationProvider
47
	 */
48
	public function testCannotConstructWithInvalidSerialization( $invalidSerialization ) {
49
		$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...
50
		new ItemId( $invalidSerialization );
51
	}
52
53
	public function invalidIdSerializationProvider() {
54
		return array(
55
			array( "Q1\n" ),
56
			array( 'q' ),
57
			array( 'p1' ),
58
			array( 'qq1' ),
59
			array( '1q' ),
60
			array( 'q01' ),
61
			array( 'q 1' ),
62
			array( ' q1' ),
63
			array( 'q1 ' ),
64
			array( '1' ),
65
			array( ' ' ),
66
			array( '' ),
67
			array( '0' ),
68
			array( 0 ),
69
			array( 1 ),
70
			array( 'Q2147483648' ),
71
			array( 'Q99999999999' ),
72
		);
73
	}
74
75
	public function testGetNumericId() {
76
		$id = new ItemId( 'Q1' );
77
		$this->assertSame( 1, $id->getNumericId() );
78
	}
79
80
	public function testGetEntityType() {
81
		$id = new ItemId( 'Q1' );
82
		$this->assertSame( 'item', $id->getEntityType() );
83
	}
84
85
	public function testSerialize() {
86
		$id = new ItemId( 'Q1' );
87
		$this->assertSame( '["item","Q1"]', $id->serialize() );
88
	}
89
90
	/**
91
	 * @dataProvider serializationProvider
92
	 */
93
	public function testUnserialize( $json, $expected ) {
94
		$id = new ItemId( 'Q1' );
95
		$id->unserialize( $json );
96
		$this->assertSame( $expected, $id->getSerialization() );
97
	}
98
99
	public function serializationProvider() {
100
		return array(
101
			array( '["item","Q2"]', 'Q2' ),
102
103
			// All these cases are kind of an injection vector and allow constructing invalid ids.
104
			array( '["string","Q2"]', 'Q2' ),
105
			array( '["","string"]', 'string' ),
106
			array( '["",""]', '' ),
107
			array( '["",2]', 2 ),
108
			array( '["",null]', null ),
109
		);
110
	}
111
112
	/**
113
	 * @dataProvider numericIdProvider
114
	 */
115
	public function testNewFromNumber( $number ) {
116
		$id = ItemId::newFromNumber( $number );
117
		$this->assertEquals( 'Q' . $number, $id->getSerialization() );
118
	}
119
120
	public function numericIdProvider() {
121
		return array(
122
			array( 42 ),
123
			array( '42' ),
124
			array( 42.0 ),
125
			// Check for 32-bit integer overflow on 32-bit PHP systems.
126
			array( 2147483647 ),
127
			array( '2147483647' ),
128
		);
129
	}
130
131
	/**
132
	 * @dataProvider invalidNumericIdProvider
133
	 */
134
	public function testNewFromNumberWithInvalidNumericId( $number ) {
135
		$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...
136
		ItemId::newFromNumber( $number );
137
	}
138
139
	public function invalidNumericIdProvider() {
140
		return array(
141
			array( 'Q1' ),
142
			array( '42.1' ),
143
			array( 42.1 ),
144
			array( 2147483648 ),
145
			array( '2147483648' ),
146
		);
147
	}
148
149
	public function testGetNumericIdThrowsExceptionOnForeignIds() {
150
		$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...
151
		( new ItemId( 'Q42', 'foo' ) )->getNumericId();
152
	}
153
154
	public function testIsEqual() {
155
		$this->assertTrue( ( new ItemId( 'Q1' ) )->equals( new ItemId( 'Q1' ) ) );
156
		$this->assertFalse( ( new ItemId( 'Q1' ) )->equals( new ItemId( 'Q2' ) ) );
157
	}
158
159
}
160