Passed
Push — currentLimits ( 82fe71...f9050f )
by no
03:50
created

PropertyIdTest::testGetNumericId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
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\PropertyId;
7
8
/**
9
 * @covers Wikibase\DataModel\Entity\PropertyId
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 PropertyIdTest extends PHPUnit_Framework_TestCase {
20
21
	/**
22
	 * @dataProvider idSerializationProvider
23
	 */
24
	public function testCanConstructId( $idSerialization ) {
25
		$id = new PropertyId( $idSerialization );
26
27
		$this->assertEquals(
28
			strtoupper( $idSerialization ),
29
			$id->getSerialization()
30
		);
31
	}
32
33
	public function idSerializationProvider() {
34
		return array(
35
			array( 'p1' ),
36
			array( 'p100' ),
37
			array( 'p1337' ),
38
			array( 'p31337' ),
39
			array( 'P31337' ),
40
			array( 'P42' ),
41
			array( 'P2147483648' ),
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 PropertyId( $invalidSerialization );
51
	}
52
53
	public function invalidIdSerializationProvider() {
54
		return array(
55
			array( "P1\n" ),
56
			array( 'p' ),
57
			array( 'q1' ),
58
			array( 'pp1' ),
59
			array( '1p' ),
60
			array( 'p01' ),
61
			array( 'p 1' ),
62
			array( ' p1' ),
63
			array( 'p1 ' ),
64
			array( '1' ),
65
			array( ' ' ),
66
			array( '' ),
67
			array( '0' ),
68
			array( 0 ),
69
			array( 1 ),
70
		);
71
	}
72
73
	public function testGetNumericId() {
74
		$id = new PropertyId( 'P1' );
75
		$this->assertSame( 1, $id->getNumericId() );
76
	}
77
78
	public function testGetEntityType() {
79
		$id = new PropertyId( 'P1' );
80
		$this->assertSame( 'property', $id->getEntityType() );
81
	}
82
83
	public function testSerialize() {
84
		$id = new PropertyId( 'P1' );
85
		$this->assertSame( '["property","P1"]', $id->serialize() );
86
	}
87
88
	/**
89
	 * @dataProvider serializationProvider
90
	 */
91
	public function testUnserialize( $json, $expected ) {
92
		$id = new PropertyId( 'P1' );
93
		$id->unserialize( $json );
94
		$this->assertSame( $expected, $id->getSerialization() );
95
	}
96
97
	public function serializationProvider() {
98
		return array(
99
			array( '["property","P2"]', 'P2' ),
100
101
			// All these cases are kind of an injection vector and allow constructing invalid ids.
102
			array( '["string","P2"]', 'P2' ),
103
			array( '["","string"]', 'string' ),
104
			array( '["",""]', '' ),
105
			array( '["",2]', 2 ),
106
			array( '["",null]', null ),
107
			array( '', null ),
108
		);
109
	}
110
111
	/**
112
	 * @dataProvider numericIdProvider
113
	 */
114
	public function testNewFromNumber( $number ) {
115
		$id = PropertyId::newFromNumber( $number );
116
		$this->assertEquals( 'P' . $number, $id->getSerialization() );
117
	}
118
119
	public function numericIdProvider() {
120
		return array(
121
			array( 42 ),
122
			array( '42' ),
123
			array( 42.0 ),
124
			// Check for 32-bit integer overflow on 32-bit PHP systems.
125
			array( 2147483648 ),
126
			array( '2147483648' ),
127
		);
128
	}
129
130
	/**
131
	 * @dataProvider invalidNumericIdProvider
132
	 */
133
	public function testNewFromNumberWithInvalidNumericId( $number ) {
134
		$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...
135
		PropertyId::newFromNumber( $number );
136
	}
137
138
	public function invalidNumericIdProvider() {
139
		return array(
140
			array( 'P1' ),
141
			array( '42.1' ),
142
			array( 42.1 ),
143
			array( 2147483648.1 ),
144
		);
145
	}
146
147
}
148