merged branch bschussek/options-resolver (PR #4292)

Commits
-------

d2c162d [OptionsResolver] Added methods isKnown() and isRequired()

Discussion
----------

[OptionsResolver] Added methods isKnown() and isRequired()

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -

---------------------------------------------------------------------------

by travisbot at 2012-05-15T14:42:10Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/1336375) (merged d2c162d8 into 563f77a3).
This commit is contained in:
Fabien Potencier 2012-05-15 17:10:00 +02:00
commit ca56446507
2 changed files with 87 additions and 0 deletions

View File

@ -198,6 +198,35 @@ class OptionsResolver
return $this;
}
/**
* Returns whether an option is known.
*
* An option is known if it has been passed to either {@link setDefaults()},
* {@link setRequired()} or {@link setOptional()} before.
*
* @param string $option The name of the option.
* @return Boolean Whether the option is known.
*/
public function isKnown($option)
{
return isset($this->knownOptions[$option]);
}
/**
* Returns whether an option is required.
*
* An option is required if it has been passed to {@link setRequired()},
* but not to {@link setDefaults()}. That is, the option has been declared
* as required and no default value has been set.
*
* @param string $option The name of the option.
* @return Boolean Whether the option is required.
*/
public function isRequired($option)
{
return isset($this->requiredOptions[$option]) && !isset($this->defaultOptions[$option]);
}
/**
* Returns the combination of the default and the passed options.
*

View File

@ -314,4 +314,62 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
'two' => '2',
), $this->resolver->resolve($options));
}
public function testKnownIfDefaultWasSet()
{
$this->assertFalse($this->resolver->isKnown('foo'));
$this->resolver->setDefaults(array(
'foo' => 'bar',
));
$this->assertTrue($this->resolver->isKnown('foo'));
}
public function testKnownIfRequired()
{
$this->assertFalse($this->resolver->isKnown('foo'));
$this->resolver->setRequired(array(
'foo',
));
$this->assertTrue($this->resolver->isKnown('foo'));
}
public function testKnownIfOptional()
{
$this->assertFalse($this->resolver->isKnown('foo'));
$this->resolver->setOptional(array(
'foo',
));
$this->assertTrue($this->resolver->isKnown('foo'));
}
public function testRequiredIfRequired()
{
$this->assertFalse($this->resolver->isRequired('foo'));
$this->resolver->setRequired(array(
'foo',
));
$this->assertTrue($this->resolver->isRequired('foo'));
}
public function testNotRequiredIfRequiredAndDefaultValue()
{
$this->assertFalse($this->resolver->isRequired('foo'));
$this->resolver->setRequired(array(
'foo',
));
$this->resolver->setDefaults(array(
'foo' => 'bar',
));
$this->assertFalse($this->resolver->isRequired('foo'));
}
}