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 Bundle Laravel Package

behat/behat-bundle

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Check Compatibility: Since this bundle is obsolete, verify if your project uses Behat 2.4+ (recommended). If so, replace this bundle with the Symfony2Extension. Run:

    composer remove behat/behat-bundle
    composer require --dev behat/symfony2-extension
    
  2. Basic Configuration: Update app/config/config.yml (or equivalent) to integrate the new extension:

    behat:
        extensions:
            Behat\Symfony2Extension\Extension:
                kernel:
                    environment: 'test'
                    debug: '%kernel.debug%'
    
  3. First Use Case: Run a simple feature test:

    vendor/bin/behat --config=features/bootstrap/behat.yml
    

    Ensure your behat.yml includes:

    default:
        extensions:
            Behat\Symfony2Extension\Extension:
                bootstrap: features/bootstrap/bootstrap.php
    

Implementation Patterns

Workflows

  1. Kernel Initialization: Use the extension to bootstrap the Symfony kernel in tests:

    // features/bootstrap/bootstrap.php
    use Behat\Symfony2Extension\ServiceContainer\ContainerInitializer;
    $container = new ContainerInitializer($kernel);
    
  2. Context Integration: Extend Behat\Symfony2Extension\Context\KernelAwareContext for kernel-aware contexts:

    use Behat\Symfony2Extension\Context\KernelAwareContext;
    class FeatureContext extends KernelAwareContext {
        protected function getKernel() {
            return $this->getSymfonyKernel();
        }
    }
    
  3. Service Injection: Access Symfony services in contexts:

    $router = $this->getContainer()->get('router');
    
  4. Environment Management: Override environments per suite in behat.yml:

    suites:
        default:
            contexts:
                - FeatureContext
            extensions:
                Behat\Symfony2Extension\Extension:
                    kernel:
                        environment: 'test'
    

Integration Tips

  • Database Testing: Use Behat\MinkExtension alongside for browser/database interactions.
  • Fixtures: Load fixtures via doctrine:fixtures:load in bootstrap.php:
    $this->getKernel()->boot();
    $this->getContainer()->get('doctrine')->getManager()->getConnection()->beginTransaction();
    
  • Caching: Disable cache in test environments to avoid stale data:
    behat:
        extensions:
            Behat\Symfony2Extension\Extension:
                kernel:
                    debug: true
                    cache_warmer: false
    

Gotchas and Tips

Pitfalls

  1. Deprecated Bundle:

    • This bundle does not work with Behat 3.x or Symfony 4/5. Migrate to symfony2-extension immediately.
    • Error: Class 'Behat\BehatBundle\BehatBundle' not found → Remove the bundle and install the extension.
  2. Kernel Bootstrapping:

    • Forgetting to call $kernel->boot() before accessing services/database will cause:
      No connection for service "doctrine.dbal.default_connection"
      
    • Fix: Ensure bootstrap.php includes:
      $kernel->boot();
      
  3. Environment Mismatch:

    • Tests may fail if the kernel environment doesn’t match your behat.yml config (e.g., test vs. dev).
    • Debug: Add var_dump($kernel->getEnvironment()) in contexts to verify.
  4. Service Container Initialization:

    • Using $this->getContainer() before the kernel is booted throws:
      Service not found: "service_id"
      
    • Fix: Initialize the container in bootstrap.php after booting the kernel.

Debugging

  • Enable Debug Mode: Set debug: true in behat.yml to get verbose error messages.
  • Log Contexts: Use dump() or var_dump() in contexts to inspect objects:
    use Symfony\Component\VarDumper\VarDumper;
    VarDumper::dump($this->getContainer()->getParameter('some_param'));
    
  • Isolated Tests: Wrap database operations in transactions and rollback:
    $connection = $this->getContainer()->get('doctrine')->getManager()->getConnection();
    $connection->beginTransaction();
    // Test logic
    $connection->rollBack();
    

Extension Points

  1. Custom Contexts: Create reusable contexts for shared logic (e.g., AuthenticationContext).
  2. Hooks: Use BeforeScenario/AfterScenario to manage test state:
    use Behat\Behat\Hook\Scope\ScenarioScope;
    /**
     * @BeforeScenario
     */
    public function clearDatabase(ScenarioScope $scope) {
        $this->getContainer()->get('doctrine')->getManager()->getConnection()->beginTransaction();
    }
    
  3. Mink Integration: Combine with Behat\MinkExtension for UI testing:
    extensions:
        Behat\MinkExtension:
            base_url: 'http://localhost:8000'
        Behat\Symfony2Extension\Extension:
            kernel: ~
    
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui