bespoke-support/symfony-test-application
Installation Add the package via Composer:
composer require --dev bespoke-support/symfony-test-application
Requires Symfony 5.4+ or 6.x.
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;
}
}
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).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'));
}
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);
}
Shared Configurations
Point to a pre-configured Symfony project (e.g., with config/packages/):
protected function getKernelProjectDir(): string
{
return __DIR__ . '/../../symfony-shared-config';
}
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());
}
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());
}
Symfony\Bundle\FrameworkBundle\Test\KernelTestCase for advanced features (e.g., client testing).--group or --exclude-group to run bundle tests in isolation.protected function createKernel(array $options = []): KernelInterface
{
$options['cache_dir'] = sys_get_temp_dir() . '/symfony_cache';
return parent::createKernel($options);
}
Kernel Caching Issues
setUp() or use a unique cache directory per test class:
public function setUp(): void
{
$this->clearContainer();
parent::setUp();
}
Bundle Autoloading
composer dump-autoload).composer.json includes the bundle in autoload-dev:
{
"autoload-dev": {
"psr-4": {
"My\\Bundle\\": "src/"
}
}
}
Environment Variables
.env.test may not be loaded by default.createKernel() to load custom env files:
protected function createKernel(array $options = []): KernelInterface
{
$options['env'] = 'test';
$options['debug'] = true;
return parent::createKernel($options);
}
Circular Dependencies
allow_circular_references: true in config/bundles.php (temporarily for testing).Database/Doctrine Tests
DoctrineTestCase or manually register entities in createKernel():
$kernel = parent::createKernel($options);
$kernel->getContainer()->get('doctrine')->getManager()->getConnection()->getDatabasePlatform();
debug: true in createKernel() for verbose errors.$this->assertArrayHasKey('missing_service', $this->getKernel()->getContainer()->getServiceIds());
$this->getKernel()->getContainer()->get('event_dispatcher')->addListener(
KernelEvents::REQUEST,
function (RequestEvent $event) {
error_log('Kernel request: ' . $event->getRequest()->getPathInfo());
}
);
Custom Kernel Classes
Override createKernel() to use a custom kernel class:
protected function createKernel(array $options = []): KernelInterface
{
return new CustomTestKernel('test', true);
}
Pre-Bootstrap Hooks
Add logic before kernel bootstrapping by overriding bootKernel():
protected function bootKernel(): void
{
// Modify config or services before boot
parent::bootKernel();
}
Post-Bootstrap Assertions Verify kernel state after bootstrapping:
public function testKernelBoot()
{
$this->bootKernel();
$this->assertSame('dev', $this->getKernel()->getEnvironment());
}
Shared Test Data
Use setUp() to load fixtures or mock data:
public function setUp(): void
{
$this->loadFixtures();
parent::setUp();
}
How can I help you explore Laravel packages today?