4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\OptionsResolver\Tests
;
14 use Symfony\Component\OptionsResolver\OptionsResolver
;
15 use Symfony\Component\OptionsResolver\Options
;
17 class OptionsResolverTest
extends \PHPUnit_Framework_TestCase
20 * @var OptionsResolver
24 protected function setUp()
26 $this->resolver
= new OptionsResolver();
29 public function testResolve()
31 $this->resolver
->setDefaults(array(
40 $this->assertEquals(array(
43 ), $this->resolver
->resolve($options));
46 public function testResolveLazy()
48 $this->resolver
->setDefaults(array(
50 'two' => function (Options
$options) {
55 $this->assertEquals(array(
58 ), $this->resolver
->resolve(array()));
61 public function testResolveLazyDependencyOnOptional()
63 $this->resolver
->setDefaults(array(
65 'two' => function (Options
$options) {
66 return $options['one'].'2';
74 $this->assertEquals(array(
77 ), $this->resolver
->resolve($options));
80 public function testResolveLazyDependencyOnMissingOptionalWithoutDefault()
84 $this->resolver
->setOptional(array(
88 $this->resolver
->setDefaults(array(
89 'two' => function (Options
$options) use ($test) {
90 /* @var \PHPUnit_Framework_TestCase $test */
91 $test->assertFalse(isset($options['one']));
100 $this->assertEquals(array(
102 ), $this->resolver
->resolve($options));
105 public function testResolveLazyDependencyOnOptionalWithoutDefault()
109 $this->resolver
->setOptional(array(
113 $this->resolver
->setDefaults(array(
114 'two' => function (Options
$options) use ($test) {
115 /* @var \PHPUnit_Framework_TestCase $test */
116 $test->assertTrue(isset($options['one']));
118 return $options['one'].'2';
126 $this->assertEquals(array(
129 ), $this->resolver
->resolve($options));
132 public function testResolveLazyDependencyOnRequired()
134 $this->resolver
->setRequired(array(
137 $this->resolver
->setDefaults(array(
138 'two' => function (Options
$options) {
139 return $options['one'].'2';
147 $this->assertEquals(array(
150 ), $this->resolver
->resolve($options));
153 public function testResolveLazyReplaceDefaults()
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');
164 $this->resolver
->replaceDefaults(array(
165 'one' => function (Options
$options, $previousValue) {
170 $this->assertEquals(array(
172 ), $this->resolver
->resolve(array()));
176 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
178 public function testResolveFailsIfNonExistingOption()
180 $this->resolver
->setDefaults(array(
184 $this->resolver
->setRequired(array(
188 $this->resolver
->setOptional(array(
192 $this->resolver
->resolve(array(
198 * @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
200 public function testResolveFailsIfMissingRequiredOption()
202 $this->resolver
->setRequired(array(
206 $this->resolver
->setDefaults(array(
210 $this->resolver
->resolve(array(
215 public function testResolveSucceedsIfOptionValueAllowed()
217 $this->resolver
->setDefaults(array(
221 $this->resolver
->setAllowedValues(array(
222 'one' => array('1', 'one'),
229 $this->assertEquals(array(
231 ), $this->resolver
->resolve($options));
234 public function testResolveSucceedsIfOptionValueAllowed2()
236 $this->resolver
->setDefaults(array(
241 $this->resolver
->setAllowedValues(array(
245 $this->resolver
->addAllowedValues(array(
255 $this->assertEquals(array(
258 ), $this->resolver
->resolve($options));
261 public function testResolveSucceedsIfOptionalWithAllowedValuesNotSet()
263 $this->resolver
->setRequired(array(
267 $this->resolver
->setOptional(array(
271 $this->resolver
->setAllowedValues(array(
272 'one' => array('1', 'one'),
273 'two' => array('2', 'two'),
280 $this->assertEquals(array(
282 ), $this->resolver
->resolve($options));
286 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
288 public function testResolveFailsIfOptionValueNotAllowed()
290 $this->resolver
->setDefaults(array(
294 $this->resolver
->setAllowedValues(array(
295 'one' => array('1', 'one'),
298 $this->resolver
->resolve(array(
303 public function testResolveSucceedsIfOptionTypeAllowed()
305 $this->resolver
->setDefaults(array(
309 $this->resolver
->setAllowedTypes(array(
317 $this->assertEquals(array(
319 ), $this->resolver
->resolve($options));
322 public function testResolveSucceedsIfOptionTypeAllowedPassArray()
324 $this->resolver
->setDefaults(array(
328 $this->resolver
->setAllowedTypes(array(
329 'one' => array('string', 'bool'),
336 $this->assertEquals(array(
338 ), $this->resolver
->resolve($options));
341 public function testResolveSucceedsIfOptionTypeAllowedPassObject()
343 $this->resolver
->setDefaults(array(
347 $this->resolver
->setAllowedTypes(array(
351 $object = new \
stdClass();
356 $this->assertEquals(array(
358 ), $this->resolver
->resolve($options));
361 public function testResolveSucceedsIfOptionTypeAllowedPassClass()
363 $this->resolver
->setDefaults(array(
367 $this->resolver
->setAllowedTypes(array(
368 'one' => '\stdClass',
371 $object = new \
stdClass();
376 $this->assertEquals(array(
378 ), $this->resolver
->resolve($options));
381 public function testResolveSucceedsIfOptionTypeAllowedAddTypes()
383 $this->resolver
->setDefaults(array(
388 $this->resolver
->setAllowedTypes(array(
392 $this->resolver
->addAllowedTypes(array(
402 $this->assertEquals(array(
405 ), $this->resolver
->resolve($options));
408 public function testResolveSucceedsIfOptionalWithTypeAndWithoutValue()
410 $this->resolver
->setOptional(array(
415 $this->resolver
->setAllowedTypes(array(
424 $this->assertEquals(array(
426 ), $this->resolver
->resolve($options));
430 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
432 public function testResolveFailsIfOptionTypeNotAllowed()
434 $this->resolver
->setDefaults(array(
438 $this->resolver
->setAllowedTypes(array(
439 'one' => array('string', 'bool'),
442 $this->resolver
->resolve(array(
448 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
450 public function testResolveFailsIfOptionTypeNotAllowedMultipleOptions()
452 $this->resolver
->setDefaults(array(
457 $this->resolver
->setAllowedTypes(array(
462 $this->resolver
->resolve(array(
469 * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
471 public function testResolveFailsIfOptionTypeNotAllowedAddTypes()
473 $this->resolver
->setDefaults(array(
477 $this->resolver
->setAllowedTypes(array(
480 $this->resolver
->addAllowedTypes(array(
484 $this->resolver
->resolve(array(
490 * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
492 public function testSetRequiredFailsIfDefaultIsPassed()
494 $this->resolver
->setRequired(array(
500 * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
502 public function testSetOptionalFailsIfDefaultIsPassed()
504 $this->resolver
->setOptional(array(
509 public function testFluidInterface()
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'));
522 $this->assertEquals(array(
525 ), $this->resolver
->resolve($options));
528 public function testKnownIfDefaultWasSet()
530 $this->assertFalse($this->resolver
->isKnown('foo'));
532 $this->resolver
->setDefaults(array(
536 $this->assertTrue($this->resolver
->isKnown('foo'));
539 public function testKnownIfRequired()
541 $this->assertFalse($this->resolver
->isKnown('foo'));
543 $this->resolver
->setRequired(array(
547 $this->assertTrue($this->resolver
->isKnown('foo'));
550 public function testKnownIfOptional()
552 $this->assertFalse($this->resolver
->isKnown('foo'));
554 $this->resolver
->setOptional(array(
558 $this->assertTrue($this->resolver
->isKnown('foo'));
561 public function testRequiredIfRequired()
563 $this->assertFalse($this->resolver
->isRequired('foo'));
565 $this->resolver
->setRequired(array(
569 $this->assertTrue($this->resolver
->isRequired('foo'));
572 public function testNotRequiredIfRequiredAndDefaultValue()
574 $this->assertFalse($this->resolver
->isRequired('foo'));
576 $this->resolver
->setRequired(array(
579 $this->resolver
->setDefaults(array(
583 $this->assertFalse($this->resolver
->isRequired('foo'));
586 public function testNormalizersTransformFinalOptions()
588 $this->resolver
->setDefaults(array(
592 $this->resolver
->setNormalizers(array(
593 'foo' => function (Options
$options, $value) {
594 return $options['bam'].'['.$value.']';
603 $this->assertEquals($expected, $this->resolver
->resolve(array()));
606 'foo' => 'boo[custom]',
610 $this->assertEquals($expected, $this->resolver
->resolve(array(
616 public function testResolveWithoutOptionSucceedsIfRequiredAndDefaultValue()
618 $this->resolver
->setRequired(array(
621 $this->resolver
->setDefaults(array(
625 $this->assertEquals(array(
627 ), $this->resolver
->resolve(array()));
630 public function testResolveWithoutOptionSucceedsIfDefaultValueAndRequired()
632 $this->resolver
->setDefaults(array(
635 $this->resolver
->setRequired(array(
639 $this->assertEquals(array(
641 ), $this->resolver
->resolve(array()));
644 public function testResolveSucceedsIfOptionRequiredAndValueAllowed()
646 $this->resolver
->setRequired(array(
649 $this->resolver
->setAllowedValues(array(
658 $this->assertEquals($options, $this->resolver
->resolve($options));
661 public function testClone()
663 $this->resolver
->setDefaults(array('one' => '1'));
665 $clone = clone $this->resolver
;
667 // Changes after cloning don't affect each other
668 $this->resolver
->setDefaults(array('two' => '2'));
669 $clone->setDefaults(array('three' => '3'));
671 $this->assertEquals(array(
674 ), $this->resolver
->resolve());
676 $this->assertEquals(array(
679 ), $clone->resolve());