liip / LiipTestFixturesBundle

This bundles enables efficient loading of Doctrine fixtures in functional test-cases for Symfony applications
https://liip.ch
MIT License
169 stars 45 forks source link

PDOException: There is no active transaction #197

Open SophieBoewa opened 2 years ago

SophieBoewa commented 2 years ago

Hey,

When i try to load Fixtures, pdo throws the exception from above. I found a workaround using TransactionHelper from Doctrine, but therefore i have to edit the Doctrine EntityManager, and i don't think that this should be the way to go.

    public function setUp(): void
    {
        parent::setUp();
        $this->client = self::createClient([],[]);
        $this->databaseTool = $this->getContainer()->get(DatabaseToolCollection::class)->get();
        $this->executor = $this->databaseTool->loadFixtures($this->getFixtureClasses());
    }

using this code, i get the following stacktrace:

1) App\Tests\Controller\AppBundle\AnonymousRedirectCrawlTest::testAnonymousRedirected with data set #0 ('/', '/login') PDOException: There is no active transaction /var/www/app/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php:1854 /var/www/app/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:253 /var/www/app/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Executor/ORMExecutor.php:76 /var/www/app/vendor/liip/test-fixtures-bundle/src/Services/DatabaseTools/ORMDatabaseTool.php:130 /var/www/app/tests/Setup/AbstractCrawlTest.php:70

I'm using PHP 8.1.7, PHPUnit 8.5.27, liip/test-fixtures-bundle 2.4.0

Maybe you got a solution, on how to fix this Problem.

Ps. It's my first time, writing an Issue, so if sth. is missing, please tell me.

alexislefebvre commented 2 years ago

Thanks for the report.

Does it work if you call the container in another way? Like this:

$this->databaseTool = static::getContainer()->get(DatabaseToolCollection::class)->get();
SophieBoewa commented 2 years ago

Hey, sorry for the long wait.

I tried that too, the Error Message stays the same.

alexislefebvre commented 2 years ago

I can't reproduce this issue in a Symfony 5.4 project that run tests with PHPUnit 9.5 on PHP 7.4, 8.0 and 8.1, on a MySQL database.

Could you please share a reproducer? The Symfony doc explains how to do it. Please commit the minimal code to reproduce the issue, without forgetting the composer.json and composer.lock files.

Here is my test class:

full code ```php class ControllerTest extends WebTestCase { protected KernelBrowser $testClient; protected AbstractDatabaseTool $databaseTool; protected function setUp(): void { parent::setUp(); $this->testClient = static::createClient(); self::bootKernel(); $container = self::$kernel->getContainer(); /** @var DatabaseToolCollection $dbToolCollection */ $dbToolCollection = $container->get(DatabaseToolCollection::class); $this->databaseTool = $dbToolCollection->get(); } public function testLeaderboardsNumberOfBadgesNoUser(): void { $this->databaseTool->loadFixtures([]); $path = '/users/leaderboards/badges/'; $crawler = $this->testClient->request('GET', $path); $this->assertSame(404, $this->testClient->getResponse()->getStatusCode()); } protected function tearDown(): void { parent::tearDown(); unset($this->databaseTool); } } ```
alexislefebvre commented 2 years ago

