From fd5cdfc18f118d8331583f053004e46290631109 Mon Sep 17 00:00:00 2001 From: Ryan Weaver Date: Thu, 17 Feb 2011 07:52:57 -0600 Subject: [PATCH] [DependencyInjection] Being sure to remove XML-remapped singular options and key attribute options after processing. This prevents these keys from being validated as extra fields. --- .../Component/Config/Definition/ArrayNode.php | 2 + .../Config/Definition/ArrayNodeTest.php | 51 ++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Config/Definition/ArrayNode.php b/src/Symfony/Component/Config/Definition/ArrayNode.php index 424c02e1270b..a1b89317f725 100644 --- a/src/Symfony/Component/Config/Definition/ArrayNode.php +++ b/src/Symfony/Component/Config/Definition/ArrayNode.php @@ -358,6 +358,7 @@ protected function normalizeValue($value) } $value[$plural] = Extension::normalizeConfig($value, $singular, $plural); + unset($value[$singular]); } if (null !== $this->prototype) { @@ -372,6 +373,7 @@ protected function normalizeValue($value) )); } else if (isset($v[$this->keyAttribute])) { $k = $v[$this->keyAttribute]; + unset($v[$this->keyAttribute]); } if (array_key_exists($k, $normalized)) { diff --git a/tests/Symfony/Tests/Component/Config/Definition/ArrayNodeTest.php b/tests/Symfony/Tests/Component/Config/Definition/ArrayNodeTest.php index 25bdb6720d59..1cd3919fb2a9 100644 --- a/tests/Symfony/Tests/Component/Config/Definition/ArrayNodeTest.php +++ b/tests/Symfony/Tests/Component/Config/Definition/ArrayNodeTest.php @@ -3,6 +3,7 @@ namespace Symfony\Tests\Component\Config\Definition; use Symfony\Component\Config\Definition\ArrayNode; +use Symfony\Component\Config\Definition\ScalarNode; class ArrayNodeTest extends \PHPUnit_Framework_TestCase { @@ -78,4 +79,52 @@ public function testNormalizeKeepsExtraArrayValues() $normalized = $node->normalize(array('foo' => 'bar')); $this->assertEquals(array('foo' => 'bar'), $normalized); } -} \ No newline at end of file + + // a remapped key (e.g. "mapping" -> "mappings") should be unset after being used + public function testRemappedKeysAreUnset() + { + $node = new ArrayNode('root'); + + $remappings = array(); + $remappings[] = array('mapping', 'mappings'); + $node->setXmlRemappings($remappings); + + $normalized = $node->normalize(array('mapping' => array('foo', 'bar'))); + $this->assertEquals(array('mappings' => array('foo', 'bar')), $normalized); + } + + /** + * Tests that when a key attribute is mapped, that key is removed from the array: + * + * + * + * + * The above should finally be mapped to an array that looks like this + * (because "id" is the key attribute). + * + * array( + * 'things' => array( + * 'option1' => 'foo', + * 'option2' => 'bar', + * ) + * ) + */ + public function testMappedAttributeKeyIsRemoved() + { + $node = new ArrayNode('root'); + $node->setKeyAttribute('id'); + + $prototype = new ArrayNode(null); + $node->setPrototype($prototype); + + $children = array(); + $children[] = array('id' => 'item_name', 'foo' => 'bar'); + $normalized = $node->normalize($children); + + $expected = array(); + $expected['item_name'] = array('foo' => 'bar'); + $this->assertEquals($expected, $normalized); + } +}