lcobucci/behat-di-builder-extension

A behat extension that allows injecting services from a container created with lcobucci/di-builder in contexts

1.3.0 2021-10-19 00:09 UTC

README

Total Downloads Latest Stable Version Unstable Version

Build Status Code Coverage

Allows injecting services from a container created using lcobucci/di-builder in a Behat context.

Installation

Package is available on Packagist, you can install it using Composer.

composer require --dev lcobucci/behat-di-builder-extension

Basic usage

You must first enable the extension on your behat configuration file:

default:
  # ...
  
  extensions:
      Lcobucci\DependencyInjection\Behat\BuilderExtension: ~

Then enable the use of the test container (create by the extension) in the suite configuration:

default:
  suites:
    my-suite:
      services: "@test_container"
  
  extensions:
      Lcobucci\DependencyInjection\Behat\BuilderExtension: ~

And finally inject the services into your contexts

default:
  suites:
    my-suite:
      services: "@test_container"
      contexts:
        - My\Lovely\Context:
          - "@my_service"
  
  extensions:
    Lcobucci\DependencyInjection\Behat\BuilderExtension: ~

Advanced configuration

You can provide the following parameters to the extension to better configure it:

  • name: if you already have an extension using test_container
  • container_builder: if your application already uses lcobucci/di-builder and you want to use it (instead of a blank container builder)
  • packages: so that you can add/override service definitions for testing

Your behat.yml would look like this with those settings:

default:
  suites:
    my-suite:
      services: "@my_container"
      contexts:
        - My\Lovely\Context:
          - "@my_service"
  
  extensions:
    Lcobucci\DependencyInjection\Behat\BuilderExtension:
      name: "my_container"
      container_builder: "config/container_builder.php"
      packages:
        My\DIBuilder\Package: ~
        My\DIBuilder\PackageWithConstructorArguments:
          - "one"
          - "two"