[DependencyInjection] append new elements for prototype nodes without a key attribute

This commit is contained in:
Johannes M. Schmitt 2011-02-06 11:47:06 +01:00 committed by Fabien Potencier
parent 0b8fef2347
commit c7ef8d98d6
2 changed files with 33 additions and 5 deletions

View File

@ -318,6 +318,12 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
}
foreach ($rightSide as $k => $v) {
// prototype, and key is irrelevant, so simply append the element
if (null !== $this->prototype && null === $this->keyAttribute) {
$leftSide[] = $v;
continue;
}
// no conflict
if (!array_key_exists($k, $leftSide)) {
if (!$this->allowNewKeys) {

View File

@ -15,9 +15,7 @@ class MergeTest extends \PHPUnit_Framework_TestCase
$tree = $tb
->root('root', 'array')
->node('foo', 'scalar')
->merge()
->denyOverwrite()
->end()
->cannotBeOverwritten()
->end()
->end()
->buildTree()
@ -42,12 +40,12 @@ class MergeTest extends \PHPUnit_Framework_TestCase
->node('foo', 'scalar')->end()
->node('bar', 'scalar')->end()
->node('unsettable', 'array')
->merge()->allowUnset()->end()
->canBeUnset()
->node('foo', 'scalar')->end()
->node('bar', 'scalar')->end()
->end()
->node('unsetted', 'array')
->merge()->allowUnset()->end()
->canBeUnset()
->prototype('scalar')->end()
->end()
->end()
@ -145,4 +143,28 @@ class MergeTest extends \PHPUnit_Framework_TestCase
)
), $tree->merge($a, $b));
}
public function testPrototypeWithoutAKeyAttribute()
{
$tb = new TreeBuilder();
$tree = $tb
->root('config', 'array')
->node('append_elements', 'array')
->prototype('scalar')->end()
->end()
->end()
->buildTree()
;
$a = array(
'append_elements' => array('a', 'b'),
);
$b = array(
'append_elements' => array('c', 'd'),
);
$this->assertEquals(array('append_elements' => array('a', 'b', 'c', 'd')), $tree->merge($a, $b));
}
}