In Symfony2, using this class setup, how can I test that each nodes is defined in the Configuration
class, and that their values are correctly configured.
The class to test
# My\Bundle\DependencyInjection\Configuration.php class Configuration implements ConfigurationInterface { /** * {@inheritDoc} */ public function getConfigTreeBuilder() { $treeBuilder = new TreeBuilder(); $treeBuilder->root('my_bundle') ->children() ->scalarNode("scalar")->defaultValue("defaultValue")->end() ->arrayNode("arrayNode") ->children() ->scalarNode("val1")->defaultValue("defaultValue1")->end() ->scalarNode("val2")->defaultValue("defaultValue2")->end() ->end() ->end() ->end() ; return $treeBuilder; } }
Here are the assertions I would like to do, in my unit test:
I tried to access the nodes as an array, but it do not seems to work. Also the TreeBuilder
do not seems to give us the possibility of getting the configurations as a array, unless they are loaded by the bundle extension.
Tests
# My\Bundle\Tests\DependencyInjection\ConfigurationTest.php $configuration = $this->getConfiguration(); $treeBuilder = $configuration->getConfigTreeBuilder(); $this->assertInstanceOf("Symfony\Component\Config\Definition\Builder\TreeBuilder", $treeBuilder); // How to access the treebuilder's nodes ? $rootNode = $treeBuilder["my_bundle"]; $scalarNode = $treeBuilder["scalar"]; $arrayNode = $treeBuilder["arrayNode"]; $val1Node = $arrayNode["val1"]; $val2Node = $arrayNode["val2"]; $this->assertInstanceOf("Symfony\...\ArrayNodeDefinition", $rootNode); $this->assertEquals("defaultValue", $scalarNode, "Test the default value of the node"); $this->assertEquals("defaultValue", $val1Node, "Test the default value of the node"); $this->assertEquals("defaultValue", $val2Node, "Test the default value of the node");
Configuration
class. - yvoyer