如何在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环境)

配置文件

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

最佳答案

功能背景

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');
    }
}

关于symfony - 在FeatureContext中访问symfony参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25816887/

10-14 01:32