dama/doctrine-test-bundle
Symfony test bundle that speeds up Doctrine tests by reusing static DBAL connections and wrapping each test in a transaction that’s rolled back for isolation. Also provides a PSR-6 static array cache for Doctrine metadata/query caching to reduce boot time and memory.
Installation:
composer require --dev dama/doctrine-test-bundle
Enable the Bundle (in config/bundles.php):
DAMA\DoctrineTestBundle\DAMADoctrineTestBundle::class => ['test' => true],
Configure PHPUnit (phpunit.xml):
<extensions>
<bootstrap class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension" />
</extensions>
Verify DBAL Configuration (if using DBAL < 4):
# config/packages/doctrine.yaml
doctrine:
dbal:
connections:
default:
use_savepoints: true
Run a test that interacts with Doctrine (e.g., WebTestCase or KernelTestCase). The bundle will:
Example:
// src/Tests/Entity/UserTest.php
public function testUserCreation(): void
{
$user = new User();
$user->setName('Test User');
$entityManager = self::getContainer()->get('doctrine')->getManager();
$entityManager->persist($user);
$entityManager->flush();
$this->assertEquals(1, $entityManager->getRepository(User::class)->count([]));
// Changes rolled back automatically after test
}
Transactional Tests:
#[SkipDatabaseRollback] to exclude specific tests/classes from rollback:
#[SkipDatabaseRollback]
public function testDebugState(): void
{
// Changes persist after test
}
Debugging Failed Tests:
public function testDebugState(): void
{
// ... test logic
\DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver::commit();
die; // Inspect DB state
}
Custom Connection Keys (for read/write replicas):
dama_doctrine_test:
connection_keys:
default:
primary: primary_key
replicas:
replica_one: replica_key
Disabling Features:
dama_doctrine_test:
enable_static_connection: false
enable_static_query_cache: false
doctrine/doctrine-fixtures-bundle with load() in setUp() (changes rolled back automatically).behat.yml:
extensions:
DAMA\DoctrineTestBundle\Behat\ServiceContainer\DoctrineExtension: ~
dama_doctrine_test:
enable_static_meta_data_cache: false
Implicit Transactions:
ALTER TABLE, DROP TABLE, or CREATE INDEX may commit transactions implicitly. Error:
PDOException: SAVEPOINT DOCTRINE2_SAVEPOINT_X does not exist
#[SkipDatabaseRollback].Replica Inconsistencies:
connection_keys if needed:
dama_doctrine_test:
connection_keys:
default:
primary: primary_key
replicas:
replica_one: replica_key
PHPUnit Version:
SYMFONY_PHPUNIT_VERSION to enforce version:
SYMFONY_PHPUNIT_VERSION=12 php bin/phpunit
Transaction State: Check active savepoints with:
$connection = $entityManager->getConnection();
$connection->getWrappedConnection()->getTransactionNestingLevel();
Cache Issues: Clear static caches manually if tests behave unexpectedly:
\DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver::clearStaticConnections();
Custom Middleware: Register middleware with higher priority:
# config/services.yaml
services:
App\Doctrine\TestMiddleware:
tags: [doctrine.middleware]
Override StaticDriver:
Extend StaticDriver to customize behavior (e.g., logging):
class CustomStaticDriver extends \DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver
{
public function beginTransaction(): void
{
\Log::info('Transaction started');
parent::beginTransaction();
}
}
Register it in config/packages/doctrine_test.yaml:
dama_doctrine_test:
static_driver_class: App\Doctrine\CustomStaticDriver
dama_doctrine_test:
enable_static_connection: true
enable_static_meta_data_cache: true
enable_static_query_cache: true
dama_doctrine_test:
enable_static_query_cache: false
#[SkipDatabaseRollback] for integration tests that require persisted state.How can I help you explore Laravel packages today?