I’m working on another PHP project that uses end-to-end tests and .env
files. However before running the tests I need to modify the .env
file to point to the test database (instead of the development one). When I work on Symfony projects I don’t believe I need to do that, it just loads the test environment automatically.
I know from some previous experience with older versions that there used to be a different front controller for each environment, like app.php
, app_dev.php
etc. but afaik that isn’t the case now.
How does Symfony know to load the test environment for end-to-end tests?
2
Answers
Which environment to use is generally set in
phpunit.xml.dist
.This is more a PhpUnit thing than a Symfony one.
You should have an entry like:
By using
force=true
, it will override the value of any existingAPP_ENV
environment variable.WebTestCase
will "simulate" the requests, as explained here. If you use something like Panther, the tests will fire up an internal web server and make "real" HTTP requests, as explained here.In either case, the
APP_ENV
used by the application is the one defined on PhpUnit configuration.The flex recipe for the
phpunit-bridge
includes aphpunit.xml
file that sets theAPP_ENV
variable totest
. That triggers symfony to load the appropiate.env.test
file.During tests the front controller isn’t normally used, as the bridge instantiates a
Request
object and passes it to the application kernel directly. But in end-to-end tests usingPantherTestCase
, the framework boots the project in the built-in PHP webserver, which is still controlled by environment variables.This is better explained in the book than the testing chapter of the documentation itself:
And in the Panther announcement: