These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace ParamProcessor\Tests; |
||
4 | |||
5 | use ParamProcessor\ProcessingError; |
||
6 | use ParamProcessor\ProcessingResult; |
||
7 | use ParamProcessor\Processor; |
||
8 | use ParamProcessor\Options; |
||
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 testNewDefault() { |
||
20 | $this->assertInstanceOf( 'ParamProcessor\Processor', Processor::newDefault() ); |
||
21 | } |
||
22 | |||
23 | public function newFromOptionsProvider() { |
||
24 | $options = []; |
||
25 | |||
26 | $option = new Options(); |
||
27 | |||
28 | $options[] = clone $option; |
||
29 | |||
30 | $option->setName( 'foobar' ); |
||
31 | $option->setLowercaseNames( false ); |
||
32 | |||
33 | $options[] = clone $option; |
||
34 | |||
35 | return $this->arrayWrap( $options ); |
||
36 | } |
||
37 | |||
38 | private function arrayWrap( array $elements ) { |
||
39 | return array_map( |
||
40 | function( $element ) { |
||
41 | return [ $element ]; |
||
42 | }, |
||
43 | $elements |
||
44 | ); |
||
45 | } |
||
46 | |||
47 | public function testNewFromOptions() { |
||
48 | $options = new Options(); |
||
49 | $validator = Processor::newFromOptions( clone $options ); |
||
50 | $this->assertInstanceOf( '\ParamProcessor\Processor', $validator ); |
||
51 | $this->assertEquals( $options, $validator->getOptions() ); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * Simple parameter definitions and values that should all pass. |
||
56 | * |
||
57 | * @return array |
||
58 | */ |
||
59 | private function getSimpleParams() { |
||
60 | $params = [ |
||
61 | 'awesome' => 'yes', |
||
62 | 'Howmuch ' => '9001', |
||
63 | 'FLOAT' => '4.2', |
||
64 | ' page' => 'Ohi there!', |
||
65 | ' text ' => 'foo bar baz o_O', |
||
66 | ]; |
||
67 | |||
68 | $definitions = [ |
||
69 | 'awesome' => [ |
||
70 | 'type' => 'boolean', |
||
71 | ], |
||
72 | 'howmuch' => [ |
||
73 | 'type' => 'integer', |
||
74 | ], |
||
75 | 'float' => [ |
||
76 | 'type' => 'float', |
||
77 | ], |
||
78 | 'page' => [ |
||
79 | 'type' => 'string', |
||
80 | 'hastoexist' => false, |
||
81 | ], |
||
82 | 'text' => [], |
||
83 | ]; |
||
84 | |||
85 | $options = new Options(); |
||
86 | |||
87 | $expected = [ |
||
88 | 'awesome' => true, |
||
89 | 'howmuch' => 9001, |
||
90 | 'float' => 4.2, |
||
91 | 'page' => 'Ohi there!', |
||
92 | 'text' => 'foo bar baz o_O', |
||
93 | ]; |
||
94 | |||
95 | return [ $params, $definitions, $options, $expected ]; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Simple parameter definitions with defaults and values |
||
100 | * that are invalid or missing and therefore default. |
||
101 | * |
||
102 | * @return array |
||
103 | */ |
||
104 | private function getDefaultingParams() { |
||
105 | $params = [ |
||
106 | 'awesome' => 'omg!', |
||
107 | 'howmuch' => 'omg!', |
||
108 | 'float' => 'omg!', |
||
109 | 'page' => 42, |
||
110 | 'whot?' => 'O_o', |
||
111 | 'integerr' => ' 9001 ', |
||
112 | ]; |
||
113 | |||
114 | $definitions = [ |
||
115 | 'awesome' => [ |
||
116 | 'type' => 'boolean', |
||
117 | 'default' => true, |
||
118 | ], |
||
119 | 'howmuch' => [ |
||
120 | 'type' => 'integer', |
||
121 | 'default' => 9001, |
||
122 | ], |
||
123 | 'float' => [ |
||
124 | 'type' => 'float', |
||
125 | 'default' => 4.2, |
||
126 | ], |
||
127 | 'page' => [ |
||
128 | 'type' => 'string', |
||
129 | 'hastoexist' => false, |
||
130 | 'default' => 'Ohi there!', |
||
131 | ], |
||
132 | 'text' => [ |
||
133 | 'default' => 'foo bar baz o_O', |
||
134 | ], |
||
135 | 'integerr' => [ |
||
136 | 'type' => 'integer', |
||
137 | 'default' => 42, |
||
138 | ], |
||
139 | ]; |
||
140 | |||
141 | $options = new Options(); |
||
142 | $options->setTrimValues( false ); |
||
143 | |||
144 | $expected = [ |
||
145 | 'awesome' => true, |
||
146 | 'howmuch' => 9001, |
||
147 | 'float' => 4.2, |
||
148 | 'page' => 'Ohi there!', |
||
149 | 'text' => 'foo bar baz o_O', |
||
150 | 'integerr' => 42, |
||
151 | ]; |
||
152 | |||
153 | return [ $params, $definitions, $options, $expected ]; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * Values and definitions in-system parameter handling. |
||
158 | * Options set to expect non-raw values. |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | private function getTypedParams() { |
||
163 | $params = [ |
||
164 | 'awesome' => true, |
||
165 | 'howmuch' => '42', |
||
166 | 'float' => 4.2, |
||
167 | 'page' => 'Ohi there!', |
||
168 | 'Text' => 'foo bar baz o_O', |
||
169 | 'text1 ' => 'foo bar baz o_O', |
||
170 | ' text2' => 'foo bar baz o_O', |
||
171 | ]; |
||
172 | |||
173 | $definitions = [ |
||
174 | 'awesome' => [ |
||
175 | 'type' => 'boolean', |
||
176 | ], |
||
177 | 'howmuch' => [ |
||
178 | 'type' => 'integer', |
||
179 | 'default' => 9001, |
||
180 | ], |
||
181 | 'float' => [ |
||
182 | 'type' => 'float', |
||
183 | 'lowerbound' => 9001, |
||
184 | 'default' => 9000.1 |
||
185 | ], |
||
186 | 'page' => [ |
||
187 | 'type' => 'string', |
||
188 | 'hastoexist' => false, |
||
189 | ], |
||
190 | 'text' => [ |
||
191 | 'default' => 'some text', |
||
192 | ], |
||
193 | 'text1' => [ |
||
194 | 'default' => 'some text', |
||
195 | ], |
||
196 | 'text2' => [ |
||
197 | 'default' => 'some text', |
||
198 | ], |
||
199 | ]; |
||
200 | |||
201 | $options = new Options(); |
||
202 | $options->setRawStringInputs( false ); |
||
203 | $options->setLowercaseNames( false ); |
||
204 | $options->setTrimNames( false ); |
||
205 | |||
206 | $expected = [ |
||
207 | 'awesome' => true, |
||
208 | 'howmuch' => 9001, |
||
209 | 'float' => 9000.1, |
||
210 | 'page' => 'Ohi there!', |
||
211 | 'text' => 'some text', |
||
212 | 'text1' => 'some text', |
||
213 | 'text2' => 'some text', |
||
214 | ]; |
||
215 | |||
216 | return [ $params, $definitions, $options, $expected ]; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Values with capitalization and preceding/tailing spaces to test |
||
221 | * of the clean options work. |
||
222 | * |
||
223 | * @return array |
||
224 | */ |
||
225 | private function getUncleanParams() { |
||
226 | $params = [ |
||
227 | 'awesome' => ' yes ', |
||
228 | 'text' => ' FOO bar ', |
||
229 | 'integerr' => ' 9001 ', |
||
230 | ]; |
||
231 | |||
232 | $definitions = [ |
||
233 | 'awesome' => [ |
||
234 | 'type' => 'boolean', |
||
235 | ], |
||
236 | 'text' => [ |
||
237 | 'default' => 'bar', |
||
238 | ], |
||
239 | 'integerr' => [ |
||
240 | 'type' => 'integer', |
||
241 | 'default' => 42, |
||
242 | ], |
||
243 | ]; |
||
244 | |||
245 | $options = new Options(); |
||
246 | $options->setLowercaseValues( true ); |
||
247 | $options->setTrimValues( true ); |
||
248 | |||
249 | $expected = [ |
||
250 | 'awesome' => true, |
||
251 | 'text' => 'foo bar', |
||
252 | 'integerr' => 9001, |
||
253 | ]; |
||
254 | |||
255 | return [ $params, $definitions, $options, $expected ]; |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * List parameters to test if list handling works correctly. |
||
260 | * |
||
261 | * @return array |
||
262 | */ |
||
263 | private function getListParams() { |
||
264 | $params = [ |
||
265 | 'awesome' => ' yes, no, on, off ', |
||
266 | 'float' => ' 9001 ; 42 ; 4.2;0', |
||
267 | ]; |
||
268 | |||
269 | $definitions = [ |
||
270 | 'awesome' => [ |
||
271 | 'type' => 'boolean', |
||
272 | 'islist' => true, |
||
273 | ], |
||
274 | 'text' => [ |
||
275 | 'default' => [ 'bar' ], |
||
276 | 'islist' => true, |
||
277 | ], |
||
278 | 'float' => [ |
||
279 | 'type' => 'float', |
||
280 | 'islist' => true, |
||
281 | 'delimiter' => ';' |
||
282 | ], |
||
283 | ]; |
||
284 | |||
285 | $options = new Options(); |
||
286 | $options->setLowercaseValues( true ); |
||
287 | $options->setTrimValues( true ); |
||
288 | |||
289 | $expected = [ |
||
290 | 'awesome' => [ true, false, true, false ], |
||
291 | 'text' => [ 'bar' ], |
||
292 | 'float' => [ 9001.0, 42.0, 4.2, 0.0 ], |
||
293 | ]; |
||
294 | |||
295 | return [ $params, $definitions, $options, $expected ]; |
||
296 | } |
||
297 | |||
298 | public function parameterProvider() { |
||
299 | // $params, $definitions [, $options] |
||
300 | $argLists = []; |
||
301 | |||
302 | $argLists[] = $this->getSimpleParams(); |
||
303 | |||
304 | $argLists[] = $this->getDefaultingParams(); |
||
305 | |||
306 | $argLists[] = $this->getTypedParams(); |
||
307 | |||
308 | $argLists[] = $this->getUncleanParams(); |
||
309 | |||
310 | $argLists[] = $this->getListParams(); |
||
311 | |||
312 | foreach ( $argLists as &$argList ) { |
||
313 | foreach ( $argList[1] as $key => &$definition ) { |
||
314 | $definition['message'] = 'test-' . $key; |
||
315 | } |
||
316 | |||
317 | if ( !array_key_exists( 2, $argList ) ) { |
||
318 | $argList[2] = new Options(); |
||
319 | } |
||
320 | } |
||
321 | |||
322 | return $argLists; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * @dataProvider parameterProvider |
||
327 | */ |
||
328 | public function testSetParameters( array $params, array $definitions, Options $options ) { |
||
329 | $validator = Processor::newFromOptions( $options ); |
||
330 | |||
331 | $validator->setParameters( $params, $definitions ); |
||
332 | |||
333 | $this->assertTrue( true ); // TODO |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * @dataProvider parameterProvider |
||
338 | */ |
||
339 | public function testValidateParameters( array $params, array $definitions, Options $options, array $expected = [] ) { |
||
340 | $validator = Processor::newFromOptions( $options ); |
||
341 | |||
342 | $validator->setParameters( $params, $definitions ); |
||
343 | |||
344 | $processingResult = $validator->processParameters(); |
||
345 | |||
346 | $actualValues = []; |
||
347 | |||
348 | foreach ( $processingResult->getParameters() as $param ) { |
||
349 | $actualValues[$param->getName()] = $param->getValue(); |
||
350 | } |
||
351 | |||
352 | $this->assertEquals( $expected, $actualValues ); |
||
353 | |||
354 | |||
355 | } |
||
356 | |||
357 | public function testProcessParametersOnEmptyOptions() { |
||
358 | $processor = Processor::newDefault(); |
||
359 | |||
360 | $this->assertInstanceOf( |
||
361 | ProcessingResult::class, |
||
362 | $processor->processParameters() |
||
363 | ); |
||
364 | } |
||
365 | |||
366 | public function testErrorsCanBeRetrievedAfterProcessing() { |
||
367 | $processor = Processor::newDefault(); |
||
368 | |||
369 | $this->processWithOneError( $processor ); |
||
370 | |||
371 | $this->assertCount( 1, $processor->getErrors() ); |
||
0 ignored issues
–
show
|
|||
372 | } |
||
373 | |||
374 | private function processWithOneError( Processor $processor ) { |
||
375 | $processor->setParameters( |
||
376 | [], |
||
377 | [ |
||
378 | 'awesome' => [ |
||
379 | 'type' => 'boolean', |
||
380 | 'message' => 'test-awesome' |
||
381 | ], |
||
382 | ] |
||
383 | ); |
||
384 | |||
385 | // There should be a single "missing required parameter" error. |
||
386 | $processor->processParameters(); |
||
387 | } |
||
388 | |||
389 | public function testErrorsAreClearedBetweenProcessingRuns() { |
||
390 | $processor = Processor::newDefault(); |
||
391 | |||
392 | $this->processWithOneError( $processor ); |
||
393 | $processor->setParameters( [], [] ); |
||
394 | $processor->processParameters(); |
||
395 | |||
396 | $this->assertEmpty( $processor->getErrors() ); |
||
397 | } |
||
398 | |||
399 | } |
||
400 |
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: