I’ve been successfully using Behat with zend-test, but only by making my contexts inherit from AbstractHttpControllerTestCase. This means I can only have one context per Behat suite.
This limits my ability to organize context code.
Is there a more clever way to use the two together? Ideally I’d like to inject a shared instance of AbstractHttpControllerTestCase into each context, but as far as I can tell Behat doesn’t support this.
There are at least two answers (yay!)
And Behat’s own “helper containers”:
Feature: Per-suite helper containers
In order to share state and behaviour between contexts
developers need to have a way to create or register shared service container
Rules:
- A single optional container is allowed per suite
- Having a container enables you to use its services as context arguments via `@name` syntax
- Container is rebuilt and is isolated between scenarios
- Container is configured via suite's `services` option
- Container is a class implementing `Psr\Container\ContainerInterface`
- There is a built-in container if you need a very simple service-sharing, configurable through the same `services` setting
- There is an extension point that allows Behat extensions provide their own containers for end-users via `@name` syntax
Out of scope:
- Extensive service configuration and deep dependency trees support for built-in container. Behat is not your DIC framework
- Sharing scalar, non-object parameters using container. Use YAML anchors and references for configuration sharing
- Multiple containers per suite. Would introduce unnecessary complexity. Also, easily achievable manually through composition
- PSR-11 support. It was not accepted as a standard as of feature implementation. Support might be added later, subject to prioritisation
Usage:
This file has been truncated. show original
Note that we wrote the roave/behat-psr11extension
because the built-in only works with a symfony container.
Ah, thanks for the clarification Marco.
I asked just that question in the Q&A channel on zf slack (at the risk of making a loop: https://zendframework.slack.com/archives/C4QB9N6E8/p1503035537000098 )
I’m not seeing the use case for a full-fledged container with Behat - I only ever want to inject some top-level, application level object.
Maybe I’m suffering from lack of imagination.
Behat is not just an E2E scenario verification tool: you can use it in isolated portions of your service layer.