auditor-bundle icon indicating copy to clipboard operation
auditor-bundle copied to clipboard

Auditor modifies doctrine connection between tests

Open Jabolek opened this issue 4 years ago • 0 comments

Q A
auditor-bundle version 4.0.3
PHP version 7.4.12
Database PostgreSQL

Summary

This issue occured after upgrading bundle from 3.* to 4.*. 3.x didn't have this issue.

When running multiple Symfony integration tests that use RefreshDatabaseTrait from https://github.com/hautelook/AliceBundle, the connection in Doctrine Registry becomes different that connection in EntityManager after execution of first test in a suite.

Current behavior

During the execution of below test in Symfony 5.2 / PHPUnit 9.5 project the second test fails.

<?php

declare(strict_types=1);

namespace App\Tests\Integration\Api;

use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\ORM\EntityManagerInterface;
use Hautelook\AliceBundle\PhpUnit\RefreshDatabaseTrait;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class DummyCaseTest extends WebTestCase
{
    use RefreshDatabaseTrait;

    protected Registry $registry;
    protected EntityManagerInterface $em;

    protected function setUp(): void
    {
        parent::setUp();

        $client = self::createClient();
        /** @var Registry $registry */
        $registry = $client->getContainer()->get('doctrine');
        $this->registry = $registry;
    }

    public function testSame(): void
    {
        $this->assertSame($this->registry->getConnection(), $this->registry->getManager()->getConnection());
    }

    public function testStillSame(): void
    {
        $this->assertSame($this->registry->getConnection(), $this->registry->getManager()->getConnection());
    }
}

RefreshDataBase trait is used to wrap everything in a transaction that can be roll-backed after the test, but this no longer works since only doctrine connection has transaction started after first test, but entity manager's connection doesn't have transaction started.

How to reproduce

Run test attached above.

Expected behavior

Doctrine and EntityManager should still use same connection even when running multiple tests.

Jabolek avatar Jan 07 '21 15:01 Jabolek