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

This commit is contained in:
Bernhard Schussek 2012-05-15 11:47:43 +02:00
parent bd07b8919d
commit d2c162d842
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'));
}
}