1 | <?php |
||
27 | class StatementTest extends \PHPUnit_Framework_TestCase { |
||
28 | |||
29 | public function testMinimalConstructor() { |
||
30 | $mainSnak = new PropertyNoValueSnak( 1 ); |
||
31 | $statement = new Statement( $mainSnak ); |
||
32 | $this->assertTrue( $mainSnak->equals( $statement->getMainSnak() ) ); |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @dataProvider validConstructorArgumentsProvider |
||
37 | */ |
||
38 | public function testConstructorWithValidArguments( |
||
39 | Snak $mainSnak, |
||
40 | SnakList $qualifiers = null, |
||
41 | ReferenceList $references = null, |
||
42 | $guid |
||
43 | ) { |
||
44 | $statement = new Statement( $mainSnak, $qualifiers, $references, $guid ); |
||
45 | $this->assertTrue( $statement->getMainSnak()->equals( $mainSnak ) ); |
||
46 | $this->assertTrue( $statement->getQualifiers()->equals( $qualifiers ?: new SnakList() ) ); |
||
47 | $this->assertTrue( $statement->getReferences()->equals( $references ?: new ReferenceList() ) ); |
||
48 | $this->assertSame( $guid, $statement->getGuid() ); |
||
49 | } |
||
50 | |||
51 | public function validConstructorArgumentsProvider() { |
||
52 | $snak = new PropertyNoValueSnak( 1 ); |
||
53 | $qualifiers = new SnakList( array( $snak ) ); |
||
54 | $references = new ReferenceList( array( new Reference( array( $snak ) ) ) ); |
||
55 | |||
56 | return array( |
||
57 | array( $snak, null, null, null ), |
||
58 | array( $snak, null, null, 'guid' ), |
||
59 | array( $snak, $qualifiers, $references, 'guid' ), |
||
60 | ); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @dataProvider instanceProvider |
||
65 | */ |
||
66 | public function testSetGuid( Statement $statement ) { |
||
70 | |||
71 | /** |
||
72 | * @dataProvider instanceProvider |
||
73 | */ |
||
74 | public function testGetGuid( Statement $statement ) { |
||
75 | $guid = $statement->getGuid(); |
||
76 | $this->assertTrue( $guid === null || is_string( $guid ) ); |
||
77 | $this->assertEquals( $guid, $statement->getGuid() ); |
||
78 | |||
79 | $statement->setGuid( 'foobar' ); |
||
80 | $this->assertEquals( 'foobar', $statement->getGuid() ); |
||
81 | } |
||
82 | |||
83 | public function testSetAndGetMainSnak() { |
||
84 | $mainSnak = new PropertyNoValueSnak( new PropertyId( 'P42' ) ); |
||
85 | $statement = new Statement( $mainSnak ); |
||
86 | $this->assertSame( $mainSnak, $statement->getMainSnak() ); |
||
87 | } |
||
88 | |||
89 | public function testSetAndGetQualifiers() { |
||
90 | $qualifiers = new SnakList( array( |
||
91 | new PropertyValueSnak( new PropertyId( 'P42' ), new StringValue( 'a' ) ) |
||
92 | ) ); |
||
93 | |||
94 | $statement = new Statement( |
||
95 | new PropertyNoValueSnak( new PropertyId( 'P42' ) ), |
||
96 | $qualifiers |
||
97 | ); |
||
98 | |||
99 | $this->assertSame( $qualifiers, $statement->getQualifiers() ); |
||
100 | } |
||
101 | |||
102 | /** |
||
103 | * @dataProvider instanceProvider |
||
104 | */ |
||
105 | public function testSerialize( Statement $statement ) { |
||
106 | $copy = unserialize( serialize( $statement ) ); |
||
107 | |||
108 | $this->assertEquals( $statement->getHash(), $copy->getHash(), 'Serialization roundtrip should not affect hash' ); |
||
109 | } |
||
110 | |||
111 | public function testGuidDoesNotAffectHash() { |
||
112 | $statement0 = new Statement( new PropertyNoValueSnak( 42 ) ); |
||
113 | $statement0->setGuid( 'statement0' ); |
||
114 | |||
115 | $statement1 = new Statement( new PropertyNoValueSnak( 42 ) ); |
||
116 | $statement1->setGuid( 'statement1' ); |
||
117 | |||
118 | $this->assertEquals( $statement0->getHash(), $statement1->getHash() ); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @dataProvider invalidGuidProvider |
||
123 | * @expectedException InvalidArgumentException |
||
124 | */ |
||
125 | public function testGivenInvalidGuid_constructorThrowsException( $guid ) { |
||
128 | |||
129 | /** |
||
130 | * @dataProvider invalidGuidProvider |
||
131 | * @expectedException InvalidArgumentException |
||
132 | */ |
||
133 | public function testGivenInvalidGuid_setGuidThrowsException( $guid ) { |
||
137 | |||
138 | public function invalidGuidProvider() { |
||
139 | $snak = new PropertyNoValueSnak( 1 ); |
||
140 | |||
141 | return array( |
||
142 | array( false ), |
||
143 | array( 1 ), |
||
144 | array( $snak ), |
||
145 | array( new Statement( $snak ) ), |
||
146 | ); |
||
147 | } |
||
148 | |||
149 | public function instanceProvider() { |
||
150 | $instances = array(); |
||
151 | |||
152 | $propertyId = new PropertyId( 'P42' ); |
||
153 | $baseInstance = new Statement( new PropertyNoValueSnak( $propertyId ) ); |
||
154 | |||
155 | $instances[] = $baseInstance; |
||
156 | |||
157 | $instance = clone $baseInstance; |
||
158 | $instance->setRank( Statement::RANK_PREFERRED ); |
||
159 | |||
160 | $instances[] = $instance; |
||
161 | |||
162 | $newInstance = clone $instance; |
||
163 | |||
164 | $instances[] = $newInstance; |
||
165 | |||
166 | $instance = clone $baseInstance; |
||
167 | |||
168 | $instance->setReferences( new ReferenceList( array( |
||
169 | new Reference( array( |
||
170 | new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) ) |
||
171 | ) ) |
||
172 | ) ) ); |
||
173 | |||
174 | $instances[] = $instance; |
||
175 | |||
176 | $argLists = array(); |
||
177 | |||
178 | foreach ( $instances as $instance ) { |
||
179 | $argLists[] = array( $instance ); |
||
180 | } |
||
181 | |||
182 | return $argLists; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @dataProvider instanceProvider |
||
187 | */ |
||
188 | public function testGetReferences( Statement $statement ) { |
||
191 | |||
192 | /** |
||
193 | * @dataProvider instanceProvider |
||
194 | */ |
||
195 | public function testSetReferences( Statement $statement ) { |
||
196 | $references = new ReferenceList( array( |
||
197 | new Reference( array( |
||
198 | new PropertyValueSnak( new PropertyId( 'P1' ), new StringValue( 'a' ) ), |
||
199 | ) ) |
||
200 | ) ); |
||
201 | |||
202 | $statement->setReferences( $references ); |
||
203 | |||
204 | $this->assertEquals( $references, $statement->getReferences() ); |
||
205 | } |
||
206 | |||
207 | /** |
||
208 | * @dataProvider instanceProvider |
||
209 | */ |
||
210 | public function testAddNewReferenceWithVariableArgumentsSyntax( Statement $statement ) { |
||
211 | $snak1 = new PropertyNoValueSnak( 256 ); |
||
212 | $snak2 = new PropertySomeValueSnak( 42 ); |
||
213 | $statement->addNewReference( $snak1, $snak2 ); |
||
214 | |||
215 | $expectedSnaks = array( $snak1, $snak2 ); |
||
216 | $this->assertTrue( $statement->getReferences()->hasReference( new Reference( $expectedSnaks ) ) ); |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @dataProvider instanceProvider |
||
221 | */ |
||
222 | public function testAddNewReferenceWithAnArrayOfSnaks( Statement $statement ) { |
||
223 | $snaks = array( |
||
224 | new PropertyNoValueSnak( 256 ), |
||
225 | new PropertySomeValueSnak( 42 ), |
||
226 | ); |
||
227 | $statement->addNewReference( $snaks ); |
||
228 | |||
229 | $this->assertTrue( $statement->getReferences()->hasReference( new Reference( $snaks ) ) ); |
||
230 | } |
||
231 | |||
232 | /** |
||
233 | * @dataProvider instanceProvider |
||
234 | */ |
||
235 | public function testGetRank( Statement $statement ) { |
||
236 | $rank = $statement->getRank(); |
||
237 | $this->assertInternalType( 'integer', $rank ); |
||
238 | |||
239 | $ranks = array( Statement::RANK_DEPRECATED, Statement::RANK_NORMAL, Statement::RANK_PREFERRED ); |
||
240 | $this->assertTrue( in_array( $rank, $ranks ), true ); |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * @dataProvider instanceProvider |
||
245 | */ |
||
246 | public function testSetRank( Statement $statement ) { |
||
250 | |||
251 | /** |
||
252 | * @dataProvider instanceProvider |
||
253 | */ |
||
254 | public function testSetInvalidRank( Statement $statement ) { |
||
258 | |||
259 | /** |
||
260 | * @dataProvider instanceProvider |
||
261 | */ |
||
262 | public function testGetPropertyId( Statement $statement ) { |
||
263 | $this->assertEquals( |
||
264 | $statement->getMainSnak()->getPropertyId(), |
||
265 | $statement->getPropertyId() |
||
266 | ); |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @dataProvider instanceProvider |
||
271 | */ |
||
272 | public function testGetAllSnaks( Statement $statement ) { |
||
273 | $snaks = $statement->getAllSnaks(); |
||
274 | |||
275 | $c = count( $statement->getQualifiers() ) + 1; |
||
276 | |||
277 | /* @var Reference $reference */ |
||
278 | foreach ( $statement->getReferences() as $reference ) { |
||
279 | $c += count( $reference->getSnaks() ); |
||
280 | } |
||
281 | |||
282 | $this->assertGreaterThanOrEqual( $c, count( $snaks ), 'At least one snak per Qualifier and Reference' ); |
||
283 | } |
||
284 | |||
285 | public function testGivenNonStatement_equalsReturnsFalse() { |
||
286 | $statement = new Statement( new PropertyNoValueSnak( 42 ) ); |
||
287 | |||
288 | $this->assertFalse( $statement->equals( null ) ); |
||
289 | $this->assertFalse( $statement->equals( 42 ) ); |
||
290 | $this->assertFalse( $statement->equals( new \stdClass() ) ); |
||
291 | } |
||
292 | |||
293 | public function testGivenSameStatement_equalsReturnsTrue() { |
||
294 | $statement = new Statement( |
||
295 | new PropertyNoValueSnak( 42 ), |
||
296 | new SnakList( array( |
||
297 | new PropertyNoValueSnak( 1337 ), |
||
298 | ) ), |
||
299 | new ReferenceList( array( |
||
300 | new Reference( array( new PropertyNoValueSnak( 1337 ) ) ), |
||
301 | ) ) |
||
302 | ); |
||
303 | |||
304 | $statement->setGuid( 'kittens' ); |
||
305 | |||
306 | $this->assertTrue( $statement->equals( $statement ) ); |
||
307 | $this->assertTrue( $statement->equals( clone $statement ) ); |
||
308 | } |
||
309 | |||
310 | public function testGivenStatementWithDifferentProperty_equalsReturnsFalse() { |
||
314 | |||
315 | public function testGivenStatementWithDifferentSnakType_equalsReturnsFalse() { |
||
319 | |||
320 | public function testStatementClaimWithDifferentQualifiers_equalsReturnsFalse() { |
||
321 | $statement = new Statement( |
||
322 | new PropertyNoValueSnak( 42 ), |
||
323 | new SnakList( array( |
||
324 | new PropertyNoValueSnak( 1337 ), |
||
325 | ) ) |
||
326 | ); |
||
327 | |||
328 | $differentStatement = new Statement( |
||
329 | new PropertyNoValueSnak( 42 ), |
||
330 | new SnakList( array( |
||
331 | new PropertyNoValueSnak( 32202 ), |
||
332 | ) ) |
||
333 | ); |
||
334 | |||
335 | $this->assertFalse( $statement->equals( $differentStatement ) ); |
||
336 | } |
||
337 | |||
338 | public function testGivenStatementWithDifferentGuids_equalsReturnsFalse() { |
||
339 | $statement = new Statement( new PropertyNoValueSnak( 42 ) ); |
||
340 | |||
341 | $differentStatement = new Statement( new PropertyNoValueSnak( 42 ) ); |
||
342 | $differentStatement->setGuid( 'kittens' ); |
||
343 | |||
344 | $this->assertFalse( $statement->equals( $differentStatement ) ); |
||
345 | } |
||
346 | |||
347 | public function testStatementClaimWithDifferentReferences_equalsReturnsFalse() { |
||
348 | $statement = new Statement( |
||
349 | new PropertyNoValueSnak( 42 ), |
||
350 | new SnakList(), |
||
351 | new ReferenceList( array( |
||
352 | new Reference( array( new PropertyNoValueSnak( 1337 ) ) ), |
||
353 | ) ) |
||
354 | ); |
||
355 | |||
356 | $differentStatement = new Statement( |
||
357 | new PropertyNoValueSnak( 42 ), |
||
358 | new SnakList(), |
||
359 | new ReferenceList( array( |
||
360 | new Reference( array( new PropertyNoValueSnak( 32202 ) ) ), |
||
361 | ) ) |
||
362 | ); |
||
363 | |||
364 | $this->assertFalse( $statement->equals( $differentStatement ) ); |
||
365 | } |
||
366 | |||
367 | public function testEquals() { |
||
368 | $statement = $this->newStatement(); |
||
369 | $target = $this->newStatement(); |
||
370 | |||
371 | $this->assertTrue( $statement->equals( $target ) ); |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * @dataProvider notEqualsProvider |
||
376 | */ |
||
377 | public function testNotEquals( Statement $statement, Statement $target, $message ) { |
||
380 | |||
381 | public function notEqualsProvider() { |
||
403 | |||
404 | private function newStatement() { |
||
405 | $qualifiers = new SnakList( array( new PropertyNoValueSnak( 23 ) ) ); |
||
406 | |||
407 | $statement = new Statement( |
||
408 | new PropertyNoValueSnak( 42 ), |
||
409 | $qualifiers, |
||
410 | new ReferenceList( array( |
||
411 | new Reference( array( new PropertyNoValueSnak( 1337 ) ) ), |
||
412 | ) ) |
||
413 | ); |
||
414 | |||
415 | $statement->setRank( Statement::RANK_NORMAL ); |
||
416 | |||
417 | return $statement; |
||
418 | } |
||
419 | |||
420 | } |
||
421 |
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.