roave / behat-psr11extension
PSR-11 Container extension for Behat
Installs: 287 361
Dependents: 2
Suggesters: 0
Security: 0
Stars: 41
Watchers: 13
Forks: 6
Open Issues: 10
Requires
- php: ~8.0.0 || ~8.1.0 || ~8.2.0 || ~8.3.0
- behat/behat: ^3.14.0
- psr/container: ^1.1.2
- symfony/config: ^6.0.19
- symfony/dependency-injection: ^6.0.20
Requires (Dev)
- doctrine/coding-standard: ^12.0.0
- laminas/laminas-servicemanager: ^3.20.0
- phpunit/phpunit: ^9.6.19
- 2.5.x-dev
- 2.4.x-dev
- 2.4.0
- 2.3.x-dev
- 2.3.0
- 2.2.x-dev
- 2.2.0
- 2.1.x-dev
- 2.1.0
- 2.0.x-dev
- 2.0.0
- 1.3.x-dev
- 1.3.0
- 1.2.x-dev
- 1.2.0
- 1.1.0
- 1.0.0
- dev-renovate/phpunit-phpunit-11.x
- dev-renovate/all-minor-patch
- dev-renovate/symfony-dependency-injection-7.x
- dev-renovate/symfony-config-7.x
- dev-renovate/laminas-laminas-servicemanager-4.x
- dev-renovate/psr-container-2.x
This package is auto-updated.
Last update: 2024-12-21 07:52:08 UTC
README
Allows injecting services from a PSR-11-compatibile container in a Behat context.
Created with lots of help from @ciaranmcnulty.
Usage
First require the extension and dependencies with Composer:
$ 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 declare(strict_types=1); use Laminas\ServiceManager\Config; use Laminas\ServiceManager\ServiceManager; // Load configuration $config = require __DIR__ . '/config.php'; // Build container $container = new ServiceManager(); (new Config($config['dependencies']))->configureServiceManager($container); // Inject config $container->setService('config', $config); return $container;
Then enable the extension in behat.yml
:
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:
default: suites: my_suite: services: "@psr_container"
And finally, add the names of any services required by your contexts in behat.yml
, for example:
default: suites: my_suite: services: "@psr_container" contexts: - MyBehatTestSuite\MyContext: - "@Whatever\\Service\\Name"
You can also use behat's built-in autowire feature, to automatically inject the dependencies to the context:
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:
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 declare(strict_types=1); $container = require 'config/container.php'; $context = new \MyBehatTestSuite\MyContext($container->get('Whatever\Service\Name'));