【发布时间】:2014-09-12 21:10:10
【问题描述】:
如何在 FeatureContext.php 中访问 symfony 参数(例如 parameters.yml)?
如果我在任何控制器中使用$this->getContainer()->getParameter('currency');,我可以获得值并且我想对FeatureContext 做同样的事情,那么我怎样才能使下面的代码成为可能?
提前致谢
我想让这个示例以某种方式工作:
namespace Football\TeamBundle\Features\Context;
use Behat\MinkExtension\Context\MinkContext;
class FeatureContext extends MinkContext
{
/**
* @Then /^I print currency$/
*/
public function iSeeCurrency()
{
echo $this->getContainer()->getParameter('currency');
sleep(60);
}
}
仅供参考;我的这个例子可以很好地访问 symfony 服务:
use Behat\MinkExtension\Context\MinkContext;
use Behat\Symfony2Extension\Context\KernelDictionary;
class FeatureContext extends MinkContext
{
private $kernel;
use KernelDictionary;
public function setKernel($kernel)
{
$this->kernel = $kernel;
}
/**
* @Then /^I want to say hello to "([^"]*)"$/
* @param $seconds
*/
public function iSayHello($name)
{
$container = $this->getContainer();
$behatService = $container->get('behat_service');
echo $behatService->sayHello($name);
sleep(60);
}
}
配置文件:(我也有 _dev.yml 和 _prod.yml 环境)
config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: globals.yml }
config_test.yml
imports:
- { resource: config.yml }
- { resource: parameters_test.yml }
parameters_test.yml
parameters:
database_driver: pdo_mysql
database_host: 127.0.0.1
database_port: null
database_name: symfony_test
database_user: root
database_password: root
globals.yml
parameters:
currency: EUR.USD.GBP
【问题讨论】:
-
您在每个环境中都有相同的参数文件吗? (产品/开发/测试)
-
我已经根据你的问题更新了上面的帖子。