Installation:
composer require-dev braincrafted/testing-bundle
Ensure you’re using a version compatible with your Symfony version (e.g., v0.3.* for Symfony 2.4–2.5).
Enable the Bundle:
Add to app/AppKernel.php:
$bundles[] = new Braincrafted\TestingBundle\BraincraftedTestingBundle();
First Use Case:
Extend WebTestCase in your test class:
use Braincrafted\TestingBundle\Tests\WebTestCase;
class MyTest extends WebTestCase
{
public function testExample()
{
$client = static::createClient();
$client->request('GET', '/');
$this->assertTrue($client->getResponse()->isSuccessful());
}
}
Key Features:
src/Acme/DemoBundle/DataFixtures/ (or configured path).Test Class Structure:
Extend WebTestCase for all functional tests requiring a fresh database state:
abstract class FunctionalTestCase extends WebTestCase
{
// Shared setup/teardown logic
}
Fixture Loading:
src/{Bundle}/DataFixtures/{ORM|MongoDB}/.UserFixture.php):
namespace Acme\DemoBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
class UserFixture implements FixtureInterface
{
public function load(ObjectManager $manager)
{
$user = new User();
$user->setEmail('test@example.com');
$manager->persist($user);
$manager->flush();
}
}
Database Isolation:
Client Management:
public function testLogin()
{
$client = static::createClient();
$client->request('POST', '/login', ['email' => 'test@example.com']);
$this->assertTrue($client->getCookieJar()->has('PHPSESSID'));
}
public function testDashboard()
{
$client = static::createClient(); // New client (fresh session)
$client->request('GET', '/dashboard');
// ...
}
Integration with Other Tools:
@depends for test dependencies:
public function testCreatePost()
{
// ...
}
/**
* @depends testCreatePost
*/
public function testListPosts()
{
$client = static::createClient();
$client->request('GET', '/posts');
// ...
}
Fixture Loading Order:
OrderedFixtureInterface or @Order annotations to control order:
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
class UserFixture implements FixtureInterface, OrderedFixtureInterface
{
public function getOrder()
{
return 1; // Load before other fixtures
}
}
Performance Overhead:
WebTestCase but override setUp() to skip schema recreation:
protected function setUp()
{
// Skip schema recreation for this test class
$this->skipSchemaRecreation = true;
parent::setUp();
}
Missing Fixtures Directory:
src/{Bundle}/DataFixtures/. If missing, tests will fail silently. Ensure the directory exists or configure a custom path in config.yml:
braincrafted_testing:
fixtures_dir: '%kernel.root_dir%/../src/Acme/DemoBundle/DataFixtures'
DoctrineFixturesBundle Dependency:
doctrine/doctrine-fixtures-bundle. Add it to composer.json if missing:
composer require --dev doctrine/doctrine-fixtures-bundle
Symfony 3/4+ Compatibility:
liip/functional-test-bundleKernelTestCase with custom setup.Schema Recreation Failures:
var/log/test.log. Common causes:
.env.test.DOCTRINE_ORM_SCHEMA_FILTER in .env.test to exclude problematic tables).Fixture Loading Issues:
# config_test.yml
doctrine:
orm:
filters:
schema_filter: ~
var/log/test.log for fixture-related errors.Client State Leaks:
public function testA()
{
$client = static::createClient();
// ...
}
public function testB()
{
$client = static::createClient(); // Fresh client
// ...
}
Custom Fixture Directories:
Override the fixture directory in config_test.yml:
braincrafted_testing:
fixtures_dir: ['%kernel.root_dir%/../data/fixtures', '%kernel.root_dir%/../src/Acme/DemoBundle/DataFixtures']
Skip Schema Recreation: Disable schema recreation for specific test classes:
class FastTest extends WebTestCase
{
protected function setUp()
{
$this->skipSchemaRecreation = true;
parent::setUp();
}
}
Add Custom Services:
Override getClientOptions() to inject test-specific services:
protected function getClientOptions()
{
$options = parent::getClientOptions();
$options['debug'] = true;
$options['parameters'] = [
'kernel.test' => true,
'custom_service' => $this->createTestService(),
];
return $options;
}
How can I help you explore Laravel packages today?