1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\Wikibase\InternalSerialization\Deserializers; |
4
|
|
|
|
5
|
|
|
use Deserializers\Deserializer; |
6
|
|
|
use Deserializers\Exceptions\DeserializationException; |
7
|
|
|
use Wikibase\InternalSerialization\Deserializers\EntityDeserializer; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @covers Wikibase\InternalSerialization\Deserializers\EntityDeserializer |
11
|
|
|
* |
12
|
|
|
* @licence GNU GPL v2+ |
13
|
|
|
* @author Jeroen De Dauw < [email protected] > |
14
|
|
|
*/ |
15
|
|
|
class EntityDeserializerTest extends \PHPUnit_Framework_TestCase { |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Deserializer |
19
|
|
|
*/ |
20
|
|
|
private $deserializer; |
21
|
|
|
|
22
|
|
|
protected function setUp() { |
23
|
|
|
$this->deserializer = new EntityDeserializer( |
24
|
|
|
$this->getStubLegacyDeserializer(), |
25
|
|
|
$this->getStubCurrentDeserializer() |
26
|
|
|
); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
private function getStubLegacyDeserializer() { |
30
|
|
|
$legacyDeserializer = $this->getMock( 'Deserializers\DispatchableDeserializer' ); |
31
|
|
|
|
32
|
|
|
$legacyDeserializer->expects( $this->any() ) |
33
|
|
|
->method( 'isDeserializerFor' ) |
34
|
|
|
->will( $this->returnCallback( function( $serialization ) { |
35
|
|
|
return array_key_exists( 'entity', $serialization ); |
36
|
|
|
} ) ); |
37
|
|
|
|
38
|
|
|
$legacyDeserializer->expects( $this->any() ) |
39
|
|
|
->method( 'deserialize' ) |
40
|
|
|
->will( $this->returnValue( 'legacy' ) ); |
41
|
|
|
|
42
|
|
|
return $legacyDeserializer; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function getStubCurrentDeserializer() { |
46
|
|
|
$currentDeserializer = $this->getMock( 'Deserializers\DispatchableDeserializer' ); |
47
|
|
|
|
48
|
|
|
$currentDeserializer->expects( $this->any() ) |
49
|
|
|
->method( 'isDeserializerFor' ) |
50
|
|
|
->will( $this->returnCallback( function( $serialization ) { |
51
|
|
|
return array_key_exists( 'id', $serialization ); |
52
|
|
|
} ) ); |
53
|
|
|
|
54
|
|
|
$currentDeserializer->expects( $this->any() ) |
55
|
|
|
->method( 'deserialize' ) |
56
|
|
|
->will( $this->returnValue( 'current' ) ); |
57
|
|
|
|
58
|
|
|
return $currentDeserializer; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function testGivenLegacySerialization_legacyIsDetected() { |
62
|
|
|
$returnValue = $this->deserializer->deserialize( array( 'entity' => array( 'item', 1 ) ) ); |
63
|
|
|
$this->assertEquals( 'legacy', $returnValue ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testGivenCurrentSerialization_currentIsDetected() { |
67
|
|
|
$returnValue = $this->deserializer->deserialize( array( 'id' => 'Q1' ) ); |
68
|
|
|
$this->assertEquals( 'current', $returnValue ); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
private function getThrowingDeserializer() { |
72
|
|
|
$currentDeserializer = $this->getMock( 'Deserializers\DispatchableDeserializer' ); |
73
|
|
|
|
74
|
|
|
$currentDeserializer->expects( $this->any() ) |
75
|
|
|
->method( 'deserialize' ) |
76
|
|
|
->will( $this->throwException( new DeserializationException() ) ); |
77
|
|
|
|
78
|
|
|
return $currentDeserializer; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function testLegacySerializationWithoutId_exceptionIsThrown() { |
82
|
|
|
$deserializer = new EntityDeserializer( |
83
|
|
|
$this->getStubLegacyDeserializer(), |
84
|
|
|
$this->getThrowingDeserializer() |
85
|
|
|
); |
86
|
|
|
|
87
|
|
|
$this->setExpectedException( 'Deserializers\Exceptions\DeserializationException' ); |
88
|
|
|
$deserializer->deserialize( $this->getLegacyItemSerializationWithoutId() ); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function getLegacyItemSerializationWithoutId() { |
92
|
|
|
return array( 'aliases' => array( |
93
|
|
|
'en' => array( 'foo', 'bar' ) |
94
|
|
|
) ); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testCurrentSerializationWithoutId_exceptionIsThrown() { |
98
|
|
|
$deserializer = new EntityDeserializer( |
99
|
|
|
$this->getThrowingDeserializer(), |
100
|
|
|
$this->getStubCurrentDeserializer() |
101
|
|
|
); |
102
|
|
|
|
103
|
|
|
$this->setExpectedException( 'Deserializers\Exceptions\DeserializationException' ); |
104
|
|
|
$deserializer->deserialize( $this->getCurrentItemSerializationWithoutId() ); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
private function getCurrentItemSerializationWithoutId() { |
108
|
|
|
return array( 'aliases' => array( |
109
|
|
|
'en' => array( |
110
|
|
|
array( |
111
|
|
|
'language' => 'en', |
112
|
|
|
'value' => 'foo', |
113
|
|
|
), |
114
|
|
|
array( |
115
|
|
|
'language' => 'en', |
116
|
|
|
'value' => 'bar', |
117
|
|
|
), |
118
|
|
|
) |
119
|
|
|
) ); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @dataProvider invalidSerializationProvider |
124
|
|
|
*/ |
125
|
|
|
public function testGivenInvalidSerialization_exceptionIsThrown( $serialization ) { |
126
|
|
|
$deserializer = new EntityDeserializer( |
127
|
|
|
$this->getThrowingDeserializer(), |
128
|
|
|
$this->getThrowingDeserializer() |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
$this->setExpectedException( 'Deserializers\Exceptions\DeserializationException' ); |
132
|
|
|
$deserializer->deserialize( $serialization ); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function invalidSerializationProvider() { |
136
|
|
|
return array( |
137
|
|
|
array( null ), |
138
|
|
|
array( 5 ), |
139
|
|
|
array( array() ), |
140
|
|
|
array( array( 'entity' => 'P42', 'datatype' => null ) ), |
141
|
|
|
); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
} |
145
|
|
|
|