[Config] Add handling for ignored keys in ArrayNode::mergeValues.
This commit is contained in:
parent
cca22c4a20
commit
311e1c4e9e
@ -396,9 +396,14 @@ class ArrayNode extends BaseNode implements PrototypeNodeInterface
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!isset($this->children[$k])) {
|
if (!isset($this->children[$k])) {
|
||||||
|
if (!$this->ignoreExtraKeys || $this->removeExtraKeys) {
|
||||||
throw new \RuntimeException('merge() expects a normalized config array.');
|
throw new \RuntimeException('merge() expects a normalized config array.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$leftSide[$k] = $v;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$leftSide[$k] = $this->children[$k]->merge($leftSide[$k], $v);
|
$leftSide[$k] = $this->children[$k]->merge($leftSide[$k], $v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -255,4 +255,66 @@ class ArrayNodeTest extends TestCase
|
|||||||
restore_error_handler();
|
restore_error_handler();
|
||||||
$this->assertTrue($deprecationTriggered, '->finalize() should trigger if the deprecated node is set');
|
$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'],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user