我需要添加内容,稍后从上下文中检索behat.yml文件中的额外选项/配置。Behat 3 - 如何在上下文中检索自定义扩展
Behat不允许我将一些随机参数添加到behat.yml文件中,所以我创建了新的自定义扩展。这个扩展让我通过特定的配置值
extensions:
App\Behat\DevToolsExtension:
api_url: "https://api.example.com"
所以现在贝哈特不是在抱怨behat.yml文件中的新配置。
现在我卡住了。如何从运行时的扩展中检索此配置?
我像这样设置公共职能负载(ContainerBuilder $容器,数组$配置)方法中,我扩展现有的参数:
$container->setParameter($configKey . $key, $config[$key]);
再次,有没有办法来检索此ContainerBuilder对象或DevToolsExtension上下文中的对象?
编辑
解决方案是创建服务容器,并通过behats ContainerBuilder到它像这样:
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);
}
然后用服务定位器来检索配置参数
$value = $this->getBehatContainer()->getParameter($key);