I was able to reproduce this issue on a different and unrelated PR (#109):

Config Mysql (Liip\Acme\Tests\Test\ConfigMysql)
 ✘ Load fixtures and purge
   │
   │ PDOException: There is no active transaction
   │
   │ /home/runner/work/LiipTestFixturesBundle/LiipTestFixturesBundle/vendor/doctrine/dbal/src/Driver/PDO/Connection.php:123
   │ /home/runner/work/LiipTestFixturesBundle/LiipTestFixturesBundle/vendor/doctrine/dbal/src/Driver/Middleware/AbstractConnectionMiddleware.php:87
   │ /home/runner/work/LiipTestFixturesBundle/LiipTestFixturesBundle/vendor/doctrine/dbal/src/Logging/Connection.php:83
   │ /home/runner/work/LiipTestFixturesBundle/LiipTestFixturesBundle/vendor/doctrine/dbal/src/Driver/Middleware/AbstractConnectionMiddleware.php:87
   │ /home/runner/work/LiipTestFixturesBundle/LiipTestFixturesBundle/vendor/symfony/doctrine-bridge/Middleware/Debug/Connection.php:173
   │ /home/runner/work/LiipTestFixturesBundle/LiipTestFixturesBundle/vendor/doctrine/dbal/src/Connection.php:1420
   │ /home/runner/work/LiipTestFixturesBundle/LiipTestFixturesBundle/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:253
   │ /home/runner/work/LiipTestFixturesBundle/LiipTestFixturesBundle/vendor/doctrine/data-fixtures/lib/Doctrine/Common/DataFixtures/Executor/ORMExecutor.php:76
   │ /home/runner/work/LiipTestFixturesBundle/LiipTestFixturesBundle/src/Services/DatabaseTools/ORMDatabaseTool.php:127
   │ /home/runner/work/LiipTestFixturesBundle/LiipTestFixturesBundle/tests/Test/ConfigMysqlTest.php:277

full log (they may be deleted in the future)

The error only appear with PHP 8.0 and 8.1, so it may be due to different versions of dependencies. But the tests are randomized so we can't be sure.


It may be related to https://github.com/doctrine/DoctrineFixturesBundle/issues/348 and/or https://github.com/doctrine/DoctrineFixturesBundle/issues/363

SophieBoewa commented 2 years ago

Sorry i wasn't able to build a reproducer till this day, i'm trying to work on this now.

alexislefebvre commented 2 years ago

So it looks like the creation and deletion of the dabase causes this error, I didn't found a solution yet.

Please try this workaround: https://github.com/liip/LiipTestFixturesBundle/blob/2.x/doc/caveats.md#damadoctrinetestbundle

liip_test_fixtures:
    keep_database_and_schema: true
TymekB commented 2 years ago

Have you found the solution? I'm facing the same issue.

Lorenzschaef commented 1 year ago

Same here.

Using: PHP 8.2 mariadb 10.8.3. phpunit 9.6.3 symfony: 6.2 doctrine: 2.14 test-fixtures-bundle 2.6.0

    public function setUp(): void
    {
        parent::setUp();
        $this->client = static::createClient();
        $this->databaseTool = static::getContainer()->get(DatabaseToolCollection::class)->get();
    }

    public function testIndexAction(): void
    {
        $this->databaseTool->loadFixtures([Fixtures::class]);
        // ...
HealthyPizza commented 2 months ago

Issue still persist in 2024

PHP 8.3 Symfony: 6.4 Phpunit 9.6.20 Doctrine: 3.1 Doctrine/doctrine-fixtures-bundle : 3.6 Test-fixtures-bundle 3.1

alexislefebvre commented 2 months ago

I can't reproduce this error with dependencies like this:

{
    "[…]": "[…]",
    "require": {
        "php": ">=8.3",
        "[…]": "[…]",
        "doctrine/doctrine-bundle": "^2.13.0",
        "doctrine/doctrine-migrations-bundle": "^3.3.1",
        "doctrine/orm": "^3.2.2",
        "[…]": "[…]",
        "symfony/framework-bundle": "^6.4.12",
        "[…]": "[…]"
    },
    "require-dev": {
        "[…]": "[…]",
        "doctrine/doctrine-fixtures-bundle": "^3.6.1",
        "liip/test-fixtures-bundle": "^3.1.0",
        "phpunit/phpunit": "^11.3.6",
        "[…]": "[…]",
        "symfony/phpunit-bridge": "^6.4.11",
        "[…]": "[…]"
    },
    "[…]": "[…]",
}

With and without the workaround from https://github.com/liip/LiipTestFixturesBundle/issues/197#issuecomment-1207419742

If you can create a project to reproduce this issue, please check this: https://github.com/liip/LiipTestFixturesBundle/issues/197#issuecomment-1176536436

flytomek commented 1 month ago

Same issue here. However changing value of the keep_database_and_schema from false to true solved the issue.