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

Unknow database/table doesn't exist errors #170

Closed redecs closed 3 years ago

redecs commented 3 years ago

Hi,

I wanted to use this bundle for testing on a new project with PHP 8, MySQL 8 and Symfony 5.3. Followed the setup instructions in the Readme (but skipped the outdated instructions, like updating AppKernel.php since, the bundle now into bundles.php and that's handled by Flex).

I encountered the issue with a simple functional test (WebTestCase) that checks a URL with a parameter that tries to perform a SELECT id FROM user WHERE param = ? query behind the scenes with that parameter, so nothing fancy.

The specific error I encountered was Table 'app_test.user' doesn't exist, but that's because my MySQL docker container is setup to create app and app_test databases on the first start.

For someone else with a standard setup that error would have been: Unknown database 'app_test'.

My guess is that is caused by the changes in the Flex setup for Doctrine Bundle 2.4 that now append the _test suffix for the database name that test are ran against. Since the test are against a different database, not the main one, I think some assumptions are not met, hence the errors.

Workarounds I've tried:

  1. Create the test schema by yourself php bin/console --env=test doctrine:schema:create
  2. Edit config/packages/test/doctrine.yml and comment out the line adding the suffix to the database name #dbname_suffix: '_test%env(default::TEST_TOKEN)%'

Another potential workaround would be to alter the test boostrap.php and add database creation logic in there, similar to what's done in the bundle, but I haven't tried that.

Possible steps going forward:

  1. Update Readme to include additional information about the steps required for this work (or the assumptions made), since by default this doesn't work with a recent Symfony application.
  2. Handle the schema creation transparently (via Trait or dedicated test case, not sure).

Also, I'm willing to work on a PR to address if you provide some guidance on how to address this.

This bundle is great alternative to some weird hacks I've done in the past while testing with a database. Thank you for you work on this!

dmaicher commented 3 years ago

I am a bit confused how that issue is related to this bundle? You will have the same issue without using this bundle, right?

redecs commented 3 years ago

@dmaicher The setup process, as described in the README, is broken (or incomplete). Since this is a Symfony bundle I would expect it to work after following the instructions but it doesn't. Hence, my issue here (and not with Doctrine Bundle or Flex, since those changes actually encourage best practices).

So, at least a Readme update to mention that managing the test schema is required for this would go a long way with developers like me, new to this bundle.

dmaicher commented 3 years ago

This bundle does not manage any database or schema creation. It purely manages wrapping test cases into transactions.

Feel free to suggest a PR updating the docs if that's not clear enough.

redecs commented 3 years ago

Ok, will give it a shot on docs. Thanks!