Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Behat Generator Bundle Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Install the Bundle

    composer require --dev acseo/behat-generator-bundle
    

    Add to composer.json under require-dev if not using autoloader.

  2. Enable the Bundle Register in config/bundles.php:

    return [
        // ...
        Acseo\BehatGeneratorBundle\AcseoBehatGeneratorBundle::class => ['dev' => true, 'test' => true],
    ];
    
  3. 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.

  4. First Use Case

    • Inspect generated files in features/ (e.g., login.feature, dashboard.feature).
    • Manually edit scenarios to add assertions, steps, or custom logic.
    • Run Behat to validate:
      vendor/bin/behat
      

Implementation Patterns

Workflows

  1. Route-Driven Generation

    • Generates one .feature file per route (annotated with #[Route]).
    • Example output for /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"
      
    • Customization: Override templates in config/packages/acseo_behat_generator.yaml:
      acseo_behat_generator:
          template: 'custom_path/templates/feature.feature.twig'
      
  2. Integration with CI/CD

    • Use in pre-commit hooks or CI pipelines to auto-generate skeleton tests.
    • Example GitHub Actions step:
      - name: Generate Behat Features
        run: php bin/console acseo:behat-generator:generate
      
  3. Context Augmentation

    • Extend 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);
          }
      }
      
  4. Dynamic Route Filtering

    • Generate features for specific routes only:
      php bin/console acseo:behat-generator:generate --routes="app.login app.dashboard"
      

Tips for Daily Use

  • Pair with Laravel Routes: Use php artisan route:list to cross-reference generated features with your route definitions.
  • Template Inheritance: Create a base template (base.feature.twig) with shared hooks (e.g., auth steps) and extend it in the bundle config.
  • Partial Updates: Regenerate only changed routes:
    php bin/console acseo:behat-generator:generate --force=false
    

Gotchas and Tips

Pitfalls

  1. Route Annotations Missing

    • Issue: Routes without #[Route] annotations won’t generate features.
    • Fix: Ensure all public routes are annotated or use --all flag (generates all controller actions):
      php bin/console acseo:behat-generator:generate --all
      
  2. Behat Context Mismatch

    • Issue: Generated steps may fail if FeatureContext lacks required methods.
    • Fix: Verify FeatureContext extends MinkContext and includes all referenced steps (e.g., I am on "/login").
  3. Template Overrides Not Loading

    • Issue: Custom templates in config/ are ignored.
    • Fix: Clear cache:
      php bin/console cache:clear
      
    • Debug: Check var/log/dev.log for Twig template errors.
  4. Zombie Driver Quirks

    • Issue: JavaScript-heavy routes fail with Zombie driver.
    • Fix: Use --driver=chrome (requires behat/mink-selenium2-driver):
      # behat.yml
      sessions:
          default:
              selenium2: ~
      

Debugging

  • Dry Run Mode: Test generation without writing files:
    php bin/console acseo:behat-generator:generate --dry-run
    
  • Verbose Output: Enable debug mode:
    php bin/console acseo:behat-generator:generate --verbose
    

Extension Points

  1. 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']
    
  2. 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)
            }
        }
    }
    
  3. Exclude Routes Skip specific routes via annotation:

    #[Route('/admin', name: 'admin_dashboard', options: ['exclude_from_behat' => true])]
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle