简体   繁体   中英

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":"api@api.com","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.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM