Skip to content

Commit 8dbcadd

Browse files
committed
[Config] Fix ArrayNode extra keys "ignore" and "remove" behaviors
1 parent 41b889c commit 8dbcadd

File tree

2 files changed

+19
-23
lines changed

2 files changed

+19
-23
lines changed

Definition/ArrayNode.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,8 @@ protected function normalizeValue($value)
303303
if (isset($this->children[$name])) {
304304
$normalized[$name] = $this->children[$name]->normalize($val);
305305
unset($value[$name]);
306-
} elseif (false === $this->removeExtraKeys) {
306+
} elseif (!$this->removeExtraKeys) {
307307
$normalized[$name] = $val;
308-
unset($value[$name]);
309308
}
310309
}
311310

Tests/Definition/ArrayNodeTest.php

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Config\Tests\Definition;
1313

1414
use Symfony\Component\Config\Definition\ArrayNode;
15+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
1516
use Symfony\Component\Config\Definition\ScalarNode;
1617

1718
class ArrayNodeTest extends \PHPUnit_Framework_TestCase
@@ -35,34 +36,30 @@ public function testExceptionThrownOnUnrecognizedChild()
3536
$node->normalize(array('foo' => 'bar'));
3637
}
3738

38-
/**
39-
* Tests that no exception is thrown for an unrecognized child if the
40-
* ignoreExtraKeys option is set to true.
41-
*
42-
* Related to testExceptionThrownOnUnrecognizedChild
43-
*/
44-
public function testIgnoreExtraKeysNoException()
39+
public function ignoreAndRemoveMatrixProvider()
4540
{
46-
$node = new ArrayNode('roo');
47-
$node->setIgnoreExtraKeys(true);
41+
$unrecognizedOptionException = new InvalidConfigurationException('Unrecognized option "foo" under "root"');
4842

49-
$node->normalize(array('foo' => 'bar'));
50-
$this->assertTrue(true, 'No exception was thrown when setIgnoreExtraKeys is true');
43+
return array(
44+
array(true, true, array(), 'no exception is thrown for an unrecognized child if the ignoreExtraKeys option is set to true'),
45+
array(true, false, array('foo' => 'bar'), 'extra keys are not removed when ignoreExtraKeys second option is set to false'),
46+
array(false, true, $unrecognizedOptionException),
47+
array(false, false, $unrecognizedOptionException),
48+
);
5149
}
5250

5351
/**
54-
* Tests that extra keys are not removed when
55-
* ignoreExtraKeys second option is set to false.
56-
*
57-
* Related to testExceptionThrownOnUnrecognizedChild
52+
* @dataProvider ignoreAndRemoveMatrixProvider
5853
*/
59-
public function testIgnoreExtraKeysNotRemoved()
54+
public function testIgnoreAndRemoveBehaviors($ignore, $remove, $expected, $message = '')
6055
{
61-
$node = new ArrayNode('roo');
62-
$node->setIgnoreExtraKeys(true, false);
63-
64-
$data = array('foo' => 'bar');
65-
$this->assertSame($data, $node->normalize($data));
56+
if ($expected instanceof \Exception) {
57+
$this->setExpectedException(get_class($expected), $expected->getMessage());
58+
}
59+
$node = new ArrayNode('root');
60+
$node->setIgnoreExtraKeys($ignore, $remove);
61+
$result = $node->normalize(array('foo' => 'bar'));
62+
$this->assertSame($expected, $result, $message);
6663
}
6764

6865
/**

0 commit comments

Comments
 (0)