darckcrystale
darckcrystale

Reputation: 1722

How to setup which Symfony environment is used by unit tests?

I am trying to setup a new Symfony environment named travis to run unit tests in a Travis container.

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

Currently, I have:

>

language: php

php:
  - "7.2.17"

services:
  - mysql

install:
  - 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

script:
  - ./vendor/bin/simple-phpunit

My project looks like this:

screenshot of the folders tree of my project

Some examples of tests I'm running:

UserTest.php which tests the User.php model:

<?php
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:

<?php
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
        $form['_username']->setValue('admin');
        $form['_password']->setValue('a');
        // Send the form
        $client->submit($form);
        $crawler = $client->followRedirect();
        $this->assertContains(
                'Bienvenue admin.' ,
                $client->getResponse()->getContent()
        );

        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.

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

Upvotes: 1

Views: 1567

Answers (2)

darckcrystale
darckcrystale

Reputation: 1722

PHPUnit uses an environment variable called APP_ENV to determines which environment is used. I had to create this environment variable in Travis.

screenshot of the Travis environment variables setting showing the environment variables I created

Upvotes: 1

xabbuh
xabbuh

Reputation: 5881

The createClient() method of the WebTestCase calls the bootKernel() method from the KernelTestCase which in turn calls createKernel(). In createKernel() there is the following code which determines in which environment the kernel should be booted:

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';
}

So in your case exporting the APP_ENV variable in your config_travis.yml file and setting it to travis should solve it.

Upvotes: 1

Related Questions