dmaicher / doctrine-test-bundle

Symfony bundle to isolate your app's doctrine database tests and improve the test performance
MIT License
1.08k stars 60 forks source link

Disable static connection but keep transactional behavior #187

Closed B-Galati closed 2 years ago

B-Galati commented 2 years ago

Hello!

Would it possible to disable static connection but still leverage the transactional behavior?

That may be not easy as it is certainly a BC-break of the behavior of StaticDriver.

My scenario is that I am changing the connection parameters just before running a test; so at runtime so the connection is not really changed behind the scene.

Cheers!

B-Galati commented 2 years ago

My current workaround is to call this kind of code in a PHPUnit hook:

class PHPUnitExtension implements BeforeTestHook
{
    public function executeBeforeTest(string $test): void
    {
        $this->resetDatabaseConnectionOfDamaTestBundle();
    }

    private function resetDatabaseConnectionOfDamaTestBundle(): void
    {
        static $reflectionProperty = null;

        if ($reflectionProperty === null) {
            $reflectionClass    = new \ReflectionClass(\DAMA\DoctrineTestBundle\Doctrine\DBAL\StaticDriver::class);
            $reflectionProperty = $reflectionClass->getProperty('connections');
            $reflectionProperty->setAccessible(true);
        }

        $reflectionProperty->setValue(StaticDriver::class, []);
    }
}
dmaicher commented 2 years ago

Hm that is quite a special case I guess. How exactly are you changing the connection parameters and why?

dmaicher commented 2 years ago

@B-Galati does this fix your issue by any chance?

https://github.com/dmaicher/doctrine-test-bundle/pull/189

B-Galati commented 2 years ago

@dmaicher yes it does! I had the same use case than @julienfalque ;-)

That's amazing! Thank you very much.

I still think that another parameter to differentiate static connection from transactional behavior would be more accurate in terms of semantic. But not sure it's useful :+1: