Skip to content

Commit 5a79a14

Browse files
bug #33124 [Config] Add handling for ignored keys in ArrayNode::mergeValues. (Alexandre Parent)
This PR was squashed before being merged into the 4.3 branch (closes #33124). Discussion ---------- [Config] Add handling for ignored keys in ArrayNode::mergeValues. | Q | A | ------------- | --- | Branch? | 4.3 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #33101 | License | MIT | Doc PR | N/A Fix case where normalized data accepting and keeping ignored keys is rejected during merge. Commits ------- 311e1c4e9e [Config] Add handling for ignored keys in ArrayNode::mergeValues.
2 parents 55646aa + cfb0226 commit 5a79a14

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)