1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495 |
- <?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\Assert;
- use PHPUnit\Framework\TestCase;
- use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
- use Symfony\Component\OptionsResolver\Options;
- use Symfony\Component\OptionsResolver\OptionsResolver;
- class OptionsResolver2Dot6Test extends TestCase
- {
- /**
- * @var OptionsResolver
- */
- private $resolver;
- protected function setUp()
- {
- $this->resolver = new OptionsResolver();
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
- * @expectedExceptionMessage The option "foo" does not exist. Defined options are: "a", "z".
- */
- public function testResolveFailsIfNonExistingOption()
- {
- $this->resolver->setDefault('z', '1');
- $this->resolver->setDefault('a', '2');
- $this->resolver->resolve(array('foo' => 'bar'));
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
- * @expectedExceptionMessage The options "baz", "foo", "ping" do not exist. Defined options are: "a", "z".
- */
- public function testResolveFailsIfMultipleNonExistingOptions()
- {
- $this->resolver->setDefault('z', '1');
- $this->resolver->setDefault('a', '2');
- $this->resolver->resolve(array('ping' => 'pong', 'foo' => 'bar', 'baz' => 'bam'));
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testResolveFailsFromLazyOption()
- {
- $this->resolver->setDefault('foo', function (Options $options) {
- $options->resolve(array());
- });
- $this->resolver->resolve();
- }
- public function testSetDefaultReturnsThis()
- {
- $this->assertSame($this->resolver, $this->resolver->setDefault('foo', 'bar'));
- }
- public function testSetDefault()
- {
- $this->resolver->setDefault('one', '1');
- $this->resolver->setDefault('two', '20');
- $this->assertEquals(array(
- 'one' => '1',
- 'two' => '20',
- ), $this->resolver->resolve());
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testFailIfSetDefaultFromLazyOption()
- {
- $this->resolver->setDefault('lazy', function (Options $options) {
- $options->setDefault('default', 42);
- });
- $this->resolver->resolve();
- }
- public function testHasDefault()
- {
- $this->assertFalse($this->resolver->hasDefault('foo'));
- $this->resolver->setDefault('foo', 42);
- $this->assertTrue($this->resolver->hasDefault('foo'));
- }
- public function testHasDefaultWithNullValue()
- {
- $this->assertFalse($this->resolver->hasDefault('foo'));
- $this->resolver->setDefault('foo', null);
- $this->assertTrue($this->resolver->hasDefault('foo'));
- }
- public function testSetLazyReturnsThis()
- {
- $this->assertSame($this->resolver, $this->resolver->setDefault('foo', function (Options $options) {}));
- }
- public function testSetLazyClosure()
- {
- $this->resolver->setDefault('foo', function (Options $options) {
- return 'lazy';
- });
- $this->assertEquals(array('foo' => 'lazy'), $this->resolver->resolve());
- }
- public function testClosureWithoutTypeHintNotInvoked()
- {
- $closure = function ($options) {
- Assert::fail('Should not be called');
- };
- $this->resolver->setDefault('foo', $closure);
- $this->assertSame(array('foo' => $closure), $this->resolver->resolve());
- }
- public function testClosureWithoutParametersNotInvoked()
- {
- $closure = function () {
- Assert::fail('Should not be called');
- };
- $this->resolver->setDefault('foo', $closure);
- $this->assertSame(array('foo' => $closure), $this->resolver->resolve());
- }
- public function testAccessPreviousDefaultValue()
- {
- // defined by superclass
- $this->resolver->setDefault('foo', 'bar');
- // defined by subclass
- $this->resolver->setDefault('foo', function (Options $options, $previousValue) {
- Assert::assertEquals('bar', $previousValue);
- return 'lazy';
- });
- $this->assertEquals(array('foo' => 'lazy'), $this->resolver->resolve());
- }
- public function testAccessPreviousLazyDefaultValue()
- {
- // defined by superclass
- $this->resolver->setDefault('foo', function (Options $options) {
- return 'bar';
- });
- // defined by subclass
- $this->resolver->setDefault('foo', function (Options $options, $previousValue) {
- Assert::assertEquals('bar', $previousValue);
- return 'lazy';
- });
- $this->assertEquals(array('foo' => 'lazy'), $this->resolver->resolve());
- }
- public function testPreviousValueIsNotEvaluatedIfNoSecondArgument()
- {
- // defined by superclass
- $this->resolver->setDefault('foo', function () {
- Assert::fail('Should not be called');
- });
- // defined by subclass, no $previousValue argument defined!
- $this->resolver->setDefault('foo', function (Options $options) {
- return 'lazy';
- });
- $this->assertEquals(array('foo' => 'lazy'), $this->resolver->resolve());
- }
- public function testOverwrittenLazyOptionNotEvaluated()
- {
- $this->resolver->setDefault('foo', function (Options $options) {
- Assert::fail('Should not be called');
- });
- $this->resolver->setDefault('foo', 'bar');
- $this->assertSame(array('foo' => 'bar'), $this->resolver->resolve());
- }
- public function testInvokeEachLazyOptionOnlyOnce()
- {
- $calls = 0;
- $this->resolver->setDefault('lazy1', function (Options $options) use (&$calls) {
- Assert::assertSame(1, ++$calls);
- $options['lazy2'];
- });
- $this->resolver->setDefault('lazy2', function (Options $options) use (&$calls) {
- Assert::assertSame(2, ++$calls);
- });
- $this->resolver->resolve();
- $this->assertSame(2, $calls);
- }
- public function testSetRequiredReturnsThis()
- {
- $this->assertSame($this->resolver, $this->resolver->setRequired('foo'));
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testFailIfSetRequiredFromLazyOption()
- {
- $this->resolver->setDefault('foo', function (Options $options) {
- $options->setRequired('bar');
- });
- $this->resolver->resolve();
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
- */
- public function testResolveFailsIfRequiredOptionMissing()
- {
- $this->resolver->setRequired('foo');
- $this->resolver->resolve();
- }
- public function testResolveSucceedsIfRequiredOptionSet()
- {
- $this->resolver->setRequired('foo');
- $this->resolver->setDefault('foo', 'bar');
- $this->assertNotEmpty($this->resolver->resolve());
- }
- public function testResolveSucceedsIfRequiredOptionPassed()
- {
- $this->resolver->setRequired('foo');
- $this->assertNotEmpty($this->resolver->resolve(array('foo' => 'bar')));
- }
- public function testIsRequired()
- {
- $this->assertFalse($this->resolver->isRequired('foo'));
- $this->resolver->setRequired('foo');
- $this->assertTrue($this->resolver->isRequired('foo'));
- }
- public function testRequiredIfSetBefore()
- {
- $this->assertFalse($this->resolver->isRequired('foo'));
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setRequired('foo');
- $this->assertTrue($this->resolver->isRequired('foo'));
- }
- public function testStillRequiredAfterSet()
- {
- $this->assertFalse($this->resolver->isRequired('foo'));
- $this->resolver->setRequired('foo');
- $this->resolver->setDefault('foo', 'bar');
- $this->assertTrue($this->resolver->isRequired('foo'));
- }
- public function testIsNotRequiredAfterRemove()
- {
- $this->assertFalse($this->resolver->isRequired('foo'));
- $this->resolver->setRequired('foo');
- $this->resolver->remove('foo');
- $this->assertFalse($this->resolver->isRequired('foo'));
- }
- public function testIsNotRequiredAfterClear()
- {
- $this->assertFalse($this->resolver->isRequired('foo'));
- $this->resolver->setRequired('foo');
- $this->resolver->clear();
- $this->assertFalse($this->resolver->isRequired('foo'));
- }
- public function testGetRequiredOptions()
- {
- $this->resolver->setRequired(array('foo', 'bar'));
- $this->resolver->setDefault('bam', 'baz');
- $this->resolver->setDefault('foo', 'boo');
- $this->assertSame(array('foo', 'bar'), $this->resolver->getRequiredOptions());
- }
- public function testIsMissingIfNotSet()
- {
- $this->assertFalse($this->resolver->isMissing('foo'));
- $this->resolver->setRequired('foo');
- $this->assertTrue($this->resolver->isMissing('foo'));
- }
- public function testIsNotMissingIfSet()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->assertFalse($this->resolver->isMissing('foo'));
- $this->resolver->setRequired('foo');
- $this->assertFalse($this->resolver->isMissing('foo'));
- }
- public function testIsNotMissingAfterRemove()
- {
- $this->resolver->setRequired('foo');
- $this->resolver->remove('foo');
- $this->assertFalse($this->resolver->isMissing('foo'));
- }
- public function testIsNotMissingAfterClear()
- {
- $this->resolver->setRequired('foo');
- $this->resolver->clear();
- $this->assertFalse($this->resolver->isRequired('foo'));
- }
- public function testGetMissingOptions()
- {
- $this->resolver->setRequired(array('foo', 'bar'));
- $this->resolver->setDefault('bam', 'baz');
- $this->resolver->setDefault('foo', 'boo');
- $this->assertSame(array('bar'), $this->resolver->getMissingOptions());
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testFailIfSetDefinedFromLazyOption()
- {
- $this->resolver->setDefault('foo', function (Options $options) {
- $options->setDefined('bar');
- });
- $this->resolver->resolve();
- }
- public function testDefinedOptionsNotIncludedInResolvedOptions()
- {
- $this->resolver->setDefined('foo');
- $this->assertSame(array(), $this->resolver->resolve());
- }
- public function testDefinedOptionsIncludedIfDefaultSetBefore()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setDefined('foo');
- $this->assertSame(array('foo' => 'bar'), $this->resolver->resolve());
- }
- public function testDefinedOptionsIncludedIfDefaultSetAfter()
- {
- $this->resolver->setDefined('foo');
- $this->resolver->setDefault('foo', 'bar');
- $this->assertSame(array('foo' => 'bar'), $this->resolver->resolve());
- }
- public function testDefinedOptionsIncludedIfPassedToResolve()
- {
- $this->resolver->setDefined('foo');
- $this->assertSame(array('foo' => 'bar'), $this->resolver->resolve(array('foo' => 'bar')));
- }
- public function testIsDefined()
- {
- $this->assertFalse($this->resolver->isDefined('foo'));
- $this->resolver->setDefined('foo');
- $this->assertTrue($this->resolver->isDefined('foo'));
- }
- public function testLazyOptionsAreDefined()
- {
- $this->assertFalse($this->resolver->isDefined('foo'));
- $this->resolver->setDefault('foo', function (Options $options) {});
- $this->assertTrue($this->resolver->isDefined('foo'));
- }
- public function testRequiredOptionsAreDefined()
- {
- $this->assertFalse($this->resolver->isDefined('foo'));
- $this->resolver->setRequired('foo');
- $this->assertTrue($this->resolver->isDefined('foo'));
- }
- public function testSetOptionsAreDefined()
- {
- $this->assertFalse($this->resolver->isDefined('foo'));
- $this->resolver->setDefault('foo', 'bar');
- $this->assertTrue($this->resolver->isDefined('foo'));
- }
- public function testGetDefinedOptions()
- {
- $this->resolver->setDefined(array('foo', 'bar'));
- $this->resolver->setDefault('baz', 'bam');
- $this->resolver->setRequired('boo');
- $this->assertSame(array('foo', 'bar', 'baz', 'boo'), $this->resolver->getDefinedOptions());
- }
- public function testRemovedOptionsAreNotDefined()
- {
- $this->assertFalse($this->resolver->isDefined('foo'));
- $this->resolver->setDefined('foo');
- $this->assertTrue($this->resolver->isDefined('foo'));
- $this->resolver->remove('foo');
- $this->assertFalse($this->resolver->isDefined('foo'));
- }
- public function testClearedOptionsAreNotDefined()
- {
- $this->assertFalse($this->resolver->isDefined('foo'));
- $this->resolver->setDefined('foo');
- $this->assertTrue($this->resolver->isDefined('foo'));
- $this->resolver->clear();
- $this->assertFalse($this->resolver->isDefined('foo'));
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
- */
- public function testSetAllowedTypesFailsIfUnknownOption()
- {
- $this->resolver->setAllowedTypes('foo', 'string');
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testFailIfSetAllowedTypesFromLazyOption()
- {
- $this->resolver->setDefault('foo', function (Options $options) {
- $options->setAllowedTypes('bar', 'string');
- });
- $this->resolver->setDefault('bar', 'baz');
- $this->resolver->resolve();
- }
- /**
- * @dataProvider provideInvalidTypes
- */
- public function testResolveFailsIfInvalidType($actualType, $allowedType, $exceptionMessage)
- {
- $this->resolver->setDefined('option');
- $this->resolver->setAllowedTypes('option', $allowedType);
- if (method_exists($this, 'expectException')) {
- $this->expectException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException');
- $this->expectExceptionMessage($exceptionMessage);
- } else {
- $this->setExpectedException('Symfony\Component\OptionsResolver\Exception\InvalidOptionsException', $exceptionMessage);
- }
- $this->resolver->resolve(array('option' => $actualType));
- }
- public function provideInvalidTypes()
- {
- return array(
- array(true, 'string', 'The option "option" with value true is expected to be of type "string", but is of type "boolean".'),
- array(false, 'string', 'The option "option" with value false is expected to be of type "string", but is of type "boolean".'),
- array(fopen(__FILE__, 'r'), 'string', 'The option "option" with value resource is expected to be of type "string", but is of type "resource".'),
- array(array(), 'string', 'The option "option" with value array is expected to be of type "string", but is of type "array".'),
- array(new OptionsResolver(), 'string', 'The option "option" with value Symfony\Component\OptionsResolver\OptionsResolver is expected to be of type "string", but is of type "Symfony\Component\OptionsResolver\OptionsResolver".'),
- array(42, 'string', 'The option "option" with value 42 is expected to be of type "string", but is of type "integer".'),
- array(null, 'string', 'The option "option" with value null is expected to be of type "string", but is of type "NULL".'),
- array('bar', '\stdClass', 'The option "option" with value "bar" is expected to be of type "\stdClass", but is of type "string".'),
- );
- }
- public function testResolveSucceedsIfValidType()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setAllowedTypes('foo', 'string');
- $this->assertNotEmpty($this->resolver->resolve());
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- * @expectedExceptionMessage The option "foo" with value 42 is expected to be of type "string" or "bool", but is of type "integer".
- */
- public function testResolveFailsIfInvalidTypeMultiple()
- {
- $this->resolver->setDefault('foo', 42);
- $this->resolver->setAllowedTypes('foo', array('string', 'bool'));
- $this->resolver->resolve();
- }
- public function testResolveSucceedsIfValidTypeMultiple()
- {
- $this->resolver->setDefault('foo', true);
- $this->resolver->setAllowedTypes('foo', array('string', 'bool'));
- $this->assertNotEmpty($this->resolver->resolve());
- }
- public function testResolveSucceedsIfInstanceOfClass()
- {
- $this->resolver->setDefault('foo', new \stdClass());
- $this->resolver->setAllowedTypes('foo', '\stdClass');
- $this->assertNotEmpty($this->resolver->resolve());
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
- */
- public function testAddAllowedTypesFailsIfUnknownOption()
- {
- $this->resolver->addAllowedTypes('foo', 'string');
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testFailIfAddAllowedTypesFromLazyOption()
- {
- $this->resolver->setDefault('foo', function (Options $options) {
- $options->addAllowedTypes('bar', 'string');
- });
- $this->resolver->setDefault('bar', 'baz');
- $this->resolver->resolve();
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- */
- public function testResolveFailsIfInvalidAddedType()
- {
- $this->resolver->setDefault('foo', 42);
- $this->resolver->addAllowedTypes('foo', 'string');
- $this->resolver->resolve();
- }
- public function testResolveSucceedsIfValidAddedType()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->addAllowedTypes('foo', 'string');
- $this->assertNotEmpty($this->resolver->resolve());
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- */
- public function testResolveFailsIfInvalidAddedTypeMultiple()
- {
- $this->resolver->setDefault('foo', 42);
- $this->resolver->addAllowedTypes('foo', array('string', 'bool'));
- $this->resolver->resolve();
- }
- public function testResolveSucceedsIfValidAddedTypeMultiple()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->addAllowedTypes('foo', array('string', 'bool'));
- $this->assertNotEmpty($this->resolver->resolve());
- }
- public function testAddAllowedTypesDoesNotOverwrite()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setAllowedTypes('foo', 'string');
- $this->resolver->addAllowedTypes('foo', 'bool');
- $this->resolver->setDefault('foo', 'bar');
- $this->assertNotEmpty($this->resolver->resolve());
- }
- public function testAddAllowedTypesDoesNotOverwrite2()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setAllowedTypes('foo', 'string');
- $this->resolver->addAllowedTypes('foo', 'bool');
- $this->resolver->setDefault('foo', false);
- $this->assertNotEmpty($this->resolver->resolve());
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
- */
- public function testSetAllowedValuesFailsIfUnknownOption()
- {
- $this->resolver->setAllowedValues('foo', 'bar');
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testFailIfSetAllowedValuesFromLazyOption()
- {
- $this->resolver->setDefault('foo', function (Options $options) {
- $options->setAllowedValues('bar', 'baz');
- });
- $this->resolver->setDefault('bar', 'baz');
- $this->resolver->resolve();
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- * @expectedExceptionMessage The option "foo" with value 42 is invalid. Accepted values are: "bar".
- */
- public function testResolveFailsIfInvalidValue()
- {
- $this->resolver->setDefined('foo');
- $this->resolver->setAllowedValues('foo', 'bar');
- $this->resolver->resolve(array('foo' => 42));
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- * @expectedExceptionMessage The option "foo" with value null is invalid. Accepted values are: "bar".
- */
- public function testResolveFailsIfInvalidValueIsNull()
- {
- $this->resolver->setDefault('foo', null);
- $this->resolver->setAllowedValues('foo', 'bar');
- $this->resolver->resolve();
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- */
- public function testResolveFailsIfInvalidValueStrict()
- {
- $this->resolver->setDefault('foo', 42);
- $this->resolver->setAllowedValues('foo', '42');
- $this->resolver->resolve();
- }
- public function testResolveSucceedsIfValidValue()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setAllowedValues('foo', 'bar');
- $this->assertEquals(array('foo' => 'bar'), $this->resolver->resolve());
- }
- public function testResolveSucceedsIfValidValueIsNull()
- {
- $this->resolver->setDefault('foo', null);
- $this->resolver->setAllowedValues('foo', null);
- $this->assertEquals(array('foo' => null), $this->resolver->resolve());
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- * @expectedExceptionMessage The option "foo" with value 42 is invalid. Accepted values are: "bar", false, null.
- */
- public function testResolveFailsIfInvalidValueMultiple()
- {
- $this->resolver->setDefault('foo', 42);
- $this->resolver->setAllowedValues('foo', array('bar', false, null));
- $this->resolver->resolve();
- }
- public function testResolveSucceedsIfValidValueMultiple()
- {
- $this->resolver->setDefault('foo', 'baz');
- $this->resolver->setAllowedValues('foo', array('bar', 'baz'));
- $this->assertEquals(array('foo' => 'baz'), $this->resolver->resolve());
- }
- public function testResolveFailsIfClosureReturnsFalse()
- {
- $this->resolver->setDefault('foo', 42);
- $this->resolver->setAllowedValues('foo', function ($value) use (&$passedValue) {
- $passedValue = $value;
- return false;
- });
- try {
- $this->resolver->resolve();
- $this->fail('Should fail');
- } catch (InvalidOptionsException $e) {
- }
- $this->assertSame(42, $passedValue);
- }
- public function testResolveSucceedsIfClosureReturnsTrue()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setAllowedValues('foo', function ($value) use (&$passedValue) {
- $passedValue = $value;
- return true;
- });
- $this->assertEquals(array('foo' => 'bar'), $this->resolver->resolve());
- $this->assertSame('bar', $passedValue);
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- */
- public function testResolveFailsIfAllClosuresReturnFalse()
- {
- $this->resolver->setDefault('foo', 42);
- $this->resolver->setAllowedValues('foo', array(
- function () { return false; },
- function () { return false; },
- function () { return false; },
- ));
- $this->resolver->resolve();
- }
- public function testResolveSucceedsIfAnyClosureReturnsTrue()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setAllowedValues('foo', array(
- function () { return false; },
- function () { return true; },
- function () { return false; },
- ));
- $this->assertEquals(array('foo' => 'bar'), $this->resolver->resolve());
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
- */
- public function testAddAllowedValuesFailsIfUnknownOption()
- {
- $this->resolver->addAllowedValues('foo', 'bar');
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testFailIfAddAllowedValuesFromLazyOption()
- {
- $this->resolver->setDefault('foo', function (Options $options) {
- $options->addAllowedValues('bar', 'baz');
- });
- $this->resolver->setDefault('bar', 'baz');
- $this->resolver->resolve();
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- */
- public function testResolveFailsIfInvalidAddedValue()
- {
- $this->resolver->setDefault('foo', 42);
- $this->resolver->addAllowedValues('foo', 'bar');
- $this->resolver->resolve();
- }
- public function testResolveSucceedsIfValidAddedValue()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->addAllowedValues('foo', 'bar');
- $this->assertEquals(array('foo' => 'bar'), $this->resolver->resolve());
- }
- public function testResolveSucceedsIfValidAddedValueIsNull()
- {
- $this->resolver->setDefault('foo', null);
- $this->resolver->addAllowedValues('foo', null);
- $this->assertEquals(array('foo' => null), $this->resolver->resolve());
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- */
- public function testResolveFailsIfInvalidAddedValueMultiple()
- {
- $this->resolver->setDefault('foo', 42);
- $this->resolver->addAllowedValues('foo', array('bar', 'baz'));
- $this->resolver->resolve();
- }
- public function testResolveSucceedsIfValidAddedValueMultiple()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->addAllowedValues('foo', array('bar', 'baz'));
- $this->assertEquals(array('foo' => 'bar'), $this->resolver->resolve());
- }
- public function testAddAllowedValuesDoesNotOverwrite()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setAllowedValues('foo', 'bar');
- $this->resolver->addAllowedValues('foo', 'baz');
- $this->assertEquals(array('foo' => 'bar'), $this->resolver->resolve());
- }
- public function testAddAllowedValuesDoesNotOverwrite2()
- {
- $this->resolver->setDefault('foo', 'baz');
- $this->resolver->setAllowedValues('foo', 'bar');
- $this->resolver->addAllowedValues('foo', 'baz');
- $this->assertEquals(array('foo' => 'baz'), $this->resolver->resolve());
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- */
- public function testResolveFailsIfAllAddedClosuresReturnFalse()
- {
- $this->resolver->setDefault('foo', 42);
- $this->resolver->setAllowedValues('foo', function () { return false; });
- $this->resolver->addAllowedValues('foo', function () { return false; });
- $this->resolver->resolve();
- }
- public function testResolveSucceedsIfAnyAddedClosureReturnsTrue()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setAllowedValues('foo', function () { return false; });
- $this->resolver->addAllowedValues('foo', function () { return true; });
- $this->assertEquals(array('foo' => 'bar'), $this->resolver->resolve());
- }
- public function testResolveSucceedsIfAnyAddedClosureReturnsTrue2()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setAllowedValues('foo', function () { return true; });
- $this->resolver->addAllowedValues('foo', function () { return false; });
- $this->assertEquals(array('foo' => 'bar'), $this->resolver->resolve());
- }
- public function testSetNormalizerReturnsThis()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->assertSame($this->resolver, $this->resolver->setNormalizer('foo', function () {}));
- }
- public function testSetNormalizerClosure()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setNormalizer('foo', function () {
- return 'normalized';
- });
- $this->assertEquals(array('foo' => 'normalized'), $this->resolver->resolve());
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException
- */
- public function testSetNormalizerFailsIfUnknownOption()
- {
- $this->resolver->setNormalizer('foo', function () {});
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testFailIfSetNormalizerFromLazyOption()
- {
- $this->resolver->setDefault('foo', function (Options $options) {
- $options->setNormalizer('foo', function () {});
- });
- $this->resolver->setDefault('bar', 'baz');
- $this->resolver->resolve();
- }
- public function testNormalizerReceivesSetOption()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setNormalizer('foo', function (Options $options, $value) {
- return 'normalized['.$value.']';
- });
- $this->assertEquals(array('foo' => 'normalized[bar]'), $this->resolver->resolve());
- }
- public function testNormalizerReceivesPassedOption()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setNormalizer('foo', function (Options $options, $value) {
- return 'normalized['.$value.']';
- });
- $resolved = $this->resolver->resolve(array('foo' => 'baz'));
- $this->assertEquals(array('foo' => 'normalized[baz]'), $resolved);
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- */
- public function testValidateTypeBeforeNormalization()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setAllowedTypes('foo', 'int');
- $this->resolver->setNormalizer('foo', function () {
- Assert::fail('Should not be called.');
- });
- $this->resolver->resolve();
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
- */
- public function testValidateValueBeforeNormalization()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setAllowedValues('foo', 'baz');
- $this->resolver->setNormalizer('foo', function () {
- Assert::fail('Should not be called.');
- });
- $this->resolver->resolve();
- }
- public function testNormalizerCanAccessOtherOptions()
- {
- $this->resolver->setDefault('default', 'bar');
- $this->resolver->setDefault('norm', 'baz');
- $this->resolver->setNormalizer('norm', function (Options $options) {
- /* @var TestCase $test */
- Assert::assertSame('bar', $options['default']);
- return 'normalized';
- });
- $this->assertEquals(array(
- 'default' => 'bar',
- 'norm' => 'normalized',
- ), $this->resolver->resolve());
- }
- public function testNormalizerCanAccessLazyOptions()
- {
- $this->resolver->setDefault('lazy', function (Options $options) {
- return 'bar';
- });
- $this->resolver->setDefault('norm', 'baz');
- $this->resolver->setNormalizer('norm', function (Options $options) {
- /* @var TestCase $test */
- Assert::assertEquals('bar', $options['lazy']);
- return 'normalized';
- });
- $this->assertEquals(array(
- 'lazy' => 'bar',
- 'norm' => 'normalized',
- ), $this->resolver->resolve());
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
- */
- public function testFailIfCyclicDependencyBetweenNormalizers()
- {
- $this->resolver->setDefault('norm1', 'bar');
- $this->resolver->setDefault('norm2', 'baz');
- $this->resolver->setNormalizer('norm1', function (Options $options) {
- $options['norm2'];
- });
- $this->resolver->setNormalizer('norm2', function (Options $options) {
- $options['norm1'];
- });
- $this->resolver->resolve();
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
- */
- public function testFailIfCyclicDependencyBetweenNormalizerAndLazyOption()
- {
- $this->resolver->setDefault('lazy', function (Options $options) {
- $options['norm'];
- });
- $this->resolver->setDefault('norm', 'baz');
- $this->resolver->setNormalizer('norm', function (Options $options) {
- $options['lazy'];
- });
- $this->resolver->resolve();
- }
- public function testCaughtExceptionFromNormalizerDoesNotCrashOptionResolver()
- {
- $throw = true;
- $this->resolver->setDefaults(array('catcher' => null, 'thrower' => null));
- $this->resolver->setNormalizer('catcher', function (Options $options) {
- try {
- return $options['thrower'];
- } catch (\Exception $e) {
- return false;
- }
- });
- $this->resolver->setNormalizer('thrower', function () use (&$throw) {
- if ($throw) {
- $throw = false;
- throw new \UnexpectedValueException('throwing');
- }
- return true;
- });
- $this->assertSame(array('catcher' => false, 'thrower' => true), $this->resolver->resolve());
- }
- public function testCaughtExceptionFromLazyDoesNotCrashOptionResolver()
- {
- $throw = true;
- $this->resolver->setDefault('catcher', function (Options $options) {
- try {
- return $options['thrower'];
- } catch (\Exception $e) {
- return false;
- }
- });
- $this->resolver->setDefault('thrower', function (Options $options) use (&$throw) {
- if ($throw) {
- $throw = false;
- throw new \UnexpectedValueException('throwing');
- }
- return true;
- });
- $this->assertSame(array('catcher' => false, 'thrower' => true), $this->resolver->resolve());
- }
- public function testInvokeEachNormalizerOnlyOnce()
- {
- $calls = 0;
- $this->resolver->setDefault('norm1', 'bar');
- $this->resolver->setDefault('norm2', 'baz');
- $this->resolver->setNormalizer('norm1', function ($options) use (&$calls) {
- Assert::assertSame(1, ++$calls);
- $options['norm2'];
- });
- $this->resolver->setNormalizer('norm2', function () use (&$calls) {
- Assert::assertSame(2, ++$calls);
- });
- $this->resolver->resolve();
- $this->assertSame(2, $calls);
- }
- public function testNormalizerNotCalledForUnsetOptions()
- {
- $this->resolver->setDefined('norm');
- $this->resolver->setNormalizer('norm', function () {
- Assert::fail('Should not be called.');
- });
- $this->assertEmpty($this->resolver->resolve());
- }
- public function testSetDefaultsReturnsThis()
- {
- $this->assertSame($this->resolver, $this->resolver->setDefaults(array('foo', 'bar')));
- }
- public function testSetDefaults()
- {
- $this->resolver->setDefault('one', '1');
- $this->resolver->setDefault('two', 'bar');
- $this->resolver->setDefaults(array(
- 'two' => '2',
- 'three' => '3',
- ));
- $this->assertEquals(array(
- 'one' => '1',
- 'two' => '2',
- 'three' => '3',
- ), $this->resolver->resolve());
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testFailIfSetDefaultsFromLazyOption()
- {
- $this->resolver->setDefault('foo', function (Options $options) {
- $options->setDefaults(array('two' => '2'));
- });
- $this->resolver->resolve();
- }
- public function testRemoveReturnsThis()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->assertSame($this->resolver, $this->resolver->remove('foo'));
- }
- public function testRemoveSingleOption()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setDefault('baz', 'boo');
- $this->resolver->remove('foo');
- $this->assertSame(array('baz' => 'boo'), $this->resolver->resolve());
- }
- public function testRemoveMultipleOptions()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setDefault('baz', 'boo');
- $this->resolver->setDefault('doo', 'dam');
- $this->resolver->remove(array('foo', 'doo'));
- $this->assertSame(array('baz' => 'boo'), $this->resolver->resolve());
- }
- public function testRemoveLazyOption()
- {
- $this->resolver->setDefault('foo', function (Options $options) {
- return 'lazy';
- });
- $this->resolver->remove('foo');
- $this->assertSame(array(), $this->resolver->resolve());
- }
- public function testRemoveNormalizer()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setNormalizer('foo', function (Options $options, $value) {
- return 'normalized';
- });
- $this->resolver->remove('foo');
- $this->resolver->setDefault('foo', 'bar');
- $this->assertSame(array('foo' => 'bar'), $this->resolver->resolve());
- }
- public function testRemoveAllowedTypes()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setAllowedTypes('foo', 'int');
- $this->resolver->remove('foo');
- $this->resolver->setDefault('foo', 'bar');
- $this->assertSame(array('foo' => 'bar'), $this->resolver->resolve());
- }
- public function testRemoveAllowedValues()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setAllowedValues('foo', array('baz', 'boo'));
- $this->resolver->remove('foo');
- $this->resolver->setDefault('foo', 'bar');
- $this->assertSame(array('foo' => 'bar'), $this->resolver->resolve());
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testFailIfRemoveFromLazyOption()
- {
- $this->resolver->setDefault('foo', function (Options $options) {
- $options->remove('bar');
- });
- $this->resolver->setDefault('bar', 'baz');
- $this->resolver->resolve();
- }
- public function testRemoveUnknownOptionIgnored()
- {
- $this->assertNotNull($this->resolver->remove('foo'));
- }
- public function testClearReturnsThis()
- {
- $this->assertSame($this->resolver, $this->resolver->clear());
- }
- public function testClearRemovesAllOptions()
- {
- $this->resolver->setDefault('one', 1);
- $this->resolver->setDefault('two', 2);
- $this->resolver->clear();
- $this->assertEmpty($this->resolver->resolve());
- }
- public function testClearLazyOption()
- {
- $this->resolver->setDefault('foo', function (Options $options) {
- return 'lazy';
- });
- $this->resolver->clear();
- $this->assertSame(array(), $this->resolver->resolve());
- }
- public function testClearNormalizer()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setNormalizer('foo', function (Options $options, $value) {
- return 'normalized';
- });
- $this->resolver->clear();
- $this->resolver->setDefault('foo', 'bar');
- $this->assertSame(array('foo' => 'bar'), $this->resolver->resolve());
- }
- public function testClearAllowedTypes()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setAllowedTypes('foo', 'int');
- $this->resolver->clear();
- $this->resolver->setDefault('foo', 'bar');
- $this->assertSame(array('foo' => 'bar'), $this->resolver->resolve());
- }
- public function testClearAllowedValues()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setAllowedValues('foo', 'baz');
- $this->resolver->clear();
- $this->resolver->setDefault('foo', 'bar');
- $this->assertSame(array('foo' => 'bar'), $this->resolver->resolve());
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testFailIfClearFromLazyption()
- {
- $this->resolver->setDefault('foo', function (Options $options) {
- $options->clear();
- });
- $this->resolver->setDefault('bar', 'baz');
- $this->resolver->resolve();
- }
- public function testClearOptionAndNormalizer()
- {
- $this->resolver->setDefault('foo1', 'bar');
- $this->resolver->setNormalizer('foo1', function (Options $options) {
- return '';
- });
- $this->resolver->setDefault('foo2', 'bar');
- $this->resolver->setNormalizer('foo2', function (Options $options) {
- return '';
- });
- $this->resolver->clear();
- $this->assertEmpty($this->resolver->resolve());
- }
- public function testArrayAccess()
- {
- $this->resolver->setDefault('default1', 0);
- $this->resolver->setDefault('default2', 1);
- $this->resolver->setRequired('required');
- $this->resolver->setDefined('defined');
- $this->resolver->setDefault('lazy1', function (Options $options) {
- return 'lazy';
- });
- $this->resolver->setDefault('lazy2', function (Options $options) {
- Assert::assertArrayHasKey('default1', $options);
- Assert::assertArrayHasKey('default2', $options);
- Assert::assertArrayHasKey('required', $options);
- Assert::assertArrayHasKey('lazy1', $options);
- Assert::assertArrayHasKey('lazy2', $options);
- Assert::assertArrayNotHasKey('defined', $options);
- Assert::assertSame(0, $options['default1']);
- Assert::assertSame(42, $options['default2']);
- Assert::assertSame('value', $options['required']);
- Assert::assertSame('lazy', $options['lazy1']);
- // Obviously $options['lazy'] and $options['defined'] cannot be
- // accessed
- });
- $this->resolver->resolve(array('default2' => 42, 'required' => 'value'));
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testArrayAccessGetFailsOutsideResolve()
- {
- $this->resolver->setDefault('default', 0);
- $this->resolver['default'];
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testArrayAccessExistsFailsOutsideResolve()
- {
- $this->resolver->setDefault('default', 0);
- isset($this->resolver['default']);
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testArrayAccessSetNotSupported()
- {
- $this->resolver['default'] = 0;
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testArrayAccessUnsetNotSupported()
- {
- $this->resolver->setDefault('default', 0);
- unset($this->resolver['default']);
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException
- * @expectedExceptionMessage The option "undefined" does not exist. Defined options are: "foo", "lazy".
- */
- public function testFailIfGetNonExisting()
- {
- $this->resolver->setDefault('foo', 'bar');
- $this->resolver->setDefault('lazy', function (Options $options) {
- $options['undefined'];
- });
- $this->resolver->resolve();
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException
- * @expectedExceptionMessage The optional option "defined" has no value set. You should make sure it is set with "isset" before reading it.
- */
- public function testFailIfGetDefinedButUnset()
- {
- $this->resolver->setDefined('defined');
- $this->resolver->setDefault('lazy', function (Options $options) {
- $options['defined'];
- });
- $this->resolver->resolve();
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
- */
- public function testFailIfCyclicDependency()
- {
- $this->resolver->setDefault('lazy1', function (Options $options) {
- $options['lazy2'];
- });
- $this->resolver->setDefault('lazy2', function (Options $options) {
- $options['lazy1'];
- });
- $this->resolver->resolve();
- }
- public function testCount()
- {
- $this->resolver->setDefault('default', 0);
- $this->resolver->setRequired('required');
- $this->resolver->setDefined('defined');
- $this->resolver->setDefault('lazy1', function () {});
- $this->resolver->setDefault('lazy2', function (Options $options) {
- Assert::assertCount(4, $options);
- });
- $this->assertCount(4, $this->resolver->resolve(array('required' => 'value')));
- }
- /**
- * In resolve() we count the options that are actually set (which may be
- * only a subset of the defined options). Outside of resolve(), it's not
- * clear what is counted.
- *
- * @expectedException \Symfony\Component\OptionsResolver\Exception\AccessException
- */
- public function testCountFailsOutsideResolve()
- {
- $this->resolver->setDefault('foo', 0);
- $this->resolver->setRequired('bar');
- $this->resolver->setDefined('bar');
- $this->resolver->setDefault('lazy1', function () {});
- \count($this->resolver);
- }
- }
|