1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Wikibase\DataModel\Deserializers; |
4
|
|
|
|
5
|
|
|
use Deserializers\Exceptions\DeserializationException; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Wikibase\DataModel\Deserializers\EntityIdDeserializer; |
8
|
|
|
use Wikibase\DataModel\Entity\EntityIdParser; |
9
|
|
|
use Wikibase\DataModel\Entity\EntityIdParsingException; |
10
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @covers Wikibase\DataModel\Deserializers\EntityIdDeserializer |
14
|
|
|
* |
15
|
|
|
* @license GPL-2.0-or-later |
16
|
|
|
* @author Thomas Pellissier Tanon |
17
|
|
|
*/ |
18
|
|
|
class EntityIdDeserializerTest extends TestCase { |
19
|
|
|
|
20
|
|
|
private function buildDeserializer() { |
21
|
|
|
$entityIdParserMock = $this->getMockBuilder( EntityIdParser::class )->getMock(); |
22
|
|
|
$entityIdParserMock->expects( $this->any() ) |
23
|
|
|
->method( 'parse' ) |
24
|
|
|
->with( $this->equalTo( 'Q42' ) ) |
25
|
|
|
->will( $this->returnValue( new ItemId( 'Q42' ) ) ); |
26
|
|
|
|
27
|
|
|
return new EntityIdDeserializer( $entityIdParserMock ); |
|
|
|
|
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @dataProvider nonDeserializableProvider |
32
|
|
|
*/ |
33
|
|
|
public function testDeserializeThrowsDeserializationException( $nonDeserializable ) { |
34
|
|
|
$deserializer = $this->buildDeserializer(); |
35
|
|
|
|
36
|
|
|
$this->expectException( DeserializationException::class ); |
37
|
|
|
$deserializer->deserialize( $nonDeserializable ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function nonDeserializableProvider() { |
41
|
|
|
return [ |
42
|
|
|
[ |
43
|
|
|
42 |
44
|
|
|
], |
45
|
|
|
[ |
46
|
|
|
[] |
47
|
|
|
], |
48
|
|
|
]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @dataProvider deserializationProvider |
53
|
|
|
*/ |
54
|
|
|
public function testDeserialization( $object, $serialization ) { |
55
|
|
|
$deserializer = $this->buildDeserializer(); |
56
|
|
|
$this->assertEquals( $object, $deserializer->deserialize( $serialization ) ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function deserializationProvider() { |
60
|
|
|
return [ |
61
|
|
|
[ |
62
|
|
|
new ItemId( 'Q42' ), |
63
|
|
|
'Q42' |
64
|
|
|
], |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function testDeserializeWithEntityIdParsingException() { |
69
|
|
|
$entityIdParserMock = $this->getMockBuilder( EntityIdParser::class )->getMock(); |
70
|
|
|
$entityIdParserMock->expects( $this->any() ) |
71
|
|
|
->method( 'parse' ) |
72
|
|
|
->will( $this->throwException( new EntityIdParsingException() ) ); |
73
|
|
|
$entityIdDeserializer = new EntityIdDeserializer( $entityIdParserMock ); |
|
|
|
|
74
|
|
|
|
75
|
|
|
$this->expectException( DeserializationException::class ); |
76
|
|
|
$entityIdDeserializer->deserialize( 'test' ); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: