I am using PHPUnit (9.5) with Symfony (5.3).
For my tests, I use the default test database config from config/packages/test/doctrine.yaml :
doctrine:
dbal:
# "TEST_TOKEN" is typically set by ParaTest
dbname_suffix: '_test%env(default::TEST_TOKEN)%'
So my tests use the same database as prod with the suffix '_test'.
I added some code to tests/bootstrap.php to automate database creation / reset before each test runs :
// delete database if exists, then create
passthru('php bin/console doctrine:database:drop --env=test --force --if-exists');
passthru('php bin/console doctrine:database:create --env=test');
// run migrations
passthru('php bin/console doctrine:migrations:migrate --env=test -n');
and I use dama/doctrine-test-bundle for automatic transactions for each tests.
That is working very well, but I have a question :
Is there a way to delete the database at the end of test run ? (like I did in bootstrap.php)
CodePudding user response:
I understand that your bootstrap.php file is running before the test, you need a solution to launch something after your test.
First, create a command that drop the test database.
In anyway, be very careful that the code in your command stop all execution, if you aren't in an explicit test environment (because it means you are in a production environment).
Then, you can alter your composer.json file to launch the created command after your test in a chain of scripts.
Here is an exemple
"scripts": {
"test-and-remove": [
"@putenv APP_ENV=test",
"phpunit --configuration phpunit.xml",
"php bin/console app:drop-test-database"
],
Then you only have to launch your test via this new command:
composer test-and-remove