dmaicher/doctrine-test-bundle

Not working on multiple connections

jmariller opened this issue Β· 11 comments

Hi @dmaicher ,

First of all thanks a lot for this bundle, I have a project with a single connection where it works perfectly and it helps us work without worrying about DB conflicts πŸ™‚

Now I am trying to use the bundle on another project which has multiple connections, and although I do not see/get any error message data is not rolled back after each test. I have properly set use_savepoints on all connections as I am using dbal 3.8.

Do you have any idea why this is not working? Let me know which Information could help you, as I am not able to share a repository since this is a big project.

Many thanks in advance for your support.

I'm using this bundle myself on a project with 2 connections and it works fine.

Can you reproduce the issue on a small project and provide a repository?

Closing this for now. Let me know in case you can provide a reproducer.

Hi @dmaicher so I did a few more tests and realized it has nothing to do with the multiple connections: when I keep only the default connection and try again, the same behaviour occurs. I even tried to copy the content of doctrine.yaml from that project mentioned above where it works to the one where it doesn't, without any success. How could I debug this further, since I do not get any error message? Thank you πŸ™

I would start by checking if the connection is kept inside DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver::$connections and what exactly happens there on rollBack() and if its called by the phpunit extension

Thanks for your help & patience @dmaicher so I did as you proposed and found out that $connections is actually empty! The rollback method is indeed called however when reaching the for loop around connections it does nothing because of the empty array πŸ€” any further idea what could be the cause?

Then maybe check next why StaticDriver::$connections is not populated. Is DAMA\DoctrineTestBundle\Doctrine\DBAL\Middleware registered and invoked? So is the StaticDriver used?

The Middleware seems to be properly registered and invoked (and so is the StaticDriver used, got it confirmed via some var_dumps), however the method StaticDriver::connect is never called πŸ˜• Here is the dummy test I am working with:

<?php

namespace App\Tests\Entity;

use App\Entity\Brand;
use Faker\Factory;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;

class BrandTest extends KernelTestCase
{
  public function testCreateBrand(): void
  {
    $faker = Factory::create();

    $brand = (new Brand())
        ->setName($faker->regexify('[A-Z]{10}'))
        ->setSlug($faker->slug())
    ;

    $manager = static::getContainer()->get('doctrine')->getManager();

    $manager->persist($brand);
    $manager->flush();
  }
}

Then I'm afraid you need to find out why StaticDriver::connect is not called

Hello @dmaicher, I think there was a regression of some kind between v8.0.2 and v8.1.0.

Today I realized that rollbacks were not working after a composer update. I could boil it down to the fact that with composer require --dev dama/doctrine-test-bundle:8.0.2 (as before) everything works great and with composer require --dev dama/doctrine-test-bundle:8.1.0 it does not. Nothing else changed in the project.

As you suggested before here I checked if $connections is populated. I placed a simple dump(self::$connections); in \DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver::rollBack(). With both versions the function is being called but with 8.0.2 it dumps my PDO/sqlite connection and with 8.1.0 it dumps an empty array.

With 8.0.2

Created database C:\Users\xxxxxx/var/test.sqlite for connection named default
   > purging database
   > loading App\DataFixtures\AutomationFixtures
PHPUnit 10.5.17 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.1.12
Configuration: C:\Users\xxxxxx\phpunit.xml.dist

^ array:1 [
  "bc2d4712f1bec316cdae0308c635afa23f97xxxx" => Doctrine\DBAL\Driver\PDO\Connection^ {#1007
    -connection: PDO {#1006
      inTransaction: true
      attributes: {
        CASE: NATURAL
        ERRMODE: EXCEPTION
        PERSISTENT: false
        DRIVER_NAME: "sqlite"
        ORACLE_NULLS: NATURAL
        CLIENT_VERSION: "3.36.0"
        SERVER_VERSION: "3.36.0"
        STATEMENT_CLASS: array:1 [
          0 => "PDOStatement"
        ]
        DEFAULT_FETCH_MODE: BOTH
      }
    }
  }
]
.                                                                   1 / 1 (100%)

With 8.1.0

Same but just dumping []

StaticDriver::connect is called and returns with the first statement:

        if (!self::isKeepStaticConnections() || !isset($params['dama.connection_key'])) {
            return parent::connect($params);
        }

⚠️This is not the case with 8.0.2 were it runs to the end of connect() and returns a new StaticConnection($connection, $platform)

@SirTomme can you please open a new issue? also with more details on the Doctrine DBAL config or how to reproduce that issue please.