skip to Main Content

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


  1. 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:

    <php>
        <server name="APP_ENV" value="test" force="true" />
    <!-- ... -->
    </php>
    

    By using force=true, it will override the value of any existing APP_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.

    Login or Signup to reply.
  2. The flex recipe for the phpunit-bridge includes a phpunit.xml file that sets the APP_ENV variable to test. 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 using PantherTestCase, 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:

    The $client variable simulates a browser. Instead of making HTTP calls
    to the server though, it calls the Symfony application directly. This
    strategy has several benefits: it is much faster than having
    round-trips between the client and the server, but it also allows the
    tests to introspect the state of the services after each HTTP request.

    And in the Panther announcement:

    However, WebTestCase doesn’t use a real web browser. It simulates one
    with pure PHP components. It doesn’t even use the HTTP protocol: it
    creates instances of HttpFoundation’s Request objects, pass them to
    the Symfony kernel, and allows to assert on the HttpFoundation
    Response instance returned by the app.
    […]
    Under the hood Panther has:

    • started your project with the built-in PHP webserver on localhost:9000
    • started the version of Chromedriver shipped with the library to automate your local Chrome
    • executed the browsing scenario defined in the test with Chrome in headless mode
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search