123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529 |
- <?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 Symfony\Component\OptionsResolver\Options;
- class OptionsTest extends \PHPUnit_Framework_TestCase
- {
- /**
- * @var Options
- */
- private $options;
- protected function setUp()
- {
- $this->options = new Options();
- }
- public function testArrayAccess()
- {
- $this->assertFalse(isset($this->options['foo']));
- $this->assertFalse(isset($this->options['bar']));
- $this->options['foo'] = 0;
- $this->options['bar'] = 1;
- $this->assertTrue(isset($this->options['foo']));
- $this->assertTrue(isset($this->options['bar']));
- unset($this->options['bar']);
- $this->assertTrue(isset($this->options['foo']));
- $this->assertFalse(isset($this->options['bar']));
- $this->assertEquals(0, $this->options['foo']);
- }
- public function testCountable()
- {
- $this->options->set('foo', 0);
- $this->options->set('bar', 1);
- $this->assertCount(2, $this->options);
- }
- /**
- * @expectedException \OutOfBoundsException
- */
- public function testGetNonExisting()
- {
- $this->options->get('foo');
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
- */
- public function testSetNotSupportedAfterGet()
- {
- $this->options->set('foo', 'bar');
- $this->options->get('foo');
- $this->options->set('foo', 'baz');
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
- */
- public function testRemoveNotSupportedAfterGet()
- {
- $this->options->set('foo', 'bar');
- $this->options->get('foo');
- $this->options->remove('foo');
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
- */
- public function testSetNormalizerNotSupportedAfterGet()
- {
- $this->options->set('foo', 'bar');
- $this->options->get('foo');
- $this->options->setNormalizer('foo', function () {});
- }
- public function testSetLazyOption()
- {
- $test = $this;
- $this->options->set('foo', function (Options $options) use ($test) {
- return 'dynamic';
- });
- $this->assertEquals('dynamic', $this->options->get('foo'));
- }
- public function testSetDiscardsPreviousValue()
- {
- $test = $this;
- // defined by superclass
- $this->options->set('foo', 'bar');
- // defined by subclass
- $this->options->set('foo', function (Options $options, $previousValue) use ($test) {
- /* @var \PHPUnit_Framework_TestCase $test */
- $test->assertNull($previousValue);
- return 'dynamic';
- });
- $this->assertEquals('dynamic', $this->options->get('foo'));
- }
- public function testOverloadKeepsPreviousValue()
- {
- $test = $this;
- // defined by superclass
- $this->options->set('foo', 'bar');
- // defined by subclass
- $this->options->overload('foo', function (Options $options, $previousValue) use ($test) {
- /* @var \PHPUnit_Framework_TestCase $test */
- $test->assertEquals('bar', $previousValue);
- return 'dynamic';
- });
- $this->assertEquals('dynamic', $this->options->get('foo'));
- }
- public function testPreviousValueIsEvaluatedIfLazy()
- {
- $test = $this;
- // defined by superclass
- $this->options->set('foo', function (Options $options) {
- return 'bar';
- });
- // defined by subclass
- $this->options->overload('foo', function (Options $options, $previousValue) use ($test) {
- /* @var \PHPUnit_Framework_TestCase $test */
- $test->assertEquals('bar', $previousValue);
- return 'dynamic';
- });
- $this->assertEquals('dynamic', $this->options->get('foo'));
- }
- public function testPreviousValueIsNotEvaluatedIfNoSecondArgument()
- {
- $test = $this;
- // defined by superclass
- $this->options->set('foo', function (Options $options) use ($test) {
- $test->fail('Should not be called');
- });
- // defined by subclass, no $previousValue argument defined!
- $this->options->overload('foo', function (Options $options) use ($test) {
- return 'dynamic';
- });
- $this->assertEquals('dynamic', $this->options->get('foo'));
- }
- public function testLazyOptionCanAccessOtherOptions()
- {
- $test = $this;
- $this->options->set('foo', 'bar');
- $this->options->set('bam', function (Options $options) use ($test) {
- /* @var \PHPUnit_Framework_TestCase $test */
- $test->assertEquals('bar', $options->get('foo'));
- return 'dynamic';
- });
- $this->assertEquals('bar', $this->options->get('foo'));
- $this->assertEquals('dynamic', $this->options->get('bam'));
- }
- public function testLazyOptionCanAccessOtherLazyOptions()
- {
- $test = $this;
- $this->options->set('foo', function (Options $options) {
- return 'bar';
- });
- $this->options->set('bam', function (Options $options) use ($test) {
- /* @var \PHPUnit_Framework_TestCase $test */
- $test->assertEquals('bar', $options->get('foo'));
- return 'dynamic';
- });
- $this->assertEquals('bar', $this->options->get('foo'));
- $this->assertEquals('dynamic', $this->options->get('bam'));
- }
- public function testNormalizer()
- {
- $this->options->set('foo', 'bar');
- $this->options->setNormalizer('foo', function () {
- return 'normalized';
- });
- $this->assertEquals('normalized', $this->options->get('foo'));
- }
- public function testNormalizerReceivesUnnormalizedValue()
- {
- $this->options->set('foo', 'bar');
- $this->options->setNormalizer('foo', function (Options $options, $value) {
- return 'normalized['.$value.']';
- });
- $this->assertEquals('normalized[bar]', $this->options->get('foo'));
- }
- public function testNormalizerCanAccessOtherOptions()
- {
- $test = $this;
- $this->options->set('foo', 'bar');
- $this->options->set('bam', 'baz');
- $this->options->setNormalizer('bam', function (Options $options) use ($test) {
- /* @var \PHPUnit_Framework_TestCase $test */
- $test->assertEquals('bar', $options->get('foo'));
- return 'normalized';
- });
- $this->assertEquals('bar', $this->options->get('foo'));
- $this->assertEquals('normalized', $this->options->get('bam'));
- }
- public function testNormalizerCanAccessOtherLazyOptions()
- {
- $test = $this;
- $this->options->set('foo', function (Options $options) {
- return 'bar';
- });
- $this->options->set('bam', 'baz');
- $this->options->setNormalizer('bam', function (Options $options) use ($test) {
- /* @var \PHPUnit_Framework_TestCase $test */
- $test->assertEquals('bar', $options->get('foo'));
- return 'normalized';
- });
- $this->assertEquals('bar', $this->options->get('foo'));
- $this->assertEquals('normalized', $this->options->get('bam'));
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
- */
- public function testFailForCyclicDependencies()
- {
- $this->options->set('foo', function (Options $options) {
- $options->get('bam');
- });
- $this->options->set('bam', function (Options $options) {
- $options->get('foo');
- });
- $this->options->get('foo');
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
- */
- public function testFailForCyclicDependenciesBetweenNormalizers()
- {
- $this->options->set('foo', 'bar');
- $this->options->set('bam', 'baz');
- $this->options->setNormalizer('foo', function (Options $options) {
- $options->get('bam');
- });
- $this->options->setNormalizer('bam', function (Options $options) {
- $options->get('foo');
- });
- $this->options->get('foo');
- }
- /**
- * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
- */
- public function testFailForCyclicDependenciesBetweenNormalizerAndLazyOption()
- {
- $this->options->set('foo', function (Options $options) {
- $options->get('bam');
- });
- $this->options->set('bam', 'baz');
- $this->options->setNormalizer('bam', function (Options $options) {
- $options->get('foo');
- });
- $this->options->get('foo');
- }
- public function testAllInvokesEachLazyOptionOnlyOnce()
- {
- $test = $this;
- $i = 1;
- $this->options->set('foo', function (Options $options) use ($test, &$i) {
- $test->assertSame(1, $i);
- ++$i;
- // Implicitly invoke lazy option for "bam"
- $options->get('bam');
- });
- $this->options->set('bam', function (Options $options) use ($test, &$i) {
- $test->assertSame(2, $i);
- ++$i;
- });
- $this->options->all();
- }
- public function testAllInvokesEachNormalizerOnlyOnce()
- {
- $test = $this;
- $i = 1;
- $this->options->set('foo', 'bar');
- $this->options->set('bam', 'baz');
- $this->options->setNormalizer('foo', function (Options $options) use ($test, &$i) {
- $test->assertSame(1, $i);
- ++$i;
- // Implicitly invoke normalizer for "bam"
- $options->get('bam');
- });
- $this->options->setNormalizer('bam', function (Options $options) use ($test, &$i) {
- $test->assertSame(2, $i);
- ++$i;
- });
- $this->options->all();
- }
- public function testReplaceClearsAndSets()
- {
- $this->options->set('one', '1');
- $this->options->replace(array(
- 'two' => '2',
- 'three' => function (Options $options) {
- return '2' === $options['two'] ? '3' : 'foo';
- }
- ));
- $this->assertEquals(array(
- 'two' => '2',
- 'three' => '3',
- ), $this->options->all());
- }
- public function testClearRemovesAllOptions()
- {
- $this->options->set('one', 1);
- $this->options->set('two', 2);
- $this->options->clear();
- $this->assertEmpty($this->options->all());
- }
- /**
- * @covers Symfony\Component\OptionsResolver\Options::replace
- * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
- */
- public function testCannotReplaceAfterOptionWasRead()
- {
- $this->options->set('one', 1);
- $this->options->all();
- $this->options->replace(array(
- 'two' => '2',
- ));
- }
- /**
- * @covers Symfony\Component\OptionsResolver\Options::overload
- * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
- */
- public function testCannotOverloadAfterOptionWasRead()
- {
- $this->options->set('one', 1);
- $this->options->all();
- $this->options->overload('one', 2);
- }
- /**
- * @covers Symfony\Component\OptionsResolver\Options::clear
- * @expectedException \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException
- */
- public function testCannotClearAfterOptionWasRead()
- {
- $this->options->set('one', 1);
- $this->options->all();
- $this->options->clear();
- }
- public function testOverloadCannotBeEvaluatedLazilyWithoutExpectedClosureParams()
- {
- $this->options->set('foo', 'bar');
- $this->options->overload('foo', function () {
- return 'test';
- });
- $this->assertNotEquals('test', $this->options->get('foo'));
- $this->assertTrue(is_callable($this->options->get('foo')));
- }
- public function testOverloadCannotBeEvaluatedLazilyWithoutFirstParamTypeHint()
- {
- $this->options->set('foo', 'bar');
- $this->options->overload('foo', function ($object) {
- return 'test';
- });
- $this->assertNotEquals('test', $this->options->get('foo'));
- $this->assertTrue(is_callable($this->options->get('foo')));
- }
- public function testOptionsIteration()
- {
- $this->options->set('foo', 'bar');
- $this->options->set('foo1', 'bar1');
- $expectedResult = array('foo' => 'bar', 'foo1' => 'bar1');
- $this->assertEquals($expectedResult, iterator_to_array($this->options, true));
- }
- public function testHasWithNullValue()
- {
- $this->options->set('foo', null);
- $this->assertTrue($this->options->has('foo'));
- }
- public function testRemoveOptionAndNormalizer()
- {
- $this->options->set('foo1', 'bar');
- $this->options->setNormalizer('foo1', function (Options $options) {
- return '';
- });
- $this->options->set('foo2', 'bar');
- $this->options->setNormalizer('foo2', function (Options $options) {
- return '';
- });
- $this->options->remove('foo2');
- $this->assertEquals(array('foo1' => ''), $this->options->all());
- }
- public function testReplaceOptionAndNormalizer()
- {
- $this->options->set('foo1', 'bar');
- $this->options->setNormalizer('foo1', function (Options $options) {
- return '';
- });
- $this->options->set('foo2', 'bar');
- $this->options->setNormalizer('foo2', function (Options $options) {
- return '';
- });
- $this->options->replace(array('foo1' => 'new'));
- $this->assertEquals(array('foo1' => 'new'), $this->options->all());
- }
- public function testClearOptionAndNormalizer()
- {
- $this->options->set('foo1', 'bar');
- $this->options->setNormalizer('foo1', function (Options $options) {
- return '';
- });
- $this->options->set('foo2', 'bar');
- $this->options->setNormalizer('foo2', function (Options $options) {
- return '';
- });
- $this->options->clear();
- $this->assertEmpty($this->options->all());
- }
- public function testNormalizerWithoutCorrespondingOption()
- {
- $test = $this;
- $this->options->setNormalizer('foo', function (Options $options, $previousValue) use ($test) {
- $test->assertNull($previousValue);
- return '';
- });
- $this->assertEquals(array('foo' => ''), $this->options->all());
- }
- }
|