Search code examples
symfonyphpunitsymfony4

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


i am trying to apply functional test using WebTestCase.

class CalculatorControllerTest extends WebTestCase
{
    public function testSumPost()
    {
        $client = static::createClient();

        $client->request('GET', '/calculator/sum');

        $this->assertEquals(200, $client->getResponse()->getStatusCode());
    }
}

I have the following error message when i run test:

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

My test framework:

framework:
 test: ~
 session:
   storage_id: session.storage.mock_file
 router:
   resource: "%kernel.root_dir%/config/routing_test.yml"
  profiler:
   collect: false

My phpunit.xml: (i tries also test instead of dev in APP_ENV & Debug 1 instead of 0)

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

<phpunit
    backupGlobals               = "false"
    backupStaticAttributes      = "false"
    colors                      = "true"
    convertErrorsToExceptions   = "true"
    convertNoticesToExceptions  = "true"
    convertWarningsToExceptions = "true"
    processIsolation            = "false"
    stopOnFailure               = "false"
    bootstrap                   = "config/bootstrap.php" >

<php>
    <ini name="error_reporting" value="-1" />
    <server name="KERNEL_CLASS" value="AppKernel" />
    <env name="APP_ENV" value="dev" />
    <env name="APP_DEBUG" value="0" />
    <env name="SHELL_VERBOSITY" value="-1" />
    <!-- define your env variables for the test env here -->
</php>

<testsuites>
    <testsuite name="Plein Test Suite">
        <directory>tests/unit</directory>
    </testsuite>
</testsuites>

I took a look of all relative issues but i can not find a good answer, any ideas please?


Solution

  • I fixed this issue by adding the following to my phpunit.xml.dist / phpunit.xml file (inside the phpunit tag):

    <php>
        <env name="APP_ENV" value="test" force="true"/>
    </php>