OptionsResolver#validateOptionTypes should check if option value exists before checking its type; added corresponding test

OptionsResolver#validateOptionsCompleteness would already have thrown exception if the option were required, so this should only affect something explicitly marked as optional
This commit is contained in:
Evan Kaufman 2012-07-31 17:59:01 -05:00
parent b1618d210c
commit c6a9638adb
2 changed files with 25 additions and 0 deletions

View File

@ -312,6 +312,10 @@ class OptionsResolver implements OptionsResolverInterface
private function validateOptionTypes(array $options)
{
foreach ($this->allowedTypes as $option => $allowedTypes) {
if (!isset($options[$option])) {
continue;
}
$value = $options[$option];
$allowedTypes = (array) $allowedTypes;

View File

@ -381,6 +381,27 @@ class OptionsResolverTest extends \PHPUnit_Framework_TestCase
), $this->resolver->resolve($options));
}
public function testResolveSucceedsIfOptionalWithTypeAndWithoutValue()
{
$this->resolver->setOptional(array(
'one',
'two',
));
$this->resolver->setAllowedTypes(array(
'one' => 'string',
'two' => 'int',
));
$options = array(
'two' => 1,
);
$this->assertEquals(array(
'two' => 1,
), $this->resolver->resolve($options));
}
/**
* @expectedException Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/