'Symfony - "framework.test" config is not set to true

Using Symfony 5.2 and PhpUnit last version, I have this error with my functional test

LogicException: You cannot create the client used in functional tests if the "framework.test" config is not set to true.

Content of config/packages/test/framework.yaml :

framework:
    test: true
    session:
        storage_id: session.storage.mock_file

PHP part of my phpunit.xml.dist :

<php>
    <ini name="error_reporting" value="-1"/>
    <server name="APP_ENV" value="test" force="true"/>
    <server name="SHELL_VERBOSITY" value="-1"/>
    <env name="BOOTSTRAP_CLEAR_CACHE_ENV" value="test"/>
</php>

Here is the content of my Test :

class JWTTokenAuthenticatorTest extends WebTestCase
{
    public function test_authentication_token()
    {
        $client = static::createClient();
        $client->request(
            'POST',
            '/authentication_token',
            [],
            [],
            ['CONTENT_TYPE' => 'application/json'],
            '{"email":"[email protected]","password":"api"}'
        );

        $this->assertResponseIsSuccessful();
        $this->assertMatchesRegularExpression('/{"token":"(.*)"}/i', $client->getResponse()->getContent());
    }
}

I have only default bootstrap.php file, I can't understand this error since the APP_ENV is well set to "test".

I tried to clear the cache.

When I dump dd($_ENV['APP_ENV']);, I have dev, I can't understand why.



Solution 1:[1]

As per the documentation, <server /> sets a variable in the super-global array $_SERVER.

For the APP_ENV you want to use <env /> instead, it sets a variable in the $_ENV array.

Solution 2:[2]

Try to add to config/packages/framework.yaml lines:

when@test:
   framework:
       test: true
       session:
           storage_factory_id: session.storage.factory.mock_file

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Christoph
Solution 2 user18917049