Setup Symfony env to be used by unit tests?

I setup this environment to distinguish it from prod and from dev .

  • a SYMFONY_ENV=travis environment variable setup in Travis
  • a config_travis.yml that contains my configuration for the Travis env
  • a app_travis.php which specify the environment to load
  • a .travis.yml :
language: php

  - "7.2.17"

  - mysql

  - composer install --no-interaction
  - echo "USE mysql;\nUPDATE user SET password=PASSWORD('${MYSQL_PASSWORD}') WHERE user='root';\nFLUSH PRIVILEGES;\n" | mysql -u root
  - ./bin/console doctrine:database:create --env=travis
  - ./bin/console doctrine:migration:migrate --env=travis --no-interaction

  - ./vendor/bin/simple-phpunit

I can layout project tree if need be.

Some examples of tests I’m running:

UserTest.php which tests the User.php model:

namespace Tests\AppBundle\Entity;

use AppBundle\Entity\User;
use PHPUnit\Framework\TestCase;
use AppBundle\Entity\Responsibility;

class UserTest extends TestCase
    public function testId()
        $user = new User();
        $id = $user->getId();
        $this->assertEquals(-1, $id);

LoginControllerTest.php which tests the LoginController.php controller:

namespace Tests\AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\BrowserKit\Cookie;
use Symfony\Component\HttpFoundation\Response;

class LoginControllerTest extends WebTestCase
     * Test the login form
     * Logins with (admin, password : a)
    public function testLogin()
        // Create a new client to browse the app
        $client = static::createClient();
        $crawler = $client->request('GET', '/login');
        $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET ");
        // Get the form
        $form = $crawler->selectButton('Connexion')->form();
        // Fill the login form input
        // Send the form
        $crawler = $client->followRedirect();
                'Bienvenue admin.' ,

        return array($client,$crawler);

My problem is: all the command run into the travis environment, except the unit tests. I want to be able to run the unit tests in dev env on my computer but in travis env in the Travis container, in other words locally.

How can I setup my PHPUnit so that it can run in travis environment and use my config_travis.yml file?


Hey @SolarUltima,

In accordance with Symfony it seems like you’re calling the wrong thing from what I’ve read. In particular and in most cases the createClient() method of the WebTestCase calls the bootKernel() method from the KernelTestCase not the other way around, which in turn calls createKernel(). In createKernel() there is the following code which determines which environment the kernel should be booted in, as follows:

if (isset($options['environment'])) {
    $env = $options['environment'];
} elseif (isset($_ENV['APP_ENV'])) {
    $env = $_ENV['APP_ENV'];
} elseif (isset($_SERVER['APP_ENV'])) {
    $env = $_SERVER['APP_ENV'];
} else {
    $env = 'test';

In your case exporting the APP_ENV variable in your config_travis.yml, file and then setting it to travis (you can enforce this as well if you still are having problems, or reverts back - I have seen this before). That my possible solution and it should solve it. If it doesn’t please post back and I will gladly help you fix it.


this worked perfectly @Montana, thank you!

Great to hear!