123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\OptionsResolver\Tests;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\OptionsResolver\Options;
- use Symfony\Component\OptionsResolver\OptionsResolver;
- /**
- * @group legacy
- */
- class LegacyOptionsResolverTest extends TestCase
- {
- /**
- * @var OptionsResolver
- */
- private $resolver;
- protected function setUp()
- {
- $this->resolver = new OptionsResolver();
- }
- public function testResolve()
- {
- $this->resolver->setDefaults(array(
- 'one' => '1',
- 'two' => '2',
- ));
- $options = array(
- 'two' => '20',
- );
- $this->assertEquals(array(
- 'one' => '1',
- 'two' => '20',
- ), $this->resolver->resolve($options));
- }
- public function testResolveNumericOptions()
- {
- $this->resolver->setDefaults(array(
- '1' => '1',
- '2' => '2',
- ));
- $options = array(
- '2' => '20',
- );
- $this->assertEquals(array(
- '1' => '1',
- '2' => '20',
- ), $this->resolver->resolve($options));
- }
- public function testResolveLazy()
- {
- $this->resolver->setDefaults(array(
- 'one' => '1',
- 'two' => function (Options $options) {
- return '20';
- },
- ));
- $this->assertEquals(array(
- 'one' => '1',
- 'two' => '20',
- ), $this->resolver->resolve(array()));
- }
- public function testTypeAliasesForAllowedTypes()
- {
- $this->resolver->setDefaults(array(
- 'force' => false,
- ));
- $this->resolver->setAllowedTypes(array(
- 'force' => 'boolean',
- ));
- $this->assertSame(array('force' => true), $this->resolver->resolve(array(
- 'force' => true,
- )));
- }
- public function testResolveLazyDependencyOnOptional()
- {
- $this->resolver->setDefaults(array(
- 'one' => '1',
- 'two' => function (Options $options) {
- return $options['one'].'2';
- },
- ));
- $options = array(
- 'one' => '10',
- );
- $this->assertEquals(array(
- 'one' => '10',
- 'two' => '102',
- ), $this->resolver->resolve($options));
- }
- public function testResolveLazyDependencyOnMissingOptionalWithoutDefault()
- {
- $test = $this;
- $this->resolver->setOptional(array(
- 'one',
- ));
- $this->resolver->setDefaults(array(
- 'two' => function (Options $options) use ($test) {
- /* @var TestCase $test */
- $test->assertArrayNotHasKey('one', $options);
- return '2';
- },
- ));
- $options = array();
- $this->assertEquals(array(
- 'two' => '2',
- ), $this->resolver->resolve($options));
- }
- public function testResolveLazyDependencyOnOptionalWithoutDefault()
- {
- $test = $this;
- $this->resolver->setOptional(array(
- 'one',
- ));
- $this->resolver->setDefaults(array(
- 'two' => function (Options $options) use ($test) {
- /* @var TestCase $test */
- $test->assertArrayHasKey('one', $options);
- return $options['one'].'2';
- },
- ));
- $options = array(
- 'one' => '10',
- );
- $this->assertEquals(array(
- 'one' => '10',
- 'two' => '102',
- ), $this->resolver->resolve($options));
- }
- public function testResolveLazyDependencyOnRequired()
- {
- $this->resolver->setRequired(array(
- 'one',
- ));
- $this->resolver->setDefaults(array(
- 'two' => function (Options $options) {
- return $options['one'].'2';
- },
- ));
- $options = array(
- 'one' => '10',
- );
- $this->assertEquals(array(
- 'one' => '10',
- 'two' => '102',
- ), $this->resolver->resolve($options));
- }
- public function testResolveLazyReplaceDefaults()
- {
- $test = $this;
- $this->resolver->setDefaults(array(
- 'one' => function (Options $options) use ($test) {
- /* @var TestCase $test */
- $test->fail('Previous closure should not be executed');
- },
- ));
- $this->resolver->replaceDefaults(array(
- 'one' => function (Options $options, $previousValue) {
- return '1';
- },
- ));
- $this->assertEquals(array(
- 'one' => '1',
- ), $this->resolver->resolve(array()));
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
- * @expectedExceptionMessage The option "foo" does not exist. Defined options are: "one", "three", "two".
- */
- public function testResolveFailsIfNonExistingOption()
- {
- $this->resolver->setDefaults(array(
- 'one' => '1',
- ));
- $this->resolver->setRequired(array(
- 'two',
- ));
- $this->resolver->setOptional(array(
- 'three',
- ));
- $this->resolver->resolve(array(
- 'foo' => 'bar',
- ));
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
- */
- public function testResolveFailsIfMissingRequiredOption()
- {
- $this->resolver->setRequired(array(
- 'one',
- ));
- $this->resolver->setDefaults(array(
- 'two' => '2',
- ));
- $this->resolver->resolve(array(
- 'two' => '20',
- ));
- }
- public function testResolveSucceedsIfOptionValueAllowed()
- {
- $this->resolver->setDefaults(array(
- 'one' => '1',
- ));
- $this->resolver->setAllowedValues(array(
- 'one' => array('1', 'one'),
- ));
- $options = array(
- 'one' => 'one',
- );
- $this->assertEquals(array(
- 'one' => 'one',
- ), $this->resolver->resolve($options));
- }
- public function testResolveSucceedsIfOptionValueAllowed2()
- {
- $this->resolver->setDefaults(array(
- 'one' => '1',
- 'two' => '2',
- ));
- $this->resolver->setAllowedValues(array(
- 'one' => '1',
- 'two' => '2',
- ));
- $this->resolver->addAllowedValues(array(
- 'one' => 'one',
- 'two' => 'two',
- ));
- $options = array(
- 'one' => '1',
- 'two' => 'two',
- );
- $this->assertEquals(array(
- 'one' => '1',
- 'two' => 'two',
- ), $this->resolver->resolve($options));
- }
- public function testResolveSucceedsIfOptionalWithAllowedValuesNotSet()
- {
- $this->resolver->setRequired(array(
- 'one',
- ));
- $this->resolver->setOptional(array(
- 'two',
- ));
- $this->resolver->setAllowedValues(array(
- 'one' => array('1', 'one'),
- 'two' => array('2', 'two'),
- ));
- $options = array(
- 'one' => '1',
- );
- $this->assertEquals(array(
- 'one' => '1',
- ), $this->resolver->resolve($options));
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- */
- public function testResolveFailsIfOptionValueNotAllowed()
- {
- $this->resolver->setDefaults(array(
- 'one' => '1',
- ));
- $this->resolver->setAllowedValues(array(
- 'one' => array('1', 'one'),
- ));
- $this->resolver->resolve(array(
- 'one' => '2',
- ));
- }
- public function testResolveSucceedsIfOptionTypeAllowed()
- {
- $this->resolver->setDefaults(array(
- 'one' => '1',
- ));
- $this->resolver->setAllowedTypes(array(
- 'one' => 'string',
- ));
- $options = array(
- 'one' => 'one',
- );
- $this->assertEquals(array(
- 'one' => 'one',
- ), $this->resolver->resolve($options));
- }
- public function testResolveSucceedsIfOptionTypeAllowedPassArray()
- {
- $this->resolver->setDefaults(array(
- 'one' => '1',
- ));
- $this->resolver->setAllowedTypes(array(
- 'one' => array('string', 'bool'),
- ));
- $options = array(
- 'one' => true,
- );
- $this->assertEquals(array(
- 'one' => true,
- ), $this->resolver->resolve($options));
- }
- public function testResolveSucceedsIfOptionTypeAllowedPassObject()
- {
- $this->resolver->setDefaults(array(
- 'one' => '1',
- ));
- $this->resolver->setAllowedTypes(array(
- 'one' => 'object',
- ));
- $object = new \stdClass();
- $options = array(
- 'one' => $object,
- );
- $this->assertEquals(array(
- 'one' => $object,
- ), $this->resolver->resolve($options));
- }
- public function testResolveSucceedsIfOptionTypeAllowedPassClass()
- {
- $this->resolver->setDefaults(array(
- 'one' => '1',
- ));
- $this->resolver->setAllowedTypes(array(
- 'one' => '\stdClass',
- ));
- $object = new \stdClass();
- $options = array(
- 'one' => $object,
- );
- $this->assertEquals(array(
- 'one' => $object,
- ), $this->resolver->resolve($options));
- }
- public function testResolveSucceedsIfOptionTypeAllowedAddTypes()
- {
- $this->resolver->setDefaults(array(
- 'one' => '1',
- 'two' => '2',
- ));
- $this->resolver->setAllowedTypes(array(
- 'one' => 'string',
- 'two' => 'bool',
- ));
- $this->resolver->addAllowedTypes(array(
- 'one' => 'float',
- 'two' => 'integer',
- ));
- $options = array(
- 'one' => 1.23,
- 'two' => false,
- );
- $this->assertEquals(array(
- 'one' => 1.23,
- 'two' => false,
- ), $this->resolver->resolve($options));
- }
- public function testResolveSucceedsIfOptionalWithTypeAndWithoutValue()
- {
- $this->resolver->setOptional(array(
- 'one',
- 'two',
- ));
- $this->resolver->setAllowedTypes(array(
- 'one' => 'string',
- 'two' => 'int',
- ));
- $options = array(
- 'two' => 1,
- );
- $this->assertEquals(array(
- 'two' => 1,
- ), $this->resolver->resolve($options));
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- */
- public function testResolveFailsIfOptionTypeNotAllowed()
- {
- $this->resolver->setDefaults(array(
- 'one' => '1',
- ));
- $this->resolver->setAllowedTypes(array(
- 'one' => array('string', 'bool'),
- ));
- $this->resolver->resolve(array(
- 'one' => 1.23,
- ));
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- */
- public function testResolveFailsIfOptionTypeNotAllowedMultipleOptions()
- {
- $this->resolver->setDefaults(array(
- 'one' => '1',
- 'two' => '2',
- ));
- $this->resolver->setAllowedTypes(array(
- 'one' => 'string',
- 'two' => 'bool',
- ));
- $this->resolver->resolve(array(
- 'one' => 'foo',
- 'two' => 1.23,
- ));
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- */
- public function testResolveFailsIfOptionTypeNotAllowedAddTypes()
- {
- $this->resolver->setDefaults(array(
- 'one' => '1',
- ));
- $this->resolver->setAllowedTypes(array(
- 'one' => 'string',
- ));
- $this->resolver->addAllowedTypes(array(
- 'one' => 'bool',
- ));
- $this->resolver->resolve(array(
- 'one' => 1.23,
- ));
- }
- public function testFluidInterface()
- {
- $this->resolver->setDefaults(array('one' => '1'))
- ->replaceDefaults(array('one' => '2'))
- ->setAllowedValues(array('one' => array('1', '2')))
- ->addAllowedValues(array('one' => array('3')))
- ->setRequired(array('two'))
- ->setOptional(array('three'));
- $options = array(
- 'two' => '2',
- );
- $this->assertEquals(array(
- 'one' => '2',
- 'two' => '2',
- ), $this->resolver->resolve($options));
- }
- public function testKnownIfDefaultWasSet()
- {
- $this->assertFalse($this->resolver->isKnown('foo'));
- $this->resolver->setDefaults(array(
- 'foo' => 'bar',
- ));
- $this->assertTrue($this->resolver->isKnown('foo'));
- }
- public function testKnownIfRequired()
- {
- $this->assertFalse($this->resolver->isKnown('foo'));
- $this->resolver->setRequired(array(
- 'foo',
- ));
- $this->assertTrue($this->resolver->isKnown('foo'));
- }
- public function testKnownIfOptional()
- {
- $this->assertFalse($this->resolver->isKnown('foo'));
- $this->resolver->setOptional(array(
- 'foo',
- ));
- $this->assertTrue($this->resolver->isKnown('foo'));
- }
- public function testRequiredIfRequired()
- {
- $this->assertFalse($this->resolver->isRequired('foo'));
- $this->resolver->setRequired(array(
- 'foo',
- ));
- $this->assertTrue($this->resolver->isRequired('foo'));
- }
- public function testNormalizersTransformFinalOptions()
- {
- $this->resolver->setDefaults(array(
- 'foo' => 'bar',
- 'bam' => 'baz',
- ));
- $this->resolver->setNormalizers(array(
- 'foo' => function (Options $options, $value) {
- return $options['bam'].'['.$value.']';
- },
- ));
- $expected = array(
- 'foo' => 'baz[bar]',
- 'bam' => 'baz',
- );
- $this->assertEquals($expected, $this->resolver->resolve(array()));
- $expected = array(
- 'foo' => 'boo[custom]',
- 'bam' => 'boo',
- );
- $this->assertEquals($expected, $this->resolver->resolve(array(
- 'foo' => 'custom',
- 'bam' => 'boo',
- )));
- }
- public function testResolveWithoutOptionSucceedsIfRequiredAndDefaultValue()
- {
- $this->resolver->setRequired(array(
- 'foo',
- ));
- $this->resolver->setDefaults(array(
- 'foo' => 'bar',
- ));
- $this->assertEquals(array(
- 'foo' => 'bar',
- ), $this->resolver->resolve(array()));
- }
- public function testResolveWithoutOptionSucceedsIfDefaultValueAndRequired()
- {
- $this->resolver->setDefaults(array(
- 'foo' => 'bar',
- ));
- $this->resolver->setRequired(array(
- 'foo',
- ));
- $this->assertEquals(array(
- 'foo' => 'bar',
- ), $this->resolver->resolve(array()));
- }
- public function testResolveSucceedsIfOptionRequiredAndValueAllowed()
- {
- $this->resolver->setRequired(array(
- 'one', 'two',
- ));
- $this->resolver->setAllowedValues(array(
- 'two' => array('2'),
- ));
- $options = array(
- 'one' => '1',
- 'two' => '2',
- );
- $this->assertEquals($options, $this->resolver->resolve($options));
- }
- public function testResolveSucceedsIfValueAllowedCallbackReturnsTrue()
- {
- $this->resolver->setRequired(array(
- 'test',
- ));
- $this->resolver->setAllowedValues(array(
- 'test' => function ($value) {
- return true;
- },
- ));
- $options = array(
- 'test' => true,
- );
- $this->assertEquals($options, $this->resolver->resolve($options));
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- */
- public function testResolveFailsIfValueAllowedCallbackReturnsFalse()
- {
- $this->resolver->setRequired(array(
- 'test',
- ));
- $this->resolver->setAllowedValues(array(
- 'test' => function ($value) {
- return false;
- },
- ));
- $options = array(
- 'test' => true,
- );
- $this->assertEquals($options, $this->resolver->resolve($options));
- }
- public function testClone()
- {
- $this->resolver->setDefaults(array('one' => '1'));
- $clone = clone $this->resolver;
- // Changes after cloning don't affect each other
- $this->resolver->setDefaults(array('two' => '2'));
- $clone->setDefaults(array('three' => '3'));
- $this->assertEquals(array(
- 'one' => '1',
- 'two' => '2',
- ), $this->resolver->resolve());
- $this->assertEquals(array(
- 'one' => '1',
- 'three' => '3',
- ), $clone->resolve());
- }
- public function testOverloadReturnsThis()
- {
- $this->assertSame($this->resolver, $this->resolver->overload('foo', 'bar'));
- }
- public function testOverloadCallsSet()
- {
- $this->resolver->overload('foo', 'bar');
- $this->assertSame(array('foo' => 'bar'), $this->resolver->resolve());
- }
- }
|