friends-of-behat/service-container-extension
Declare custom Symfony DI services in Behat without writing a full extension. Import XML/YAML/PHP service definition files via behat.yml so your contexts and helpers can be wired through the Behat service container.
Illuminate/Container. This enables seamless injection of test-specific services (e.g., mock repositories, API clients) into Behat’s context without polluting Laravel’s production container. Aligns with Laravel’s principle of environment-specific configurations (e.g., .env.testing).TestUserFactory into feature contexts.MockStripeClient in payment workflow tests.TestLogger, ScreenshotService) via DI.app() in tests).composer.json under require-dev.behat.yml with path references to Laravel’s config structure (e.g., features/bootstrap/config/services.yml).config/services.php with services.yml for Behat..env.testing in service definitions (e.g., parameters: { test_db: "%env(TEST_DB_DSN)%" }).--dev flag in composer install for Behat runs (standard for Laravel).Definition class changes).composer.json and monitor for forks (e.g., behat/behat:^4.0 + this package).bind()).TESTING.md.behat.test_api_client) and avoid overlapping with Laravel’s bindings.app()->singleton().behat --stop-on-failure and validate service states across scenarios.Behat Version Lock:
behat --version and check Behat’s upgrade guide for container changes.Service Isolation:
AppServiceProvider for shared test services and document exclusion rules.PHP Version Support:
php -v and composer validate.phpunit.xml check:
<php>
<env name="PHP_VERSION" value=">=8.1.0"/>
</php>
Long-Term Maintenance:
createMock() + partialMock()) if overkill.Team Familiarity:
services.yml (parallel to Laravel’s config/services.php).getMockBuilder()) may suffice.Preparation Phase:
new TestUserRepository()).TestUserRepository → services.yml).composer.json has:
"require-dev": {
"friends-of-behat/service-container-extension": "^2.0"
}
Proof of Concept (PoC):
behat.yml:
# behat.yml
default:
extensions:
FriendsOfBehat\ServiceContainerExtension:
imports:
- "%paths.config%/behat/services.yml"
features/bootstrap/config/services.yml:
services:
behat.test_user_repository:
class: App\Tests\TestUserRepository
arguments: ["@database_connection.testing"]
class UserContext {
public function __construct(private TestUserRepository $repository) {}
public function i_create_a_user() {
$this->repository->create(['name' => 'John']);
}
}
./vendor/bin/behat. Verify the service is resolved.Incremental Rollout:
user.feature).services.yml (avoid duplicating XML/PHP).services.testing.yml for CI).Configuration Standardization:
behat. (e.g., behat.test_api_client)..env.testing for dynamic values:
parameters:
test_api_url: "%env(TEST_API_URL)%"
services:
behat.test_api_client:
class: App\Tests\TestApiClient
arguments: ["@test_api_url"]
imports:
- "%paths.config%/behat/services.yml"
- "%paths.config%/behat/services.ci.yml" # CI-specific
How can I help you explore Laravel packages today?