acseo/behat-generator-bundle
Symfony bundle that generates Behat .feature files automatically from your app’s routes, jumpstarting BDD coverage. Includes setup guidance for Behat + Mink with the Zombie.js driver and an example FeatureContext for form submission.
Install the Bundle
composer require --dev acseo/behat-generator-bundle
Add to composer.json under require-dev if not using autoloader.
Enable the Bundle
Register in config/bundles.php:
return [
// ...
Acseo\BehatGeneratorBundle\AcseoBehatGeneratorBundle::class => ['dev' => true, 'test' => true],
];
Generate Feature Files Run the generator command:
php bin/console acseo:behat-generator:generate
Outputs .feature files in features/ (default) with basic route-based scenarios.
First Use Case
features/ (e.g., login.feature, dashboard.feature).vendor/bin/behat
Route-Driven Generation
.feature file per route (annotated with #[Route])./login:
Feature: Login
As a user
I want to access the login page
So I can authenticate
Scenario: Visit login page
Given I am on "/login"
Then I should see "Login"
config/packages/acseo_behat_generator.yaml:
acseo_behat_generator:
template: 'custom_path/templates/feature.feature.twig'
Integration with CI/CD
- name: Generate Behat Features
run: php bin/console acseo:behat-generator:generate
Context Augmentation
FeatureContext to add domain-specific steps:
// features/bootstrap/FeatureContext.php
use Acme\Domain\UserContext;
class FeatureContext extends MinkContext {
public function __construct($container) {
$this->userContext = new UserContext($container);
}
/**
* @Given I am logged in as :username
*/
public function iAmLoggedInAs($username) {
$this->userContext->login($username);
}
}
Dynamic Route Filtering
php bin/console acseo:behat-generator:generate --routes="app.login app.dashboard"
php artisan route:list to cross-reference generated features with your route definitions.base.feature.twig) with shared hooks (e.g., auth steps) and extend it in the bundle config.php bin/console acseo:behat-generator:generate --force=false
Route Annotations Missing
#[Route] annotations won’t generate features.--all flag (generates all controller actions):
php bin/console acseo:behat-generator:generate --all
Behat Context Mismatch
FeatureContext lacks required methods.FeatureContext extends MinkContext and includes all referenced steps (e.g., I am on "/login").Template Overrides Not Loading
config/ are ignored.php bin/console cache:clear
var/log/dev.log for Twig template errors.Zombie Driver Quirks
--driver=chrome (requires behat/mink-selenium2-driver):
# behat.yml
sessions:
default:
selenium2: ~
php bin/console acseo:behat-generator:generate --dry-run
php bin/console acseo:behat-generator:generate --verbose
Custom Step Generators
Override step generation via event listeners. Example in EventSubscriber:
use Acseo\BehatGeneratorBundle\Event\GenerateFeatureEvent;
class CustomStepSubscriber implements EventSubscriber {
public static function getSubscribedEvents() {
return [
GenerateFeatureEvent::class => 'onGenerateFeature',
];
}
public function onGenerateFeature(GenerateFeatureEvent $event) {
$event->addStep('Then I should see a success message');
}
}
Register in services.yaml:
services:
App\EventSubscriber\CustomStepSubscriber:
tags: ['kernel.event_subscriber']
Post-Generation Hooks
Use Symfony’s kernel.terminate event to process generated files:
// src/EventListener/PostGenerateListener.php
use Acseo\BehatGeneratorBundle\Event\PostGenerateEvent;
class PostGenerateListener {
public function onPostGenerate(PostGenerateEvent $event) {
foreach ($event->getGeneratedFiles() as $file) {
// Add custom logic (e.g., linting, notifications)
}
}
}
Exclude Routes Skip specific routes via annotation:
#[Route('/admin', name: 'admin_dashboard', options: ['exclude_from_behat' => true])]
How can I help you explore Laravel packages today?