[OptionsResolver] fix allowed values with null

This commit is contained in:
Tobias Schultze 2015-03-01 03:42:24 +01:00
parent e3e9a4d269
commit cb37fbe707
2 changed files with 24 additions and 6 deletions

View File

@ -484,7 +484,7 @@ class OptionsResolver implements Options, OptionsResolverInterface
));
}
$this->allowedValues[$option] = $allowedValues instanceof \Closure ? array($allowedValues) : (array) $allowedValues;
$this->allowedValues[$option] = is_array($allowedValues) ? $allowedValues : array($allowedValues);
// Make sure the option is processed
unset($this->resolved[$option]);
@ -538,12 +538,14 @@ class OptionsResolver implements Options, OptionsResolverInterface
));
}
if ($allowedValues instanceof \Closure) {
$this->allowedValues[$option][] = $allowedValues;
} elseif (!isset($this->allowedValues[$option])) {
$this->allowedValues[$option] = (array) $allowedValues;
if (!is_array($allowedValues)) {
$allowedValues = array($allowedValues);
}
if (!isset($this->allowedValues[$option])) {
$this->allowedValues[$option] = $allowedValues;
} else {
$this->allowedValues[$option] = array_merge($this->allowedValues[$option], (array) $allowedValues);
$this->allowedValues[$option] = array_merge($this->allowedValues[$option], $allowedValues);
}
// Make sure the option is processed

View File

@ -724,6 +724,14 @@ class OptionsResolver2Dot6Test extends \PHPUnit_Framework_TestCase
$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.
@ -847,6 +855,14 @@ class OptionsResolver2Dot6Test extends \PHPUnit_Framework_TestCase
$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
*/