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 ); |
|
|
|
|
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 testGetEntityType() { |
85
|
|
|
$id = new PropertyId( 'P1' ); |
86
|
|
|
$this->assertSame( 'property', $id->getEntityType() ); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testSerialize() { |
90
|
|
|
$id = new PropertyId( 'P1' ); |
91
|
|
|
$this->assertSame( '["property","P1"]', $id->serialize() ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @dataProvider serializationProvider |
96
|
|
|
*/ |
97
|
|
|
public function testUnserialize( $json, $expected ) { |
98
|
|
|
$id = new PropertyId( 'P1' ); |
99
|
|
|
$id->unserialize( $json ); |
100
|
|
|
$this->assertSame( $expected, $id->getSerialization() ); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function serializationProvider() { |
104
|
|
|
return [ |
105
|
|
|
[ '["property","P2"]', 'P2' ], |
106
|
|
|
|
107
|
|
|
// All these cases are kind of an injection vector and allow constructing invalid ids. |
108
|
|
|
[ '["string","P2"]', 'P2' ], |
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 = PropertyId::newFromNumber( $number ); |
122
|
|
|
$this->assertEquals( 'P' . $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 ); |
|
|
|
|
141
|
|
|
PropertyId::newFromNumber( $number ); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
public function invalidNumericIdProvider() { |
145
|
|
|
return [ |
146
|
|
|
[ 'P1' ], |
147
|
|
|
[ '42.1' ], |
148
|
|
|
[ 42.1 ], |
149
|
|
|
[ 2147483648 ], |
150
|
|
|
[ '2147483648' ], |
151
|
|
|
]; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
public function testGetNumericIdThrowsExceptionOnForeignIds() { |
155
|
|
|
$this->setExpectedException( RuntimeException::class ); |
|
|
|
|
156
|
|
|
( new PropertyId( 'foo:P42' ) )->getNumericId(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
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.