friends-of-behat/context-service-extension
Deprecated Behat extension that lets you register Behat context classes as Symfony DI services and load them into a scenario-scoped container via imported service config files (XML/YAML/PHP), using the fob.context_service tag.
Installation
composer require friends-of-behat/context-service-extension --dev
Configure Behat
Add the extension to your behat.yml with service file imports:
default:
extensions:
FriendsOfBehat\ContextServiceExtension:
imports:
- "%paths.base%/features/bootstrap/config/services.yml"
Define a Context Service
Create a service in services.yml with the fob.context_service tag:
services:
Acme\FeatureContext:
tags:
- { name: fob.context_service }
Use in Suite
Reference the service in your suite’s contexts_services:
suites:
default:
contexts_services:
- Acme\FeatureContext
Replace a traditional class-based context with a service:
// Before: Class-based context
class FeatureContext implements Context { ... }
// After: Service-based context (injected via DI)
class FeatureContext {
public function __construct(private SomeDependency $dependency) {}
public function someStep() { ... }
}
Service Definition Define contexts as services with dependencies:
services:
Acme\AuthContext:
arguments:
- "@auth_service"
- "@logger"
tags:
- { name: fob.context_service }
Scenario-Scoped Container Leverage the extension’s scenario-scoped container to manage context lifecycles:
// Context service can access scenario-specific data
class UserContext {
public function __construct(private UserRepository $repo) {}
public function iHaveAnAccount() {
$this->repo->createTestUser(); // Scenario-scoped DB state
}
}
Integration with Laravel
AppServiceProvider:
public function register() {
$this->app->register(FriendsOfBehat\ContextServiceExtension\ContextServiceExtension::class);
}
# behat.yml
extensions:
FriendsOfBehat\ContextServiceExtension:
imports:
- "%paths.base%/config/behat-services.php"
// config/behat-services.php
$container->loadFromExtension('framework', ['secret' => env('APP_KEY')]);
context_initializers to set up services:
services:
Acme\Initializer:
tags:
- { name: fob.context_initializer }
extensions:
FriendsOfBehat\ContextServiceExtension:
autoconfigure: true
Deprecation Warning
SymfonyExtension v2 for long-term support.SymfonyExtension’s contexts key instead of contexts_services in newer versions.Service Lifecycle Mismatch
// Dump container services in a context
public function __construct(private ContainerInterface $container) {}
Configuration Overrides
bin/behat -d to check loaded services.fob.context_service tags are correctly applied (check behat.yml logs).services:
Acme\AdminContext:
tags:
- { name: fob.context_service, priority: 100 }
context_initializers to modify services after creation:
class ContextInitializer implements ContextInitializer {
public function initialize(ContextService $context) {
$context->setOption('timeout', 30);
}
}
$this->app->when(FriendsOfBehat\ContextServiceExtension\ContextService::class)
->needs('logger')
->give($this->app->make('log'));
How can I help you explore Laravel packages today?