1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ParamProcessor\Tests\Integration; |
4
|
|
|
|
5
|
|
|
use ParamProcessor\Options; |
6
|
|
|
use ParamProcessor\ParamDefinitionFactory; |
7
|
|
|
use ParamProcessor\ProcessingResult; |
8
|
|
|
use ParamProcessor\Processor; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @covers \ParamProcessor\Processor |
13
|
|
|
* |
14
|
|
|
* @licence GNU GPL v2+ |
15
|
|
|
* @author Jeroen De Dauw < [email protected] > |
16
|
|
|
*/ |
17
|
|
|
class ProcessorTest extends TestCase { |
18
|
|
|
|
19
|
|
|
public function newFromOptionsProvider() { |
20
|
|
|
$options = []; |
21
|
|
|
|
22
|
|
|
$option = new Options(); |
23
|
|
|
|
24
|
|
|
$options[] = clone $option; |
25
|
|
|
|
26
|
|
|
$option->setName( 'foobar' ); |
27
|
|
|
$option->setLowercaseNames( false ); |
28
|
|
|
|
29
|
|
|
$options[] = clone $option; |
30
|
|
|
|
31
|
|
|
return $this->arrayWrap( $options ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
private function arrayWrap( array $elements ) { |
35
|
|
|
return array_map( |
36
|
|
|
function( $element ) { |
37
|
|
|
return [ $element ]; |
38
|
|
|
}, |
39
|
|
|
$elements |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function testNewFromOptions() { |
44
|
|
|
$this->assertEquals( new Options(), Processor::newFromOptions( new Options() )->getOptions() ); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Simple parameter definitions and values that should all pass. |
49
|
|
|
* |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
private function getSimpleParams() { |
53
|
|
|
$params = [ |
54
|
|
|
'awesome' => 'yes', |
55
|
|
|
'Howmuch ' => '9001', |
56
|
|
|
'FLOAT' => '4.2', |
57
|
|
|
' page' => 'Ohi there!', |
58
|
|
|
' text ' => 'foo bar baz o_O', |
59
|
|
|
]; |
60
|
|
|
|
61
|
|
|
$definitions = [ |
62
|
|
|
'awesome' => [ |
63
|
|
|
'type' => 'boolean', |
64
|
|
|
], |
65
|
|
|
'howmuch' => [ |
66
|
|
|
'type' => 'integer', |
67
|
|
|
], |
68
|
|
|
'float' => [ |
69
|
|
|
'type' => 'float', |
70
|
|
|
], |
71
|
|
|
'page' => [ |
72
|
|
|
'type' => 'string', |
73
|
|
|
'hastoexist' => false, |
74
|
|
|
], |
75
|
|
|
'text' => [], |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
$options = new Options(); |
79
|
|
|
|
80
|
|
|
$expected = [ |
81
|
|
|
'awesome' => true, |
82
|
|
|
'howmuch' => 9001, |
83
|
|
|
'float' => 4.2, |
84
|
|
|
'page' => 'Ohi there!', |
85
|
|
|
'text' => 'foo bar baz o_O', |
86
|
|
|
]; |
87
|
|
|
|
88
|
|
|
return [ $params, $definitions, $options, $expected ]; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Simple parameter definitions with defaults and values |
93
|
|
|
* that are invalid or missing and therefore default. |
94
|
|
|
* |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
private function getDefaultingParams() { |
98
|
|
|
$params = [ |
99
|
|
|
'awesome' => 'omg!', |
100
|
|
|
'howmuch' => 'omg!', |
101
|
|
|
'float' => 'omg!', |
102
|
|
|
'page' => 42, |
103
|
|
|
'whot?' => 'O_o', |
104
|
|
|
'integerr' => ' 9001 ', |
105
|
|
|
]; |
106
|
|
|
|
107
|
|
|
$definitions = [ |
108
|
|
|
'awesome' => [ |
109
|
|
|
'type' => 'boolean', |
110
|
|
|
'default' => true, |
111
|
|
|
], |
112
|
|
|
'howmuch' => [ |
113
|
|
|
'type' => 'integer', |
114
|
|
|
'default' => 9001, |
115
|
|
|
], |
116
|
|
|
'float' => [ |
117
|
|
|
'type' => 'float', |
118
|
|
|
'default' => 4.2, |
119
|
|
|
], |
120
|
|
|
'page' => [ |
121
|
|
|
'type' => 'string', |
122
|
|
|
'hastoexist' => false, |
123
|
|
|
'default' => 'Ohi there!', |
124
|
|
|
], |
125
|
|
|
'text' => [ |
126
|
|
|
'default' => 'foo bar baz o_O', |
127
|
|
|
], |
128
|
|
|
'integerr' => [ |
129
|
|
|
'type' => 'integer', |
130
|
|
|
'default' => 42, |
131
|
|
|
], |
132
|
|
|
]; |
133
|
|
|
|
134
|
|
|
$options = new Options(); |
135
|
|
|
$options->setTrimValues( false ); |
136
|
|
|
|
137
|
|
|
$expected = [ |
138
|
|
|
'awesome' => true, |
139
|
|
|
'howmuch' => 9001, |
140
|
|
|
'float' => 4.2, |
141
|
|
|
'page' => 'Ohi there!', |
142
|
|
|
'text' => 'foo bar baz o_O', |
143
|
|
|
'integerr' => 42, |
144
|
|
|
]; |
145
|
|
|
|
146
|
|
|
return [ $params, $definitions, $options, $expected ]; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Values and definitions in-system parameter handling. |
151
|
|
|
* Options set to expect non-raw values. |
152
|
|
|
* |
153
|
|
|
* @return array |
154
|
|
|
*/ |
155
|
|
|
private function getTypedParams() { |
156
|
|
|
$params = [ |
157
|
|
|
'awesome' => true, |
158
|
|
|
'howmuch' => '42', |
159
|
|
|
'float' => 4.2, |
160
|
|
|
'page' => 'Ohi there!', |
161
|
|
|
'Text' => 'foo bar baz o_O', |
162
|
|
|
'text1 ' => 'foo bar baz o_O', |
163
|
|
|
' text2' => 'foo bar baz o_O', |
164
|
|
|
]; |
165
|
|
|
|
166
|
|
|
$definitions = [ |
167
|
|
|
'awesome' => [ |
168
|
|
|
'type' => 'boolean', |
169
|
|
|
], |
170
|
|
|
'howmuch' => [ |
171
|
|
|
'type' => 'integer', |
172
|
|
|
'default' => 9001, |
173
|
|
|
], |
174
|
|
|
'float' => [ |
175
|
|
|
'type' => 'float', |
176
|
|
|
'lowerbound' => 9001, |
177
|
|
|
'default' => 9000.1 |
178
|
|
|
], |
179
|
|
|
'page' => [ |
180
|
|
|
'type' => 'string', |
181
|
|
|
'hastoexist' => false, |
182
|
|
|
], |
183
|
|
|
'text' => [ |
184
|
|
|
'default' => 'some text', |
185
|
|
|
], |
186
|
|
|
'text1' => [ |
187
|
|
|
'default' => 'some text', |
188
|
|
|
], |
189
|
|
|
'text2' => [ |
190
|
|
|
'default' => 'some text', |
191
|
|
|
], |
192
|
|
|
]; |
193
|
|
|
|
194
|
|
|
$options = new Options(); |
195
|
|
|
$options->setRawStringInputs( false ); |
196
|
|
|
$options->setLowercaseNames( false ); |
197
|
|
|
$options->setTrimNames( false ); |
198
|
|
|
|
199
|
|
|
$expected = [ |
200
|
|
|
'awesome' => true, |
201
|
|
|
'howmuch' => 9001, |
202
|
|
|
'float' => 9000.1, |
203
|
|
|
'page' => 'Ohi there!', |
204
|
|
|
'text' => 'some text', |
205
|
|
|
'text1' => 'some text', |
206
|
|
|
'text2' => 'some text', |
207
|
|
|
]; |
208
|
|
|
|
209
|
|
|
return [ $params, $definitions, $options, $expected ]; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Values with capitalization and preceding/tailing spaces to test |
214
|
|
|
* of the clean options work. |
215
|
|
|
* |
216
|
|
|
* @return array |
217
|
|
|
*/ |
218
|
|
|
private function getUncleanParams() { |
219
|
|
|
$params = [ |
220
|
|
|
'awesome' => ' yes ', |
221
|
|
|
'text' => ' FOO bar ', |
222
|
|
|
'integerr' => ' 9001 ', |
223
|
|
|
]; |
224
|
|
|
|
225
|
|
|
$definitions = [ |
226
|
|
|
'awesome' => [ |
227
|
|
|
'type' => 'boolean', |
228
|
|
|
], |
229
|
|
|
'text' => [ |
230
|
|
|
'default' => 'bar', |
231
|
|
|
], |
232
|
|
|
'integerr' => [ |
233
|
|
|
'type' => 'integer', |
234
|
|
|
'default' => 42, |
235
|
|
|
], |
236
|
|
|
]; |
237
|
|
|
|
238
|
|
|
$options = new Options(); |
239
|
|
|
$options->setLowercaseValues( true ); |
240
|
|
|
$options->setTrimValues( true ); |
241
|
|
|
|
242
|
|
|
$expected = [ |
243
|
|
|
'awesome' => true, |
244
|
|
|
'text' => 'foo bar', |
245
|
|
|
'integerr' => 9001, |
246
|
|
|
]; |
247
|
|
|
|
248
|
|
|
return [ $params, $definitions, $options, $expected ]; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* List parameters to test if list handling works correctly. |
253
|
|
|
* |
254
|
|
|
* @return array |
255
|
|
|
*/ |
256
|
|
|
private function getListParams() { |
257
|
|
|
$params = [ |
258
|
|
|
'awesome' => ' yes, no, on, off ', |
259
|
|
|
'float' => ' 9001 ; 42 ; 4.2;0', |
260
|
|
|
]; |
261
|
|
|
|
262
|
|
|
$definitions = [ |
263
|
|
|
'awesome' => [ |
264
|
|
|
'type' => 'boolean', |
265
|
|
|
'islist' => true, |
266
|
|
|
], |
267
|
|
|
'text' => [ |
268
|
|
|
'default' => [ 'bar' ], |
269
|
|
|
'islist' => true, |
270
|
|
|
], |
271
|
|
|
'float' => [ |
272
|
|
|
'type' => 'float', |
273
|
|
|
'islist' => true, |
274
|
|
|
'delimiter' => ';' |
275
|
|
|
], |
276
|
|
|
]; |
277
|
|
|
|
278
|
|
|
$options = new Options(); |
279
|
|
|
$options->setLowercaseValues( true ); |
280
|
|
|
$options->setTrimValues( true ); |
281
|
|
|
|
282
|
|
|
$expected = [ |
283
|
|
|
'awesome' => [ true, false, true, false ], |
284
|
|
|
'text' => [ 'bar' ], |
285
|
|
|
'float' => [ 9001.0, 42.0, 4.2, 0.0 ], |
286
|
|
|
]; |
287
|
|
|
|
288
|
|
|
return [ $params, $definitions, $options, $expected ]; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
public function parameterProvider() { |
292
|
|
|
// $params, $definitions [, $options] |
293
|
|
|
$argLists = []; |
294
|
|
|
|
295
|
|
|
$argLists[] = $this->getSimpleParams(); |
296
|
|
|
|
297
|
|
|
$argLists[] = $this->getDefaultingParams(); |
298
|
|
|
|
299
|
|
|
$argLists[] = $this->getTypedParams(); |
300
|
|
|
|
301
|
|
|
$argLists[] = $this->getUncleanParams(); |
302
|
|
|
|
303
|
|
|
$argLists[] = $this->getListParams(); |
304
|
|
|
|
305
|
|
|
foreach ( $argLists as &$argList ) { |
306
|
|
|
foreach ( $argList[1] as $key => &$definition ) { |
307
|
|
|
$definition['message'] = 'test-' . $key; |
308
|
|
|
} |
309
|
|
|
|
310
|
|
|
if ( !array_key_exists( 2, $argList ) ) { |
311
|
|
|
$argList[2] = new Options(); |
312
|
|
|
} |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
return $argLists; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* @dataProvider parameterProvider |
320
|
|
|
*/ |
321
|
|
|
public function testSetParameters( array $params, array $definitions, Options $options ) { |
322
|
|
|
$validator = Processor::newFromOptions( $options ); |
323
|
|
|
|
324
|
|
|
$validator->setParameters( $params, $definitions ); |
325
|
|
|
|
326
|
|
|
$this->assertTrue( true ); // TODO |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* @dataProvider parameterProvider |
331
|
|
|
*/ |
332
|
|
|
public function testValidateParameters( array $params, array $definitions, Options $options, array $expected = [] ) { |
333
|
|
|
$validator = Processor::newFromOptions( $options ); |
334
|
|
|
|
335
|
|
|
$validator->setParameters( $params, $definitions ); |
336
|
|
|
|
337
|
|
|
$processingResult = $validator->processParameters(); |
338
|
|
|
|
339
|
|
|
$actualValues = []; |
340
|
|
|
|
341
|
|
|
foreach ( $processingResult->getParameters() as $param ) { |
342
|
|
|
$actualValues[$param->getName()] = $param->getValue(); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
$this->assertEquals( $expected, $actualValues ); |
346
|
|
|
|
347
|
|
|
|
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
public function testProcessParametersOnEmptyOptions() { |
351
|
|
|
$processor = Processor::newDefault(); |
352
|
|
|
|
353
|
|
|
$this->assertInstanceOf( |
354
|
|
|
ProcessingResult::class, |
355
|
|
|
$processor->processParameters() |
356
|
|
|
); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
public function testErrorsCanBeRetrievedAfterProcessing() { |
360
|
|
|
$processor = Processor::newDefault(); |
361
|
|
|
|
362
|
|
|
$this->processWithOneError( $processor ); |
363
|
|
|
|
364
|
|
|
$this->assertCount( 1, $processor->getErrors() ); |
365
|
|
|
} |
366
|
|
|
|
367
|
|
|
private function processWithOneError( Processor $processor ) { |
368
|
|
|
$processor->setParameters( |
369
|
|
|
[], |
370
|
|
|
[ |
371
|
|
|
'awesome' => [ |
372
|
|
|
'type' => 'boolean', |
373
|
|
|
'message' => 'test-awesome' |
374
|
|
|
], |
375
|
|
|
] |
376
|
|
|
); |
377
|
|
|
|
378
|
|
|
// There should be a single "missing required parameter" error. |
379
|
|
|
$processor->processParameters(); |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
public function testErrorsAreClearedBetweenProcessingRuns() { |
383
|
|
|
$processor = Processor::newDefault(); |
384
|
|
|
|
385
|
|
|
$this->processWithOneError( $processor ); |
386
|
|
|
$processor->setParameters( [], [] ); |
387
|
|
|
$processor->processParameters(); |
388
|
|
|
|
389
|
|
|
$this->assertEmpty( $processor->getErrors() ); |
390
|
|
|
} |
391
|
|
|
|
392
|
|
|
public function testInvalidListElementsAreOmitted() { |
393
|
|
|
$processor = Processor::newDefault(); |
394
|
|
|
|
395
|
|
|
$processor->setFunctionParams( |
396
|
|
|
[ |
397
|
|
|
'some-list=1,2,3, ,4,' |
398
|
|
|
], |
399
|
|
|
[ |
400
|
|
|
'some-list' => [ |
401
|
|
|
'type' => 'integer', |
402
|
|
|
'message' => 'test', |
403
|
|
|
'islist' => true |
404
|
|
|
], |
405
|
|
|
] |
406
|
|
|
); |
407
|
|
|
|
408
|
|
|
$this->assertSame( |
409
|
|
|
[ 1, 2, 3, 4 ], |
410
|
|
|
$processor->processParameters()->getParameters()['some-list']->getValue() |
411
|
|
|
); |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
public function testListParametersAreNotDefaultedWhenSomeElementsAreInvalid() { |
415
|
|
|
$processor = Processor::newDefault(); |
416
|
|
|
|
417
|
|
|
$processor->setFunctionParams( |
418
|
|
|
[ |
419
|
|
|
'some-list=1,nan' |
420
|
|
|
], |
421
|
|
|
[ |
422
|
|
|
'some-list' => [ |
423
|
|
|
'type' => 'integer', |
424
|
|
|
'message' => 'test', |
425
|
|
|
'islist' => true, |
426
|
|
|
'default' => [] |
427
|
|
|
], |
428
|
|
|
] |
429
|
|
|
); |
430
|
|
|
|
431
|
|
|
$this->assertSame( |
432
|
|
|
[ 1 ], |
433
|
|
|
$processor->processParameters()->getParameters()['some-list']->getValue() |
434
|
|
|
); |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
public function testListParametersAreDefaultedWhenAllElementsAreInvalid() { |
438
|
|
|
$processor = Processor::newDefault(); |
439
|
|
|
|
440
|
|
|
$processor->setFunctionParams( |
441
|
|
|
[ |
442
|
|
|
'some-list=such,nan' |
443
|
|
|
], |
444
|
|
|
[ |
445
|
|
|
'some-list' => [ |
446
|
|
|
'type' => 'integer', |
447
|
|
|
'message' => 'test', |
448
|
|
|
'islist' => true, |
449
|
|
|
'default' => [ 42 ] |
450
|
|
|
], |
451
|
|
|
] |
452
|
|
|
); |
453
|
|
|
|
454
|
|
|
$this->assertSame( |
455
|
|
|
[ 42 ], |
456
|
|
|
$processor->processParameters()->getParameters()['some-list']->getValue() |
457
|
|
|
); |
458
|
|
|
} |
459
|
|
|
|
460
|
|
|
public function testSetParameterDefinitions() { |
461
|
|
|
$processor = Processor::newDefault(); |
462
|
|
|
|
463
|
|
|
$processor->setFunctionParams( [ 'some-list=42,23,9001' ] ); |
464
|
|
|
|
465
|
|
|
$processor->setParameterDefinitions( |
466
|
|
|
[ |
467
|
|
|
( ParamDefinitionFactory::newDefault() )->newDefinitionFromArray( |
468
|
|
|
[ |
469
|
|
|
'name' => 'some-list', |
470
|
|
|
'type' => 'integer', |
471
|
|
|
'message' => 'test', |
472
|
|
|
'islist' => true |
473
|
|
|
] |
474
|
|
|
) |
475
|
|
|
] |
476
|
|
|
); |
477
|
|
|
|
478
|
|
|
$this->assertSame( |
479
|
|
|
[ 42, 23, 9001 ], |
480
|
|
|
$processor->processParameters()->getParameters()['some-list']->getValue() |
481
|
|
|
); |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
} |
485
|
|
|
|