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 Test Application Laravel Package

bespoke-support/symfony-test-application

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps to Begin

  1. Installation Add the package via Composer:

    composer require --dev bespoke-support/symfony-test-application
    

    Requires Symfony 5.4+ or 6.x.

  2. First Use Case: Testing a Bundle Create a test class extending SymfonyTestingApplication:

    use BespokeSupport\SymfonyTestingApplication\SymfonyTestingApplication;
    
    class MyBundleTest extends SymfonyTestingApplication
    {
        protected function getBundleClass(): string
        {
            return MyBundle::class;
        }
    }
    
  3. Where to Look First

    • SymfonyTestingApplication class: Core functionality for bootstrapping a Symfony kernel with your bundle.
    • createKernel(): Override to customize kernel configuration (e.g., debug mode, environment).
    • getBundleClass(): Required method to specify the bundle under test.
    • getKernelProjectDir(): Override to point to a custom Symfony project directory (e.g., for shared configs).

Implementation Patterns

Workflows

  1. Basic Bundle Testing Use the base class to load your bundle in isolation:

    public function testBundleServices()
    {
        $container = $this->getKernel()->getContainer();
        $this->assertTrue($container->has('my_bundle.service'));
    }
    
  2. Custom Kernel Configuration Override createKernel() to modify Symfony’s behavior:

    protected function createKernel(array $options = []): KernelInterface
    {
        $options['debug'] = true; // Force debug mode
        return parent::createKernel($options);
    }
    
  3. Shared Configurations Point to a pre-configured Symfony project (e.g., with config/packages/):

    protected function getKernelProjectDir(): string
    {
        return __DIR__ . '/../../symfony-shared-config';
    }
    
  4. Dependency Injection Testing Use the container to test services:

    public function testServiceDependency()
    {
        $service = $this->getKernel()->getContainer()->get('my_bundle.service');
        $this->assertInstanceOf(MyDependency::class, $service->getDependency());
    }
    
  5. Event Listener/Subscriber Testing Dispatch events and verify listeners:

    public function testEventListener()
    {
        $event = new MyEvent();
        $this->getKernel()->getContainer()->get('event_dispatcher')->dispatch($event, MyEvent::NAME);
        $this->assertTrue($event->isHandled());
    }
    

Integration Tips

  • Leverage Symfony’s Test Utilities: Combine with Symfony\Bundle\FrameworkBundle\Test\KernelTestCase for advanced features (e.g., client testing).
  • Mock External Services: Use PHPUnit’s mocking to replace dependencies like databases or APIs.
  • Parallel Testing: Configure PHPUnit’s --group or --exclude-group to run bundle tests in isolation.
  • CI/CD Optimization: Cache the Symfony kernel between tests to speed up execution:
    protected function createKernel(array $options = []): KernelInterface
    {
        $options['cache_dir'] = sys_get_temp_dir() . '/symfony_cache';
        return parent::createKernel($options);
    }
    

Gotchas and Tips

Pitfalls

  1. Kernel Caching Issues

    • Problem: Symfony’s cache may persist between test runs, causing stale configurations.
    • Fix: Clear the cache in setUp() or use a unique cache directory per test class:
      public function setUp(): void
      {
          $this->clearContainer();
          parent::setUp();
      }
      
  2. Bundle Autoloading

    • Problem: The bundle under test must be autoloaded (e.g., via composer dump-autoload).
    • Fix: Ensure your composer.json includes the bundle in autoload-dev:
      {
          "autoload-dev": {
              "psr-4": {
                  "My\\Bundle\\": "src/"
              }
          }
      }
      
  3. Environment Variables

    • Problem: .env.test may not be loaded by default.
    • Fix: Override createKernel() to load custom env files:
      protected function createKernel(array $options = []): KernelInterface
      {
          $options['env'] = 'test';
          $options['debug'] = true;
          return parent::createKernel($options);
      }
      
  4. Circular Dependencies

    • Problem: Testing bundles with circular dependencies may fail silently.
    • Fix: Use allow_circular_references: true in config/bundles.php (temporarily for testing).
  5. Database/Doctrine Tests

    • Problem: Doctrine entities may not be registered without a full Symfony setup.
    • Fix: Extend DoctrineTestCase or manually register entities in createKernel():
      $kernel = parent::createKernel($options);
      $kernel->getContainer()->get('doctrine')->getManager()->getConnection()->getDatabasePlatform();
      

Debugging Tips

  • Enable Debug Mode: Always set debug: true in createKernel() for verbose errors.
  • Inspect the Container: Dump the container to debug missing services:
    $this->assertArrayHasKey('missing_service', $this->getKernel()->getContainer()->getServiceIds());
    
  • Log Kernel Events: Subscribe to kernel events to trace bootstrapping:
    $this->getKernel()->getContainer()->get('event_dispatcher')->addListener(
        KernelEvents::REQUEST,
        function (RequestEvent $event) {
            error_log('Kernel request: ' . $event->getRequest()->getPathInfo());
        }
    );
    

Extension Points

  1. Custom Kernel Classes Override createKernel() to use a custom kernel class:

    protected function createKernel(array $options = []): KernelInterface
    {
        return new CustomTestKernel('test', true);
    }
    
  2. Pre-Bootstrap Hooks Add logic before kernel bootstrapping by overriding bootKernel():

    protected function bootKernel(): void
    {
        // Modify config or services before boot
        parent::bootKernel();
    }
    
  3. Post-Bootstrap Assertions Verify kernel state after bootstrapping:

    public function testKernelBoot()
    {
        $this->bootKernel();
        $this->assertSame('dev', $this->getKernel()->getEnvironment());
    }
    
  4. Shared Test Data Use setUp() to load fixtures or mock data:

    public function setUp(): void
    {
        $this->loadFixtures();
        parent::setUp();
    }
    
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.
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon
itsemon245/lamet
baks-dev/dashboard
amoifr/pickle-panther-bundle
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle