Skip to content
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
7 changes: 6 additions & 1 deletion src/Symfony/Component/Config/Definition/ArrayNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,12 @@ protected function mergeValues($leftSide, $rightSide)
}

if (!isset($this->children[$k])) {
throw new \RuntimeException('merge() expects a normalized config array.');
if (!$this->ignoreExtraKeys || $this->removeExtraKeys) {
throw new \RuntimeException('merge() expects a normalized config array.');
}

$leftSide[$k] = $v;
continue;
}

$leftSide[$k] = $this->children[$k]->merge($leftSide[$k], $v);
Expand Down
62 changes: 62 additions & 0 deletions src/Symfony/Component/Config/Tests/Definition/ArrayNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,4 +255,66 @@ public function testSetDeprecated()
restore_error_handler();
$this->assertTrue($deprecationTriggered, '->finalize() should trigger if the deprecated node is set');
}

/**
* @dataProvider getDataWithIncludedExtraKeys
*/
public function testMergeWithoutIgnoringExtraKeys($prenormalizeds, $merged)
{
$this->expectException('RuntimeException');
$this->expectExceptionMessage('merge() expects a normalized config array.');
$node = new ArrayNode('root');
$node->addChild(new ScalarNode('foo'));
$node->addChild(new ScalarNode('bar'));
$node->setIgnoreExtraKeys(false);

$r = new \ReflectionMethod($node, 'mergeValues');
$r->setAccessible(true);

$r->invoke($node, ...$prenormalizeds);
}

/**
* @dataProvider getDataWithIncludedExtraKeys
*/
public function testMergeWithIgnoringAndRemovingExtraKeys($prenormalizeds, $merged)
{
$this->expectException('RuntimeException');
$this->expectExceptionMessage('merge() expects a normalized config array.');
$node = new ArrayNode('root');
$node->addChild(new ScalarNode('foo'));
$node->addChild(new ScalarNode('bar'));
$node->setIgnoreExtraKeys(true);

$r = new \ReflectionMethod($node, 'mergeValues');
$r->setAccessible(true);

$r->invoke($node, ...$prenormalizeds);
}

/**
* @dataProvider getDataWithIncludedExtraKeys
*/
public function testMergeWithIgnoringExtraKeys($prenormalizeds, $merged)
{
$node = new ArrayNode('root');
$node->addChild(new ScalarNode('foo'));
$node->addChild(new ScalarNode('bar'));
$node->setIgnoreExtraKeys(true, false);

$r = new \ReflectionMethod($node, 'mergeValues');
$r->setAccessible(true);

$this->assertEquals($merged, $r->invoke($node, ...$prenormalizeds));
}

public function getDataWithIncludedExtraKeys()
{
return [
[
[['foo' => 'bar', 'baz' => 'not foo'], ['bar' => 'baz', 'baz' => 'foo']],
['foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo'],
],
];
}
}