Skip to content

Commit 235e243

Browse files
Merge branch 'emodric-issue_29_fix'
2 parents 502b6b7 + 05f2fa6 commit 235e243

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

Partial/PartialNode.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,8 @@ public static function excludeEverythingNotInPath(ArrayNode $node, array $path =
3939
}
4040

4141
$nextNodeName = array_shift($path);
42-
4342
$nextNode = self::childNode($node, $nextNodeName);
4443

45-
if (!($nextNode instanceof ArrayNode)) {
46-
throw new ChildIsNotAnArrayNode($node, $nextNodeName);
47-
}
48-
4944
$children = self::nodeChildrenProperty()->getValue($node);
5045
foreach ($children as $name => $child) {
5146
if ($name !== $nextNodeName) {
@@ -54,6 +49,14 @@ public static function excludeEverythingNotInPath(ArrayNode $node, array $path =
5449
}
5550
self::nodeChildrenProperty()->setValue($node, $children);
5651

52+
if (!($nextNode instanceof ArrayNode)) {
53+
if (!empty($path)) {
54+
throw new ChildIsNotAnArrayNode($node, $nextNodeName);
55+
}
56+
57+
return;
58+
}
59+
5760
self::excludeEverythingNotInPath($nextNode, $path);
5861
}
5962

Tests/Partial/PartialNodeTest.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,33 @@ public function it_strips_children_that_are_not_in_the_given_path_with_several_n
7373
$this->nodeOnlyHasChild($node1, 'node_1_b');
7474
}
7575

76+
/**
77+
* @test
78+
*/
79+
public function it_strips_children_when_leaf_node_is_not_an_array()
80+
{
81+
$treeBuilder = new TreeBuilder();
82+
$root = $treeBuilder->root('root');
83+
$root
84+
->children()
85+
->arrayNode('node_1')
86+
->children()
87+
->scalarNode('node_1_scalar_node')
88+
->end()
89+
->end()
90+
->end()
91+
->scalarNode('node_2')
92+
->end()
93+
->scalarNode('node_3');
94+
95+
$node = $treeBuilder->buildTree();
96+
/** @var ArrayNode $node */
97+
98+
PartialNode::excludeEverythingNotInPath($node, array('node_3'));
99+
100+
$this->nodeOnlyHasChild($node, 'node_3');
101+
}
102+
76103
/**
77104
* @test
78105
*/
@@ -98,7 +125,7 @@ public function it_fails_when_a_requested_child_node_does_not_exist()
98125
/**
99126
* @test
100127
*/
101-
public function it_fails_when_a_requested_child_node_is_no_array_node_itself()
128+
public function it_fails_when_a_requested_child_node_is_no_array_node_itself_and_path_not_empty()
102129
{
103130
$treeBuilder = new TreeBuilder();
104131
$root = $treeBuilder->root('root');
@@ -114,7 +141,7 @@ public function it_fails_when_a_requested_child_node_is_no_array_node_itself()
114141
'Matthias\SymfonyConfigTest\Partial\Exception\ChildIsNotAnArrayNode',
115142
'Child node "scalar_node" is not an array node (current path: "root.sub_node")'
116143
);
117-
PartialNode::excludeEverythingNotInPath($node, array('sub_node', 'scalar_node'));
144+
PartialNode::excludeEverythingNotInPath($node, array('sub_node', 'scalar_node', 'extra_node'));
118145
}
119146

120147
private function nodeOnlyHasChild(ArrayNode $node, $nodeName)

0 commit comments

Comments
 (0)