1
|
|
|
<?php |
|
|
|
|
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
/* |
4
|
|
|
* This file is part of the KleijnWeb\SwaggerBundle package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace KleijnWeb\SwaggerBundle\Tests\Request\ContentDecoder; |
11
|
|
|
|
12
|
|
|
use KleijnWeb\SwaggerBundle\Request\ParameterCoercer; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author John Kleijn <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class ParameterCoercerTest extends \PHPUnit_Framework_TestCase |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @dataProvider primitiveConversionProvider |
21
|
|
|
* @test |
22
|
|
|
* |
23
|
|
|
* @param string $type |
24
|
|
|
* @param mixed $value |
25
|
|
|
* @param mixed $expected |
26
|
|
|
* @param c $format |
27
|
|
|
*/ |
28
|
|
|
public function willInterpretPrimitivesAsExpected(string $type, $value, $expected, string $format = null) |
29
|
|
|
{ |
30
|
|
|
$spec = ['type' => $type, 'name' => $value]; |
31
|
|
|
if ($type === 'array') { |
32
|
|
|
$spec['collectionFormat'] = $format; |
33
|
|
|
} |
34
|
|
|
if ($type === 'string') { |
35
|
|
|
$spec['format'] = $format; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
$actual = ParameterCoercer::coerceParameter((object)$spec, $value); |
39
|
|
|
|
40
|
|
|
$this->assertEquals($expected, $actual); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @dataProvider malformedPrimitiveConversionProvider |
45
|
|
|
* @test |
46
|
|
|
* |
47
|
|
|
* @param string $type |
48
|
|
|
* @param mixed $value |
49
|
|
|
*/ |
50
|
|
|
public function willNotChangeUninterpretablePrimitives(string $type, $value) |
51
|
|
|
{ |
52
|
|
|
$actual = ParameterCoercer::coerceParameter((object)['type' => $type, 'name' => $value], $value); |
53
|
|
|
$this->assertSame($value, $actual); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @dataProvider malformedDateTimeConversionProvider |
58
|
|
|
* @test |
59
|
|
|
* |
60
|
|
|
* @param string $format |
61
|
|
|
* @param mixed $value |
62
|
|
|
*/ |
63
|
|
|
public function willNotChangeUninterpretableDateTimeAsExpected($format, $value) |
64
|
|
|
{ |
65
|
|
|
$actual = ParameterCoercer::coerceParameter( |
66
|
|
|
(object)['type' => 'string', 'format' => $format, 'name' => $value], |
67
|
|
|
$value |
68
|
|
|
); |
69
|
|
|
$this->assertSame($value, $actual); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @dataProvider unsupportedPrimitiveConversionProvider |
74
|
|
|
* @test |
75
|
|
|
* |
76
|
|
|
* @expectedException \KleijnWeb\SwaggerBundle\Exception\UnsupportedException |
77
|
|
|
* |
78
|
|
|
* @param array $spec |
79
|
|
|
* @param mixed $value |
80
|
|
|
*/ |
81
|
|
|
public function willThrowUnsupportedExceptionInPredefinedCases(array $spec, $value) |
82
|
|
|
{ |
83
|
|
|
$spec = array_merge(['type' => 'string', 'name' => $value], $spec); |
84
|
|
|
ParameterCoercer::coerceParameter((object)$spec, $value); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public static function primitiveConversionProvider() |
91
|
|
|
{ |
92
|
|
|
$now = new \DateTimeImmutable(); |
93
|
|
|
$midnight = new \DateTimeImmutable('midnight today'); |
94
|
|
|
$new = \DateTimeZone::AFRICA; |
|
|
|
|
95
|
|
|
|
96
|
|
|
return [ |
97
|
|
|
['boolean', '0', false], |
98
|
|
|
['boolean', 'FALSE', false], |
99
|
|
|
['boolean', 'false', false], |
100
|
|
|
['boolean', '1', true], |
101
|
|
|
['boolean', 'TRUE', true], |
102
|
|
|
['boolean', 'true', true], |
103
|
|
|
['integer', '1', 1], |
104
|
|
|
['integer', '21474836470', 21474836470], |
105
|
|
|
['integer', '00005', 5], |
106
|
|
|
['number', '1', 1.0], |
107
|
|
|
['number', '1.5', 1.5], |
108
|
|
|
['number', '1', 1.0], |
109
|
|
|
['number', '1.5', 1.5], |
110
|
|
|
['string', '1', '1'], |
111
|
|
|
['string', '1.5', '1.5'], |
112
|
|
|
['string', '€', '€'], |
113
|
|
|
['null', '', null], |
114
|
|
|
['string', $midnight->format('Y-m-d'), $midnight, 'date'], |
115
|
|
|
['string', $now->format(ParameterCoercer::DATE_TIME_ZULU), $now, 'date-time'], |
116
|
|
|
[ |
117
|
|
|
'string', |
118
|
|
|
$now->format(\DateTime::W3C), // Workaround for timezone_type |
119
|
|
|
\DateTimeImmutable::createFromFormat(\DateTime::W3C, $now->format(\DateTime::W3C)), |
120
|
|
|
'date-time' |
121
|
|
|
], |
122
|
|
|
['array', [1, 2, 3, 4], [1, 2, 3, 4]], |
123
|
|
|
['array', 'a', ['a']], |
124
|
|
|
['array', 'a,b,c', ['a', 'b', 'c']], |
125
|
|
|
['array', 'a, b,c ', ['a', ' b', 'c ']], |
126
|
|
|
['array', 'a', ['a'], 'ssv'], |
127
|
|
|
['array', 'a b c', ['a', 'b', 'c'], 'ssv'], |
128
|
|
|
['array', 'a b c ', ['a', '', 'b', 'c', ''], 'ssv'], |
129
|
|
|
['array', 'a', ['a'], 'tsv'], |
130
|
|
|
['array', "a\tb\tc", ['a', 'b', 'c'], 'tsv'], |
131
|
|
|
['array', "a\t b\tc ", ['a', ' b', 'c '], 'tsv'], |
132
|
|
|
['array', 'a', ['a'], 'pipes'], |
133
|
|
|
['array', 'a|b|c', ['a', 'b', 'c'], 'pipes'], |
134
|
|
|
['array', 'a| b|c ', ['a', ' b', 'c '], 'pipes'] |
135
|
|
|
]; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
|
|
public static function malformedPrimitiveConversionProvider() |
142
|
|
|
{ |
143
|
|
|
return [ |
144
|
|
|
['boolean', 'a'], |
145
|
|
|
['boolean', ''], |
146
|
|
|
['boolean', "\0"], |
147
|
|
|
['boolean', null], |
148
|
|
|
['integer', '1.0'], |
149
|
|
|
['integer', 'TRUE'], |
150
|
|
|
['integer', ''], |
151
|
|
|
['number', 'b'], |
152
|
|
|
['number', 'FALSE'], |
153
|
|
|
['null', '0'], |
154
|
|
|
['null', 'FALSE'] |
155
|
|
|
]; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return array |
160
|
|
|
*/ |
161
|
|
|
public static function malformedDateTimeConversionProvider() |
162
|
|
|
{ |
163
|
|
|
return [ |
164
|
|
|
['date', '01-01-1970'], |
165
|
|
|
['date-time', '1970-01-01TH:i:s'], # Missing timezone |
166
|
|
|
]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @return array |
171
|
|
|
*/ |
172
|
|
|
public static function unsupportedPrimitiveConversionProvider() |
173
|
|
|
{ |
174
|
|
|
return [ |
175
|
|
|
[['type' => 'array', 'collectionFormat' => 'multi'], ''], |
176
|
|
|
[['type' => 'array', 'collectionFormat' => 'foo'], ''], |
177
|
|
|
]; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.