8

ConfigurationSymfony2 では、このクラス設定を使用して、各ノードがクラスで定義されていること、およびそれらの値が正しく構成されていることをどのようにテストできますか。

テストするクラス

# 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, "ノードのデフォルト値をテストする");
4

2 に答える 2

11

JMSSecurityBundleに基づいて機能するソリューションを見つけました。

構成をテストする代わりに、構成のカバレッジを追加する拡張機能をテストします。そうすれば、デフォルトの構成が設定されたと断言できます。

たとえば、このExtension.

#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",
            "val2" => "array_value_2",
        );
        $this->assertTrue($container->hasParameter($this->root . ".array_node"));
        $this->assertEquals($expected, $container->getParameter($this->root . ".array_node"));
    }

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

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

        return $container;
    }
}
于 2012-12-21T15:36:53.563 に答える
4

構成を単独でテストするには、次のようにします。

use Foo\YourBundle\DependencyInjection\Configuration;
use PHPUnit\Framework\TestCase;

class ConfigurationTest extends TestCase
{
    /**
     * @dataProvider dataTestConfiguration
     *
     * @param mixed $inputConfig
     * @param mixed $expectedConfig
     */
    public function testConfiguration($inputConfig, $expectedConfig)
    {
        $configuration = new Configuration();

        $node = $configuration->getConfigTreeBuilder()
            ->buildTree();
        $normalizedConfig = $node->normalize($inputConfig);
        $finalizedConfig = $node->finalize($normalizedConfig);

        $this->assertEquals($expectedConfig, $finalizedConfig);
    }

    public function dataTestConfiguration()
    {
        return [
            'test configuration'   => [
                ['input'],
                ['expected_config']
            ],
            // ...
        ];
    }
}
于 2017-07-22T15:56:44.333 に答える