Skip to content

Commit

Permalink
[Config] Add handling for ignored keys in ArrayNode::mergeValues.
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-dev authored and nicolas-grekas committed Aug 20, 2019
1 parent cca22c4 commit 311e1c4
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Symfony/Component/Config/Definition/ArrayNode.php
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
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'],
],
];
}
}

0 comments on commit 311e1c4

Please sign in to comment.