【问题标题】:Access symfony parameters within the FeatureContext在 FeatureContext 中访问 symfony 参数
【发布时间】: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

【问题讨论】:

  • 您在每个环境中都有相同的参数文件吗? (产品/开发/测试)
  • 我已经根据你的问题更新了上面的帖子。

标签: symfony behat mink


【解决方案1】:

专题背景

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

【讨论】:

  • 不错的把戏。在 Behat 3.x 上尝试成功。刚刚出现[Symfony\Component\Debug\Exception\ContextErrorException] Runtime Notice: AppBundle\Features\Context\FeatureContext and Behat\Symfony2Extension\Context\KernelDictionary define the same property ($kernel) in the composition of AppBundl e\Features\Context\FeatureContext. This might be incompatible, to improve maintainability consider using accessor methods in traits instead. Class was composed 错误。所以我刚刚从你的 sn-p 中删除了private $kernel 来解决这个错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-23
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
相关资源
最近更新 更多