1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Tests\DataValues\Deserializers; |
4
|
|
|
|
5
|
|
|
use DataValues\BooleanValue; |
6
|
|
|
use DataValues\Deserializers\DataValueDeserializer; |
7
|
|
|
use DataValues\NumberValue; |
8
|
|
|
use DataValues\StringValue; |
9
|
|
|
use Deserializers\Exceptions\DeserializationException; |
10
|
|
|
use Deserializers\Exceptions\MissingAttributeException; |
11
|
|
|
use Deserializers\Exceptions\MissingTypeException; |
12
|
|
|
use Deserializers\Exceptions\UnsupportedTypeException; |
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use PHPUnit_Framework_TestCase; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @covers DataValues\Deserializers\DataValueDeserializer |
18
|
|
|
* |
19
|
|
|
* @license GPL-2.0+ |
20
|
|
|
* @author Jeroen De Dauw < [email protected] > |
21
|
|
|
*/ |
22
|
|
|
class DataValueDeserializerTest extends PHPUnit_Framework_TestCase { |
23
|
|
|
|
24
|
|
|
public function testGivenEmptyArray_isDeserializerForReturnsFalse() { |
25
|
|
|
$deserializer = $this->newDeserializer(); |
26
|
|
|
$this->assertFalse( $deserializer->isDeserializerFor( array() ) ); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
private function newDeserializer() { |
30
|
|
|
return new DataValueDeserializer( array( |
31
|
|
|
'boolean' => function( $bool ) { |
32
|
|
|
return new BooleanValue( $bool ); |
33
|
|
|
}, |
34
|
|
|
'number' => 'DataValues\NumberValue', |
35
|
|
|
'string' => 'DataValues\StringValue', |
36
|
|
|
) ); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @dataProvider notAnArrayProvider |
41
|
|
|
*/ |
42
|
|
|
public function testGivenNonArray_isDeserializerForReturnsFalse( $notAnArray ) { |
43
|
|
|
$deserializer = $this->newDeserializer(); |
44
|
|
|
$this->assertFalse( $deserializer->isDeserializerFor( $notAnArray ) ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function notAnArrayProvider() { |
48
|
|
|
return array( |
49
|
|
|
array( null ), |
50
|
|
|
array( 0 ), |
51
|
|
|
array( true ), |
52
|
|
|
array( (object)array() ), |
53
|
|
|
array( 'foo' ), |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @dataProvider notADataValuesListProvider |
59
|
|
|
*/ |
60
|
|
|
public function testGivenNonDataValues_constructorThrowsException( array $invalidDVList ) { |
61
|
|
|
$this->setExpectedException( InvalidArgumentException::class ); |
62
|
|
|
|
63
|
|
|
new DataValueDeserializer( $invalidDVList ); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function notADataValuesListProvider() { |
67
|
|
|
return array( |
68
|
|
|
array( |
69
|
|
|
array( |
70
|
|
|
'foo', |
71
|
|
|
null, |
72
|
|
|
array(), |
73
|
|
|
true, |
74
|
|
|
42, |
75
|
|
|
) |
76
|
|
|
), |
77
|
|
|
array( |
78
|
|
|
array( |
79
|
|
|
'string' => 'foo', |
80
|
|
|
) |
81
|
|
|
), |
82
|
|
|
array( |
83
|
|
|
array( |
84
|
|
|
'string' => 'DataValues\StringValue', |
85
|
|
|
'number' => 42, |
86
|
|
|
) |
87
|
|
|
), |
88
|
|
|
array( |
89
|
|
|
array( |
90
|
|
|
'string' => 'DataValues\StringValue', |
91
|
|
|
'object' => 'stdClass', |
92
|
|
|
) |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testGivenSerializationNoType_deserializeThrowsException() { |
98
|
|
|
$deserializer = $this->newDeserializer(); |
99
|
|
|
|
100
|
|
|
$this->setExpectedException( MissingTypeException::class ); |
101
|
|
|
$deserializer->deserialize( array() ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function testGivenSerializationWithUnknownType_deserializeThrowsException() { |
105
|
|
|
$deserializer = $this->newDeserializer(); |
106
|
|
|
|
107
|
|
|
$this->setExpectedException( UnsupportedTypeException::class ); |
108
|
|
|
$deserializer->deserialize( |
109
|
|
|
array( |
110
|
|
|
'type' => 'ohi' |
111
|
|
|
) |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function testGivenSerializationWithNoValue_deserializeThrowsException() { |
116
|
|
|
$deserializer = $this->newDeserializer(); |
117
|
|
|
|
118
|
|
|
$this->setExpectedException( MissingAttributeException::class ); |
119
|
|
|
$deserializer->deserialize( |
120
|
|
|
array( |
121
|
|
|
'type' => 'number' |
122
|
|
|
) |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @dataProvider invalidDataValueSerializationProvider |
128
|
|
|
*/ |
129
|
|
|
public function testGivenInvalidDataValue_deserializeThrowsException( $invalidSerialization ) { |
130
|
|
|
$deserializer = $this->newDeserializer(); |
131
|
|
|
|
132
|
|
|
$this->setExpectedException( DeserializationException::class ); |
133
|
|
|
$deserializer->deserialize( $invalidSerialization ); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function invalidDataValueSerializationProvider() { |
137
|
|
|
return [ |
138
|
|
|
[ 'foo' ], |
139
|
|
|
[ null ], |
140
|
|
|
[ [] ], |
141
|
|
|
[ [ 'hax' ] ], |
142
|
|
|
[ [ 'type' => 'hax' ] ], |
143
|
|
|
[ [ 'type' => 'number', 'value' => [] ] ], |
144
|
|
|
[ [ 'type' => 'boolean', 'value' => 'not a boolean' ] ], |
145
|
|
|
]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function testInvalidValueSerialization_throwsDeserializationException() { |
149
|
|
|
$serialization = array( |
150
|
|
|
'value' => array( 0, 0 ), |
151
|
|
|
'type' => 'string', |
152
|
|
|
'error' => 'omg an error!' |
153
|
|
|
); |
154
|
|
|
|
155
|
|
|
$deserializer = $this->newDeserializer(); |
156
|
|
|
$this->setExpectedException( DeserializationException::class ); |
157
|
|
|
$deserializer->deserialize( $serialization ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @dataProvider dataValueSerializationProvider |
162
|
|
|
*/ |
163
|
|
|
public function testGivenDataValueSerialization_isDeserializerForReturnsTrue( $dvSerialization ) { |
164
|
|
|
$deserializer = $this->newDeserializer(); |
165
|
|
|
$this->assertTrue( $deserializer->isDeserializerFor( $dvSerialization ) ); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function dataValueSerializationProvider() { |
169
|
|
|
$boolean = new BooleanValue( false ); |
170
|
|
|
$string = new StringValue( 'foo bar baz' ); |
171
|
|
|
$number = new NumberValue( 42 ); |
172
|
|
|
|
173
|
|
|
return array( |
174
|
|
|
array( $boolean->toArray(), 'boolean' ), |
175
|
|
|
array( $string->toArray(), 'string' ), |
176
|
|
|
array( $number->toArray(), 'number' ), |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @dataProvider dataValueSerializationProvider |
182
|
|
|
*/ |
183
|
|
|
public function testGivenDataValueSerialization_deserializeReturnsDataValue( $dvSerialization, $expectedType ) { |
184
|
|
|
$deserializer = $this->newDeserializer(); |
185
|
|
|
|
186
|
|
|
$dataValue = $deserializer->deserialize( $dvSerialization ); |
187
|
|
|
|
188
|
|
|
$this->assertInstanceOf( 'DataValues\DataValue', $dataValue ); |
189
|
|
|
$this->assertEquals( $expectedType, $dataValue->getType() ); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
} |
193
|
|
|
|