bug #13816 [OptionsResolver] fix allowed values with null (Tobion)

This PR was merged into the 2.6 branch.

Discussion
----------

[OptionsResolver] fix allowed values with null

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        | -

1. setNormalizers was missing deprecated annotation
2. setAllowedValues/addAllowedValues with `null` was broken because `(array) null === array()`

Commits
-------

cb37fbe [OptionsResolver] fix allowed values with null
e3e9a4d [OptionsResolver] add missing deprecated phpdoc for setNormalizers
This commit is contained in:
Tobias Schultze 2015-03-02 11:19:04 +01:00
commit 38a5ebdb05
2 changed files with 36 additions and 7 deletions

View File

@ -418,7 +418,18 @@ class OptionsResolver implements Options, OptionsResolverInterface
}
/**
* {@inheritdoc}
* Sets the normalizers for an array of options.
*
* @param array $normalizers An array of closures
*
* @return OptionsResolver This instance
*
* @throws UndefinedOptionsException If the option is undefined
* @throws AccessException If called from a lazy option or normalizer
*
* @see setNormalizer()
*
* @deprecated since version 2.6, to be removed in 3.0.
*/
public function setNormalizers(array $normalizers)
{
@ -473,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]);
@ -527,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
*/