aureja/test-framework-bundle
Installation:
composer require aureja/test-framework-bundle "dev-master"
Ensure your composer.json meets the package's PHP/Symfony version requirements (PHP ≥5.5, Symfony 2.7/3.0).
Register the Bundle:
Add to AppKernel.php (Symfony 2.x) or config/bundles.php (Symfony 3.x+):
new Aureja\Bundle\TestFrameworkBundle\AurejaTestFrameworkBundle(),
First Test Case:
Extend Aureja\Bundle\TestFrameworkBundle\Test\WebTestCase and annotate with @dbIsolation or @dbIsolationPerTest:
use Aureja\Bundle\TestFrameworkBundle\Test\WebTestCase;
/**
* @dbIsolationPerTest
*/
class UserTest extends WebTestCase
{
protected function setUp()
{
$this->initClient(); // Required for Symfony's WebTestCase
}
public function testUserCreation()
{
// Test logic here
}
}
Run Tests:
phpunit
Database Isolation for Functional Tests:
@dbIsolation to share a database schema across all tests in a class (faster, but tests may interfere).@dbIsolationPerTest to reset the database schema before each test (slower, but isolated state).Workflow:
setUp() to initialize the client (Symfony's initClient()) and load fixtures if needed./**
* @dbIsolation
*/
class SharedDbTest extends WebTestCase { ... }
/**
* @dbIsolationPerTest
*/
class IsolatedDbTest extends WebTestCase { ... }
Example with Fixtures:
protected function setUp()
{
$this->initClient();
$this->loadFixtures([
'AppBundle/DataFixtures/ORM/LoadUserData.php',
]);
}
Client Initialization:
Always call $this->initClient() in setUp() to ensure Symfony's test client is ready for HTTP requests.
Doctrine Integration:
Leverage Doctrine's EntityManager for database operations:
public function testEntityPersistence()
{
$em = $this->getContainer()->get('doctrine')->getManager();
$user = new User();
$em->persist($user);
$em->flush();
// Assertions...
}
Extend WebTestCase for reusable test logic:
abstract class BaseTest extends WebTestCase
{
protected function setUp()
{
$this->initClient();
$this->enableProfiler(); // Optional: For debugging
}
protected function enableProfiler()
{
if (!$this->client->getProfile()) {
$this->client->enableProfiler();
}
}
}
Usage:
class ApiTest extends BaseTest { ... }
Environment-Specific Config:
Override database settings in phpunit.xml or .env.testing:
<!-- phpunit.xml -->
<env name="DATABASE_URL" value="sqlite:///:memory:" />
Bundle Configuration:
The bundle does not expose public configuration options, but you can override Doctrine's test configuration via config/packages/test/doctrine.yaml:
doctrine:
dbal:
url: '%env(DATABASE_URL)%'
# Other DBAL config...
Missing initClient():
$this->initClient() in setUp() will cause HTTP-related tests to fail with Client not initialized errors.Database Isolation Overhead:
@dbIsolationPerTest resets the database before each test, which can slow down test suites. Use @dbIsolation for faster tests when isolation isn’t critical.Symfony 3.x+ Compatibility:
AppKernel.php (e.g., registerBundles() vs. bundles.php).Doctrine Migrations:
setUp():
$this->runMigrations();
(Note: The bundle does not provide this functionality; implement custom logic if needed.)Database State:
SchemaTool to inspect the database schema:
$schemaTool = new \Doctrine\ORM\Tools\SchemaTool($this->getEntityManager());
$schema = $schemaTool->getSchema();
Profiler:
setUp() to debug HTTP requests:
$this->client->enableProfiler();
$this->client->request('GET', '/');
$this->client->getProfile()->getCollector('db')->getData();
Logs:
phpunit.xml:
<php>
<env name="DOCTRINE_LOGGING" value="1" />
</php>
Custom Database Isolation:
WebTestCase and implementing your own setUp()/tearDown():
class CustomTest extends WebTestCase
{
protected function setUp()
{
$this->initClient();
$this->customDbSetup();
}
private function customDbSetup()
{
// Custom logic (e.g., seed data, truncate tables)
}
}
Event Listeners:
$em->getEventManager()->addEventListener(
\Doctrine\ORM\Events::onFlush,
function () { /* Pre-flush logic */ }
);
Fixtures:
Hautelook/AliceBundle for complex fixtures:
$this->loadFixtures([
new LoadUserData(),
new LoadProductData(),
]);
@dbIsolation.@dbIsolation for unit tests that don’t require a full database reset.How can I help you explore Laravel packages today?