访问FeatureContext中的symfony参数

时间:2014-09-12 21:10:11

标签: symfony behat mink

如何在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

1 个答案:

答案 0 :(得分:5)

功能背景

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 access only currency parameter$/
     */
    public function printCurrency()
    {
        $container = $this->getContainer();
        echo $container->getParameter('currency');
    }
}