Answer the question
In order to leave comments, you need to log in
How to properly set up a test environment in Symfony 4 with .env files?
Good afternoon.
How to properly set up a test environment in Symfony 4?
Previously, there were prod, test, dev configs and parameters.yml files for each environment.
Now the configs are also different, and instead of parameters.yml there is now a .env file.
The documentation recommends setting the environment in this file. But how to set up a dev and test environment with this one file? I will not change the .env file before running the tests and then return the values back.
For example, different environments have different databases. Previously, in parameters.yml there were settings for connecting to the dev database and in parameters_test.yml there were settings for the test database.
Answer the question
In order to leave comments, you need to log in
Environment variables can be set in the phpunit configuration file. This is exactly what is done in the official demo application: https://github.com/symfony/demo/blob/3c5d376ac8e3f...
If you want to use a separate .env file for testing - create a bootstrap file (see https://symfony.com/ doc/current/testing/bootstrap.html) in which, using the Dotenv component, load the desired file.
.env is the original, you can not touch it at all,
you need to create .env.local, copy the data from .env there and configure it for the desired environment
.env.local must be registered in .gitignore
You can take a look at Laravel, they have been with .env for a long time
https://laravel.com/docs/5.6/testing#environment
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question