12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559 |
- <?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();
- }
- ////////////////////////////////////////////////////////////////////////////
- // resolve()
- ////////////////////////////////////////////////////////////////////////////
- /**
- * @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();
- }
- ////////////////////////////////////////////////////////////////////////////
- // setDefault()/hasDefault()
- ////////////////////////////////////////////////////////////////////////////
- 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'));
- }
- ////////////////////////////////////////////////////////////////////////////
- // lazy setDefault()
- ////////////////////////////////////////////////////////////////////////////
- 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);
- }
- ////////////////////////////////////////////////////////////////////////////
- // setRequired()/isRequired()/getRequiredOptions()
- ////////////////////////////////////////////////////////////////////////////
- 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());
- }
- ////////////////////////////////////////////////////////////////////////////
- // isMissing()/getMissingOptions()
- ////////////////////////////////////////////////////////////////////////////
- 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());
- }
- ////////////////////////////////////////////////////////////////////////////
- // setDefined()/isDefined()/getDefinedOptions()
- ////////////////////////////////////////////////////////////////////////////
- /**
- * @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'));
- }
- ////////////////////////////////////////////////////////////////////////////
- // setAllowedTypes()
- ////////////////////////////////////////////////////////////////////////////
- /**
- * @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());
- }
- ////////////////////////////////////////////////////////////////////////////
- // addAllowedTypes()
- ////////////////////////////////////////////////////////////////////////////
- /**
- * @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());
- }
- ////////////////////////////////////////////////////////////////////////////
- // setAllowedValues()
- ////////////////////////////////////////////////////////////////////////////
- /**
- * @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());
- }
- ////////////////////////////////////////////////////////////////////////////
- // addAllowedValues()
- ////////////////////////////////////////////////////////////////////////////
- /**
- * @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());
- }
- ////////////////////////////////////////////////////////////////////////////
- // setNormalizer()
- ////////////////////////////////////////////////////////////////////////////
- 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());
- }
- ////////////////////////////////////////////////////////////////////////////
- // setDefaults()
- ////////////////////////////////////////////////////////////////////////////
- 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();
- }
- ////////////////////////////////////////////////////////////////////////////
- // remove()
- ////////////////////////////////////////////////////////////////////////////
- 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'));
- }
- ////////////////////////////////////////////////////////////////////////////
- // clear()
- ////////////////////////////////////////////////////////////////////////////
- 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());
- }
- ////////////////////////////////////////////////////////////////////////////
- // ArrayAccess
- ////////////////////////////////////////////////////////////////////////////
- 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();
- }
- ////////////////////////////////////////////////////////////////////////////
- // Countable
- ////////////////////////////////////////////////////////////////////////////
- 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);
- }
- }
|