我需要添加,然后从上下文中从 behat.yml 文件检索额外的选项/配置。
Behat 不允许我将一些随机参数添加到 behat.yml 文件中,因此我创建了新的自定义扩展。此扩展允许我传递特定的配置值
extensions:
App\Behat\DevToolsExtension:
api_url: "https://api.example.com"
所以现在 behat 不会抱怨 behat.yml 文件中的新配置。
现在我被困住了。如何在运行时从我的扩展中检索此配置?
我在公共函数 load(ContainerBuilder $container, array $config) 方法中设置扩展中的现有参数,如下所示:
$container->setParameter($configKey . $key, $config[$key]);
同样,有没有办法在 Context 中检索这个 ContainerBuilder 对象或 DevToolsExtension 对象?
Edit
解决方案是创建服务容器并将行为 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);