[Config] Fix ArrayNode extra keys "ignore" and "remove" behaviors
This commit is contained in:
parent
4cc8b76787
commit
d961f7f344
|
@ -303,9 +303,8 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
|
|||
if (isset($this->children[$name])) {
|
||||
$normalized[$name] = $this->children[$name]->normalize($val);
|
||||
unset($value[$name]);
|
||||
} elseif (false === $this->removeExtraKeys) {
|
||||
} elseif (!$this->removeExtraKeys) {
|
||||
$normalized[$name] = $val;
|
||||
unset($value[$name]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,6 +12,7 @@
|
|||
namespace Symfony\Component\Config\Tests\Definition;
|
||||
|
||||
use Symfony\Component\Config\Definition\ArrayNode;
|
||||
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
|
||||
use Symfony\Component\Config\Definition\ScalarNode;
|
||||
|
||||
class ArrayNodeTest extends \PHPUnit_Framework_TestCase
|
||||
|
@ -35,34 +36,30 @@ class ArrayNodeTest extends \PHPUnit_Framework_TestCase
|
|||
$node->normalize(array('foo' => 'bar'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that no exception is thrown for an unrecognized child if the
|
||||
* ignoreExtraKeys option is set to true.
|
||||
*
|
||||
* Related to testExceptionThrownOnUnrecognizedChild
|
||||
*/
|
||||
public function testIgnoreExtraKeysNoException()
|
||||
public function ignoreAndRemoveMatrixProvider()
|
||||
{
|
||||
$node = new ArrayNode('roo');
|
||||
$node->setIgnoreExtraKeys(true);
|
||||
$unrecognizedOptionException = new InvalidConfigurationException('Unrecognized option "foo" under "root"');
|
||||
|
||||
$node->normalize(array('foo' => 'bar'));
|
||||
$this->assertTrue(true, 'No exception was thrown when setIgnoreExtraKeys is true');
|
||||
return array(
|
||||
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
|
||||
* ignoreExtraKeys second option is set to false.
|
||||
*
|
||||
* Related to testExceptionThrownOnUnrecognizedChild
|
||||
* @dataProvider ignoreAndRemoveMatrixProvider
|
||||
*/
|
||||
public function testIgnoreExtraKeysNotRemoved()
|
||||
public function testIgnoreAndRemoveBehaviors($ignore, $remove, $expected, $message = '')
|
||||
{
|
||||
$node = new ArrayNode('roo');
|
||||
$node->setIgnoreExtraKeys(true, false);
|
||||
|
||||
$data = array('foo' => 'bar');
|
||||
$this->assertSame($data, $node->normalize($data));
|
||||
if ($expected instanceof \Exception) {
|
||||
$this->setExpectedException(get_class($expected), $expected->getMessage());
|
||||
}
|
||||
$node = new ArrayNode('root');
|
||||
$node->setIgnoreExtraKeys($ignore, $remove);
|
||||
$result = $node->normalize(array('foo' => 'bar'));
|
||||
$this->assertSame($expected, $result, $message);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Reference in New Issue