1 | <?php |
||
19 | class ItemIdTest extends PHPUnit_Framework_TestCase { |
||
20 | |||
21 | /** |
||
22 | * @dataProvider idSerializationProvider |
||
23 | */ |
||
24 | public function testCanConstructId( $idSerialization, $normalizedIdSerialization ) { |
||
32 | |||
33 | public function idSerializationProvider() { |
||
34 | return [ |
||
35 | [ 'q1', 'Q1' ], |
||
36 | [ 'q100', 'Q100' ], |
||
37 | [ 'q1337', 'Q1337' ], |
||
38 | [ 'q31337', 'Q31337' ], |
||
39 | [ 'Q31337', 'Q31337' ], |
||
40 | [ 'Q42', 'Q42' ], |
||
41 | [ ':Q42', 'Q42' ], |
||
42 | [ 'foo:Q42', 'foo:Q42' ], |
||
43 | [ 'foo:bar:q42', 'foo:bar:Q42' ], |
||
44 | [ 'Q2147483647', 'Q2147483647' ], |
||
45 | ]; |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * @dataProvider invalidIdSerializationProvider |
||
50 | */ |
||
51 | public function testCannotConstructWithInvalidSerialization( $invalidSerialization ) { |
||
52 | $this->setExpectedException( InvalidArgumentException::class ); |
||
53 | new ItemId( $invalidSerialization ); |
||
54 | } |
||
55 | |||
56 | public function invalidIdSerializationProvider() { |
||
57 | return [ |
||
58 | [ "Q1\n" ], |
||
59 | [ 'q' ], |
||
60 | [ 'p1' ], |
||
61 | [ 'qq1' ], |
||
62 | [ '1q' ], |
||
63 | [ 'q01' ], |
||
64 | [ 'q 1' ], |
||
65 | [ ' q1' ], |
||
66 | [ 'q1 ' ], |
||
67 | [ '1' ], |
||
68 | [ ' ' ], |
||
69 | [ '' ], |
||
70 | [ '0' ], |
||
71 | [ 0 ], |
||
72 | [ 1 ], |
||
73 | [ 'Q2147483648' ], |
||
74 | [ 'Q99999999999' ], |
||
75 | ]; |
||
76 | } |
||
77 | |||
78 | public function testGetNumericId() { |
||
79 | $id = new ItemId( 'Q1' ); |
||
80 | $this->assertSame( 1, $id->getNumericId() ); |
||
81 | } |
||
82 | |||
83 | public function testGetNumericId_foreignId() { |
||
84 | $id = new ItemId( 'foo:Q1' ); |
||
85 | $this->assertSame( 1, $id->getNumericId() ); |
||
86 | } |
||
87 | |||
88 | public function testGetEntityType() { |
||
89 | $id = new ItemId( 'Q1' ); |
||
90 | $this->assertSame( 'item', $id->getEntityType() ); |
||
91 | } |
||
92 | |||
93 | public function testSerialize() { |
||
94 | $id = new ItemId( 'Q1' ); |
||
95 | $this->assertSame( 'Q1', $id->serialize() ); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @dataProvider serializationProvider |
||
100 | */ |
||
101 | public function testUnserialize( $json, $expected ) { |
||
102 | $id = new ItemId( 'Q1' ); |
||
103 | $id->unserialize( $json ); |
||
104 | $this->assertSame( $expected, $id->getSerialization() ); |
||
105 | } |
||
106 | |||
107 | public function serializationProvider() { |
||
108 | return [ |
||
109 | [ 'Q2', 'Q2' ], |
||
110 | [ '["item","Q2"]', 'Q2' ], |
||
111 | |||
112 | // All these cases are kind of an injection vector and allow constructing invalid ids. |
||
113 | [ '["string","Q2"]', 'Q2' ], |
||
114 | [ '["","string"]', 'string' ], |
||
115 | [ '["",""]', '' ], |
||
116 | [ '["",2]', 2 ], |
||
117 | [ '["",null]', null ], |
||
118 | [ '', '' ], |
||
119 | ]; |
||
120 | } |
||
121 | |||
122 | /** |
||
123 | * @dataProvider numericIdProvider |
||
124 | */ |
||
125 | public function testNewFromNumber( $number ) { |
||
129 | |||
130 | public function numericIdProvider() { |
||
140 | |||
141 | /** |
||
142 | * @dataProvider invalidNumericIdProvider |
||
143 | */ |
||
144 | public function testNewFromNumberWithInvalidNumericId( $number ) { |
||
148 | |||
149 | public function invalidNumericIdProvider() { |
||
158 | |||
159 | public function testNewFromRepositoryAndNumber() { |
||
160 | $id = ItemId::newFromRepositoryAndNumber( 'foo', 1 ); |
||
161 | $this->assertSame( 'foo:Q1', $id->getSerialization() ); |
||
162 | } |
||
163 | |||
164 | public function testNewFromRepositoryAndNumberWithInvalidNumericId() { |
||
165 | $this->setExpectedException( InvalidArgumentException::class ); |
||
166 | ItemId::newFromRepositoryAndNumber( '', 'Q1' ); |
||
167 | } |
||
168 | |||
169 | } |
||
170 |