Installation
composer require draw/tester-bundle
Add the bundle to config/bundles.php:
return [
// ...
Draw\Bundle\TesterBundle\TesterBundle::class => ['test' => true],
];
First Use Case: Kernel Event Dispatcher Validation
Symfony\Bundle\FrameworkBundle\Test\KernelTestCase.EventDispatcherTesterTrait to validate event listeners.event_dispatcher.xml fixture.// tests/EventDispatcherTest.php
namespace App\Tests;
use Draw\Bundle\TesterBundle\EventDispatcher\EventDispatcherTesterTrait;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class EventDispatcherTest extends KernelTestCase
{
use EventDispatcherTesterTrait;
public function testEventDispatcherConfiguration(): void
{
$this->assertEventDispatcherConfiguration(
__DIR__.'/fixtures/event_dispatcher.xml'
);
}
}
Run the Test
php bin/phpunit tests/EventDispatcherTest
event_dispatcher.xml in your fixtures directory.Event Dispatcher Validation
EventDispatcherTesterTrait to assert that all event listeners/subcribers are correctly registered.$this->assertEventDispatcherConfiguration(
__DIR__.'/fixtures/event_dispatcher.xml',
'event_dispatcher' // Optional: Custom service ID if not using default
);
Service Container Validation (Future-Proofing)
ServiceContainerTesterTrait).KernelTestCase and use provided traits to assert service configurations.use Draw\Bundle\TesterBundle\ServiceContainer\ServiceContainerTesterTrait;
class ServiceTest extends KernelTestCase
{
use ServiceContainerTesterTrait;
public function testServiceConfiguration(): void
{
$this->assertServiceConfiguration(
__DIR__.'/fixtures/services.xml'
);
}
}
Integration with Custom Test Suites
post-merge Git hook to catch misconfigurations early.Fixtures Management
event_dispatcher.xml) in tests/fixtures/ or a dedicated config/ directory..gitignore for intermediate files (e.g., event_dispatcher.tmp.xml) but commit the finalized fixtures.Customizing Assertions
assertEventDispatcherConfiguration method to add custom validation logic (e.g., ignore specific listeners).protected function assertEventDispatcherConfiguration(string $fixturePath): void
{
$this->ignoreListeners(['kernel.request', 'some.ignored.listener']);
parent::assertEventDispatcherConfiguration($fixturePath);
}
Debugging
debug:event-dispatcher command to manually inspect the current state:
php bin/console debug:event-dispatcher
Fixture Drift
.git/hooks/pre-commit):
#!/bin/sh
php bin/phpunit --testdox-html=coverage.xml tests/EventDispatcherTest
if [ $? -ne 0 ]; then
echo "Event dispatcher test failed! Update fixtures or fix configuration."
exit 1
fi
Service ID Mismatches
assertEventDispatcherConfiguration.'event_dispatcher', but verify the correct ID with:
php bin/console debug:container | grep event.dispatcher
Performance Overhead
Manual Fixture Generation
php bin/console debug:event-dispatcher > event_dispatcher.xml
diff or a visual diff tool.Selective Testing
SecurityBundle, DoctrineBundle).public function testSecurityEventListeners(): void
{
$this->assertEventDispatcherConfiguration(
__DIR__.'/fixtures/security_events.xml',
'event_dispatcher'
);
}
Extension Points
CommandTesterTrait).// src/EventDispatcher/CommandTesterTrait.php (hypothetical)
trait CommandTesterTrait
{
public function assertCommandConfiguration(string $fixturePath): void
{
// Logic to validate commands...
}
}
Bundle Loading Order
TesterBundle is loaded after your application bundles in config/bundles.php (Symfony 4+).return [
// ...
App\SomeBundle\SomeBundle::class => ['all' => true],
Draw\Bundle\TesterBundle\TesterBundle::class => ['test' => true],
];
KernelTestCase Requirements
StaticTestCase or non-kernel tests.// ❌ Wrong: StaticTestCase cannot access the kernel
use Symfony\Bundle\FrameworkBundle\Test\StaticTestCase;
XML Fixture Format
<event-dispatcher>
<listeners>
<listener event="kernel.request" method="onKernelRequest" service="app.event_listener"/>
</listeners>
<subscribers>
<subscriber service="app.event_subscriber"/>
</subscribers>
</event-dispatcher>
How can I help you explore Laravel packages today?