zenstruck / foundry

A model factory library for creating expressive, auto-completable, on-demand dev/test fixtures with Symfony and Doctrine.
https://symfony.com/bundles/ZenstruckFoundryBundle/current/index.html
MIT License
643 stars 70 forks source link

[2.0] ResetDatabase with DamaDoctrineTestBundle and symfony/doctrine-messenger library #658

Open maloy1984 opened 3 months ago

maloy1984 commented 3 months ago

After migrate to version 2.0. My test was broken, because database not refresh before each test. I have warning

Exception in third-party event subscriber: There is already an active transaction

#0 /var/www/vendor/doctrine/dbal/src/Driver/PDO/Connection.php(119): Doctrine\DBAL\Driver\PDO\PDOException::new(Object(PDOException))

#1 /var/www/vendor/dama/doctrine-test-bundle/src/DAMA/DoctrineTestBundle/Doctrine/DBAL/StaticDriver.php(60): Doctrine\DBAL\Driver\PDO\Connection->beginTransaction()

#2 /var/www/vendor/dama/doctrine-test-bundle/src/DAMA/DoctrineTestBundle/PHPUnit/PHPUnitExtension.php(57): DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver::beginTransaction()

My stack

After debug, I found problem (I think so) In function PersistenceManager::resetDatabase

line 97 - 102


if ($isDAMADoctrineTestBundleEnabled) {
    // re-enable static connections
     StaticDriver::setKeepStaticConnections(true);
}

$shutdownKernel();

In function $shutdownKernel call KernelTestCase::ensureKernelShutdown And in KernelTestCase::ensureKernelShutdown exist code

if ($container instanceof ResetInterface) {
    $container->reset();
}

And if install library symfony/doctrine-messenger for doctrine transport In this reset execute query $this->executeStatement(sprintf('UNLISTEN "%s"', $this->configuration['table_name']));

But static connection in DAMADoctrineTestBundle already is true.

If in PersistenceManager::resetDatabase first call $shutdownKernel(); and then set static connection, all works fine.

Siniliote commented 2 months ago

I have same problem. I remove symfony/doctrine-messenger and go use RabbitMq ;)