Symfony 5.4 .env.test does not overwrite .env when using phpunit

101 views Asked by At

I have upgraded Symfony from 4.4 to 5.4. Every dependencies have been upgraded and recipes installed.

I have a .env and a .env.test (nothing else in terms of dotenv).

I also have a phpunit.xml.dist as described in the Symfony recipe (the exact same file, with the ). My tests/bootstrap.php is also the same.

When doing functional tests, the environment variables are loaded from the .env but the .env.test isn't loaded. I know that because the tests does not find variables set in the .env.test. I also find out that the APP_ENV has not changed and still "dev", and I really don't know why. I thought that the APP_ENV in phpunit.xml.dist was the key to load the test environment, but it's not.

The command I use to run the tests

php bin/phpunit

Does someone already have this kind of problem with a Symfony upgrade ? I really tested everything I know to solve this issue.

As my files are all the same than the recipe, I just provide my composer.json. Maybe the problem comes from here, and an example of a test I do.

Feel free to ask anything else.

{
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": "^7.4",
        "ext-calendar": "*",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "ext-json": "*",
        "ext-ldap": "*",
        "ext-openssl": "*",
        "ext-redis": "*",
        "ext-zip": "*",
        "beberlei/doctrineextensions": "^1.2",
        "composer/package-versions-deprecated": "1.11.99.1",
        "doctrine/annotations": "^1.0",
        "doctrine/doctrine-bundle": "^2.2",
        "doctrine/doctrine-migrations-bundle": "^3.0",
        "doctrine/orm": "^2.7",
        "hslavich/oneloginsaml-bundle": "^2",
        "icewind/smb": "^3.5",
        "knplabs/knp-paginator-bundle": "^5.9",
        "knplabs/knp-snappy-bundle": "^1.6",
        "phpdocumentor/reflection-docblock": "^5.2",
        "phpoffice/phpspreadsheet": "^1.16",
        "scienta/doctrine-json-functions": "~4.0",
        "sensio/framework-extra-bundle": "^5.1",
        "setasign/fpdf": "1.8.*",
        "setasign/fpdi": "^2.0",
        "stof/doctrine-extensions-bundle": "^1.3",
        "symfony/apache-pack": "^1.0",
        "symfony/asset": "5.4.*",
        "symfony/console": "5.4.*",
        "symfony/dotenv": "5.4.*",
        "symfony/expression-language": "5.4.*",
        "symfony/flex": "^1.1",
        "symfony/form": "5.4.*",
        "symfony/framework-bundle": "5.4.*",
        "symfony/http-client": "5.4.*",
        "symfony/intl": "5.4.*",
        "symfony/mime": "5.4.*",
        "symfony/monolog-bundle": "^3.1",
        "symfony/process": "5.4.*",
        "symfony/property-access": "5.4.*",
        "symfony/property-info": "5.4.*",
        "symfony/proxy-manager-bridge": "5.4.*",
        "symfony/runtime": "5.4.*",
        "symfony/security-bundle": "5.4.*",
        "symfony/serializer": "5.4.*",
        "symfony/swiftmailer-bundle": "^3.1",
        "symfony/translation": "5.4.*",
        "symfony/twig-bundle": "5.4.*",
        "symfony/validator": "5.4.*",
        "symfony/web-link": "5.4.*",
        "symfony/workflow": "5.4.*",
        "symfony/yaml": "5.4.*",
        "tomasvotruba/barcode-bundle": "^1.3",
        "twig/extensions": "^1.5"
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.2",
        "phpunit/phpunit": "^9.5",
        "squizlabs/php_codesniffer": "3.*",
        "fakerphp/faker": "^1.23",
        "symfony/browser-kit": "^5.4",
        "symfony/css-selector": "^5.4",
        "symfony/debug-bundle": "^5.4",
        "symfony/maker-bundle": "^1.0",
        "symfony/phpunit-bridge": "^5.4",
        "symfony/stopwatch": "^5.4",
        "symfony/web-profiler-bundle": "^5.4"
    },
    "config": {
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true,
        "secure-http": false,
        "disable-tls": true,
        "allow-plugins": {
            "symfony/flex": true,
            "phpstan/extension-installer": true,
            "symfony/runtime": true
        }
    },
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "App\\": "src/",
            "App\\Tests\\": "tests/"
        }
    },
    "replace": {
        "paragonie/random_compat": "2.*",
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php71": "*",
        "symfony/polyfill-php70": "*",
        "symfony/polyfill-php56": "*"
    },
    "scripts": {
        "auto-scripts": {
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "5.4.*"
        }
    }
}

The test example :

class AccueilControllerTest extends WebTestCase
{
    private KernelBrowser $client;

    protected function setUp(): void
    {
        $kernel = self::bootKernel();
        static::ensureKernelShutdown();
        $this->client = static::createClient();
    }

    /**
     * @return void
     */
    public function testUserNotLoggedIndexRedirectsLogin()
    {
        $this->client->request('GET', '/calculatrice_nos/');
        $this->assertEquals(Response::HTTP_FOUND, $this->client->getResponse()->getStatusCode());

        $expectedRedirectUrl = $this->client->getContainer()->get('router')->generate('app_login', [], UrlGeneratorInterface::ABSOLUTE_URL);
        $this->assertTrue($this->client->getResponse()->isRedirect($expectedRedirectUrl));
    }
    
    //...
}

This is my phpunit.xml.dist which does not work. As a workaround, I managed to add APP_ENV as server and env var in a new version. But that's not clean.

<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="tests/bootstrap.php"
         convertDeprecationsToExceptions="false"
>
    <php>
        <ini name="display_errors" value="1" />
        <ini name="error_reporting" value="-1" />
        <server name="APP_ENV" value="test" force="true" />
        <!-- My workaround to make it works -->
        <env name="APP_ENV" value="test" force="true" />
        <server name="SHELL_VERBOSITY" value="-1" />
        <server name="SYMFONY_PHPUNIT_REMOVE" value="" />
        <server name="SYMFONY_PHPUNIT_VERSION" value="9.6" />
    </php>

    <testsuites>
        <testsuite name="main">
            <directory>tests</directory>
        </testsuite>
        <testsuite name="unit_only">
            <directory>tests/Unit</directory>
        </testsuite>
        <testsuite name="functional_only">
            <directory>tests/Functional</directory>
        </testsuite>
    </testsuites>

    <coverage processUncoveredFiles="true">
        <include>
            <directory suffix=".php">src</directory>
        </include>
    </coverage>

    <listeners>
        <listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener" />
    </listeners>
</phpunit>
1

There are 1 answers

1
craigh On

In phpunit.xml.dist set (at least) the APP_ENV var

<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="tests/bootstrap.php"
         convertDeprecationsToExceptions="false"
>
    <php>
        <server name="APP_ENV" value="test" force="true" />
    </php>

</phpunit>