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.
Accelerating Test-Driven Development (TDD) in Laravel: Enables teams to rapidly prototype and iterate on Behat test suites by injecting custom services (e.g., mock APIs, test repositories, or domain-specific utilities) without building full extensions. Reduces the cognitive load for QA engineers and developers collaborating on BDD workflows.
TestPaymentGateway into a PaymentFeatureContext to validate edge cases without touching production code.Decoupling Test Infrastructure from Laravel’s Core: Supports a "build vs. buy" decision by avoiding custom extension development for test-specific needs. Aligns with Laravel’s philosophy of separation of concerns (e.g., test services vs. production services).
new TestUserFactory() calls with container-managed instances to enforce consistency and reduce duplication.Scaling Behat for Complex Laravel Applications: Justifies investment in Behat for projects with modular architectures (e.g., microservices, multi-tenancy) by providing a maintainable way to extend test coverage without refactoring core test logic.
Use Cases in Laravel Ecosystem:
Stripe, Twilio) to isolate tests from live systems.
stripe_mock in services.yml and inject it into a SubscriptionFeatureContext.TestDatabaseSeeder, TestAuthHelper) across feature contexts without global state pollution.
TestUserFactory in services.php and reuse it in UserManagementContext and ProfileContext.TestSlackNotifier, TestS3Uploader) for test reporting or artifact handling.
services.yml to use a TestMailer that logs emails instead of sending them in CI.createMock(), Mockery) but provides tighter DI integration for complex scenarios.
TestHttpClient that wraps Laravel’s Http facade for controlled API testing.Adopt When:
Behat\MinkExtension, Behat\SymfonyExtension).new TestService()) in step definitions or hooks, indicating a pattern for abstraction.Illuminate/Container) and want to leverage familiar syntax (XML/YAML/PHP) for test configurations.Look Elsewhere If:
BeforeScenario, AfterFeature), which require a full extension (e.g., Behat\HookExtension).Testing facade).config/ and app/Providers/ may mitigate this).Testing facade extensively and find it sufficient for mocking (e.g., createMock(), fake()). This package adds value primarily for complex DI scenarios.*"This package lets our QA and development teams plug in custom test services—like mock payment processors, test database seeders, or CI-specific notifiers—into our Behat tests without building custom extensions from scratch. It’s like using Laravel’s service container for tests, but tailored for Behat.
Why it matters:
Impact:
Risk:
Ask:
*"This package lets us inject custom services into Behat’s container using Symfony’s DI syntax (XML/YAML/PHP), which we’re already familiar with from Laravel. It’s a lightweight way to avoid writing custom extensions for simple use cases.
When to Use It:
Stripe, Twilio) without polluting global state.TestUserFactory, TestDatabaseSeeder) across feature contexts.new TestApiClient()).How It Works:
--dev scope).behat.yml to import your service definitions (e.g., services.yml in features/bootstrap/config/).# features/bootstrap/config/services.yml
services:
test.payment_gateway:
class: App\Tests\Services\MockPaymentGateway
arguments: ['@test.logger']
class PaymentFeatureContext {
public function __construct(private MockPaymentGateway $paymentGateway) {}
}
Pros:
Cons/Risks:
Recommendation:
Example Workflow: Instead of this:
// Manual instantiation (messy)
$client = new TestApiClient(config('test.api'));
Do this:
# services.yml
services:
test.api_client:
class: App\Tests\Services\TestApiClient
arguments: ['%env(TEST_API_URL)%']
// Clean injection
class ApiFeatureContext {
public function __construct(private TestApiClient $client) {}
}
```*
---
### **For Technical Leads/Architects**:
*"This package addresses a gap in Laravel’s Behat integration by providing a **Symfony DI-compatible service container extension** for Behat. It’s particularly valuable for teams
How can I help you explore Laravel packages today?