[Config] Fix ArrayNode extra keys "ignore" and "remove" behaviors

This commit is contained in:
ogizanagi 2015-08-23 21:34:36 +02:00
parent 4cc8b76787
commit d961f7f344
2 changed files with 19 additions and 23 deletions

View File

@ -303,9 +303,8 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
if (isset($this->children[$name])) { if (isset($this->children[$name])) {
$normalized[$name] = $this->children[$name]->normalize($val); $normalized[$name] = $this->children[$name]->normalize($val);
unset($value[$name]); unset($value[$name]);
} elseif (false === $this->removeExtraKeys) { } elseif (!$this->removeExtraKeys) {
$normalized[$name] = $val; $normalized[$name] = $val;
unset($value[$name]);
} }
} }

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\Config\Tests\Definition; namespace Symfony\Component\Config\Tests\Definition;
use Symfony\Component\Config\Definition\ArrayNode; use Symfony\Component\Config\Definition\ArrayNode;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\ScalarNode; use Symfony\Component\Config\Definition\ScalarNode;
class ArrayNodeTest extends \PHPUnit_Framework_TestCase class ArrayNodeTest extends \PHPUnit_Framework_TestCase
@ -35,34 +36,30 @@ class ArrayNodeTest extends \PHPUnit_Framework_TestCase
$node->normalize(array('foo' => 'bar')); $node->normalize(array('foo' => 'bar'));
} }
/** public function ignoreAndRemoveMatrixProvider()
* Tests that no exception is thrown for an unrecognized child if the
* ignoreExtraKeys option is set to true.
*
* Related to testExceptionThrownOnUnrecognizedChild
*/
public function testIgnoreExtraKeysNoException()
{ {
$node = new ArrayNode('roo'); $unrecognizedOptionException = new InvalidConfigurationException('Unrecognized option "foo" under "root"');
$node->setIgnoreExtraKeys(true);
$node->normalize(array('foo' => 'bar')); return array(
$this->assertTrue(true, 'No exception was thrown when setIgnoreExtraKeys is true'); array(true, true, array(), 'no exception is thrown for an unrecognized child if the ignoreExtraKeys option is set to true'),
array(true, false, array('foo' => 'bar'), 'extra keys are not removed when ignoreExtraKeys second option is set to false'),
array(false, true, $unrecognizedOptionException),
array(false, false, $unrecognizedOptionException),
);
} }
/** /**
* Tests that extra keys are not removed when * @dataProvider ignoreAndRemoveMatrixProvider
* ignoreExtraKeys second option is set to false.
*
* Related to testExceptionThrownOnUnrecognizedChild
*/ */
public function testIgnoreExtraKeysNotRemoved() public function testIgnoreAndRemoveBehaviors($ignore, $remove, $expected, $message = '')
{ {
$node = new ArrayNode('roo'); if ($expected instanceof \Exception) {
$node->setIgnoreExtraKeys(true, false); $this->setExpectedException(get_class($expected), $expected->getMessage());
}
$data = array('foo' => 'bar'); $node = new ArrayNode('root');
$this->assertSame($data, $node->normalize($data)); $node->setIgnoreExtraKeys($ignore, $remove);
$result = $node->normalize(array('foo' => 'bar'));
$this->assertSame($expected, $result, $message);
} }
/** /**