Skip to content

Commit 07158c7

Browse files
author
RJ Garcia
committed
Added allowExtraKeys parameter
Signed-off-by: RJ Garcia <[email protected]>
1 parent 650eca5 commit 07158c7

File tree

3 files changed

+20
-9
lines changed

3 files changed

+20
-9
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ Original RFC Pull Request to Symfony: https://github.com/symfony/symfony/issues/
171171

172172
No formal API documentation is setup, but the src dir is under 200loc at this point. Also the tests directory gives a good overview of the various features as well.
173173

174+
- [Symfony Config Feature Tests](./test/feature/SymfonyConfigTest.php)
175+
174176
## Testing
175177

176178
Run `composer test` to run the test suite.

src/ProcessSchema/SymfonyConfig/process-schema.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ function configureNode(NodeParentInterface $configNode, Node $node): void {
3030
throw new \RuntimeException('Unexpected dict node type.');
3131
}
3232

33+
if ($node->attribute('allowExtraKeys')) {
34+
$resNode->ignoreExtraKeys(false);
35+
}
36+
3337
foreach ($node->attribute('nodesByName') ?? [] as $name => $childConfigNode) {
3438
configureNode($resNode->children(), $childConfigNode->withAddedAttributes(['name' => $name]));
3539
}
@@ -59,7 +63,6 @@ function configureNode(NodeParentInterface $configNode, Node $node): void {
5963
}
6064
/** @var ArrayNodeDefinition $resNode */
6165
if ($node->is('dict')) {
62-
$resNode->ignoreExtraKeys();
6366
$resNode->useAttributeAsKey($node->attribute('attribute_key') ?? 'key');
6467
}
6568
configureArrayNode($resNode, $node->attribute('node'));

test/feature/SymfonyConfigTest.php

+14-8
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,13 @@
44

55
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
66
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
7-
use Symfony\Component\Config\Definition\Dumper\YamlReferenceDumper;
87
use function Krak\Schema\ProcessSchema\SymfonyConfig\configTree;
98

109
final class SymfonyConfigTest extends \PHPUnit\Framework\TestCase
1110
{
1211
/** @dataProvider provide_config_trees */
1312
public function test_processing_of_schema_to_config_tree(TreeBuilder $expected, TreeBuilder $actual) {
14-
$dumper = new YamlReferenceDumper();
15-
$this->assertEquals(
16-
$dumper->dumpNode($expected->buildTree()),
17-
$dumper->dumpNode($actual->buildTree())
18-
);
13+
$this->assertEquals($expected->buildTree(), $actual->buildTree());
1914
}
2015

2116
public function provide_config_trees() {
@@ -101,7 +96,7 @@ public function provide_config_trees() {
10196
])),
10297
];
10398

104-
yield 'tree with ignoreExtraKeys' => [
99+
yield 'tree with custom configuration' => [
105100
(new TreeBuilder('root'))->getRootNode()
106101
->ignoreExtraKeys()
107102
->children()
@@ -113,6 +108,18 @@ public function provide_config_trees() {
113108
], [ 'configure' => function($def) { $def->ignoreExtraKeys(); } ]))
114109
];
115110

111+
yield 'tree with allowExtraKeys' => [
112+
(new TreeBuilder('root'))->getRootNode()
113+
->ignoreExtraKeys(false)
114+
->children()
115+
->integerNode('int')->end()
116+
->end()
117+
->end(),
118+
configTree('root', struct([
119+
'int' => int(),
120+
], ['allowExtraKeys' => true]))
121+
];
122+
116123
yield 'tree with configured array nodes' => [
117124
(new TreeBuilder('root'))->getRootNode()
118125
->children()
@@ -135,7 +142,6 @@ public function provide_config_trees() {
135142
(new TreeBuilder('root'))->getRootNode()
136143
->children()
137144
->arrayNode('dict')
138-
->ignoreExtraKeys()
139145
->useAttributeAsKey('key')
140146
->scalarPrototype()->end()
141147
->end()

0 commit comments

Comments
 (0)