2012-12-21 5 views
6

Symfony2では、このクラス設定を使用して、各ノードがConfigurationクラスで定義されていることをテストし、その値が正しく設定されていることを確認できます。Symfony2:設定ノードの値とインデックスをテストする方法

ここ

 
# 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は、バンドル拡張でロードされていない限り、構成を配列として取得する可能性はありません。私はJMSSecurityBundleに基づいて仕事ができるソリューションを発見

テスト

 
# 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"); 
+0

私はそれを考えていないが、へactualy良いアイデアですテスト設定を行い、その方法でテストします。 – Ziumin

+0

@ Ziumin、値がデフォルト値に設定されていることを確認するには、どのような方法をお勧めしますか?デフォルトのサービスIDはconfirgured ... – yvoyer

+0

テストセットを準備し、構成クラス – Ziumin

答えて

9

構成をテストする代わりに、構成の適用範囲を途中まで追加する拡張機能をテストします。そうすれば、私はデフォルト設定が設定されていると主張できます。

たとえば、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; 
    } 
} 
+1

はい、それはそれをテストする方がはるかに良い方法です。 – Ziumin

+0

ご意見ありがとうございます – yvoyer

1

が孤立であなたの設定をテストするには、この操作を行うことができます。

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'] 
      ], 
      // ... 
     ]; 
    } 
} 
+0

この素晴らしい例をありがとう –

関連する問題