Completed
Push — master ( 269f49...8b2bf2 )
by Jeroen De
45s
created

PropertyIdTest::testGetNumericId()   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\PropertyId;
7
use InvalidArgumentException;
8
use RuntimeException;
9
10
/**
11
 * @covers Wikibase\DataModel\Entity\PropertyId
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 PropertyIdTest extends PHPUnit_Framework_TestCase {
21
22
	/**
23
	 * @dataProvider idSerializationProvider
24
	 */
25
	public function testCanConstructId( $idSerialization, $normalizedIdSerialization ) {
26
		$id = new PropertyId( $idSerialization );
27
28
		$this->assertEquals(
29
			$normalizedIdSerialization,
30
			$id->getSerialization()
31
		);
32
	}
33
34
	public function idSerializationProvider() {
35
		return [
36
			[ 'p1', 'P1' ],
37
			[ 'p100', 'P100' ],
38
			[ 'p1337', 'P1337' ],
39
			[ 'p31337', 'P31337' ],
40
			[ 'P31337', 'P31337' ],
41
			[ 'P42', 'P42' ],
42
			[ ':P42', 'P42' ],
43
			[ 'foo:P42', 'foo:P42' ],
44
			[ 'foo:bar:p42', 'foo:bar:P42' ],
45
			[ 'P2147483647', 'P2147483647' ],
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 PropertyId( $invalidSerialization );
55
	}
56
57
	public function invalidIdSerializationProvider() {
58
		return [
59
			[ "P1\n" ],
60
			[ 'p' ],
61
			[ 'q1' ],
62
			[ 'pp1' ],
63
			[ '1p' ],
64
			[ 'p01' ],
65
			[ 'p 1' ],
66
			[ ' p1' ],
67
			[ 'p1 ' ],
68
			[ '1' ],
69
			[ ' ' ],
70
			[ '' ],
71
			[ '0' ],
72
			[ 0 ],
73
			[ 1 ],
74
			[ 'P2147483648' ],
75
			[ 'P99999999999' ],
76
		];
77
	}
78
79
	public function testGetNumericId() {
80
		$id = new PropertyId( 'P1' );
81
		$this->assertSame( 1, $id->getNumericId() );
82
	}
83
84
	public function testGetNumericId_foreignId() {
85
		$id = new PropertyId( 'foo:P1' );
86
		$this->assertSame( 1, $id->getNumericId() );
87
	}
88
89
	public function testGetEntityType() {
90
		$id = new PropertyId( 'P1' );
91
		$this->assertSame( 'property', $id->getEntityType() );
92
	}
93
94
	public function testSerialize() {
95
		$id = new PropertyId( 'P1' );
96
		$this->assertSame( '["property","P1"]', $id->serialize() );
97
	}
98
99
	/**
100
	 * @dataProvider serializationProvider
101
	 */
102
	public function testUnserialize( $json, $expected ) {
103
		$id = new PropertyId( 'P1' );
104
		$id->unserialize( $json );
105
		$this->assertSame( $expected, $id->getSerialization() );
106
	}
107
108
	public function serializationProvider() {
109
		return [
110
			[ '["property","P2"]', 'P2' ],
111
112
			// All these cases are kind of an injection vector and allow constructing invalid ids.
113
			[ '["string","P2"]', 'P2' ],
114
			[ '["","string"]', 'string' ],
115
			[ '["",""]', '' ],
116
			[ '["",2]', 2 ],
117
			[ '["",null]', null ],
118
			[ '', null ],
119
		];
120
	}
121
122
	/**
123
	 * @dataProvider numericIdProvider
124
	 */
125
	public function testNewFromNumber( $number ) {
126
		$id = PropertyId::newFromNumber( $number );
127
		$this->assertEquals( 'P' . $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 );
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...
146
		PropertyId::newFromNumber( $number );
147
	}
148
149
	public function invalidNumericIdProvider() {
150
		return [
151
			[ 'P1' ],
152
			[ '42.1' ],
153
			[ 42.1 ],
154
			[ 2147483648 ],
155
			[ '2147483648' ],
156
		];
157
	}
158
159
}
160