在 Symfony2 中,使用此类设置,我如何测试每个节点是否在Configuration
类中定义,以及它们的值是否正确配置。
要测试的类
# My\Bundle\DependencyInjection\Configuration.php 类 Configuration 实现 ConfigurationInterface { /** * {@inheritDoc} */ 公共函数 getConfigTreeBuilder() { $treeBuilder = 新 TreeBuilder(); $treeBuilder->root('my_bundle') ->儿童() ->scalarNode("scalar")->defaultValue("defaultValue")->end() ->arrayNode("arrayNode") ->儿童() ->scalarNode("val1")->defaultValue("defaultValue1")->end() ->scalarNode("val2")->defaultValue("defaultValue2")->end() ->结束() ->结束() ->结束() ; 返回$treeBuilder; } }
以下是我想做的断言,在我的单元测试中:
我试图以数组的形式访问节点,但它似乎不起作用。此外TreeBuilder
,除非它们由捆绑扩展加载,否则似乎不给我们以数组形式获取配置的可能性。
测试
# My\Bundle\Tests\DependencyInjection\ConfigurationTest.php $configuration = $this->getConfiguration(); $treeBuilder = $configuration->getConfigTreeBuilder(); $this->assertInstanceOf("Symfony\Component\Config\Definition\Builder\TreeBuilder", $treeBuilder); // 如何访问树生成器的节点? $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, "测试节点的默认值"); $this->assertEquals("defaultValue", $val1Node, "测试节点的默认值"); $this->assertEquals("defaultValue", $val2Node, "测试节点的默认值");