cubicmushroom/symfony-behat-context-bundle
Installation Add the bundle to your Symfony project via Composer:
composer require cubicmushroom/symfony-behat-context-bundle
Register the bundle in config/bundles.php:
return [
// ...
CubicMushroom\SymfonyBehatContextBundle\CubicMushroomSymfonyBehatContextBundle::class => ['all' => true],
];
First Use Case
Enable the bundle’s default context in your Behat behat.yml:
default:
suites:
default:
contexts:
- CubicMushroom\SymfonyBehatContextBundle\Context\SymfonyContext
Use it in a feature file:
Feature: Symfony Context Example
Scenario: Check Symfony container service
Given I have a service "router" from the Symfony container
Then I should see "router" in the container
Service Access
Use the I have a service step to fetch Symfony services in tests:
Given I have a service "security.token_storage" from the Symfony container
Request/Response Testing
Leverage the I make a request step for HTTP interactions:
Given I make a request to "/api/users"
Then the response status code should be 200
Event Dispatching
Simulate events with I dispatch an event:
Given I dispatch an event "kernel.request" with data {"_route": "homepage"}
Custom Contexts: Extend SymfonyContext to add project-specific steps:
namespace App\Tests\Context;
use CubicMushroom\SymfonyBehatContextBundle\Context\SymfonyContext;
class CustomSymfonyContext extends SymfonyContext {
public function someCustomStep() {
// Logic here
}
}
Register it in behat.yml:
contexts:
- App\Tests\Context\CustomSymfonyContext
Dependency Injection: Use the container directly in steps:
$this->getContainer()->get('service_id');
Container Initialization
Ensure the Symfony kernel is bootstrapped before using the context. Add this to your FeatureContext:
use Symfony\Component\HttpKernel\KernelInterface;
class FeatureContext implements AfterScenarioInterface {
public function afterScenario(Scenario $scenario) {
// Reset container if needed (e.g., for isolated tests)
}
}
Service Unavailability
Steps like I have a service will fail if the service isn’t registered. Verify services exist in config/services.yaml or are tagged correctly.
Event Dispatching
Events must match Symfony’s event names exactly (e.g., kernel.request, not kernel.request.event).
APP_ENV=dev in .env to get detailed error messages.$this->getContainer()->get('logger')->debug('Step executed');
Custom Steps Override or extend existing steps by redefining methods in a child context class.
Configuration
Modify bundle behavior via config/packages/cubicmushroom_symfony_behat_context.yaml (if supported in future versions).
Mocking Services Use PHPUnit’s mocking tools to replace real services in tests:
$this->getContainer()->set('service_id', $this->createMock(ServiceInterface::class));
How can I help you explore Laravel packages today?