Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
gianiaz opened this issue Jun 13, 2024 · 3 comments
Open

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

gianiaz opened this issue Jun 13, 2024 · 3 comments

Comments

@gianiaz
Copy link

gianiaz commented Jun 13, 2024

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
Copy link
Owner

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
Copy link
Author

gianiaz commented Jun 17, 2024

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:

  • if a test does not use the entity manager the value will be 0
  • if a test uses the entity manager we ensure that the only active level is the one that will be rolled back by your bundle
  • if the value is > 1 then we had a begin transaction in our code that has not been rolled back or committed (for example for us an early exit prevented it).

@Jean85
Copy link
Contributor

Jean85 commented Jun 17, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants