gpt4 book ai didi

Symfony2 : How to test Configuration nodes values and index

转载 作者:行者123 更新时间:2023-12-04 05:54:12 25 4
gpt4 key购买 nike

在 Symfony2 中,使用此类设置,我如何测试每个节点是否在 Configuration 中定义?类,并且它们的值已正确配置。

测试类

# My\Bundle\DependencyInjection\Configuration.php

类 Configuration 实现 ConfigurationInterface
{
/**
* {@inheritDoc}
*/
公共(public)函数 getConfigTreeBuilder()
{
$treeBuilder = 新 TreeBuilder();
$treeBuilder->root('my_bundle')
-> child ()
->scalarNode("scalar")->defaultValue("defaultValue")->end()
->arrayNode("arrayNode")
-> child ()
->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, "测试节点的默认值");

最佳答案

我发现了一个可以基于 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;
}
}

关于Symfony2 : How to test Configuration nodes values and index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13991098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com