zalas / behat-no-extension
终极Behat扩展
v2.2.0
2019-12-15 09:16 UTC
Requires
- php: ^7.2
- behat/behat: ^3.0
- symfony/dependency-injection: ^3.4||^4.4||^5.0
Requires (Dev)
- bossa/phpspec2-expect: ^3.0
- phpspec/phpspec: ^6.1
- sebastian/comparator: ^2.0||^3.0
- symfony/filesystem: ^3.4||^4.4||^5.0
- symfony/process: ^3.4||^4.4||^5.0
README
此Behat扩展使得无需自己编写扩展即可扩展Behat。
安装
此扩展需要
- Behat ^3.0
- PHP ^7.1
安装它的最简单方法是使用Composer
$ composer require --dev zalas/behat-no-extension
接下来,在您的behat.yml
中激活扩展
# behat.yml default: extensions: Zalas\Behat\NoExtension: ~
导入服务定义
扩展使您能够从在导入
部分指定的配置文件中加载服务定义和参数
# behat.yml default: extensions: Zalas\Behat\NoExtension: imports: - features/bootstrap/config/services.yml
这些应该是Symfony的服务容器配置文件
# features/bootstrap/config/services.yml services: Acme\SimpleArgumentResolver: tags: - { name: context.argument_resolver } parameters: acme.foo: boo!
当前支持Yaml,xml和php格式。
请注意,您想使用的任何类都应该由Composer自动加载。对于上面的示例,autoload-dev
或autoload
应包括Acme\\
自动加载前缀。
将服务注入到上下文中
启用参数解析器以利用内置的服务注入支持
# behat.yml default: extensions: Zalas\Behat\NoExtension: argument_resolver: true imports: - features/bootstrap/config/services.yml
假设您想注入到上下文中的服务定义在features/bootstrap/Acme
中,并且它们由Composer自动加载,您现在可以在配置文件中开始定义它们
# features/bootstrap/config/services.yml services: Acme\: resource: '../Acme' public: true autowire: true
上面的示例依赖于自动加载,但您也可以明确定义每个服务。
一个示例Composer自动加载配置
{ "autoload-dev": { "psr-4": { "Acme\\": "features/bootstrap/Acme" } } }
给定一个类Acme\Foo
的定义,它现在可以注入到上下文中
use Acme\Foo; use Behat\Behat\Context\Context; class FeatureContext implements Context { private $foo; public function __construct(Foo $foo) { $this->foo = $foo; } }
定义参数
在导入的文件中定义的参数也适用于behat.yml
# behat.yml default: suites: search: contexts: - SearchContext: myFoo: '%acme.foo%' # ...
此外,参数还可以在扩展的配置中直接在behat.yml
中定义
# behat.yml default: extensions: Zalas\Behat\NoExtension: parameters: foo: bar baz: a: 1 b: 'bazinga!'