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

Symfony Extension Laravel Package

friends-of-behat/symfony-extension

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation Add the package via Composer:

    composer require --dev friends-of-behat/symfony-extension
    

    Ensure behat/behat and symfony/browser-kit are also installed.

  2. Configure behat.yml Add the extension to your Behat config:

    default:
      extensions:
        FriendsOfBehat\SymfonyExtension:
          kernel:
            class: App\Kernel
            env: test
    
  3. First Use Case: Testing a Controller Define a context as a Symfony service (e.g., FeatureContext):

    // src/FeatureContext.php
    namespace App\Tests\Feature;
    
    use Symfony\Component\HttpKernel\KernelInterface;
    use Behat\Behat\Context\Context;
    use Behat\Behat\Context\SnippetAcceptingContextInterface;
    
    class FeatureContext implements Context, SnippetAcceptingContextInterface
    {
        public function __construct(private KernelInterface $kernel) {}
    
        /**
         * @Given I access the homepage
         */
        public function iAccessTheHomepage(): void
        {
            $client = $this->kernel->getContainer()->get('test.client');
            $client->request('GET', '/');
        }
    }
    

    Run Behat:

    ./vendor/bin/behat
    

Implementation Patterns

Autowiring and Service-Based Contexts

  • Register contexts as services in config/services.yaml:
    services:
      App\Tests\Feature\FeatureContext:
        tags: ['behat.context']
    
  • Leverage Symfony’s DI to inject dependencies (e.g., EntityManager, Router):
    use Doctrine\ORM\EntityManagerInterface;
    
    class FeatureContext {
        public function __construct(private EntityManagerInterface $em) {}
    }
    

Mink Integration for Functional Testing

  • Configure Mink in behat.yml:
    FriendsOfBehat\SymfonyExtension:
      mink:
        base_url: 'http://localhost'
        sessions:
          default:
            symfony: ~
    
  • Use Mink steps in contexts:
    use Behat\MinkExtension\Context\MinkContext;
    
    class FeatureContext extends MinkContext {
        /**
         * @Given I am on the homepage
         */
        public function iAmOnTheHomepage(): void
        {
            $this->visitPath('/');
        }
    }
    

Kernel and Environment Management

  • Switch environments dynamically via config:
    FriendsOfBehat\SymfonyExtension:
      kernel:
        env: test  # or 'dev', 'prod'
    
  • Mock services for isolated testing:
    $container = $this->kernel->getContainer();
    $container->set('some.service', $mockService);
    

Tagging and Scenario Filtering

  • Tag contexts for selective execution:
    # behat.yml
    suites:
      default:
        contexts:
          - App\Tests\Feature\FeatureContext:
              tags: ['@smoke', '@api']
    
  • Run specific tags:
    ./vendor/bin/behat --tags "@smoke"
    

Gotchas and Tips

Common Pitfalls

  1. Kernel Initialization Issues

    • Ensure env: test is set to avoid conflicts with production/secrets.
    • Debug kernel bootstrapping with:
      $this->kernel->boot();
      
      if services fail to load.
  2. Mink Session Configuration

    • If using Symfony’s BrowserKit driver, ensure FriendsOfBehat\SymfonyExtension is loaded after Behat\MinkExtension in behat.yml.
    • Avoid mixing symfony and goutte sessions in the same suite.
  3. Circular Dependencies

    • Contexts autowired as services may cause issues if they depend on other contexts. Use constructor injection sparingly.
  4. Database Transactions

    • Enable transactions in behat.yml to roll back changes:
      FriendsOfBehat\SymfonyExtension:
        kernel:
          env: test
          debug: true
          transactional: true
      

Debugging Tips

  • Enable Debug Mode

    FriendsOfBehat\SymfonyExtension:
      kernel:
        debug: true
    

    This dumps kernel events and errors to the console.

  • Inspect the Kernel Container Use a custom context to debug services:

    public function dumpContainer(): void
    {
        $container = $this->kernel->getContainer();
        dump($container->getServiceIds());
    }
    
  • Log Behat Events Add a listener to log context initialization:

    use Behat\Behat\Event\SuiteEvent;
    use Behat\Behat\Event\SuiteEvents;
    
    $eventDispatcher->addListener(SuiteEvents::BEFORE_SUITE, function (SuiteEvent $event) {
        \Behat\Testwork\Output\ConsoleOutput::getInstance()->writeln('Suite started!');
    });
    

Extension Points

  1. Custom Kernel Factories Override kernel creation for complex setups:

    use FriendsOfBehat\SymfonyExtension\Kernel\KernelFactoryInterface;
    
    class CustomKernelFactory implements KernelFactoryInterface
    {
        public function createKernel(array $config): KernelInterface
        {
            return new CustomKernel($config['env'], $config['debug']);
        }
    }
    

    Register it in behat.yml:

    FriendsOfBehat\SymfonyExtension:
      kernel_factory: App\Tests\CustomKernelFactory
    
  2. Event Subscribers Hook into Symfony events (e.g., KernelEvents::REQUEST) from contexts:

    use Symfony\Component\HttpKernel\Event\RequestEvent;
    use Symfony\Component\HttpKernel\KernelEvents;
    
    $dispatcher = $this->kernel->getContainer()->get('event_dispatcher');
    $dispatcher->addListener(KernelEvents::REQUEST, function (RequestEvent $event) {
        // Modify request/response
    });
    
  3. Dynamic Configuration Load Behat config from Symfony parameters:

    # config/packages/behat.yaml
    behat:
      suites:
        default:
          paths: ['%kernel.project_dir%/tests/Feature']
    

    Access via:

    $projectDir = $this->kernel->getProjectDir();
    
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.
comsave/common
alecsammon/php-raml-parser
chrome-php/wrench
lendable/composer-license-checker
typhoon/reflection
mesilov/moneyphp-percentage
mike42/gfx-php
bookdown/themes
aura/view
aura/html
aura/cli
povils/phpmnd
nayjest/manipulator
omnipay/tests
psr-mock/http-message-implementation
psr-mock/http-factory-implementation
psr-mock/http-client-implementation
voku/email-check
voku/urlify
rtheunissen/guzzle-log-middleware