merged branch EvanK/master (PR #5132)

Commits
-------

aa890ae validateOptionTypes checking existence of key rather than value
c6a9638 OptionsResolver#validateOptionTypes should check if option value exists before checking its type; added corresponding test

Discussion
----------

[OptionsResolver] validateOptionTypes raises error for optional, nonexistant option values

The [`OptionsResolver#validateOptionTypes`](https://github.com/symfony/OptionsResolver/blob/master/OptionsResolver.php#L315) method should check if a given option value exists before trying to test its type:

```php
<?php
// Two optional 'one' and 'two' options that, if they exist, must be a string and int respectively
$resolver = new OptionsResolver();
$resolver->setOptional(array('one', 'two'))->setAllowedTypes(array('one' => 'string', 'two' => 'int'));

// Correctly fails as wrong type
$resolver->resolve(array('one' => 1, 'two' => 'alpha'));

// Correctly succeeds
$resolver->resolve(array('one' => 'alpha', 'two' => 1));

// Raises error "Undefined index: two", see OptionsResolver.php line 315
$resolver->resolve(array('one' => 'alpha'));
```

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

by travisbot at 2012-08-01T01:46:50Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/2006049) (merged c6a9638a into b1618d21).

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

by ericclemmons at 2012-08-03T05:15:27Z

I hope this makes it in for the 2.1 release.  We have several types attempting to take advantage of the `OptionsResolver` component and want to help resolve this.

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

by stof at 2012-08-03T07:30:44Z

👍

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

by travisbot at 2012-08-03T17:25:12Z

This pull request [passes](http://travis-ci.org/symfony/symfony/builds/2030053) (merged aa890aed into b1618d21).
This commit is contained in:
Fabien Potencier 2012-08-04 11:23:52 +02:00
commit 7a75a1160b
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 (!array_key_exists($option, $options)) {
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
*/