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