Symfony2:如何测试配置节点的值和索引

前端之家收集整理的这篇文章主要介绍了Symfony2:如何测试配置节点的值和索引前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在Symfony2中,使用此类设置,如何测试Configuration类中定义的每个节点,以及它们的值是否已正确配置.

要测试的课程

# 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;
    }
}

以下是我想要做的断言,在我的单元测试中:

我试图以节点的形式访问节点,但它似乎不起作用.此外,TreeBuilder似乎没有让我们将配置作为数组获取,除非它们由bundle扩展加载.

测试

# 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,$val2Node,"Test the default value of the node");
我发现了一个可以在 JMSSecurityBundle基础上运行的解决方案.

我没有测试配置,而是测试扩展,这将为配置增加覆盖范围.这样,我可以断言已设置默认配置.

例如,这个扩展.

#My\Bundle\DependencyInjection\MyBundleExtension
class MyBundleExtension extends Extension
{
    /**
     * {@inheritDoc}
     */
    public function load(array $configs,ContainerBuilder $container)
    {
        $configuration = new Configuration();
        $config        = $this->processConfiguration($configuration,$configs);

        $container->setParameter("crak_landing_frontend.scalar",$config["scalar"]);
        $container->setParameter("crak_landing_frontend.array_node",$config["array_node"]);

        $loader = new Loader\XmlFileLoader($container,new FileLocator(__DIR__.'/../Resources/config'));
        $loader->load('services.xml');
    }
}

可以测试如下:

#My\Bundle\Tests\DependencyInjection\MyBundleExtensionTest
class MyBundleExtensionTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var MyBundleExtension
     */
    private $extension;

    /**
     * Root name of the configuration
     *
     * @var string
     */
    private $root;

    public function setUp()
    {
        parent::setUp();

        $this->extension = $this->getExtension();
        $this->root      = "my_bundle";
    }

    public function testGetConfigWithDefaultValues()
    {
        $this->extension->load(array(),$container = $this->getContainer());

        $this->assertTrue($container->hasParameter($this->root . ".scalar"));
        $this->assertEquals("defaultValue",$container->getParameter($this->root . ".scalar"));

        $expected = array(
            "val1" => "defaultValue1","val2" => "defaultValue2",);
        $this->assertTrue($container->hasParameter($this->root . ".array_node"));
        $this->assertEquals($expected,$container->getParameter($this->root . ".array_node"));
    }

    public function testGetConfigWithOverrideValues()
    {
        $configs = array(
            "scalar"     => "scalarValue","array_node" => array(
                "val1" => "array_value_1","val2" => "array_value_2",),);

        $this->extension->load(array($configs),$container = $this->getContainer());

        $this->assertTrue($container->hasParameter($this->root . ".scalar"));
        $this->assertEquals("scalarValue",$container->getParameter($this->root . ".scalar"));

        $expected = array(
            "val1" => "array_value_1",$container->getParameter($this->root . ".array_node"));
    }

    /**
     * @return MyBundleExtension
     */
    protected function getExtension()
    {
        return new MyBundleExtension();
    }

    /**
     * @return ContainerBuilder
     */
    private function getContainer()
    {
        $container = new ContainerBuilder();

        return $container;
    }
}

猜你在找的PHP相关文章