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( [] ) ); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function newDeserializer() { |
31
|
|
|
return new DataValueDeserializer( [ |
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 [ |
50
|
|
|
[ null ], |
51
|
|
|
[ 0 ], |
52
|
|
|
[ true ], |
53
|
|
|
[ new \stdClass() ], |
54
|
|
|
[ '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 [ |
69
|
|
|
[ |
70
|
|
|
[ |
71
|
|
|
'foo', |
72
|
|
|
null, |
73
|
|
|
[], |
74
|
|
|
true, |
75
|
|
|
42, |
76
|
|
|
] |
77
|
|
|
], |
78
|
|
|
[ |
79
|
|
|
[ |
80
|
|
|
'string' => 'foo', |
81
|
|
|
] |
82
|
|
|
], |
83
|
|
|
[ |
84
|
|
|
[ |
85
|
|
|
'string' => StringValue::class, |
86
|
|
|
'number' => 42, |
87
|
|
|
] |
88
|
|
|
], |
89
|
|
|
[ |
90
|
|
|
[ |
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( [] ); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function testGivenSerializationWithUnknownType_deserializeThrowsException() { |
106
|
|
|
$deserializer = $this->newDeserializer(); |
107
|
|
|
|
108
|
|
|
$this->setExpectedException( UnsupportedTypeException::class ); |
109
|
|
|
$deserializer->deserialize( [ 'type' => 'ohi' ] ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testGivenSerializationWithNoValue_deserializeThrowsException() { |
113
|
|
|
$deserializer = $this->newDeserializer(); |
114
|
|
|
|
115
|
|
|
$this->setExpectedException( MissingAttributeException::class ); |
116
|
|
|
$deserializer->deserialize( [ 'type' => 'number' ] ); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @dataProvider invalidDataValueSerializationProvider |
121
|
|
|
*/ |
122
|
|
|
public function testGivenInvalidDataValue_deserializeThrowsException( $invalidSerialization ) { |
123
|
|
|
$deserializer = $this->newDeserializer(); |
124
|
|
|
|
125
|
|
|
$this->setExpectedException( DeserializationException::class ); |
126
|
|
|
$deserializer->deserialize( $invalidSerialization ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
public function invalidDataValueSerializationProvider() { |
130
|
|
|
return [ |
131
|
|
|
[ 'foo' ], |
132
|
|
|
[ null ], |
133
|
|
|
[ [] ], |
134
|
|
|
[ [ 'hax' ] ], |
135
|
|
|
[ [ 'type' => 'hax' ] ], |
136
|
|
|
[ [ 'type' => 'number', 'value' => [] ] ], |
137
|
|
|
[ [ 'type' => 'boolean', 'value' => 'not a boolean' ] ], |
138
|
|
|
]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function testInvalidValueSerialization_throwsDeserializationException() { |
142
|
|
|
$serialization = [ |
143
|
|
|
'value' => [ 0, 0 ], |
144
|
|
|
'type' => 'string', |
145
|
|
|
'error' => 'omg an error!' |
146
|
|
|
]; |
147
|
|
|
|
148
|
|
|
$deserializer = $this->newDeserializer(); |
149
|
|
|
$this->setExpectedException( DeserializationException::class ); |
150
|
|
|
$deserializer->deserialize( $serialization ); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @dataProvider dataValueSerializationProvider |
155
|
|
|
*/ |
156
|
|
|
public function testGivenDataValueSerialization_isDeserializerForReturnsTrue( $dvSerialization ) { |
157
|
|
|
$deserializer = $this->newDeserializer(); |
158
|
|
|
$this->assertTrue( $deserializer->isDeserializerFor( $dvSerialization ) ); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function dataValueSerializationProvider() { |
162
|
|
|
$boolean = new BooleanValue( false ); |
163
|
|
|
$string = new StringValue( 'foo bar baz' ); |
164
|
|
|
$number = new NumberValue( 42 ); |
165
|
|
|
|
166
|
|
|
return [ |
167
|
|
|
[ $boolean->toArray(), 'boolean' ], |
168
|
|
|
[ $string->toArray(), 'string' ], |
169
|
|
|
[ $number->toArray(), 'number' ], |
170
|
|
|
]; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @dataProvider dataValueSerializationProvider |
175
|
|
|
*/ |
176
|
|
|
public function testGivenDataValueSerialization_deserializeReturnsDataValue( $dvSerialization, $expectedType ) { |
177
|
|
|
$deserializer = $this->newDeserializer(); |
178
|
|
|
|
179
|
|
|
$dataValue = $deserializer->deserialize( $dvSerialization ); |
180
|
|
|
|
181
|
|
|
$this->assertInstanceOf( DataValue::class, $dataValue ); |
182
|
|
|
$this->assertEquals( $expectedType, $dataValue->getType() ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
} |
186
|
|
|
|