|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Matthias\SymfonyConfigTest\Tests\Partial; |
| 4 | + |
| 5 | +use Matthias\SymfonyConfigTest\Partial\PartialNode; |
| 6 | +use Symfony\Component\Config\Definition\ArrayNode; |
| 7 | +use Symfony\Component\Config\Definition\Builder\TreeBuilder; |
| 8 | + |
| 9 | +class PartialNodeTest extends \PHPUnit_Framework_TestCase |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @test |
| 13 | + */ |
| 14 | + public function it_strips_children_that_are_not_in_the_given_path_with_one_name() |
| 15 | + { |
| 16 | + $treeBuilder = new TreeBuilder(); |
| 17 | + $root = $treeBuilder->root('root'); |
| 18 | + $root |
| 19 | + ->children() |
| 20 | + ->arrayNode('node_1') |
| 21 | + ->children() |
| 22 | + ->scalarNode('node_1_scalar_node') |
| 23 | + ->end() |
| 24 | + ->end() |
| 25 | + ->end() |
| 26 | + ->arrayNode('node_2') |
| 27 | + ->children() |
| 28 | + ->scalarNode('node_2_scalar_node'); |
| 29 | + |
| 30 | + $node = $treeBuilder->buildTree(); |
| 31 | + /** @var ArrayNode $node */ |
| 32 | + |
| 33 | + PartialNode::excludeEverythingNotInPath($node, ['node_2']); |
| 34 | + |
| 35 | + $this->nodeOnlyHasChild($node, 'node_2'); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @test |
| 40 | + */ |
| 41 | + public function it_strips_children_that_are_not_in_the_given_path_with_several_names() |
| 42 | + { |
| 43 | + $treeBuilder = new TreeBuilder(); |
| 44 | + $root = $treeBuilder->root('root'); |
| 45 | + $root |
| 46 | + ->children() |
| 47 | + ->arrayNode('node_1') |
| 48 | + ->children() |
| 49 | + ->arrayNode('node_1_a') |
| 50 | + ->children() |
| 51 | + ->scalarNode('scalar_node') |
| 52 | + ->end() |
| 53 | + ->end() |
| 54 | + ->end() |
| 55 | + ->arrayNode('node_1_b') |
| 56 | + ->children() |
| 57 | + ->scalarNode('scalar_node') |
| 58 | + ->end() |
| 59 | + ->end() |
| 60 | + ->end() |
| 61 | + ->end() |
| 62 | + ->end() |
| 63 | + ->arrayNode('node_2') |
| 64 | + ->children() |
| 65 | + ->scalarNode('scalar_node'); |
| 66 | + |
| 67 | + $node = $treeBuilder->buildTree(); |
| 68 | + /** @var ArrayNode $node */ |
| 69 | + |
| 70 | + PartialNode::excludeEverythingNotInPath($node, ['node_1', 'node_1_b']); |
| 71 | + |
| 72 | + $node1 = $this->nodeOnlyHasChild($node, 'node_1'); |
| 73 | + $this->nodeOnlyHasChild($node1, 'node_1_b'); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * @test |
| 78 | + */ |
| 79 | + public function it_fails_when_a_requested_child_node_does_not_exist() |
| 80 | + { |
| 81 | + $treeBuilder = new TreeBuilder(); |
| 82 | + $root = $treeBuilder->root('root'); |
| 83 | + $root |
| 84 | + ->children() |
| 85 | + ->arrayNode('sub_node') |
| 86 | + ->children() |
| 87 | + ->arrayNode('sub_sub_tree'); |
| 88 | + |
| 89 | + $node = $treeBuilder->buildTree(); |
| 90 | + |
| 91 | + $this->setExpectedException( |
| 92 | + 'Matthias\SymfonyConfigTest\Partial\Exception\UndefinedChildNode', |
| 93 | + 'Undefined child node "non_existing_node" (the part of the path that was successful: "root.sub_node")' |
| 94 | + ); |
| 95 | + PartialNode::excludeEverythingNotInPath($node, ['sub_node', 'non_existing_node']); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * @test |
| 100 | + */ |
| 101 | + public function it_fails_when_a_requested_child_node_is_no_array_node_itself() |
| 102 | + { |
| 103 | + $treeBuilder = new TreeBuilder(); |
| 104 | + $root = $treeBuilder->root('root'); |
| 105 | + $root |
| 106 | + ->children() |
| 107 | + ->arrayNode('sub_node') |
| 108 | + ->children() |
| 109 | + ->scalarNode('scalar_node'); |
| 110 | + |
| 111 | + $node = $treeBuilder->buildTree(); |
| 112 | + |
| 113 | + $this->setExpectedException( |
| 114 | + 'Matthias\SymfonyConfigTest\Partial\Exception\ChildIsNotAnArrayNode', |
| 115 | + 'Child node "scalar_node" is not an array node (current path: "root.sub_node")' |
| 116 | + ); |
| 117 | + PartialNode::excludeEverythingNotInPath($node, ['sub_node', 'scalar_node']); |
| 118 | + } |
| 119 | + |
| 120 | + private function nodeOnlyHasChild(ArrayNode $node, $nodeName) |
| 121 | + { |
| 122 | + $children = $node->getChildren(); |
| 123 | + $this->assertCount(1, $children); |
| 124 | + $firstChild = reset($children); |
| 125 | + $this->assertSame($nodeName, $firstChild->getName()); |
| 126 | + |
| 127 | + return $firstChild; |
| 128 | + } |
| 129 | +} |
0 commit comments