Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
D

doctrine-test-bundle

> 编程语言
Open source

Symfony bundle to isolate your app's doctrine database tests and improve the test performance

1.2K stars0 likes0 views
WebsiteGitHub

About

Symfony bundle to isolate your app's doctrine database tests and improve the test performance

What does it do? :blush:

This bundle provides features that help you run your Symfony-framework-based App's testsuite more efficiently with isolated tests.

It provides a StaticDriver that will wrap your originally configured Driver class (like DBAL\Driver\PDOMysql\Driver) and keeps a database connection statically in the current php process.

With the help of a PHPUnit extension class it will begin a transaction before every testcase and roll it back again after the test finished for all configured DBAL connections. This results in a performance boost as there is no need to rebuild the schema, import a backup SQL dump or re-insert fixtures before every testcase. As long as you avoid issuing DDL queries that might result in implicit transaction commits (Like ALTER TABLE, DROP TABLE etc; see https://wiki.postgresql.org/wiki/Transactional_DDL_in_PostgreSQL:_A_Competitive_Analysis) your tests will be isolated and all see the same database state.

It also includes a Psr6StaticArrayCache that will be automatically configured as meta data & query cache for all EntityManagers. This improved the speed and memory usage for my testsuites dramatically! This is especially beneficial if you have a lot of tests that boot kernels (like Controller tests or ContainerAware tests) and use Doctrine entities.

How to install and use this Bundle?

  1. install via composer
bash
composer require --dev dama/doctrine-test-bundle
  1. If you're not using Flex, enable the bundle by adding the class to bundles.php
php
 ['test' => true],
    //...
];
  1. Starting from version 8 **and only when using DBAL
…

php #[SkipDatabaseRollback] // this will skip it for all tests in a class public class MyTest extends \PHPUnit\Framework\TestCase {}

#[SkipDatabaseRollback] // also supported on (abstract) parent classes. So all tests in child classes will skip the rollback logic. abstract class MyAbstractTest extends \PHPUnit\Framework\TestCase {}

#[SkipDatabaseRollback] // this will skip it for only one test method public function MyTest() {}


#### Using the Bundle with Behat

Enable the extension in your Behat config (e.g. `behat.yml`)

```yaml
default:
   # ...
   extensions:
       DAMA\DoctrineTestBundle\Behat\ServiceContainer\DoctrineExtension: ~

That's it! From now on whatever changes you do to the database within each scenario are automatically rolled back for you.

Please note that this is only works if the tests are executed in the same process as Behat. This means it cannot work when using e.g. Selenium to call your application.

Configuration

The bundle exposes a configuration that looks like this by default:

yaml
dama_doctrine_test:
    enable_static_connection: true
    enable_static_meta_data_cache: true
    enable_static_query_cache: true

Setting enable_static_connection: true means it will enable it for all configured doctrine dbal connections.

You can selectively only enable it for some connections if required:

yaml
dama_doctrine_test:
    enable_static_connection:
        connection_a: true

Controlling how connections are kept statically in the current php process

By default, every configured doctrine DBAL connection will have its own driver connection that is managed in the current php process. In case you need to customize this behavior you can choose different "connection keys" that are used to select driver connections.

Example for 2 connections that will re-use the same driver connection instance:

yaml
doctrine:
    dbal:
        connections:
            default:
                url: '%database.url1%'

            foo:
                url: '%database.url2%'

dama_doctrine_test:
    connection_keys:
        # assigning the same key will result in the same internal driver connection being re-used for both DBAL connections
        default: custom_key
        foo: custom_key

Since v8.1.0: For connections with read/write replicas the bundle will use the same underlying driver connection by default for the primary and also for replicas. This addresses an issue where inconsistencies happened when reading/writing to different connections. This can also be customized as follows:

yaml
doctrine:
    dbal:
        connections:
            default:
                url: '%database.url%'
                replicas:
                    replica_one:
                        url: '%database.url_replica%'

dama_doctrine_test:
    connection_keys:
        # assigning different keys will result in separate internal driver connections being used for primary and replica
        default:
            primary: custom_key_primary
            replicas:
                replica_one: custom_key_replica

…

php
public function testMyTestCaseThatINeedToDebug()
{
    // ... something thats changes the DB state
    \DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver::commit();
    die;
    // now the DB changes are actually persisted and you can debug them
}

Troubleshooting

In case you are running (maybe without knowing it) queries during your tests that are implicitly committing any open transaction (see https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html for example) you might see an error like this:

Doctrine\DBAL\Driver\PDOException: SQLSTATE[42000]: Syntax error or access violation: 1305 SAVEPOINT DOCTRINE2_SAVEPOINT_2 does not exist

Currently there is no way for this bundle to work with those queries as they simply cannot be rolled back after the test case finished.

See also https://github.com/dmaicher/doctrine-test-bundle/issues/58

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPdoctrine2isolated-testsphpphpunit

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言