8000 [Config] Fix ArrayNode extra keys "ignore" and "remove" behaviors by ogizanagi · Pull Request #15593 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 30, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/Symfony/Component/Config/Definition/ArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,8 @@ protected function normalizeValue($value)
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]);
}
}

Expand Down
39 changes: 18 additions & 21 deletions src/Symfony/Component/Config/Tests/Definition/ArrayNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,34 +36,30 @@ public function testExceptionThrownOnUnrecognizedChild()
$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);
}

/**
Expand Down
0