dmaicher / doctrine-test-bundle

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

How to check if a transaction has not been closed #302

Open gianiaz opened 1 month ago

gianiaz commented 1 month ago

Hi, we have just faced a bug in our application because we forgot to commit a transaction.

Our tests were green, and I'm wondering if the bundle should have warned us, or if at least should give us some sort of utility to assert that all beginTransaction has been followed with either a rollback or a commit.

Thank you

dmaicher commented 1 month ago

Indeed I'm aware of this issue although I never saw it happening in practice so far. I will see if I find the time to look into this.

gianiaz commented 1 month ago

Hi @dmaicher , don't know if this is the best way to achieve the result, but we put in an abstract teardown this assertion:

    protected function tearDown(): void
    {

        $em = $this->getContainer()->get(EntityManagerInterface::class);
        $connection = $em->getConnection();

        $this->assertLessThanOrEqual(
            1, 
            $connection->getTransactionNestingLevel(), 
            'Begin transaction not closed'
        );

        parent::tearDown();
    }

Assuming that:

Jean85 commented 1 month ago

A small suggestion: you shouldn't do that in the tearDown method, but in the assertPostConditions method, which is the correct one, since it's executed just after the test method, and (more importantly) before the test outcome is finalized.