Skip to content

Commit cfb0226

Browse files
alex-devnicolas-grekas
authored andcommitted
[Config] Add handling for ignored keys in ArrayNode::mergeValues.
1 parent e5d7a4d commit cfb0226

File tree

2 files changed

+68
-1
lines changed

2 files changed

+68
-1
lines changed

Definition/ArrayNode.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,12 @@ protected function mergeValues($leftSide, $rightSide)
396396
}
397397

398398
if (!isset($this->children[$k])) {
399-
throw new \RuntimeException('merge() expects a normalized config array.');
399+
if (!$this->ignoreExtraKeys || $this->removeExtraKeys) {
400+
throw new \RuntimeException('merge() expects a normalized config array.');
401+
}
402+
403+
$leftSide[$k] = $v;
404+
continue;
400405
}
401406

402407
$leftSide[$k] = $this->children[$k]->merge($leftSide[$k], $v);

Tests/Definition/ArrayNodeTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,4 +255,66 @@ public function testSetDeprecated()
255255
restore_error_handler();
256256
$this->assertTrue($deprecationTriggered, '->finalize() should trigger if the deprecated node is set');
257257
}
258+
259+
/**
260+
* @dataProvider getDataWithIncludedExtraKeys
261+
*/
262+
public function testMergeWithoutIgnoringExtraKeys($prenormalizeds, $merged)
263+
{
264+
$this->expectException('RuntimeException');
265+
$this->expectExceptionMessage('merge() expects a normalized config array.');
266+
$node = new ArrayNode('root');
267+
$node->addChild(new ScalarNode('foo'));
268+
$node->addChild(new ScalarNode('bar'));
269+
$node->setIgnoreExtraKeys(false);
270+
271+
$r = new \ReflectionMethod($node, 'mergeValues');
272+
$r->setAccessible(true);
273+
274+
$r->invoke($node, ...$prenormalizeds);
275+
}
276+
277+
/**
278+
* @dataProvider getDataWithIncludedExtraKeys
279+
*/
280+
public function testMergeWithIgnoringAndRemovingExtraKeys($prenormalizeds, $merged)
281+
{
282+
$this->expectException('RuntimeException');
283+
$this->expectExceptionMessage('merge() expects a normalized config array.');
284+
$node = new ArrayNode('root');
285+
$node->addChild(new ScalarNode('foo'));
286+
$node->addChild(new ScalarNode('bar'));
287+
$node->setIgnoreExtraKeys(true);
288+
289+
$r = new \ReflectionMethod($node, 'mergeValues');
290+
$r->setAccessible(true);
291+
292+
$r->invoke($node, ...$prenormalizeds);
293+
}
294+
295+
/**
296+
* @dataProvider getDataWithIncludedExtraKeys
297+
*/
298+
public function testMergeWithIgnoringExtraKeys($prenormalizeds, $merged)
299+
{
300+
$node = new ArrayNode('root');
301+
$node->addChild(new ScalarNode('foo'));
302+
$node->addChild(new ScalarNode('bar'));
303+
$node->setIgnoreExtraKeys(true, false);
304+
305+
$r = new \ReflectionMethod($node, 'mergeValues');
306+
$r->setAccessible(true);
307+
308+
$this->assertEquals($merged, $r->invoke($node, ...$prenormalizeds));
309+
}
310+
311+
public function getDataWithIncludedExtraKeys()
312+
{
313+
return [
314+
[
315+
[['foo' => 'bar', 'baz' => 'not foo'], ['bar' => 'baz', 'baz' => 'foo']],
316+
['foo' => 'bar', 'bar' => 'baz', 'baz' => 'foo'],
317+
],
318+
];
319+
}
258320
}

0 commit comments

Comments
 (0)