Installation
Add the bundle to your composer.json:
composer require culabs/testing-bundle
Enable the bundle in config/bundles.php:
return [
// ...
Culabs\TestingBundle\CulabsTestingBundle::class => ['all' => true],
];
First Use Case
The bundle provides a base TestCase class for Symfony applications. Extend it in your test class:
use Culabs\TestingBundle\Test\WebTestCase;
class MyTest extends WebTestCase
{
public function testExample()
{
$client = static::createClient();
$client->request('GET', '/');
$this->assertEquals(200, $client->getResponse()->getStatusCode());
}
}
Where to Look First
Culabs\TestingBundle\Test\WebTestCase for built-in methods and overrides.config/packages/culabs_testing.yaml (if provided) for customizable settings.Base TestCase Extension
Extend WebTestCase for all functional tests to inherit common setup/teardown logic:
class ApiTest extends WebTestCase
{
protected function setUp(): void
{
parent::setUp();
// Custom setup (e.g., load fixtures)
}
}
Client Management
Reuse the createClient() method for API/HTTP tests:
public function testLogin()
{
$client = static::createClient();
$client->request('POST', '/api/login', [
'json' => ['email' => 'test@example.com', 'password' => 'password']
]);
$this->assertJson($client->getResponse()->getContent());
}
Assertion Helpers Use built-in assertions (if any) or integrate with PHPUnit/Symfony’s assertions:
$this->assertResponseIsSuccessful();
$this->assertJsonContains(['status' => 'success']);
Database Transactions
Leverage Symfony’s test database isolation (enabled by default in WebTestCase):
// No need for manual rollback; transactions auto-commit after tests.
Service Container Access
Access services in tests via static::$container:
$mailer = static::$container->get('mailer');
Feature Testing Workflow
WebTestCase.createClient() for HTTP interactions.Integration with Fixtures
Load fixtures in setUp():
protected function setUp(): void
{
parent::setUp();
$this->loadFixtures([UserFixtures::class]);
}
Mocking Services Override services in tests:
protected function getKernelClass()
{
return Kernel::class;
}
protected function createKernel()
{
$kernel = parent::createKernel();
$kernel->getContainer()->set('my_service', $this->createMock(MyService::class));
return $kernel;
}
Combine with Other Bundles
Custom Assertions Add reusable assertions to a trait:
trait Assertions
{
protected function assertJsonPath($path, $expected)
{
$json = json_decode($this->getJsonResponseContent(), true);
$this->assertArrayHasKey($path, $json, "JSON path '$path' not found");
$this->assertEquals($expected, $json[$path]);
}
}
Parallel Testing
Configure PHPUnit for parallel execution in phpunit.xml.dist:
<phpunit>
<extensions>
<extension class="Parallel\Tests\ParallelExtension" />
</extensions>
</phpunit>
Lack of Documentation
src/Test/WebTestCase.php) for behavior.Symfony 2 vs. 5+ Compatibility
No Built-in Assertions
Database Isolation
setUp()/tearDown() to reset state.Service Container Access
static::$container bypasses dependency injection and may cause issues in some contexts.self::$kernel->getContainer() or inject dependencies via constructor.Kernel Boot Issues
KernelException, ensure the bundle is enabled in bundles.php and dependencies are installed.php bin/console debug:container to check for missing services.Client Configuration
$client = static::createClient(['environment' => 'test']);
$this->assertEquals('test', $client->getKernel()->getEnvironment());
Fixture Loading
config/packages/dev/doctrine.yaml:
doctrine:
dbal:
logging: true
profiling: true
No Configuration File
culabs_testing.yaml config file by default.config/packages/culabs_testing.yaml if needed:
culabs_testing:
default_locale: en
# Custom settings (if supported)
Environment-Specific Settings
test vs. dev).createClient(['environment' => 'test']) explicitly to avoid surprises.Custom TestCase
Override WebTestCase to add shared logic:
class CustomTestCase extends WebTestCase
{
protected function createClient(array $options = [], array $server = [])
{
$options['debug'] = true; // Enable debug toolbar for all tests
return parent::createClient($options, $server);
}
}
Event Listeners Attach listeners to test events (if the bundle supports them):
// Example: Listen to kernel.events (hypothetical)
$dispatcher = static::$kernel->getContainer()->get('event_dispatcher');
$dispatcher->addListener(KernelEvents::REQUEST, function () {
// Pre-request logic
});
Fixtures Integration
Extend fixture loading in setUp():
protected function loadFixtures(array $fixtures)
{
$loader = static::$kernel->getContainer()->get('doctrine.fixtures.loader');
foreach ($fixtures as $fixture) {
$loader->load($fixture);
}
}
API Testing Helpers Create a trait for common API test patterns:
trait ApiTestTrait
{
protected function assertApiSuccess($response)
{
$this->assertEquals(200, $response->getStatusCode());
$this->assertJson($response->getContent());
}
protected function assertApiError($response, $status = 400)
{
$this->assertEquals($status, $response->getStatusCode());
$this->assertJson($response
How can I help you explore Laravel packages today?