1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace KleijnWeb\SwaggerBundle\Tests\Request\ContentDecoder; |
10
|
|
|
|
11
|
|
|
use KleijnWeb\SwaggerBundle\Request\ParameterCoercer; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @author John Kleijn <[email protected]> |
15
|
|
|
*/ |
16
|
|
|
class ParameterCoercerTest extends \PHPUnit_Framework_TestCase |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @dataProvider conversionProvider |
20
|
|
|
* @test |
21
|
|
|
* |
22
|
|
|
* @param string $type |
23
|
|
|
* @param mixed $value |
24
|
|
|
* @param mixed $expected |
25
|
|
|
* @param string $format |
26
|
|
|
*/ |
27
|
|
|
public function willInterpretValuesAsExpected($type, $value, $expected, $format = null) |
28
|
|
|
{ |
29
|
|
|
$spec = ['type' => $type, 'name' => $value]; |
30
|
|
|
if ($type === 'array') { |
31
|
|
|
$spec['collectionFormat'] = $format; |
32
|
|
|
} |
33
|
|
|
if ($type === 'string') { |
34
|
|
|
$spec['format'] = $format; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
$actual = ParameterCoercer::coerceParameter((object)$spec, $value); |
38
|
|
|
|
39
|
|
|
if (is_object($expected)) { |
40
|
|
|
$this->assertEquals($expected, $actual); |
41
|
|
|
return; |
42
|
|
|
} |
43
|
|
|
$this->assertSame($expected, $actual); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @dataProvider malformedConversionProvider |
48
|
|
|
* @test |
49
|
|
|
* |
50
|
|
|
* @param string $type |
51
|
|
|
* @param mixed $value |
52
|
|
|
*/ |
53
|
|
|
public function willNotChangeUninterpretableValues($type, $value) |
54
|
|
|
{ |
55
|
|
|
$actual = ParameterCoercer::coerceParameter((object)['type' => $type, 'name' => $value], $value); |
56
|
|
|
$this->assertSame($value, $actual); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @dataProvider malformedDateTimeConversionProvider |
61
|
|
|
* @test |
62
|
|
|
* |
63
|
|
|
* @param string $format |
64
|
|
|
* @param mixed $value |
65
|
|
|
*/ |
66
|
|
|
public function willNotChangeUninterpretableDateTimeAsExpected($format, $value) |
67
|
|
|
{ |
68
|
|
|
$actual = ParameterCoercer::coerceParameter( |
69
|
|
|
(object)['type' => 'string', 'format' => $format, 'name' => $value], |
70
|
|
|
$value |
71
|
|
|
); |
72
|
|
|
$this->assertSame($value, $actual); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @dataProvider unsupportedPrimitiveConversionProvider |
77
|
|
|
* @test |
78
|
|
|
* |
79
|
|
|
* @expectedException \KleijnWeb\SwaggerBundle\Exception\UnsupportedException |
80
|
|
|
* |
81
|
|
|
* @param array $spec |
82
|
|
|
* @param mixed $value |
83
|
|
|
*/ |
84
|
|
|
public function willThrowUnsupportedExceptionInPredefinedCases($spec, $value) |
85
|
|
|
{ |
86
|
|
|
$spec = array_merge(['type' => 'string', 'name' => $value], $spec); |
87
|
|
|
ParameterCoercer::coerceParameter((object)$spec, $value); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return array |
92
|
|
|
*/ |
93
|
|
|
public static function conversionProvider() |
94
|
|
|
{ |
95
|
|
|
$now = new \DateTime(); |
96
|
|
|
$midnight = new \DateTime('midnight today'); |
97
|
|
|
$object = new \stdClass; |
98
|
|
|
$object->a = 'b'; |
99
|
|
|
$object->c = 'd'; |
100
|
|
|
|
101
|
|
|
return [ |
102
|
|
|
['boolean', '0', false], |
103
|
|
|
['boolean', 'FALSE', false], |
104
|
|
|
['boolean', 'false', false], |
105
|
|
|
['boolean', '1', true], |
106
|
|
|
['boolean', 'TRUE', true], |
107
|
|
|
['boolean', 'true', true], |
108
|
|
|
['integer', '1', 1], |
109
|
|
|
['integer', '21474836470', 21474836470], |
110
|
|
|
['integer', '00005', 5], |
111
|
|
|
['number', '1', 1.0], |
112
|
|
|
['number', '1.5', 1.5], |
113
|
|
|
['number', '1', 1.0], |
114
|
|
|
['number', '1.5', 1.5], |
115
|
|
|
['string', '1', '1'], |
116
|
|
|
['string', '1.5', '1.5'], |
117
|
|
|
['string', '€', '€'], |
118
|
|
|
['null', '', null], |
119
|
|
|
['string', $midnight->format('Y-m-d'), $midnight, 'date'], |
120
|
|
|
['string', $now->format(\DateTime::W3C), $now, 'date-time'], |
121
|
|
|
['array', [1, 2, 3, 4], [1, 2, 3, 4]], |
122
|
|
|
['array', 'a', ['a']], |
123
|
|
|
['array', 'a,b,c', ['a', 'b', 'c']], |
124
|
|
|
['array', 'a, b,c ', ['a', ' b', 'c ']], |
125
|
|
|
['array', 'a', ['a'], 'ssv'], |
126
|
|
|
['array', 'a b c', ['a', 'b', 'c'], 'ssv'], |
127
|
|
|
['array', 'a b c ', ['a', '', 'b', 'c', ''], 'ssv'], |
128
|
|
|
['array', 'a', ['a'], 'tsv'], |
129
|
|
|
['array', "a\tb\tc", ['a', 'b', 'c'], 'tsv'], |
130
|
|
|
['array', "a\t b\tc ", ['a', ' b', 'c '], 'tsv'], |
131
|
|
|
['array', 'a', ['a'], 'pipes'], |
132
|
|
|
['array', 'a|b|c', ['a', 'b', 'c'], 'pipes'], |
133
|
|
|
['array', 'a| b|c ', ['a', ' b', 'c '], 'pipes'], |
134
|
|
|
['object', ['a' => 'b', 'c' => 'd'], $object], |
135
|
|
|
['object', '', null] |
136
|
|
|
]; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @return array |
141
|
|
|
*/ |
142
|
|
|
public static function malformedConversionProvider() |
143
|
|
|
{ |
144
|
|
|
return [ |
145
|
|
|
['boolean', 'a'], |
146
|
|
|
['boolean', ''], |
147
|
|
|
['boolean', "\0"], |
148
|
|
|
['boolean', null], |
149
|
|
|
['integer', '1.0'], |
150
|
|
|
['integer', 'TRUE'], |
151
|
|
|
['integer', ''], |
152
|
|
|
['number', 'b'], |
153
|
|
|
['number', 'FALSE'], |
154
|
|
|
['null', '0'], |
155
|
|
|
['null', 'FALSE'], |
156
|
|
|
['object', ['a', 'c']], |
157
|
|
|
['object', 'FALSE'] |
158
|
|
|
]; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @return array |
163
|
|
|
*/ |
164
|
|
|
public static function malformedDateTimeConversionProvider() |
165
|
|
|
{ |
166
|
|
|
return [ |
167
|
|
|
['date', '01-01-1970'], |
168
|
|
|
['date-time', '1970-01-01TH:i:s'], # Missing timezone |
169
|
|
|
]; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* @return array |
174
|
|
|
*/ |
175
|
|
|
public static function unsupportedPrimitiveConversionProvider() |
176
|
|
|
{ |
177
|
|
|
return [ |
178
|
|
|
[['type' => 'array', 'collectionFormat' => 'multi'], ''], |
179
|
|
|
[['type' => 'array', 'collectionFormat' => 'foo'], ''], |
180
|
|
|
]; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|