foundry icon indicating copy to clipboard operation
foundry copied to clipboard

Stuck between DAMA and FOUNDRY/RESTDATABASE

Open DigitalTimK opened this issue 3 years ago • 5 comments

Good Morning!

Epilogue

To be honest, maybe I am doing something totally wrong, since my issue is a common use-case, but it seems that I am the only one having this issue.

Environment

  • Symfony 5.4
  • PHPunit is using DAMA\DoctrineTestBundle\PHPUnit\PHPUnitExtension-extension
  • Functional tests are using trait Zenstruck\Foundry\Test\ResetDatabase
  • MariaDB

Use Case

  • Functional test (PantherTestCase) of a simple contact form (name, email-address, message) on a website
  • The controller treats the submitted form and is checking if email-address is already known to allocate the message to that existing person-object
  • So this is why the functional test creates a person-object ($person = PersonFactory::createOne(['email' => '[email protected]']);) before the form is filled and submitted.

Problem 1

Once the form is submitted by the test it gets treated by the controller, but the Person-object is not existing anymore (the entire database is empty). The database seems be rolled back by DAMA.

Solution 1

According to the proposal here, I added this to the above mentioned function test-class.

    public static function setUpBeforeClass(): void
    {
        parent::setUpBeforeClass();
        StaticDriver::setKeepStaticConnections(false);
    }

    public static function tearDownAfterClass(): void
    {
        StaticDriver::setKeepStaticConnections(true);
        parent::tearDownAfterClass();
    }

Problem 2

Solution 1 is working perfectly. But only if that test is executed in isolation. Once the full test-set is launched it explodes for subsequent tests with:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'something' for key 'PRIMARY'

So it seems that RESTDATABASE is not executed anymore from that test-class and on ...

Solution 2

--> I have no idea how to solve this. Any idea? Or is this a bug?

Many thanks for sharing your thoughts!

Cheers Tim!

DigitalTimK avatar Feb 05 '22 08:02 DigitalTimK

Hey @DigitalTimK,

I believe I understand the problem but I don't know there is a great solution. The logic to "change how reset database" resets the database when Dama is enabled assumes you are running it for your entire suite.

To clarify, does your test suite run successfully if Dama is disabled?

For you app, what about grouping the panther tests together and running them as a separate run with Dama disabled? It would require a second phpunit.xml which is unfortunate.

For a proper solution, I think we may have to wait until PHPUnit 10's event system as suggested here, and for DAMA to utilize these events.

kbond avatar Feb 08 '22 19:02 kbond

Hello @kbond,

thanks for your response. I am not the best friend of the second phpunit.xml workaround. But as it isn't that urgent currently I will hope and wait for PHPUnit 10 ;-)

But what makes me really wonder is that I am the first coming up with the issue. Nobody doing functional testing?

DigitalTimK avatar Feb 26 '22 07:02 DigitalTimK

But what makes me really wonder is that I am the first coming up with the issue. Nobody doing functional testing?

To clarify, this is a problem if using Foundry, DAMA, and Panther together. I suspect it must be rare to use all three in the same suite? My functional tests use Foundry, DAMA and the standard kernel browser (no panther).

kbond avatar Feb 27 '22 13:02 kbond

Hello @kbond ,

I'm having this problem too, I need Panther to test an app that uses Symfony UX, so lots of javascript.

Is there an approach you recommend to take advantage of fixtures with Panther?

Cafeine42 avatar Oct 18 '22 08:10 Cafeine42

Hi @Cafeine42, at this point, you'll need to create a second panther-specific phpunit.xml that doesn't have DAMA enabled.

phpunit.xml: integration/unit tests with DAMA enabled phpunit-panther.xml: e2e/panther tests with DAMA disabled

You'll have to run two test suites:

phpunit -c phpunit.xml # unit/integration tests
phpunit -c phpunit-panther.xml #e2e/panther tests

kbond avatar Oct 18 '22 13:10 kbond