Database Integration Tests with Doctrine (without a framework)

Written by Filis Futsarov
on July 20 of 2025

PHP Automated Tests Legacy Projects

Who said you need to use a full fledged framework to do integration tests?

WIP

As today, it has 29 million downloads!

https://github.com/dmaicher/doctrine-test-bundle/issues/318

https://packagist.org/packages/dama/doctrine-test-bundle https://github.com/dmaicher/doctrine-test-bundle

composer require dama/doctrine-test-bundle:^8.3 --dev
$config = \Doctrine\ORM\ORMSetup::createAttributeMetadataConfiguration(
    paths: [
        __DIR__ . '/../../PathToYour/Doctrine/Entities',
    ],
    isDevMode: true,
);

if ($isTesting) {
    $config->setMiddlewares([new \DAMA\DoctrineTestBundle\Doctrine\DBAL\Middleware()]);
    \DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver::setKeepStaticConnections(true);
}
$db = 'products';
if ($isTesting) {
    $db = 'products_test';
}

$connection = DriverManager::getConnection([
    'driver'   => 'pdo_pgsql',
    'host' => '10.5.0.5',
    'user'     => 'postgresql',
    'password' => 'postgresql',
    'dbname'   => $db,
    // this can be anything, but its required to be set
    'dama.connection_key' => 'test',
    'driverOptions' => [
        // in dev we don't want to wait much
        \PDO::ATTR_TIMEOUT => 1,
    ]
], $config);
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
    <!-- ...other stuff ... -->
    <extensions>
        <bootstrap class="DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension"/>
    </extensions>
</phpunit>

Special thanks to David Maicher (@dmaicher), the maintainer of the doctrine-test-bundle project, for helping me set this up.

Talk back!

Share thoughts, correct something or simply thank me for writing this post.