Behat 3-如何在上下文中检索自定义扩展 [英] Behat 3 - how to retrieve custom extension in context

查看:62
本文介绍了Behat 3-如何在上下文中检索自定义扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要添加,以后再从上下文中从behat.yml文件中检索其他选项/配置.

I need to add and later from context retrieve extra options/configuration from behat.yml file.

由于Behat不允许我向behat.yml文件中添加一些随机参数,因此我创建了新的自定义扩展名.此扩展使我可以传递特定的配置值

Behat is not allowing me to add some random parameters into behat.yml file, so I created new custom extension. This extension is allowing me to pass specific config values

extensions:
    App\Behat\DevToolsExtension:
        api_url: "https://api.example.com"

因此,behat现在不再抱怨behat.yml文件中的新配置.

So now behat is not complaining about new configuration in behat.yml file.

现在,我被卡住了.如何在运行时从扩展中检索此配置?

Now I'm stuck. How can I retrieve this configuration from my extension in runtime?

我在公共功能load(ContainerBuilder $ container,array $ config)方法中的扩展程序中设置现有参数,如下所示:

I'm setting existing parameters in my extension within public function load(ContainerBuilder $container, array $config) method like so:

$container->setParameter($configKey . $key, $config[$key]);

还是可以在上下文中检索此ContainerBuilder对象或DevToolsExtension对象吗?

Again, is there a way to retrieve this ContainerBuilder object or DevToolsExtension object in Context?

解决方案是创建服务容器,然后像这样将behats ContainerBuilder传递给它:

Solution was to create service container and pass behats ContainerBuilder into it like so:

class AppExtension implements ExtensionInterface
{
    // ...
    public function load(ContainerBuilder $container, array $config)
    {
        $configKey = $this->getConfigKey() . '.';

        foreach ($this->keys as $key) {
            $keyValue = $configKey . $key;
            $container->setParameter($keyValue, $config[$key]);
        }

        $this->getServiceLocator()->setBehatContainer($container);
    }

然后使用服务定位器检索配置参数

And then use service locator to retrieve config parameters

$value = $this->getBehatContainer()->getParameter($key);

推荐答案

这是一个很好的问题.基本上下文没有返回顶部的引用,因此您无法真正从那里找出要加载的扩展.扩展被初始化并挂接到内部事件系统中.初始化上下文后,它们会得到通知,并可以将必要的参数传递给它们.知道了这一点,您可以创建一个适当的扩展程序,该扩展程序可以挂接到Behat事件系统中,并将配置传递到您的上下文中-这就是这样做的方法.

That's a good question. Basic contexts have no references back to the top, so you can't really find out from there what extensions are loaded. Extensions are initialised and hooked into the internal event system. When contexts get initialised they get notified and can pass the necessary parameters down to them. Knowing that, you can create a proper extension that hooks into Behat event system and passes the configuration into your context – that would be the way to do this.

Mink扩展就是一个很好的例子,您可能找不到任何文档,但是您可以在其中一个(Mink)上下文中创建一个构造函数,在其中添加一个断点,然后按照跟踪来了解如何整个过程都有效.

Mink extension is a good example of that, you probably won't find any documentation, but you can create a constructor in one of your (Mink) contexts, add a break point there and follow the trace to find out how the whole thing works.

但是……在大多数情况下,这是过大的了.使用外部配置文件(可以从上下文中加载)或通过将参数传递到上下文中(可以像现在对扩展名进行的操作一样)可以(并且应该)实现相同的目的.如果您有许多需要该配置的上下文,则最后一个选项可能无法很好地工作.

But… In most cases that would be an overkill. The same can be (and should be) achieved using either an external configuration file which you can load from your context or by passing parameters to you context (in the same way you do now with the extension). The last option might not work very well if you have many contexts which need that configuration.

这篇关于Behat 3-如何在上下文中检索自定义扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