1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TraderInteractive; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use PHPUnit\Framework\TestCase; |
8
|
|
|
use stdClass; |
9
|
|
|
use TraderInteractive\Exceptions\FilterException; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @coversDefaultClass \TraderInteractive\Filterer |
13
|
|
|
* @covers ::<private> |
14
|
|
|
*/ |
15
|
|
|
final class FiltererTest extends TestCase |
16
|
|
|
{ |
17
|
|
|
public function setUp() |
18
|
|
|
{ |
19
|
|
|
Filterer::setFilterAliases(Filterer::DEFAULT_FILTER_ALIASES); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @test |
24
|
|
|
* @covers ::filter |
25
|
|
|
* @dataProvider provideValidFilterData |
26
|
|
|
* |
27
|
|
|
* @param array $spec The filter specification to be use. |
28
|
|
|
* @param array $input The input to be filtered. |
29
|
|
|
* @param array $options The filterer options |
30
|
|
|
* @param array $expectedResult The expected filterer result. |
31
|
|
|
*/ |
32
|
|
|
public function filter(array $spec, array $input, array $options, array $expectedResult) |
33
|
|
|
{ |
34
|
|
|
$this->assertSame($expectedResult, Filterer::filter($spec, $input, $options)); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function provideValidFilterData() : array |
38
|
|
|
{ |
39
|
|
|
return [ |
40
|
|
|
'not required field not present' => [ |
41
|
|
|
'spec' => ['fieldOne' => ['required' => false]], |
42
|
|
|
'input' => [], |
43
|
|
|
'options' => [], |
44
|
|
|
'result' => [true, [], null, []], |
45
|
|
|
], |
46
|
|
|
'Required With A Default Without Input' => [ |
47
|
|
|
'spec' => ['fieldOne' => ['required' => true, 'default' => 'theDefault']], |
48
|
|
|
'input' => [], |
49
|
|
|
'options' => [], |
50
|
|
|
'result' => [true, ['fieldOne' => 'theDefault'], null, []], |
51
|
|
|
], |
52
|
|
|
'Required With A Null Default Without Input' => [ |
53
|
|
|
'spec' => ['fieldOne' => ['required' => true, 'default' => null]], |
54
|
|
|
'input' => [], |
55
|
|
|
'options' => [], |
56
|
|
|
'result' => [true, ['fieldOne' => null], null, []], |
57
|
|
|
], |
58
|
|
|
'Required With A Default With Input' => [ |
59
|
|
|
'spec' => ['fieldOne' => ['required' => true, 'default' => 'theDefault']], |
60
|
|
|
'input' => ['fieldOne' => 'notTheDefault'], |
61
|
|
|
'options' => [], |
62
|
|
|
'result' => [true, ['fieldOne' => 'notTheDefault'], null, []], |
63
|
|
|
], |
64
|
|
|
'Not Required With A Default Without Input' => [ |
65
|
|
|
'spec' => ['fieldOne' => ['default' => 'theDefault']], |
66
|
|
|
'input' => [], |
67
|
|
|
'options' => [], |
68
|
|
|
'result' => [true, ['fieldOne' => 'theDefault'], null, []], |
69
|
|
|
], |
70
|
|
|
'Not Required With A Default With Input' => [ |
71
|
|
|
'spec' => ['fieldOne' => ['default' => 'theDefault']], |
72
|
|
|
'input' => ['fieldOne' => 'notTheDefault'], |
73
|
|
|
'options' => [], |
74
|
|
|
'result' => [true, ['fieldOne' => 'notTheDefault'], null, []], |
75
|
|
|
], |
76
|
|
|
'Required Fail' => [ |
77
|
|
|
'spec' => ['fieldOne' => ['required' => true]], |
78
|
|
|
'input' => [], |
79
|
|
|
'options' => [], |
80
|
|
|
'result' => [false, null, "Field 'fieldOne' was required and not present", []], |
81
|
|
|
], |
82
|
|
|
'Required Default Pass' => [ |
83
|
|
|
'spec' => ['fieldOne' => []], |
84
|
|
|
'input' => [], |
85
|
|
|
'options' => [], |
86
|
|
|
'result' => [true, [], null, []], |
87
|
|
|
], |
88
|
|
|
'requiredDefaultFail' => [ |
89
|
|
|
'spec' => ['fieldOne' => []], |
90
|
|
|
'input' => [], |
91
|
|
|
'options' => ['defaultRequired' => true], |
92
|
|
|
'result' => [false, null, "Field 'fieldOne' was required and not present", []], |
93
|
|
|
], |
94
|
|
|
'filterPass' => [ |
95
|
|
|
'spec' => ['fieldOne' => [['floatval']]], |
96
|
|
|
'input' => ['fieldOne' => '3.14'], |
97
|
|
|
'options' => [], |
98
|
|
|
'result' => [true, ['fieldOne' => 3.14], null, []], |
99
|
|
|
], |
100
|
|
|
'filterDefaultShortNamePass' => [ |
101
|
|
|
'spec' => ['fieldOne' => [['float']]], |
102
|
|
|
'input' => ['fieldOne' => '3.14'], |
103
|
|
|
'options' => [], |
104
|
|
|
'result' => [true, ['fieldOne' => 3.14], null, []], |
105
|
|
|
], |
106
|
|
|
'filterFail' => [ |
107
|
|
|
'spec' => ['fieldOne' => [['\TraderInteractive\FiltererTest::failingFilter']]], |
108
|
|
|
'input' => ['fieldOne' => 'valueOne'], |
109
|
|
|
'options' => [], |
110
|
|
|
'result' => [ |
111
|
|
|
false, |
112
|
|
|
null, |
113
|
|
|
"Field 'fieldOne' with value 'valueOne' failed filtering, message 'i failed'", |
114
|
|
|
[], |
115
|
|
|
], |
116
|
|
|
], |
117
|
|
|
'chainPass' => [ |
118
|
|
|
'spec' => ['fieldOne' => [['trim', 'a'], ['floatval']]], |
119
|
|
|
'input' => ['fieldOne' => 'a3.14'], |
120
|
|
|
'options' => [], |
121
|
|
|
'result' => [true, ['fieldOne' => 3.14], null, []], |
122
|
|
|
], |
123
|
|
|
'chainFail' => [ |
124
|
|
|
'spec' => ['fieldOne' => [['trim'], ['\TraderInteractive\FiltererTest::failingFilter']]], |
125
|
|
|
'input' => ['fieldOne' => 'the value'], |
126
|
|
|
'options' => [], |
127
|
|
|
'result' => [ |
128
|
|
|
false, |
129
|
|
|
null, |
130
|
|
|
"Field 'fieldOne' with value 'the value' failed filtering, message 'i failed'", |
131
|
|
|
[], |
132
|
|
|
], |
133
|
|
|
], |
134
|
|
|
'multiInputPass' => [ |
135
|
|
|
'spec' => ['fieldOne' => [['trim']], 'fieldTwo' => [['strtoupper']]], |
136
|
|
|
'input' => ['fieldOne' => ' value', 'fieldTwo' => 'bob'], |
137
|
|
|
'options' => [], |
138
|
|
|
'result' => [true, ['fieldOne' => 'value', 'fieldTwo' => 'BOB'], null, []], |
139
|
|
|
], |
140
|
|
|
'multiInputFail' => [ |
141
|
|
|
'spec' => [ |
142
|
|
|
'fieldOne' => [['\TraderInteractive\FiltererTest::failingFilter']], |
143
|
|
|
'fieldTwo' => [['\TraderInteractive\FiltererTest::failingFilter']], |
144
|
|
|
], |
145
|
|
|
'input' => ['fieldOne' => 'value one', 'fieldTwo' => 'value two'], |
146
|
|
|
'options' => [], |
147
|
|
|
'result' => [ |
148
|
|
|
false, |
149
|
|
|
null, |
150
|
|
|
"Field 'fieldOne' with value 'value one' failed filtering, message 'i failed'\n" |
151
|
|
|
. "Field 'fieldTwo' with value 'value two' failed filtering, message 'i failed'", |
152
|
|
|
[], |
153
|
|
|
], |
154
|
|
|
], |
155
|
|
|
'emptyFilter' => [ |
156
|
|
|
'spec' => ['fieldOne' => [[]]], |
157
|
|
|
'input' => ['fieldOne' => 0], |
158
|
|
|
'options' => [], |
159
|
|
|
'result' => [true, ['fieldOne' => 0], null, []], |
160
|
|
|
], |
161
|
|
|
'unknownsAllowed' => [ |
162
|
|
|
'spec' => [], |
163
|
|
|
'input'=> ['fieldTwo' => 0], |
164
|
|
|
'options' => ['allowUnknowns' => true], |
165
|
|
|
'result' => [true, [], null, ['fieldTwo' => 0]], |
166
|
|
|
], |
167
|
|
|
'unknownsNotAllowed' => [ |
168
|
|
|
'spec' => [], |
169
|
|
|
'input' => ['fieldTwo' => 0], |
170
|
|
|
'options' => [], |
171
|
|
|
'result' => [false, null, "Field 'fieldTwo' with value '0' is unknown", ['fieldTwo' => 0]], |
172
|
|
|
], |
173
|
|
|
'objectFilter' => [ |
174
|
|
|
'spec' => ['fieldOne' => [[[$this, 'passingFilter']]]], |
175
|
|
|
'input' => ['fieldOne' => 'foo'], |
176
|
|
|
'options' => [], |
177
|
|
|
'result' => [true, ['fieldOne' => 'fooboo'], null, []], |
178
|
|
|
], |
179
|
|
|
'filterWithCustomError' => [ |
180
|
|
|
'spec' => [ |
181
|
|
|
'fieldOne' => [ |
182
|
|
|
'error' => 'My custom error message', |
183
|
|
|
['\TraderInteractive\FiltererTest::failingFilter'], |
184
|
|
|
], |
185
|
|
|
], |
186
|
|
|
'input' => ['fieldOne' => 'valueOne'], |
187
|
|
|
'options' => [], |
188
|
|
|
'result' => [false, null, 'My custom error message', []], |
189
|
|
|
], |
190
|
|
|
'filterWithCustomErrorContainingValuePlaceholder' => [ |
191
|
|
|
'spec' => [ |
192
|
|
|
'fieldOne' => [ |
193
|
|
|
'error' => "The value '{value}' is invalid.", |
194
|
|
|
['uint'], |
195
|
|
|
], |
196
|
|
|
], |
197
|
|
|
'input' => ['fieldOne' => 'abc'], |
198
|
|
|
'options' => [], |
199
|
|
|
'result' => [false, null, "The value 'abc' is invalid.", []], |
200
|
|
|
], |
201
|
|
|
'arrayizeAliasIsCalledProperly' => [ |
202
|
|
|
'spec' => ['field' => [['arrayize']]], |
203
|
|
|
'input' => ['field' => 'a string value'], |
204
|
|
|
'options' => [], |
205
|
|
|
'result' => [true, ['field' => ['a string value']], null, []], |
206
|
|
|
], |
207
|
|
|
'concatAliasIsCalledProperly' => [ |
208
|
|
|
'spec' => ['field' => [['concat', '%', '%']]], |
209
|
|
|
'input' => ['field' => 'value'], |
210
|
|
|
'options' => [], |
211
|
|
|
'result' => [true, ['field' => '%value%'], null, []], |
212
|
|
|
], |
213
|
|
|
'translate alias' => [ |
214
|
|
|
'spec' => ['field' => [['translate', ['active' => 'A', 'inactive' => 'I']]]], |
215
|
|
|
'input' => ['field' => 'inactive'], |
216
|
|
|
'options' => [], |
217
|
|
|
'result' => [true, ['field' => 'I'], null, []], |
218
|
|
|
], |
219
|
|
|
'redact alias' => [ |
220
|
|
|
'spec' => ['field' => [['redact', ['other'], '*']]], |
221
|
|
|
'input' => ['field' => 'one or other'], |
222
|
|
|
'options' => [], |
223
|
|
|
'result' => [true, ['field' => 'one or *****'], null, []], |
224
|
|
|
], |
225
|
|
|
]; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @test |
230
|
|
|
* @covers ::filter |
231
|
|
|
*/ |
232
|
|
|
public function filterReturnsResponseType() |
233
|
|
|
{ |
234
|
|
|
$specification = ['id' => [['uint']]]; |
235
|
|
|
$input = ['id' => 1]; |
236
|
|
|
$options = ['responseType' => Filterer::RESPONSE_TYPE_FILTER]; |
237
|
|
|
|
238
|
|
|
$result = Filterer::filter($specification, $input, $options); |
239
|
|
|
|
240
|
|
|
$this->assertInstanceOf(FilterResponse::class, $result); |
241
|
|
|
$this->assertSame(true, $result->success); |
242
|
|
|
$this->assertSame($input, $result->filteredValue); |
243
|
|
|
$this->assertSame([], $result->errors); |
244
|
|
|
$this->assertSame(null, $result->errorMessage); |
245
|
|
|
$this->assertSame([], $result->unknowns); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @test |
250
|
|
|
* @covers ::filter |
251
|
|
|
*/ |
252
|
|
|
public function filterReturnsResponseTypeWithErrors() |
253
|
|
|
{ |
254
|
|
|
$specification = ['name' => [['string']]]; |
255
|
|
|
$input = ['name' => 'foo', 'id' => 1]; |
256
|
|
|
$options = ['responseType' => Filterer::RESPONSE_TYPE_FILTER]; |
257
|
|
|
|
258
|
|
|
$result = Filterer::filter($specification, $input, $options); |
259
|
|
|
|
260
|
|
|
$this->assertInstanceOf(FilterResponse::class, $result); |
261
|
|
|
$this->assertSame(false, $result->success); |
262
|
|
|
$this->assertSame(['name' => 'foo'], $result->filteredValue); |
263
|
|
|
$this->assertSame(['id' => "Field 'id' with value '1' is unknown"], $result->errors); |
264
|
|
|
$this->assertSame("Field 'id' with value '1' is unknown", $result->errorMessage); |
265
|
|
|
$this->assertSame(['id' => 1], $result->unknowns); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* @test |
270
|
|
|
* @covers ::filter |
271
|
|
|
* @covers ::setFilterAliases |
272
|
|
|
*/ |
273
|
|
|
public function filterCustomShortNamePass() |
274
|
|
|
{ |
275
|
|
|
Filterer::setFilterAliases(['fval' => 'floatval']); |
276
|
|
|
$result = Filterer::filter(['fieldOne' => [['fval']]], ['fieldOne' => '3.14']); |
277
|
|
|
$this->assertSame([true, ['fieldOne' => 3.14], null, []], $result); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @test |
282
|
|
|
* @covers ::filter |
283
|
|
|
* @covers ::setFilterAliases |
284
|
|
|
* @covers ::getFilterAliases |
285
|
|
|
*/ |
286
|
|
|
public function filterGetSetKnownFilters() |
287
|
|
|
{ |
288
|
|
|
$knownFilters = ['lower' => 'strtolower', 'upper' => 'strtoupper']; |
289
|
|
|
Filterer::setFilterAliases($knownFilters); |
290
|
|
|
$this->assertSame($knownFilters, Filterer::getFilterAliases()); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* @test |
295
|
|
|
* @covers ::filter |
296
|
|
|
* @expectedException Exception |
297
|
|
|
* @expectedExceptionMessage Function 'boo' for field 'foo' is not callable |
298
|
|
|
*/ |
299
|
|
|
public function notCallable() |
300
|
|
|
{ |
301
|
|
|
Filterer::filter(['foo' => [['boo']]], ['foo' => 0]); |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* @test |
306
|
|
|
* @covers ::filter |
307
|
|
|
* @expectedException InvalidArgumentException |
308
|
|
|
* @expectedExceptionMessage 'allowUnknowns' option was not a bool |
309
|
|
|
*/ |
310
|
|
|
public function allowUnknownsNotBool() |
311
|
|
|
{ |
312
|
|
|
Filterer::filter([], [], ['allowUnknowns' => 1]); |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* @test |
317
|
|
|
* @covers ::filter |
318
|
|
|
* @expectedException InvalidArgumentException |
319
|
|
|
* @expectedExceptionMessage 'defaultRequired' option was not a bool |
320
|
|
|
*/ |
321
|
|
|
public function defaultRequiredNotBool() |
322
|
|
|
{ |
323
|
|
|
Filterer::filter([], [], ['defaultRequired' => 1]); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
/** |
327
|
|
|
* @test |
328
|
|
|
* @covers ::filter |
329
|
|
|
*/ |
330
|
|
|
public function filterThrowsExceptionOnInvalidResponseType() |
331
|
|
|
{ |
332
|
|
|
$this->expectException(InvalidArgumentException::class); |
333
|
|
|
$this->expectExceptionMessage("'responseType' was not a recognized value"); |
334
|
|
|
|
335
|
|
|
Filterer::filter([], [], ['responseType' => 'invalid']); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @test |
340
|
|
|
* @covers ::filter |
341
|
|
|
* @expectedException InvalidArgumentException |
342
|
|
|
* @expectedExceptionMessage filters for field 'boo' was not a array |
343
|
|
|
*/ |
344
|
|
|
public function filtersNotArrayInLeftOverSpec() |
345
|
|
|
{ |
346
|
|
|
Filterer::filter(['boo' => 1], []); |
347
|
|
|
} |
348
|
|
|
|
349
|
|
|
/** |
350
|
|
|
* @test |
351
|
|
|
* @covers ::filter |
352
|
|
|
* @expectedException InvalidArgumentException |
353
|
|
|
* @expectedExceptionMessage filters for field 'boo' was not a array |
354
|
|
|
*/ |
355
|
|
|
public function filtersNotArrayWithInput() |
356
|
|
|
{ |
357
|
|
|
Filterer::filter(['boo' => 1], ['boo' => 'notUnderTest']); |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* @test |
362
|
|
|
* @covers ::filter |
363
|
|
|
* @expectedException InvalidArgumentException |
364
|
|
|
* @expectedExceptionMessage filter for field 'boo' was not a array |
365
|
|
|
*/ |
366
|
|
|
public function filterNotArray() |
367
|
|
|
{ |
368
|
|
|
Filterer::filter(['boo' => [1]], ['boo' => 'notUnderTest']); |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
/** |
372
|
|
|
* @test |
373
|
|
|
* @covers ::filter |
374
|
|
|
* @expectedException InvalidArgumentException |
375
|
|
|
* @expectedExceptionMessage 'required' for field 'boo' was not a bool |
376
|
|
|
*/ |
377
|
|
|
public function requiredNotBool() |
378
|
|
|
{ |
379
|
|
|
Filterer::filter(['boo' => ['required' => 1]], []); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* @test |
384
|
|
|
* @covers ::registerAlias |
385
|
|
|
* @expectedException InvalidArgumentException |
386
|
|
|
* @expectedExceptionMessage $alias was not a string or int |
387
|
|
|
*/ |
388
|
|
|
public function registerAliasAliasNotString() |
389
|
|
|
{ |
390
|
|
|
Filterer::registerAlias(true, 'strtolower'); |
|
|
|
|
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* @test |
395
|
|
|
* @covers ::registerAlias |
396
|
|
|
* @expectedException Exception |
397
|
|
|
* @expectedExceptionMessage Alias 'upper' exists |
398
|
|
|
*/ |
399
|
|
|
public function registerExistingAliasOverwriteFalse() |
400
|
|
|
{ |
401
|
|
|
Filterer::setFilterAliases([]); |
402
|
|
|
Filterer::registerAlias('upper', 'strtoupper'); |
403
|
|
|
Filterer::registerAlias('upper', 'strtoupper', false); |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* @test |
408
|
|
|
* @covers ::registerAlias |
409
|
|
|
*/ |
410
|
|
|
public function registerExistingAliasOverwriteTrue() |
411
|
|
|
{ |
412
|
|
|
Filterer::setFilterAliases(['upper' => 'strtoupper', 'lower' => 'strtolower']); |
413
|
|
|
Filterer::registerAlias('upper', 'ucfirst', true); |
414
|
|
|
$this->assertSame(['upper' => 'ucfirst', 'lower' => 'strtolower'], Filterer::getFilterAliases()); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
public static function failingFilter() |
418
|
|
|
{ |
419
|
|
|
throw new Exception('i failed'); |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
public static function passingFilter($value) |
423
|
|
|
{ |
424
|
|
|
return $value . 'boo'; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Verify behavior of filter() when 'error' is not a string value. |
429
|
|
|
* |
430
|
|
|
* @test |
431
|
|
|
* @covers ::filter |
432
|
|
|
* @expectedException InvalidArgumentException |
433
|
|
|
* @expectedExceptionMessage error for field 'fieldOne' was not a non-empty string |
434
|
|
|
* |
435
|
|
|
* @return void |
436
|
|
|
*/ |
437
|
|
View Code Duplication |
public function filterWithNonStringError() |
|
|
|
|
438
|
|
|
{ |
439
|
|
|
Filterer::filter( |
440
|
|
|
['fieldOne' => [['strtoupper'], 'error' => new stdClass()]], |
441
|
|
|
['fieldOne' => 'valueOne'] |
442
|
|
|
); |
443
|
|
|
} |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* Verify behavior of filter() when 'error' is an empty string. |
447
|
|
|
* |
448
|
|
|
* @test |
449
|
|
|
* @covers ::filter |
450
|
|
|
* @expectedException InvalidArgumentException |
451
|
|
|
* @expectedExceptionMessage error for field 'fieldOne' was not a non-empty string |
452
|
|
|
* |
453
|
|
|
* @return void |
454
|
|
|
*/ |
455
|
|
View Code Duplication |
public function filterWithEmptyStringError() |
|
|
|
|
456
|
|
|
{ |
457
|
|
|
Filterer::filter( |
458
|
|
|
['fieldOne' => [['strtoupper'], 'error' => "\n \t"]], |
459
|
|
|
['fieldOne' => 'valueOne'] |
460
|
|
|
); |
461
|
|
|
} |
462
|
|
|
|
463
|
|
|
/** |
464
|
|
|
* @test |
465
|
|
|
* @covers ::ofScalars |
466
|
|
|
*/ |
467
|
|
|
public function ofScalars() |
468
|
|
|
{ |
469
|
|
|
$this->assertSame([1, 2], Filterer::ofScalars(['1', '2'], [['uint']])); |
470
|
|
|
} |
471
|
|
|
|
472
|
|
|
/** |
473
|
|
|
* @test |
474
|
|
|
* @covers ::ofScalars |
475
|
|
|
*/ |
476
|
|
|
public function ofScalarsChained() |
477
|
|
|
{ |
478
|
|
|
$this->assertSame([3.3, 5.5], Filterer::ofScalars(['a3.3', 'a5.5'], [['trim', 'a'], ['floatval']])); |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
/** |
482
|
|
|
* @test |
483
|
|
|
* @covers ::ofScalars |
484
|
|
|
*/ |
485
|
|
|
public function ofScalarsWithMeaninglessKeys() |
486
|
|
|
{ |
487
|
|
|
$this->assertSame(['key1' => 1, 'key2' => 2], Filterer::ofScalars(['key1' => '1', 'key2' => '2'], [['uint']])); |
488
|
|
|
} |
489
|
|
|
|
490
|
|
|
/** |
491
|
|
|
* @test |
492
|
|
|
* @covers ::ofScalars |
493
|
|
|
*/ |
494
|
|
View Code Duplication |
public function ofScalarsFail() |
|
|
|
|
495
|
|
|
{ |
496
|
|
|
try { |
497
|
|
|
Filterer::ofScalars(['1', [], new stdClass], [['string']]); |
498
|
|
|
$this->fail(); |
499
|
|
|
} catch (FilterException $e) { |
500
|
|
|
$expected = <<<TXT |
501
|
|
|
Field '1' with value 'array ( |
502
|
|
|
)' failed filtering, message 'Value 'array ( |
503
|
|
|
)' is not a string' |
504
|
|
|
Field '2' with value 'stdClass::__set_state(array( |
505
|
|
|
))' failed filtering, message 'Value 'stdClass::__set_state(array( |
506
|
|
|
))' is not a string' |
507
|
|
|
TXT; |
508
|
|
|
$this->assertSame($expected, $e->getMessage()); |
509
|
|
|
} |
510
|
|
|
} |
511
|
|
|
|
512
|
|
|
/** |
513
|
|
|
* @test |
514
|
|
|
* @covers ::ofArrays |
515
|
|
|
*/ |
516
|
|
|
public function ofArrays() |
517
|
|
|
{ |
518
|
|
|
$expected = [['key' => 1], ['key' => 2]]; |
519
|
|
|
$this->assertSame($expected, Filterer::ofArrays([['key' => '1'], ['key' => '2']], ['key' => [['uint']]])); |
520
|
|
|
} |
521
|
|
|
|
522
|
|
|
/** |
523
|
|
|
* @test |
524
|
|
|
* @covers ::ofArrays |
525
|
|
|
*/ |
526
|
|
|
public function ofArraysChained() |
527
|
|
|
{ |
528
|
|
|
$expected = [['key' => 3.3], ['key' => 5.5]]; |
529
|
|
|
$spec = ['key' => [['trim', 'a'], ['floatval']]]; |
530
|
|
|
$this->assertSame($expected, Filterer::ofArrays([['key' => 'a3.3'], ['key' => 'a5.5']], $spec)); |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
/** |
534
|
|
|
* @test |
535
|
|
|
* @covers ::ofArrays |
536
|
|
|
*/ |
537
|
|
View Code Duplication |
public function ofArraysRequiredAndUnknown() |
|
|
|
|
538
|
|
|
{ |
539
|
|
|
try { |
540
|
|
|
Filterer::ofArrays([['key' => '1'], ['key2' => '2']], ['key' => ['required' => true, ['uint']]]); |
541
|
|
|
$this->fail(); |
542
|
|
|
} catch (Exception $e) { |
543
|
|
|
$expected = "Field 'key' was required and not present\nField 'key2' with value '2' is unknown"; |
544
|
|
|
$this->assertSame($expected, $e->getMessage()); |
545
|
|
|
} |
546
|
|
|
} |
547
|
|
|
|
548
|
|
|
/** |
549
|
|
|
* @test |
550
|
|
|
* @covers ::ofArrays |
551
|
|
|
*/ |
552
|
|
|
public function ofArraysFail() |
553
|
|
|
{ |
554
|
|
|
try { |
555
|
|
|
Filterer::ofArrays( |
556
|
|
|
[['key' => new stdClass], ['key' => []], ['key' => null], 'key'], |
557
|
|
|
['key' => [['string']]] |
558
|
|
|
); |
559
|
|
|
$this->fail(); |
560
|
|
|
} catch (FilterException $e) { |
561
|
|
|
$expected = <<<TXT |
562
|
|
|
Field 'key' with value 'stdClass::__set_state(array( |
563
|
|
|
))' failed filtering, message 'Value 'stdClass::__set_state(array( |
564
|
|
|
))' is not a string' |
565
|
|
|
Field 'key' with value 'array ( |
566
|
|
|
)' failed filtering, message 'Value 'array ( |
567
|
|
|
)' is not a string' |
568
|
|
|
Field 'key' with value 'NULL' failed filtering, message 'Value failed filtering, \$allowNull is set to false' |
569
|
|
|
Value at position '3' was not an array |
570
|
|
|
TXT; |
571
|
|
|
$this->assertSame($expected, $e->getMessage()); |
572
|
|
|
} |
573
|
|
|
} |
574
|
|
|
|
575
|
|
|
/** |
576
|
|
|
* @test |
577
|
|
|
* @covers ::ofArray |
578
|
|
|
*/ |
579
|
|
|
public function ofArray() |
580
|
|
|
{ |
581
|
|
|
$expected = ['key1' => 1, 'key2' => 2]; |
582
|
|
|
$spec = ['key1' => [['uint']], 'key2' => [['uint']]]; |
583
|
|
|
$this->assertSame($expected, Filterer::ofArray(['key1' => '1', 'key2' => '2'], $spec)); |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
/** |
587
|
|
|
* @test |
588
|
|
|
* @covers ::ofArray |
589
|
|
|
*/ |
590
|
|
|
public function ofArrayChained() |
591
|
|
|
{ |
592
|
|
|
$expected = ['key' => 3.3]; |
593
|
|
|
$spec = ['key' => [['trim', 'a'], ['floatval']]]; |
594
|
|
|
$this->assertSame($expected, Filterer::ofArray(['key' => 'a3.3'], $spec)); |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
/** |
598
|
|
|
* @test |
599
|
|
|
* @covers ::ofArray |
600
|
|
|
*/ |
601
|
|
|
public function ofArrayRequiredSuccess() |
602
|
|
|
{ |
603
|
|
|
$expected = ['key2' => 2]; |
604
|
|
|
$spec = ['key1' => [['uint']], 'key2' => ['required' => true, ['uint']]]; |
605
|
|
|
$this->assertSame($expected, Filterer::ofArray(['key2' => '2'], $spec)); |
606
|
|
|
} |
607
|
|
|
|
608
|
|
|
/** |
609
|
|
|
* @test |
610
|
|
|
* @covers ::ofArray |
611
|
|
|
*/ |
612
|
|
View Code Duplication |
public function ofArrayRequiredFail() |
|
|
|
|
613
|
|
|
{ |
614
|
|
|
try { |
615
|
|
|
Filterer::ofArray(['key1' => '1'], ['key1' => [['uint']], 'key2' => ['required' => true, ['uint']]]); |
616
|
|
|
$this->fail(); |
617
|
|
|
} catch (FilterException $e) { |
618
|
|
|
$expected = "Field 'key2' was required and not present"; |
619
|
|
|
$this->assertSame($expected, $e->getMessage()); |
620
|
|
|
} |
621
|
|
|
} |
622
|
|
|
|
623
|
|
|
/** |
624
|
|
|
* @test |
625
|
|
|
* @covers ::ofArray |
626
|
|
|
*/ |
627
|
|
View Code Duplication |
public function ofArrayUnknown() |
|
|
|
|
628
|
|
|
{ |
629
|
|
|
try { |
630
|
|
|
Filterer::ofArray(['key' => '1'], ['key2' => [['uint']]]); |
631
|
|
|
$this->fail(); |
632
|
|
|
} catch (FilterException $e) { |
633
|
|
|
$expected = "Field 'key' with value '1' is unknown"; |
634
|
|
|
$this->assertSame($expected, $e->getMessage()); |
635
|
|
|
} |
636
|
|
|
} |
637
|
|
|
} |
638
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: