Closed rubenrubiob closed 3 years ago
Are you able to provide a minimal reproducer that shows a failing test case?
I am trying to reproduce in a brand-new repository, but I am not able to get it to fail, it works well. Therefore, I think it is a problem with the set-up I had in my project. I will look into that to see if I find the issue. I will let you know if I find something weird, but I think the bundle works well 👍
I will close the issue, as it seems it works well.
Sorry for bothering you, and thank you for your time!
I stumbled on this, or something similar. I've found that the issue was that the container was inadvertently triggered very early, nested in a data provider, trying to get the EntityManager, which would spawn the connections too early, before the transaction started. This would result in stuff being flushed to DB.
I got this error when accidentally added both listener and extension to phpunit.xml.dist
. This caused rollback to call twice.
If the connection doesn't exists in AbstractStaticDriver::beginTransaction
, the transaction will be started here:
In case someone wants to try https://github.com/dmaicher/doctrine-test-bundle/pull/232
This changes the whole flow a bit to be compatible with the upcoming Doctrine DBAL v4. Maybe this issue is then also resolved :thinking:
I just ran into this (in v7.2.1
) and debugged extensively.
The issue is that self::$connections
is empty in \DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver::beginTransaction
.
This happens because self::$keepStaticConnections
is false
in \DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver::connect
.
This is unexpected because it should be set to true
in \DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension::executeBeforeFirstTest
.
This leads me to the conclusion that a database connection is made before executeBeforeFirstTest
can be called.
Dumping debug_backtrace()
in \StaticDriver::connect
finds the culprit:
I have tests that fetch entities from the database in a dataProvider
. These dataproviders are called before executeBeforeFirstTest
.
TLDR Don't open database connections in a dataProvider
.
FYI this is the reason why PHPUnit 10 deprecates having non-static data providers: to discourage having complex logic that reaches into the app under test and its services, since data providers are called very early in PHPUnit lifecycle.
Hi!
I am trying this bundle within a brand new Symfony project, in its 5.3 version and some customizations that I think are not relevant for this case. However, I am not able to make the bundle work.
I installed the bundle using Symfony Flex and I set up the extension in the
phpunit.xml.dist
file. I use PHPUnit standalone (phpunit/phpunit
):Hooks are actually called, but I think I am missing something, because I do not think they are called in the right order. My (simplified) test case is this one:
The bootstrap file is the one that comes with Symfony:
When I run my test and I inspect the calls using xDebug, I see this execution order:
\DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension::executeBeforeFirstTest
is called, which is correct.\DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension::executeBeforeTest
is called. This method calls\DAMA\DoctrineTestBundle\Doctrine\DBAL\AbstractStaticDriver::beginTransaction
, that starts the transaction on each connection defined. However, when I inspect the connections using xDebug, I see that there is no connection defined:ConnectionInterface
from the container, that\DAMA\DoctrineTestBundle\Doctrine\DBAL\AbstractStaticDriver::__construct
is called, creating the connection.\DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver::commit()
, I see there is a connection defined, that it is correct. However, as no transaction has been started, I get an error sayingThere is no active transaction
.The problem, as you can see, is that the transaction is started before the connection is created. Am I missing something?
I saw that in https://github.com/dmaicher/doctrine-test-bundle/blob/master/tests/bootstrap.php you boot the kernel and create a Connection to create the database. Maybe that is what I am missing, creating a Connection before any test execution, so the
StaticDriver
has the connection defined (even though you then shutdown the kernel)?I have checked the library internally, and searched through the issues of this repository, but I am not able to see the problem.
Just in case you need it, these are the require sections of my
composer.json
:Thank you!
Regards!