https://github.com/roave/behat-psr11extension
PSR-11 Container extension for Behat
https://github.com/roave/behat-psr11extension
Last synced: about 1 year ago
JSON representation
PSR-11 Container extension for Behat
- Host: GitHub
- URL: https://github.com/roave/behat-psr11extension
- Owner: Roave
- Created: 2017-08-09T20:44:50.000Z (almost 9 years ago)
- Default Branch: 2.6.x
- Last Pushed: 2025-04-28T20:29:06.000Z (about 1 year ago)
- Last Synced: 2025-04-28T21:39:40.232Z (about 1 year ago)
- Language: PHP
- Size: 313 KB
- Stars: 41
- Watchers: 12
- Forks: 6
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# PSR-11 Container extension for Behat
[](https://travis-ci.org/Roave/behat-psr11extension) [](https://scrutinizer-ci.com/g/Roave/behat-psr11extension/?branch=master) [](https://scrutinizer-ci.com/g/Roave/behat-psr11extension/?branch=master) [](https://packagist.org/packages/roave/behat-psr11extension) [](https://packagist.org/packages/roave/behat-psr11extension)
Allows injecting services from a PSR-11-compatibile container in a Behat context.
Created with lots of help from [@ciaranmcnulty](https://github.com/ciaranmcnulty).
## Usage
First require the extension and dependencies with Composer:
```bash
$ composer require --dev roave/behat-psr11extension
```
First, if you don't already have one, create a file that will be included by the extension that returns a PSR-11
compatible container, for example using `Laminas\ServiceManager`:
```php
configureServiceManager($container);
// Inject config
$container->setService('config', $config);
return $container;
```
Then enable the extension in `behat.yml`:
```yaml
extensions:
Roave\BehatPsrContainer\PsrContainerExtension:
container: 'config/container.php'
```
Then enable the use of the `psr_container` service container (this is provided by the extension) in your `behat.yml`
suite configuration, for example:
```yaml
default:
suites:
my_suite:
services: "@psr_container"
```
And finally, add the names of any services required by your contexts in `behat.yml`, for example:
```yaml
default:
suites:
my_suite:
services: "@psr_container"
contexts:
- MyBehatTestSuite\MyContext:
- "@Whatever\\Service\\Name"
```
You can also use behat's built-in [autowire feature](https://github.com/Behat/Behat/pull/1071), to automatically inject the dependencies to the context:
```yaml
default:
suites:
my_suite:
autowire: true
services: "@psr_container"
contexts:
- MyBehatTestSuite\MyContext
```
If for some reason you want to use a name other than `psr_container` for the container (e.g. collision with another extension) this can
be overridden:
```yaml
extensions:
Roave\BehatPsrContainer\PsrContainerExtension:
container: 'config/container.php'
name: 'my_container'
```
Just for clarity (and hopefully ease of understanding), this would be the equivalent of doing this in plain PHP:
```php
get('Whatever\Service\Name'));
```