Skip to content

Commit f4c8cf2

Browse files
committed
Allow to partially test below prototypes nodes
1 parent 432bf99 commit f4c8cf2

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

Partial/PartialNode.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@
55
use Matthias\SymfonyConfigTest\Partial\Exception\ChildIsNotAnArrayNode;
66
use Matthias\SymfonyConfigTest\Partial\Exception\UndefinedChildNode;
77
use Symfony\Component\Config\Definition\ArrayNode;
8+
use Symfony\Component\Config\Definition\NodeInterface;
9+
use Symfony\Component\Config\Definition\PrototypedArrayNode;
810

911
class PartialNode
1012
{
13+
/**
14+
* @var \ReflectionProperty
15+
*/
1116
private static $nodeChildrenProperty;
1217

18+
/**
19+
* @var \ReflectionProperty
20+
*/
21+
private static $nodePrototypeProperty;
22+
1323
/**
1424
* Provide an ArrayNode instance (e.g. the root node created by a TreeBuilder) and a path that is relevant to you,
1525
* e.g. "dbal.connections": this will strip every node that is not contained in the given path (e.g. the "orm" node
@@ -60,8 +70,18 @@ public static function excludeEverythingNotInPath(ArrayNode $node, array $path =
6070
self::excludeEverythingNotInPath($nextNode, $path);
6171
}
6272

73+
/**
74+
* @param ArrayNode $node
75+
* @param string $childNodeName
76+
*
77+
* @return NodeInterface
78+
*/
6379
private static function childNode(ArrayNode $node, $childNodeName)
6480
{
81+
if ($node instanceof PrototypedArrayNode && '*' === $childNodeName) {
82+
return self::nodePrototypeProperty()->getValue($node);
83+
}
84+
6585
$children = self::nodeChildrenProperty()->getValue($node);
6686

6787
if (!isset($children[$childNodeName])) {
@@ -74,6 +94,9 @@ private static function childNode(ArrayNode $node, $childNodeName)
7494
return $children[$childNodeName];
7595
}
7696

97+
/**
98+
* @return \ReflectionProperty
99+
*/
77100
private static function nodeChildrenProperty()
78101
{
79102
if (!isset(self::$nodeChildrenProperty)) {
@@ -86,4 +109,20 @@ private static function nodeChildrenProperty()
86109

87110
return self::$nodeChildrenProperty;
88111
}
112+
113+
/**
114+
* @return \ReflectionProperty
115+
*/
116+
private static function nodePrototypeProperty()
117+
{
118+
if (!isset(self::$nodePrototypeProperty)) {
119+
self::$nodePrototypeProperty = new \ReflectionProperty(
120+
'Symfony\Component\Config\Definition\PrototypedArrayNode',
121+
'prototype'
122+
);
123+
self::$nodePrototypeProperty->setAccessible(true);
124+
}
125+
126+
return self::$nodePrototypeProperty;
127+
}
89128
}

Tests/Partial/PartialNodeTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44

55
use Matthias\SymfonyConfigTest\Partial\PartialNode;
66
use Symfony\Component\Config\Definition\ArrayNode;
7+
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
78
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
9+
use Symfony\Component\Config\Definition\PrototypedArrayNode;
810

911
class PartialNodeTest extends \PHPUnit_Framework_TestCase
1012
{
@@ -100,6 +102,38 @@ public function it_strips_children_when_leaf_node_is_not_an_array()
100102
$this->nodeOnlyHasChild($node, 'node_3');
101103
}
102104

105+
/**
106+
* @test
107+
*/
108+
public function it_does_not_crash_on_prototypes()
109+
{
110+
$treeBuilder = new TreeBuilder();
111+
/** @var ArrayNodeDefinition $root */
112+
$root = $treeBuilder->root('root');
113+
$root
114+
->prototype('array')
115+
->children()
116+
->arrayNode('node_1')
117+
->children()
118+
->scalarNode('node_1_scalar_node')->end()
119+
->end()
120+
->end()
121+
->arrayNode('node_2')
122+
->children()
123+
->scalarNode('node_2_scalar_node')
124+
;
125+
126+
/** @var PrototypedArrayNode $node */
127+
$node = $treeBuilder->buildTree();
128+
129+
/** @var ArrayNode $prototypeNode */
130+
$prototypeNode = $node->getPrototype();
131+
132+
PartialNode::excludeEverythingNotInPath($node, array('*', 'node_1'));
133+
134+
$this->nodeOnlyHasChild($prototypeNode, 'node_1');
135+
}
136+
103137
/**
104138
* @test
105139
*/

0 commit comments

Comments
 (0)