2012-12-21 6 views
6

In Symfony2, usando questa impostazione di classe, come posso verificare che ciascun nodo sia definito nella classe Configuration e che i loro valori siano configurati correttamente.Symfony2: Come testare i valori dei nodi di configurazione e l'indice

La classe per testare

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

Qui ci sono le affermazioni che vorrei fare, nel mio test di unità:

ho provato ad accedere ai nodi come una matrice, ma non lo fanno sembra funzionare. Anche lo TreeBuilder non sembra darci la possibilità di ottenere le configurazioni come array, a meno che non vengano caricati dall'estensione del bundle.

Test

 
# 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

Io non credo che sia actualy buona idea test di configurazione e anche testarlo in questo modo. – Ziumin

+0

@Ziumin, quale metodo suggeriresti per garantire che un valore abbia un valore predefinito impostato, o un ID di servizio predefinito dovrebbe essere configurato ... – yvoyer

+0

Ti suggerisco di preparare un set di test di congigurazioni e controllare se sono stati elaborati correttamente con il tuo Classe di configurazione – Ziumin

risposta

9

ho scoperto una soluzione che potrebbe funzionare sulla base di JMSSecurityBundle.

Invece di testare la configurazione, provo l'estensione che aggiungerà copertura per la configurazione. In questo modo, posso affermare che è stata impostata una configurazione predefinita.

Ad esempio, questo 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'); 
    } 
} 

Could be test come:

#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

Sì, questo è il modo migliore per testarlo. – Ziumin

+0

Grazie per il tuo feedback – yvoyer

1

per verificare la configurazione in isolamento, si può fare questo:

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

Grazie per questo grande esempio –

Problemi correlati