diff options
Diffstat (limited to 'vendor/symfony/options-resolver/Symfony/Component/OptionsResolver/Tests')
2 files changed, 1210 insertions, 0 deletions
diff --git a/vendor/symfony/options-resolver/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php b/vendor/symfony/options-resolver/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php new file mode 100644 index 00000000..d50cd3fc --- /dev/null +++ b/vendor/symfony/options-resolver/Symfony/Component/OptionsResolver/Tests/OptionsResolverTest.php | |||
@@ -0,0 +1,681 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | namespace Symfony\Component\OptionsResolver\Tests; | ||
13 | |||
14 | use Symfony\Component\OptionsResolver\OptionsResolver; | ||
15 | use Symfony\Component\OptionsResolver\Options; | ||
16 | |||
17 | class OptionsResolverTest extends \PHPUnit_Framework_TestCase | ||
18 | { | ||
19 | /** | ||
20 | * @var OptionsResolver | ||
21 | */ | ||
22 | private $resolver; | ||
23 | |||
24 | protected function setUp() | ||
25 | { | ||
26 | $this->resolver = new OptionsResolver(); | ||
27 | } | ||
28 | |||
29 | public function testResolve() | ||
30 | { | ||
31 | $this->resolver->setDefaults(array( | ||
32 | 'one' => '1', | ||
33 | 'two' => '2', | ||
34 | )); | ||
35 | |||
36 | $options = array( | ||
37 | 'two' => '20', | ||
38 | ); | ||
39 | |||
40 | $this->assertEquals(array( | ||
41 | 'one' => '1', | ||
42 | 'two' => '20', | ||
43 | ), $this->resolver->resolve($options)); | ||
44 | } | ||
45 | |||
46 | public function testResolveLazy() | ||
47 | { | ||
48 | $this->resolver->setDefaults(array( | ||
49 | 'one' => '1', | ||
50 | 'two' => function (Options $options) { | ||
51 | return '20'; | ||
52 | }, | ||
53 | )); | ||
54 | |||
55 | $this->assertEquals(array( | ||
56 | 'one' => '1', | ||
57 | 'two' => '20', | ||
58 | ), $this->resolver->resolve(array())); | ||
59 | } | ||
60 | |||
61 | public function testResolveLazyDependencyOnOptional() | ||
62 | { | ||
63 | $this->resolver->setDefaults(array( | ||
64 | 'one' => '1', | ||
65 | 'two' => function (Options $options) { | ||
66 | return $options['one'].'2'; | ||
67 | }, | ||
68 | )); | ||
69 | |||
70 | $options = array( | ||
71 | 'one' => '10', | ||
72 | ); | ||
73 | |||
74 | $this->assertEquals(array( | ||
75 | 'one' => '10', | ||
76 | 'two' => '102', | ||
77 | ), $this->resolver->resolve($options)); | ||
78 | } | ||
79 | |||
80 | public function testResolveLazyDependencyOnMissingOptionalWithoutDefault() | ||
81 | { | ||
82 | $test = $this; | ||
83 | |||
84 | $this->resolver->setOptional(array( | ||
85 | 'one', | ||
86 | )); | ||
87 | |||
88 | $this->resolver->setDefaults(array( | ||
89 | 'two' => function (Options $options) use ($test) { | ||
90 | /* @var \PHPUnit_Framework_TestCase $test */ | ||
91 | $test->assertFalse(isset($options['one'])); | ||
92 | |||
93 | return '2'; | ||
94 | }, | ||
95 | )); | ||
96 | |||
97 | $options = array( | ||
98 | ); | ||
99 | |||
100 | $this->assertEquals(array( | ||
101 | 'two' => '2', | ||
102 | ), $this->resolver->resolve($options)); | ||
103 | } | ||
104 | |||
105 | public function testResolveLazyDependencyOnOptionalWithoutDefault() | ||
106 | { | ||
107 | $test = $this; | ||
108 | |||
109 | $this->resolver->setOptional(array( | ||
110 | 'one', | ||
111 | )); | ||
112 | |||
113 | $this->resolver->setDefaults(array( | ||
114 | 'two' => function (Options $options) use ($test) { | ||
115 | /* @var \PHPUnit_Framework_TestCase $test */ | ||
116 | $test->assertTrue(isset($options['one'])); | ||
117 | |||
118 | return $options['one'].'2'; | ||
119 | }, | ||
120 | )); | ||
121 | |||
122 | $options = array( | ||
123 | 'one' => '10', | ||
124 | ); | ||
125 | |||
126 | $this->assertEquals(array( | ||
127 | 'one' => '10', | ||
128 | 'two' => '102', | ||
129 | ), $this->resolver->resolve($options)); | ||
130 | } | ||
131 | |||
132 | public function testResolveLazyDependencyOnRequired() | ||
133 | { | ||
134 | $this->resolver->setRequired(array( | ||
135 | 'one', | ||
136 | )); | ||
137 | $this->resolver->setDefaults(array( | ||
138 | 'two' => function (Options $options) { | ||
139 | return $options['one'].'2'; | ||
140 | }, | ||
141 | )); | ||
142 | |||
143 | $options = array( | ||
144 | 'one' => '10', | ||
145 | ); | ||
146 | |||
147 | $this->assertEquals(array( | ||
148 | 'one' => '10', | ||
149 | 'two' => '102', | ||
150 | ), $this->resolver->resolve($options)); | ||
151 | } | ||
152 | |||
153 | public function testResolveLazyReplaceDefaults() | ||
154 | { | ||
155 | $test = $this; | ||
156 | |||
157 | $this->resolver->setDefaults(array( | ||
158 | 'one' => function (Options $options) use ($test) { | ||
159 | /* @var \PHPUnit_Framework_TestCase $test */ | ||
160 | $test->fail('Previous closure should not be executed'); | ||
161 | }, | ||
162 | )); | ||
163 | |||
164 | $this->resolver->replaceDefaults(array( | ||
165 | 'one' => function (Options $options, $previousValue) { | ||
166 | return '1'; | ||
167 | }, | ||
168 | )); | ||
169 | |||
170 | $this->assertEquals(array( | ||
171 | 'one' => '1', | ||
172 | ), $this->resolver->resolve(array())); | ||
173 | } | ||
174 | |||
175 | /** | ||
176 | * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException | ||
177 | */ | ||
178 | public function testResolveFailsIfNonExistingOption() | ||
179 | { | ||
180 | $this->resolver->setDefaults(array( | ||
181 | 'one' => '1', | ||
182 | )); | ||
183 | |||
184 | $this->resolver->setRequired(array( | ||
185 | 'two', | ||
186 | )); | ||
187 | |||
188 | $this->resolver->setOptional(array( | ||
189 | 'three', | ||
190 | )); | ||
191 | |||
192 | $this->resolver->resolve(array( | ||
193 | 'foo' => 'bar', | ||
194 | )); | ||
195 | } | ||
196 | |||
197 | /** | ||
198 | * @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException | ||
199 | */ | ||
200 | public function testResolveFailsIfMissingRequiredOption() | ||
201 | { | ||
202 | $this->resolver->setRequired(array( | ||
203 | 'one', | ||
204 | )); | ||
205 | |||
206 | $this->resolver->setDefaults(array( | ||
207 | 'two' => '2', | ||
208 | )); | ||
209 | |||
210 | $this->resolver->resolve(array( | ||
211 | 'two' => '20', | ||
212 | )); | ||
213 | } | ||
214 | |||
215 | public function testResolveSucceedsIfOptionValueAllowed() | ||
216 | { | ||
217 | $this->resolver->setDefaults(array( | ||
218 | 'one' => '1', | ||
219 | )); | ||
220 | |||
221 | $this->resolver->setAllowedValues(array( | ||
222 | 'one' => array('1', 'one'), | ||
223 | )); | ||
224 | |||
225 | $options = array( | ||
226 | 'one' => 'one', | ||
227 | ); | ||
228 | |||
229 | $this->assertEquals(array( | ||
230 | 'one' => 'one', | ||
231 | ), $this->resolver->resolve($options)); | ||
232 | } | ||
233 | |||
234 | public function testResolveSucceedsIfOptionValueAllowed2() | ||
235 | { | ||
236 | $this->resolver->setDefaults(array( | ||
237 | 'one' => '1', | ||
238 | 'two' => '2', | ||
239 | )); | ||
240 | |||
241 | $this->resolver->setAllowedValues(array( | ||
242 | 'one' => '1', | ||
243 | 'two' => '2', | ||
244 | )); | ||
245 | $this->resolver->addAllowedValues(array( | ||
246 | 'one' => 'one', | ||
247 | 'two' => 'two', | ||
248 | )); | ||
249 | |||
250 | $options = array( | ||
251 | 'one' => '1', | ||
252 | 'two' => 'two', | ||
253 | ); | ||
254 | |||
255 | $this->assertEquals(array( | ||
256 | 'one' => '1', | ||
257 | 'two' => 'two', | ||
258 | ), $this->resolver->resolve($options)); | ||
259 | } | ||
260 | |||
261 | public function testResolveSucceedsIfOptionalWithAllowedValuesNotSet() | ||
262 | { | ||
263 | $this->resolver->setRequired(array( | ||
264 | 'one', | ||
265 | )); | ||
266 | |||
267 | $this->resolver->setOptional(array( | ||
268 | 'two', | ||
269 | )); | ||
270 | |||
271 | $this->resolver->setAllowedValues(array( | ||
272 | 'one' => array('1', 'one'), | ||
273 | 'two' => array('2', 'two'), | ||
274 | )); | ||
275 | |||
276 | $options = array( | ||
277 | 'one' => '1', | ||
278 | ); | ||
279 | |||
280 | $this->assertEquals(array( | ||
281 | 'one' => '1', | ||
282 | ), $this->resolver->resolve($options)); | ||
283 | } | ||
284 | |||
285 | /** | ||
286 | * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException | ||
287 | */ | ||
288 | public function testResolveFailsIfOptionValueNotAllowed() | ||
289 | { | ||
290 | $this->resolver->setDefaults(array( | ||
291 | 'one' => '1', | ||
292 | )); | ||
293 | |||
294 | $this->resolver->setAllowedValues(array( | ||
295 | 'one' => array('1', 'one'), | ||
296 | )); | ||
297 | |||
298 | $this->resolver->resolve(array( | ||
299 | 'one' => '2', | ||
300 | )); | ||
301 | } | ||
302 | |||
303 | public function testResolveSucceedsIfOptionTypeAllowed() | ||
304 | { | ||
305 | $this->resolver->setDefaults(array( | ||
306 | 'one' => '1', | ||
307 | )); | ||
308 | |||
309 | $this->resolver->setAllowedTypes(array( | ||
310 | 'one' => 'string', | ||
311 | )); | ||
312 | |||
313 | $options = array( | ||
314 | 'one' => 'one', | ||
315 | ); | ||
316 | |||
317 | $this->assertEquals(array( | ||
318 | 'one' => 'one', | ||
319 | ), $this->resolver->resolve($options)); | ||
320 | } | ||
321 | |||
322 | public function testResolveSucceedsIfOptionTypeAllowedPassArray() | ||
323 | { | ||
324 | $this->resolver->setDefaults(array( | ||
325 | 'one' => '1', | ||
326 | )); | ||
327 | |||
328 | $this->resolver->setAllowedTypes(array( | ||
329 | 'one' => array('string', 'bool'), | ||
330 | )); | ||
331 | |||
332 | $options = array( | ||
333 | 'one' => true, | ||
334 | ); | ||
335 | |||
336 | $this->assertEquals(array( | ||
337 | 'one' => true, | ||
338 | ), $this->resolver->resolve($options)); | ||
339 | } | ||
340 | |||
341 | public function testResolveSucceedsIfOptionTypeAllowedPassObject() | ||
342 | { | ||
343 | $this->resolver->setDefaults(array( | ||
344 | 'one' => '1', | ||
345 | )); | ||
346 | |||
347 | $this->resolver->setAllowedTypes(array( | ||
348 | 'one' => 'object', | ||
349 | )); | ||
350 | |||
351 | $object = new \stdClass(); | ||
352 | $options = array( | ||
353 | 'one' => $object, | ||
354 | ); | ||
355 | |||
356 | $this->assertEquals(array( | ||
357 | 'one' => $object, | ||
358 | ), $this->resolver->resolve($options)); | ||
359 | } | ||
360 | |||
361 | public function testResolveSucceedsIfOptionTypeAllowedPassClass() | ||
362 | { | ||
363 | $this->resolver->setDefaults(array( | ||
364 | 'one' => '1', | ||
365 | )); | ||
366 | |||
367 | $this->resolver->setAllowedTypes(array( | ||
368 | 'one' => '\stdClass', | ||
369 | )); | ||
370 | |||
371 | $object = new \stdClass(); | ||
372 | $options = array( | ||
373 | 'one' => $object, | ||
374 | ); | ||
375 | |||
376 | $this->assertEquals(array( | ||
377 | 'one' => $object, | ||
378 | ), $this->resolver->resolve($options)); | ||
379 | } | ||
380 | |||
381 | public function testResolveSucceedsIfOptionTypeAllowedAddTypes() | ||
382 | { | ||
383 | $this->resolver->setDefaults(array( | ||
384 | 'one' => '1', | ||
385 | 'two' => '2', | ||
386 | )); | ||
387 | |||
388 | $this->resolver->setAllowedTypes(array( | ||
389 | 'one' => 'string', | ||
390 | 'two' => 'bool', | ||
391 | )); | ||
392 | $this->resolver->addAllowedTypes(array( | ||
393 | 'one' => 'float', | ||
394 | 'two' => 'integer', | ||
395 | )); | ||
396 | |||
397 | $options = array( | ||
398 | 'one' => 1.23, | ||
399 | 'two' => false, | ||
400 | ); | ||
401 | |||
402 | $this->assertEquals(array( | ||
403 | 'one' => 1.23, | ||
404 | 'two' => false, | ||
405 | ), $this->resolver->resolve($options)); | ||
406 | } | ||
407 | |||
408 | public function testResolveSucceedsIfOptionalWithTypeAndWithoutValue() | ||
409 | { | ||
410 | $this->resolver->setOptional(array( | ||
411 | 'one', | ||
412 | 'two', | ||
413 | )); | ||
414 | |||
415 | $this->resolver->setAllowedTypes(array( | ||
416 | 'one' => 'string', | ||
417 | 'two' => 'int', | ||
418 | )); | ||
419 | |||
420 | $options = array( | ||
421 | 'two' => 1, | ||
422 | ); | ||
423 | |||
424 | $this->assertEquals(array( | ||
425 | 'two' => 1, | ||
426 | ), $this->resolver->resolve($options)); | ||
427 | } | ||
428 | |||
429 | /** | ||
430 | * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException | ||
431 | */ | ||
432 | public function testResolveFailsIfOptionTypeNotAllowed() | ||
433 | { | ||
434 | $this->resolver->setDefaults(array( | ||
435 | 'one' => '1', | ||
436 | )); | ||
437 | |||
438 | $this->resolver->setAllowedTypes(array( | ||
439 | 'one' => array('string', 'bool'), | ||
440 | )); | ||
441 | |||
442 | $this->resolver->resolve(array( | ||
443 | 'one' => 1.23, | ||
444 | )); | ||
445 | } | ||
446 | |||
447 | /** | ||
448 | * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException | ||
449 | */ | ||
450 | public function testResolveFailsIfOptionTypeNotAllowedMultipleOptions() | ||
451 | { | ||
452 | $this->resolver->setDefaults(array( | ||
453 | 'one' => '1', | ||
454 | 'two' => '2', | ||
455 | )); | ||
456 | |||
457 | $this->resolver->setAllowedTypes(array( | ||
458 | 'one' => 'string', | ||
459 | 'two' => 'bool', | ||
460 | )); | ||
461 | |||
462 | $this->resolver->resolve(array( | ||
463 | 'one' => 'foo', | ||
464 | 'two' => 1.23, | ||
465 | )); | ||
466 | } | ||
467 | |||
468 | /** | ||
469 | * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException | ||
470 | */ | ||
471 | public function testResolveFailsIfOptionTypeNotAllowedAddTypes() | ||
472 | { | ||
473 | $this->resolver->setDefaults(array( | ||
474 | 'one' => '1', | ||
475 | )); | ||
476 | |||
477 | $this->resolver->setAllowedTypes(array( | ||
478 | 'one' => 'string', | ||
479 | )); | ||
480 | $this->resolver->addAllowedTypes(array( | ||
481 | 'one' => 'bool', | ||
482 | )); | ||
483 | |||
484 | $this->resolver->resolve(array( | ||
485 | 'one' => 1.23, | ||
486 | )); | ||
487 | } | ||
488 | |||
489 | /** | ||
490 | * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException | ||
491 | */ | ||
492 | public function testSetRequiredFailsIfDefaultIsPassed() | ||
493 | { | ||
494 | $this->resolver->setRequired(array( | ||
495 | 'one' => '1', | ||
496 | )); | ||
497 | } | ||
498 | |||
499 | /** | ||
500 | * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException | ||
501 | */ | ||
502 | public function testSetOptionalFailsIfDefaultIsPassed() | ||
503 | { | ||
504 | $this->resolver->setOptional(array( | ||
505 | 'one' => '1', | ||
506 | )); | ||
507 | } | ||
508 | |||
509 | public function testFluidInterface() | ||
510 | { | ||
511 | $this->resolver->setDefaults(array('one' => '1')) | ||
512 | ->replaceDefaults(array('one' => '2')) | ||
513 | ->setAllowedValues(array('one' => array('1', '2'))) | ||
514 | ->addAllowedValues(array('one' => array('3'))) | ||
515 | ->setRequired(array('two')) | ||
516 | ->setOptional(array('three')); | ||
517 | |||
518 | $options = array( | ||
519 | 'two' => '2', | ||
520 | ); | ||
521 | |||
522 | $this->assertEquals(array( | ||
523 | 'one' => '2', | ||
524 | 'two' => '2', | ||
525 | ), $this->resolver->resolve($options)); | ||
526 | } | ||
527 | |||
528 | public function testKnownIfDefaultWasSet() | ||
529 | { | ||
530 | $this->assertFalse($this->resolver->isKnown('foo')); | ||
531 | |||
532 | $this->resolver->setDefaults(array( | ||
533 | 'foo' => 'bar', | ||
534 | )); | ||
535 | |||
536 | $this->assertTrue($this->resolver->isKnown('foo')); | ||
537 | } | ||
538 | |||
539 | public function testKnownIfRequired() | ||
540 | { | ||
541 | $this->assertFalse($this->resolver->isKnown('foo')); | ||
542 | |||
543 | $this->resolver->setRequired(array( | ||
544 | 'foo', | ||
545 | )); | ||
546 | |||
547 | $this->assertTrue($this->resolver->isKnown('foo')); | ||
548 | } | ||
549 | |||
550 | public function testKnownIfOptional() | ||
551 | { | ||
552 | $this->assertFalse($this->resolver->isKnown('foo')); | ||
553 | |||
554 | $this->resolver->setOptional(array( | ||
555 | 'foo', | ||
556 | )); | ||
557 | |||
558 | $this->assertTrue($this->resolver->isKnown('foo')); | ||
559 | } | ||
560 | |||
561 | public function testRequiredIfRequired() | ||
562 | { | ||
563 | $this->assertFalse($this->resolver->isRequired('foo')); | ||
564 | |||
565 | $this->resolver->setRequired(array( | ||
566 | 'foo', | ||
567 | )); | ||
568 | |||
569 | $this->assertTrue($this->resolver->isRequired('foo')); | ||
570 | } | ||
571 | |||
572 | public function testNotRequiredIfRequiredAndDefaultValue() | ||
573 | { | ||
574 | $this->assertFalse($this->resolver->isRequired('foo')); | ||
575 | |||
576 | $this->resolver->setRequired(array( | ||
577 | 'foo', | ||
578 | )); | ||
579 | $this->resolver->setDefaults(array( | ||
580 | 'foo' => 'bar', | ||
581 | )); | ||
582 | |||
583 | $this->assertFalse($this->resolver->isRequired('foo')); | ||
584 | } | ||
585 | |||
586 | public function testNormalizersTransformFinalOptions() | ||
587 | { | ||
588 | $this->resolver->setDefaults(array( | ||
589 | 'foo' => 'bar', | ||
590 | 'bam' => 'baz', | ||
591 | )); | ||
592 | $this->resolver->setNormalizers(array( | ||
593 | 'foo' => function (Options $options, $value) { | ||
594 | return $options['bam'].'['.$value.']'; | ||
595 | }, | ||
596 | )); | ||
597 | |||
598 | $expected = array( | ||
599 | 'foo' => 'baz[bar]', | ||
600 | 'bam' => 'baz', | ||
601 | ); | ||
602 | |||
603 | $this->assertEquals($expected, $this->resolver->resolve(array())); | ||
604 | |||
605 | $expected = array( | ||
606 | 'foo' => 'boo[custom]', | ||
607 | 'bam' => 'boo', | ||
608 | ); | ||
609 | |||
610 | $this->assertEquals($expected, $this->resolver->resolve(array( | ||
611 | 'foo' => 'custom', | ||
612 | 'bam' => 'boo', | ||
613 | ))); | ||
614 | } | ||
615 | |||
616 | public function testResolveWithoutOptionSucceedsIfRequiredAndDefaultValue() | ||
617 | { | ||
618 | $this->resolver->setRequired(array( | ||
619 | 'foo', | ||
620 | )); | ||
621 | $this->resolver->setDefaults(array( | ||
622 | 'foo' => 'bar', | ||
623 | )); | ||
624 | |||
625 | $this->assertEquals(array( | ||
626 | 'foo' => 'bar' | ||
627 | ), $this->resolver->resolve(array())); | ||
628 | } | ||
629 | |||
630 | public function testResolveWithoutOptionSucceedsIfDefaultValueAndRequired() | ||
631 | { | ||
632 | $this->resolver->setDefaults(array( | ||
633 | 'foo' => 'bar', | ||
634 | )); | ||
635 | $this->resolver->setRequired(array( | ||
636 | 'foo', | ||
637 | )); | ||
638 | |||
639 | $this->assertEquals(array( | ||
640 | 'foo' => 'bar' | ||
641 | ), $this->resolver->resolve(array())); | ||
642 | } | ||
643 | |||
644 | public function testResolveSucceedsIfOptionRequiredAndValueAllowed() | ||
645 | { | ||
646 | $this->resolver->setRequired(array( | ||
647 | 'one', 'two', | ||
648 | )); | ||
649 | $this->resolver->setAllowedValues(array( | ||
650 | 'two' => array('2'), | ||
651 | )); | ||
652 | |||
653 | $options = array( | ||
654 | 'one' => '1', | ||
655 | 'two' => '2' | ||
656 | ); | ||
657 | |||
658 | $this->assertEquals($options, $this->resolver->resolve($options)); | ||
659 | } | ||
660 | |||
661 | public function testClone() | ||
662 | { | ||
663 | $this->resolver->setDefaults(array('one' => '1')); | ||
664 | |||
665 | $clone = clone $this->resolver; | ||
666 | |||
667 | // Changes after cloning don't affect each other | ||
668 | $this->resolver->setDefaults(array('two' => '2')); | ||
669 | $clone->setDefaults(array('three' => '3')); | ||
670 | |||
671 | $this->assertEquals(array( | ||
672 | 'one' => '1', | ||
673 | 'two' => '2', | ||
674 | ), $this->resolver->resolve()); | ||
675 | |||
676 | $this->assertEquals(array( | ||
677 | 'one' => '1', | ||
678 | 'three' => '3', | ||
679 | ), $clone->resolve()); | ||
680 | } | ||
681 | } | ||
diff --git a/vendor/symfony/options-resolver/Symfony/Component/OptionsResolver/Tests/OptionsTest.php b/vendor/symfony/options-resolver/Symfony/Component/OptionsResolver/Tests/OptionsTest.php new file mode 100644 index 00000000..e24a7647 --- /dev/null +++ b/vendor/symfony/options-resolver/Symfony/Component/OptionsResolver/Tests/OptionsTest.php | |||
@@ -0,0 +1,529 @@ | |||
1 | <?php | ||
2 | |||
3 | /* | ||
4 | * This file is part of the Symfony package. | ||
5 | * | ||
6 | * (c) Fabien Potencier <fabien@symfony.com> | ||
7 | * | ||
8 | * For the full copyright and license information, please view the LICENSE | ||
9 | * file that was distributed with this source code. | ||
10 | */ | ||
11 | |||
12 | namespace Symfony\Component\OptionsResolver\Tests; | ||
13 | |||
14 | use Symfony\Component\OptionsResolver\Options; | ||
15 | |||
16 | class OptionsTest extends \PHPUnit_Framework_TestCase | ||
17 | { | ||
18 | /** | ||
19 | * @var Options | ||
20 | */ | ||
21 | private $options; | ||
22 | |||
23 | protected function setUp() | ||
24 | { | ||
25 | $this->options = new Options(); | ||
26 | } | ||
27 | |||
28 | public function testArrayAccess() | ||
29 | { | ||
30 | $this->assertFalse(isset($this->options['foo'])); | ||
31 | $this->assertFalse(isset($this->options['bar'])); | ||
32 | |||
33 | $this->options['foo'] = 0; | ||
34 | $this->options['bar'] = 1; | ||
35 | |||
36 | $this->assertTrue(isset($this->options['foo'])); | ||
37 | $this->assertTrue(isset($this->options['bar'])); | ||
38 | |||
39 | unset($this->options['bar']); | ||
40 | |||
41 | $this->assertTrue(isset($this->options['foo'])); | ||
42 | $this->assertFalse(isset($this->options['bar'])); | ||
43 | $this->assertEquals(0, $this->options['foo']); | ||
44 | } | ||
45 | |||
46 | public function testCountable() | ||
47 | { | ||
48 | $this->options->set('foo', 0); | ||
49 | $this->options->set('bar', 1); | ||
50 | |||
51 | $this->assertCount(2, $this->options); | ||
52 | } | ||
53 | |||
54 | /** | ||
55 | * @expectedException \OutOfBoundsException | ||
56 | */ | ||
57 | public function testGetNonExisting() | ||
58 | { | ||
59 | $this->options->get('foo'); | ||
60 | } | ||
61 | |||
62 | /** | ||
63 | * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException | ||
64 | */ | ||
65 | public function testSetNotSupportedAfterGet() | ||
66 | { | ||
67 | $this->options->set('foo', 'bar'); | ||
68 | $this->options->get('foo'); | ||
69 | $this->options->set('foo', 'baz'); | ||
70 | } | ||
71 | |||
72 | /** | ||
73 | * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException | ||
74 | */ | ||
75 | public function testRemoveNotSupportedAfterGet() | ||
76 | { | ||
77 | $this->options->set('foo', 'bar'); | ||
78 | $this->options->get('foo'); | ||
79 | $this->options->remove('foo'); | ||
80 | } | ||
81 | |||
82 | /** | ||
83 | * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException | ||
84 | */ | ||
85 | public function testSetNormalizerNotSupportedAfterGet() | ||
86 | { | ||
87 | $this->options->set('foo', 'bar'); | ||
88 | $this->options->get('foo'); | ||
89 | $this->options->setNormalizer('foo', function () {}); | ||
90 | } | ||
91 | |||
92 | public function testSetLazyOption() | ||
93 | { | ||
94 | $test = $this; | ||
95 | |||
96 | $this->options->set('foo', function (Options $options) use ($test) { | ||
97 | return 'dynamic'; | ||
98 | }); | ||
99 | |||
100 | $this->assertEquals('dynamic', $this->options->get('foo')); | ||
101 | } | ||
102 | |||
103 | public function testSetDiscardsPreviousValue() | ||
104 | { | ||
105 | $test = $this; | ||
106 | |||
107 | // defined by superclass | ||
108 | $this->options->set('foo', 'bar'); | ||
109 | |||
110 | // defined by subclass | ||
111 | $this->options->set('foo', function (Options $options, $previousValue) use ($test) { | ||
112 | /* @var \PHPUnit_Framework_TestCase $test */ | ||
113 | $test->assertNull($previousValue); | ||
114 | |||
115 | return 'dynamic'; | ||
116 | }); | ||
117 | |||
118 | $this->assertEquals('dynamic', $this->options->get('foo')); | ||
119 | } | ||
120 | |||
121 | public function testOverloadKeepsPreviousValue() | ||
122 | { | ||
123 | $test = $this; | ||
124 | |||
125 | // defined by superclass | ||
126 | $this->options->set('foo', 'bar'); | ||
127 | |||
128 | // defined by subclass | ||
129 | $this->options->overload('foo', function (Options $options, $previousValue) use ($test) { | ||
130 | /* @var \PHPUnit_Framework_TestCase $test */ | ||
131 | $test->assertEquals('bar', $previousValue); | ||
132 | |||
133 | return 'dynamic'; | ||
134 | }); | ||
135 | |||
136 | $this->assertEquals('dynamic', $this->options->get('foo')); | ||
137 | } | ||
138 | |||
139 | public function testPreviousValueIsEvaluatedIfLazy() | ||
140 | { | ||
141 | $test = $this; | ||
142 | |||
143 | // defined by superclass | ||
144 | $this->options->set('foo', function (Options $options) { | ||
145 | return 'bar'; | ||
146 | }); | ||
147 | |||
148 | // defined by subclass | ||
149 | $this->options->overload('foo', function (Options $options, $previousValue) use ($test) { | ||
150 | /* @var \PHPUnit_Framework_TestCase $test */ | ||
151 | $test->assertEquals('bar', $previousValue); | ||
152 | |||
153 | return 'dynamic'; | ||
154 | }); | ||
155 | |||
156 | $this->assertEquals('dynamic', $this->options->get('foo')); | ||
157 | } | ||
158 | |||
159 | public function testPreviousValueIsNotEvaluatedIfNoSecondArgument() | ||
160 | { | ||
161 | $test = $this; | ||
162 | |||
163 | // defined by superclass | ||
164 | $this->options->set('foo', function (Options $options) use ($test) { | ||
165 | $test->fail('Should not be called'); | ||
166 | }); | ||
167 | |||
168 | // defined by subclass, no $previousValue argument defined! | ||
169 | $this->options->overload('foo', function (Options $options) use ($test) { | ||
170 | return 'dynamic'; | ||
171 | }); | ||
172 | |||
173 | $this->assertEquals('dynamic', $this->options->get('foo')); | ||
174 | } | ||
175 | |||
176 | public function testLazyOptionCanAccessOtherOptions() | ||
177 | { | ||
178 | $test = $this; | ||
179 | |||
180 | $this->options->set('foo', 'bar'); | ||
181 | |||
182 | $this->options->set('bam', function (Options $options) use ($test) { | ||
183 | /* @var \PHPUnit_Framework_TestCase $test */ | ||
184 | $test->assertEquals('bar', $options->get('foo')); | ||
185 | |||
186 | return 'dynamic'; | ||
187 | }); | ||
188 | |||
189 | $this->assertEquals('bar', $this->options->get('foo')); | ||
190 | $this->assertEquals('dynamic', $this->options->get('bam')); | ||
191 | } | ||
192 | |||
193 | public function testLazyOptionCanAccessOtherLazyOptions() | ||
194 | { | ||
195 | $test = $this; | ||
196 | |||
197 | $this->options->set('foo', function (Options $options) { | ||
198 | return 'bar'; | ||
199 | }); | ||
200 | |||
201 | $this->options->set('bam', function (Options $options) use ($test) { | ||
202 | /* @var \PHPUnit_Framework_TestCase $test */ | ||
203 | $test->assertEquals('bar', $options->get('foo')); | ||
204 | |||
205 | return 'dynamic'; | ||
206 | }); | ||
207 | |||
208 | $this->assertEquals('bar', $this->options->get('foo')); | ||
209 | $this->assertEquals('dynamic', $this->options->get('bam')); | ||
210 | } | ||
211 | |||
212 | public function testNormalizer() | ||
213 | { | ||
214 | $this->options->set('foo', 'bar'); | ||
215 | |||
216 | $this->options->setNormalizer('foo', function () { | ||
217 | return 'normalized'; | ||
218 | }); | ||
219 | |||
220 | $this->assertEquals('normalized', $this->options->get('foo')); | ||
221 | } | ||
222 | |||
223 | public function testNormalizerReceivesUnnormalizedValue() | ||
224 | { | ||
225 | $this->options->set('foo', 'bar'); | ||
226 | |||
227 | $this->options->setNormalizer('foo', function (Options $options, $value) { | ||
228 | return 'normalized['.$value.']'; | ||
229 | }); | ||
230 | |||
231 | $this->assertEquals('normalized[bar]', $this->options->get('foo')); | ||
232 | } | ||
233 | |||
234 | public function testNormalizerCanAccessOtherOptions() | ||
235 | { | ||
236 | $test = $this; | ||
237 | |||
238 | $this->options->set('foo', 'bar'); | ||
239 | $this->options->set('bam', 'baz'); | ||
240 | |||
241 | $this->options->setNormalizer('bam', function (Options $options) use ($test) { | ||
242 | /* @var \PHPUnit_Framework_TestCase $test */ | ||
243 | $test->assertEquals('bar', $options->get('foo')); | ||
244 | |||
245 | return 'normalized'; | ||
246 | }); | ||
247 | |||
248 | $this->assertEquals('bar', $this->options->get('foo')); | ||
249 | $this->assertEquals('normalized', $this->options->get('bam')); | ||
250 | } | ||
251 | |||
252 | public function testNormalizerCanAccessOtherLazyOptions() | ||
253 | { | ||
254 | $test = $this; | ||
255 | |||
256 | $this->options->set('foo', function (Options $options) { | ||
257 | return 'bar'; | ||
258 | }); | ||
259 | $this->options->set('bam', 'baz'); | ||
260 | |||
261 | $this->options->setNormalizer('bam', function (Options $options) use ($test) { | ||
262 | /* @var \PHPUnit_Framework_TestCase $test */ | ||
263 | $test->assertEquals('bar', $options->get('foo')); | ||
264 | |||
265 | return 'normalized'; | ||
266 | }); | ||
267 | |||
268 | $this->assertEquals('bar', $this->options->get('foo')); | ||
269 | $this->assertEquals('normalized', $this->options->get('bam')); | ||
270 | } | ||
271 | |||
272 | /** | ||
273 | * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException | ||
274 | */ | ||
275 | public function testFailForCyclicDependencies() | ||
276 | { | ||
277 | $this->options->set('foo', function (Options $options) { | ||
278 | $options->get('bam'); | ||
279 | }); | ||
280 | |||
281 | $this->options->set('bam', function (Options $options) { | ||
282 | $options->get('foo'); | ||
283 | }); | ||
284 | |||
285 | $this->options->get('foo'); | ||
286 | } | ||
287 | |||
288 | /** | ||
289 | * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException | ||
290 | */ | ||
291 | public function testFailForCyclicDependenciesBetweenNormalizers() | ||
292 | { | ||
293 | $this->options->set('foo', 'bar'); | ||
294 | $this->options->set('bam', 'baz'); | ||
295 | |||
296 | $this->options->setNormalizer('foo', function (Options $options) { | ||
297 | $options->get('bam'); | ||
298 | }); | ||
299 | |||
300 | $this->options->setNormalizer('bam', function (Options $options) { | ||
301 | $options->get('foo'); | ||
302 | }); | ||
303 | |||
304 | $this->options->get('foo'); | ||
305 | } | ||
306 | |||
307 | /** | ||
308 | * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException | ||
309 | */ | ||
310 | public function testFailForCyclicDependenciesBetweenNormalizerAndLazyOption() | ||
311 | { | ||
312 | $this->options->set('foo', function (Options $options) { | ||
313 | $options->get('bam'); | ||
314 | }); | ||
315 | $this->options->set('bam', 'baz'); | ||
316 | |||
317 | $this->options->setNormalizer('bam', function (Options $options) { | ||
318 | $options->get('foo'); | ||
319 | }); | ||
320 | |||
321 | $this->options->get('foo'); | ||
322 | } | ||
323 | |||
324 | public function testAllInvokesEachLazyOptionOnlyOnce() | ||
325 | { | ||
326 | $test = $this; | ||
327 | $i = 1; | ||
328 | |||
329 | $this->options->set('foo', function (Options $options) use ($test, &$i) { | ||
330 | $test->assertSame(1, $i); | ||
331 | ++$i; | ||
332 | |||
333 | // Implicitly invoke lazy option for "bam" | ||
334 | $options->get('bam'); | ||
335 | }); | ||
336 | $this->options->set('bam', function (Options $options) use ($test, &$i) { | ||
337 | $test->assertSame(2, $i); | ||
338 | ++$i; | ||
339 | }); | ||
340 | |||
341 | $this->options->all(); | ||
342 | } | ||
343 | |||
344 | public function testAllInvokesEachNormalizerOnlyOnce() | ||
345 | { | ||
346 | $test = $this; | ||
347 | $i = 1; | ||
348 | |||
349 | $this->options->set('foo', 'bar'); | ||
350 | $this->options->set('bam', 'baz'); | ||
351 | |||
352 | $this->options->setNormalizer('foo', function (Options $options) use ($test, &$i) { | ||
353 | $test->assertSame(1, $i); | ||
354 | ++$i; | ||
355 | |||
356 | // Implicitly invoke normalizer for "bam" | ||
357 | $options->get('bam'); | ||
358 | }); | ||
359 | $this->options->setNormalizer('bam', function (Options $options) use ($test, &$i) { | ||
360 | $test->assertSame(2, $i); | ||
361 | ++$i; | ||
362 | }); | ||
363 | |||
364 | $this->options->all(); | ||
365 | } | ||
366 | |||
367 | public function testReplaceClearsAndSets() | ||
368 | { | ||
369 | $this->options->set('one', '1'); | ||
370 | |||
371 | $this->options->replace(array( | ||
372 | 'two' => '2', | ||
373 | 'three' => function (Options $options) { | ||
374 | return '2' === $options['two'] ? '3' : 'foo'; | ||
375 | } | ||
376 | )); | ||
377 | |||
378 | $this->assertEquals(array( | ||
379 | 'two' => '2', | ||
380 | 'three' => '3', | ||
381 | ), $this->options->all()); | ||
382 | } | ||
383 | |||
384 | public function testClearRemovesAllOptions() | ||
385 | { | ||
386 | $this->options->set('one', 1); | ||
387 | $this->options->set('two', 2); | ||
388 | |||
389 | $this->options->clear(); | ||
390 | |||
391 | $this->assertEmpty($this->options->all()); | ||
392 | |||
393 | } | ||
394 | |||
395 | /** | ||
396 | * @covers Symfony\Component\OptionsResolver\Options::replace | ||
397 | * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException | ||
398 | */ | ||
399 | public function testCannotReplaceAfterOptionWasRead() | ||
400 | { | ||
401 | $this->options->set('one', 1); | ||
402 | $this->options->all(); | ||
403 | |||
404 | $this->options->replace(array( | ||
405 | 'two' => '2', | ||
406 | )); | ||
407 | } | ||
408 | |||
409 | /** | ||
410 | * @covers Symfony\Component\OptionsResolver\Options::overload | ||
411 | * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException | ||
412 | */ | ||
413 | public function testCannotOverloadAfterOptionWasRead() | ||
414 | { | ||
415 | $this->options->set('one', 1); | ||
416 | $this->options->all(); | ||
417 | |||
418 | $this->options->overload('one', 2); | ||
419 | } | ||
420 | |||
421 | /** | ||
422 | * @covers Symfony\Component\OptionsResolver\Options::clear | ||
423 | * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException | ||
424 | */ | ||
425 | public function testCannotClearAfterOptionWasRead() | ||
426 | { | ||
427 | $this->options->set('one', 1); | ||
428 | $this->options->all(); | ||
429 | |||
430 | $this->options->clear(); | ||
431 | } | ||
432 | |||
433 | public function testOverloadCannotBeEvaluatedLazilyWithoutExpectedClosureParams() | ||
434 | { | ||
435 | $this->options->set('foo', 'bar'); | ||
436 | |||
437 | $this->options->overload('foo', function () { | ||
438 | return 'test'; | ||
439 | }); | ||
440 | |||
441 | $this->assertNotEquals('test', $this->options->get('foo')); | ||
442 | $this->assertTrue(is_callable($this->options->get('foo'))); | ||
443 | } | ||
444 | |||
445 | public function testOverloadCannotBeEvaluatedLazilyWithoutFirstParamTypeHint() | ||
446 | { | ||
447 | $this->options->set('foo', 'bar'); | ||
448 | |||
449 | $this->options->overload('foo', function ($object) { | ||
450 | return 'test'; | ||
451 | }); | ||
452 | |||
453 | $this->assertNotEquals('test', $this->options->get('foo')); | ||
454 | $this->assertTrue(is_callable($this->options->get('foo'))); | ||
455 | } | ||
456 | |||
457 | public function testOptionsIteration() | ||
458 | { | ||
459 | $this->options->set('foo', 'bar'); | ||
460 | $this->options->set('foo1', 'bar1'); | ||
461 | $expectedResult = array('foo' => 'bar', 'foo1' => 'bar1'); | ||
462 | |||
463 | $this->assertEquals($expectedResult, iterator_to_array($this->options, true)); | ||
464 | } | ||
465 | |||
466 | public function testHasWithNullValue() | ||
467 | { | ||
468 | $this->options->set('foo', null); | ||
469 | |||
470 | $this->assertTrue($this->options->has('foo')); | ||
471 | } | ||
472 | |||
473 | public function testRemoveOptionAndNormalizer() | ||
474 | { | ||
475 | $this->options->set('foo1', 'bar'); | ||
476 | $this->options->setNormalizer('foo1', function (Options $options) { | ||
477 | return ''; | ||
478 | }); | ||
479 | $this->options->set('foo2', 'bar'); | ||
480 | $this->options->setNormalizer('foo2', function (Options $options) { | ||
481 | return ''; | ||
482 | }); | ||
483 | |||
484 | $this->options->remove('foo2'); | ||
485 | $this->assertEquals(array('foo1' => ''), $this->options->all()); | ||
486 | } | ||
487 | |||
488 | public function testReplaceOptionAndNormalizer() | ||
489 | { | ||
490 | $this->options->set('foo1', 'bar'); | ||
491 | $this->options->setNormalizer('foo1', function (Options $options) { | ||
492 | return ''; | ||
493 | }); | ||
494 | $this->options->set('foo2', 'bar'); | ||
495 | $this->options->setNormalizer('foo2', function (Options $options) { | ||
496 | return ''; | ||
497 | }); | ||
498 | |||
499 | $this->options->replace(array('foo1' => 'new')); | ||
500 | $this->assertEquals(array('foo1' => 'new'), $this->options->all()); | ||
501 | } | ||
502 | |||
503 | public function testClearOptionAndNormalizer() | ||
504 | { | ||
505 | $this->options->set('foo1', 'bar'); | ||
506 | $this->options->setNormalizer('foo1', function (Options $options) { | ||
507 | return ''; | ||
508 | }); | ||
509 | $this->options->set('foo2', 'bar'); | ||
510 | $this->options->setNormalizer('foo2', function (Options $options) { | ||
511 | return ''; | ||
512 | }); | ||
513 | |||
514 | $this->options->clear(); | ||
515 | $this->assertEmpty($this->options->all()); | ||
516 | } | ||
517 | |||
518 | public function testNormalizerWithoutCorrespondingOption() | ||
519 | { | ||
520 | $test = $this; | ||
521 | |||
522 | $this->options->setNormalizer('foo', function (Options $options, $previousValue) use ($test) { | ||
523 | $test->assertNull($previousValue); | ||
524 | |||
525 | return ''; | ||
526 | }); | ||
527 | $this->assertEquals(array('foo' => ''), $this->options->all()); | ||
528 | } | ||
529 | } | ||