1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Wikibase\DataModel\Deserializers; |
4
|
|
|
|
5
|
|
|
use PHPUnit_Framework_TestCase; |
6
|
|
|
use Wikibase\DataModel\Deserializers\EntityIdDeserializer; |
7
|
|
|
use Wikibase\DataModel\Entity\EntityIdParsingException; |
8
|
|
|
use Wikibase\DataModel\Entity\ItemId; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers Wikibase\DataModel\Deserializers\EntityIdDeserializer |
12
|
|
|
* |
13
|
|
|
* @licence GNU GPL v2+ |
14
|
|
|
* @author Thomas Pellissier Tanon |
15
|
|
|
*/ |
16
|
|
|
class EntityIdDeserializerTest extends PHPUnit_Framework_TestCase { |
17
|
|
|
|
18
|
|
|
private function buildDeserializer() { |
19
|
|
|
$entityIdParserMock = $this->getMock( '\Wikibase\DataModel\Entity\EntityIdParser' ); |
20
|
|
|
$entityIdParserMock->expects( $this->any() ) |
21
|
|
|
->method( 'parse' ) |
22
|
|
|
->with( $this->equalTo( 'Q42' ) ) |
23
|
|
|
->will( $this->returnValue( new ItemId( 'Q42' ) ) ); |
24
|
|
|
|
25
|
|
|
return new EntityIdDeserializer( $entityIdParserMock ); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @dataProvider nonDeserializableProvider |
30
|
|
|
*/ |
31
|
|
|
public function testDeserializeThrowsDeserializationException( $nonDeserializable ) { |
32
|
|
|
$deserializer = $this->buildDeserializer(); |
33
|
|
|
$this->setExpectedException( 'Deserializers\Exceptions\DeserializationException' ); |
34
|
|
|
$deserializer->deserialize( $nonDeserializable ); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function nonDeserializableProvider() { |
38
|
|
|
return array( |
39
|
|
|
array( |
40
|
|
|
42 |
41
|
|
|
), |
42
|
|
|
array( |
43
|
|
|
array() |
44
|
|
|
), |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @dataProvider deserializationProvider |
50
|
|
|
*/ |
51
|
|
|
public function testDeserialization( $object, $serialization ) { |
52
|
|
|
$deserializer = $this->buildDeserializer(); |
53
|
|
|
$this->assertEquals( $object, $deserializer->deserialize( $serialization ) ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function deserializationProvider() { |
57
|
|
|
return array( |
58
|
|
|
array( |
59
|
|
|
new ItemId( 'Q42' ), |
60
|
|
|
'Q42' |
61
|
|
|
), |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function testDeserializeWithEntityIdParsingException() { |
66
|
|
|
$entityIdParserMock = $this->getMock( '\Wikibase\DataModel\Entity\EntityIdParser' ); |
67
|
|
|
$entityIdParserMock->expects( $this->any() ) |
68
|
|
|
->method( 'parse' ) |
69
|
|
|
->will( $this->throwException( new EntityIdParsingException() ) ); |
70
|
|
|
$entityIdDeserializer = new EntityIdDeserializer( $entityIdParserMock ); |
71
|
|
|
|
72
|
|
|
$this->setExpectedException( '\Deserializers\Exceptions\DeserializationException' ); |
73
|
|
|
$entityIdDeserializer->deserialize( 'test' ); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
} |
77
|
|
|
|