|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace TraderInteractive\Filter; |
|
4
|
|
|
|
|
5
|
|
|
use PHPUnit\Framework\TestCase; |
|
6
|
|
|
use TraderInteractive\Exceptions\FilterException; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @coversDefaultClass \TraderInteractive\Filter\Json |
|
10
|
|
|
* @covers ::<private> |
|
11
|
|
|
*/ |
|
12
|
|
|
final class JsonFilterTest extends TestCase |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @test |
|
16
|
|
|
* @covers ::parse |
|
17
|
|
|
* @dataProvider provideParse |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $value The value to filter. |
|
20
|
|
|
* @param mixed $expected The expected result. |
|
21
|
|
|
*/ |
|
22
|
|
|
public function parse(string $value, $expected) |
|
23
|
|
|
{ |
|
24
|
|
|
$result = Json::parse($value); |
|
25
|
|
|
|
|
26
|
|
|
$this->assertSame($expected, $result); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @return array |
|
31
|
|
|
*/ |
|
32
|
|
|
public function provideParse() : array |
|
33
|
|
|
{ |
|
34
|
|
|
return [ |
|
35
|
|
|
'json' => [ |
|
36
|
|
|
'value' => '{"a":"b","c":[1,2,[3],{"4":"d"}], "e": "f"}', |
|
37
|
|
|
'expected' => [ |
|
38
|
|
|
'a' => 'b', |
|
39
|
|
|
'c' => [ |
|
40
|
|
|
1, |
|
41
|
|
|
2, |
|
42
|
|
|
[3], |
|
43
|
|
|
[4 => 'd'], |
|
44
|
|
|
], |
|
45
|
|
|
'e' => 'f', |
|
46
|
|
|
], |
|
47
|
|
|
], |
|
48
|
|
|
'null string' => [ |
|
49
|
|
|
'value' => 'null', |
|
50
|
|
|
'expected' => null, |
|
51
|
|
|
], |
|
52
|
|
|
'integer string' => [ |
|
53
|
|
|
'value' => '1', |
|
54
|
|
|
'expected' => 1, |
|
55
|
|
|
], |
|
56
|
|
|
'float string' => [ |
|
57
|
|
|
'value' => '0.000001', |
|
58
|
|
|
'expected' => 0.000001, |
|
59
|
|
|
], |
|
60
|
|
|
'double string' => [ |
|
61
|
|
|
'value' => '1.56e10', |
|
62
|
|
|
'expected' => 1.56e10, |
|
63
|
|
|
], |
|
64
|
|
|
'true string' => [ |
|
65
|
|
|
'value' => 'true', |
|
66
|
|
|
'expected' => true, |
|
67
|
|
|
], |
|
68
|
|
|
'false string' => [ |
|
69
|
|
|
'value' => 'false', |
|
70
|
|
|
'expected' => false, |
|
71
|
|
|
], |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @test |
|
77
|
|
|
* @covers ::parse |
|
78
|
|
|
*/ |
|
79
|
|
|
public function parseNullWithAllowNull() |
|
80
|
|
|
{ |
|
81
|
|
|
$value = null; |
|
82
|
|
|
$result = Json::parse($value, true); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
$this->assertSame($value, $result); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @test |
|
89
|
|
|
* @covers ::parse |
|
90
|
|
|
* @dataProvider provideInvalidJSON |
|
91
|
|
|
* |
|
92
|
|
|
* @param mixed $value The value to filter. |
|
93
|
|
|
* @param string $message The expected error message. |
|
94
|
|
|
*/ |
|
95
|
|
|
public function parseThrowsException($value, string $message) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->expectException(FilterException::class); |
|
98
|
|
|
$this->expectExceptionMessage($message); |
|
99
|
|
|
|
|
100
|
|
|
Json::parse($value); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @test |
|
105
|
|
|
* @covers ::parse |
|
106
|
|
|
*/ |
|
107
|
|
|
public function parseThrowsExceptionOnNull() |
|
108
|
|
|
{ |
|
109
|
|
|
$this->expectException(FilterException::class); |
|
110
|
|
|
$this->expectExceptionMessage(Json::ERROR_CANNOT_BE_NULL); |
|
111
|
|
|
|
|
112
|
|
|
Json::parse(null); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @test |
|
117
|
|
|
* @covers ::parse |
|
118
|
|
|
*/ |
|
119
|
|
|
public function parseThrowsExceptionForRecursionDepth() |
|
120
|
|
|
{ |
|
121
|
|
|
$this->expectException(FilterException::class); |
|
122
|
|
|
$this->expectExceptionMessage(sprintf(Json::ERROR_INVALID_JSON, 'Maximum stack depth exceeded')); |
|
123
|
|
|
|
|
124
|
|
|
Json::parse('[[]]', false, 1); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @test |
|
129
|
|
|
* @covers ::validate |
|
130
|
|
|
* @dataProvider provideValidate |
|
131
|
|
|
* |
|
132
|
|
|
* @param string $value The value to filter. |
|
133
|
|
|
*/ |
|
134
|
|
|
public function validate(string $value) |
|
135
|
|
|
{ |
|
136
|
|
|
$result = Json::validate($value); |
|
137
|
|
|
|
|
138
|
|
|
$this->assertSame($value, $result); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return array |
|
143
|
|
|
*/ |
|
144
|
|
|
public function provideValidate() : array |
|
145
|
|
|
{ |
|
146
|
|
|
return [ |
|
147
|
|
|
'json' => ['{"a": "b", "c":[1, {"2": 3},[4]], "d": "e"}'], |
|
148
|
|
|
'null' => ['null'], |
|
149
|
|
|
'integer string' => ['12345'], |
|
150
|
|
|
'float string' => ['1.000003'], |
|
151
|
|
|
'double string' => ['445.2e100'], |
|
152
|
|
|
'true string' => ['true'], |
|
153
|
|
|
'false string' => ['false'], |
|
154
|
|
|
]; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @test |
|
159
|
|
|
* @covers ::validate |
|
160
|
|
|
*/ |
|
161
|
|
|
public function validateNullWithAllowNull() |
|
162
|
|
|
{ |
|
163
|
|
|
$value = null; |
|
164
|
|
|
$result = Json::validate($value, true); |
|
|
|
|
|
|
165
|
|
|
|
|
166
|
|
|
$this->assertSame($value, $result); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @test |
|
171
|
|
|
* @covers ::validate |
|
172
|
|
|
* @dataProvider provideInvalidJSON |
|
173
|
|
|
* |
|
174
|
|
|
* @param mixed $value The value to filter. |
|
175
|
|
|
* @param string $message The expected error message. |
|
176
|
|
|
*/ |
|
177
|
|
|
public function validateThrowsException($value, string $message) |
|
178
|
|
|
{ |
|
179
|
|
|
$this->expectException(FilterException::class); |
|
180
|
|
|
$this->expectExceptionMessage($message); |
|
181
|
|
|
|
|
182
|
|
|
Json::validate($value); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @test |
|
187
|
|
|
* @covers ::validate |
|
188
|
|
|
*/ |
|
189
|
|
|
public function validateThrowsExceptionOnNull() |
|
190
|
|
|
{ |
|
191
|
|
|
$this->expectException(FilterException::class); |
|
192
|
|
|
$this->expectExceptionMessage(Json::ERROR_CANNOT_BE_NULL); |
|
193
|
|
|
|
|
194
|
|
|
Json::validate(null); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @test |
|
199
|
|
|
* @covers ::validate |
|
200
|
|
|
*/ |
|
201
|
|
|
public function validateThrowsExceptionForRecursionDepth() |
|
202
|
|
|
{ |
|
203
|
|
|
$this->expectException(FilterException::class); |
|
204
|
|
|
$this->expectExceptionMessage(sprintf(Json::ERROR_INVALID_JSON, 'Maximum stack depth exceeded')); |
|
205
|
|
|
|
|
206
|
|
|
Json::validate('[[]]', false, 1); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @return array |
|
211
|
|
|
*/ |
|
212
|
|
|
public function provideInvalidJSON() : array |
|
213
|
|
|
{ |
|
214
|
|
|
return [ |
|
215
|
|
|
'not a string' => [ |
|
216
|
|
|
'value' => [], |
|
217
|
|
|
'message' => sprintf(Json::ERROR_NOT_A_STRING, var_export([], true)), |
|
218
|
|
|
], |
|
219
|
|
|
'empty string' => [ |
|
220
|
|
|
'value' => '', |
|
221
|
|
|
'message' => sprintf(Json::ERROR_INVALID_JSON, 'Syntax error'), |
|
222
|
|
|
], |
|
223
|
|
|
'only whitespace' => [ |
|
224
|
|
|
'value' => ' ', |
|
225
|
|
|
'message' => sprintf(Json::ERROR_INVALID_JSON, 'Syntax error'), |
|
226
|
|
|
], |
|
227
|
|
|
'non-json string' => [ |
|
228
|
|
|
'value' => 'some string', |
|
229
|
|
|
'message' => sprintf(Json::ERROR_INVALID_JSON, 'Syntax error'), |
|
230
|
|
|
], |
|
231
|
|
|
'invalid json' => [ |
|
232
|
|
|
'value' => '{"incomplete":', |
|
233
|
|
|
'message' => sprintf(Json::ERROR_INVALID_JSON, 'Syntax error'), |
|
234
|
|
|
], |
|
235
|
|
|
'unpaired UTF-16 surrogate' => [ |
|
236
|
|
|
'value' => '["\uD834"]', |
|
237
|
|
|
'message' => sprintf(Json::ERROR_INVALID_JSON, 'Single unpaired UTF-16 surrogate in unicode escape'), |
|
238
|
|
|
], |
|
239
|
|
|
]; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.