Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variables for tests in Symfony 4

I have a config in services.yaml

parameters:
   locale: 'en'
   uri: '%env(resolve:DEV_ENV_URI)%'

But when I execute tests, where I have used this parameter like

public function setUp()
{
    $this->client = static::createClient();

    static::bootKernel();

    $this->container = static::$kernel->getContainer();

    $this->guzzle = new Client(
        [
            'base_uri' => $this->container->getParameter('uri'),
            'verify' => false
        ]
    );
}

I have got the following error:

Environment variable not found: "DEV_ENV_URI".

Here is my .env:

APP_ENV=test
DEV_ENV_URI=http://0.0.0.0:8000/

In dev environment during requests everything is great, but when I run tests from the terminal, the environment is not working.

Maybe you can suggest any other idea, how to solve the problem with establishing base URI for testing reason?

Thanks a lot for any idea!

like image 849
Viktor Sydorenko Avatar asked Oct 16 '25 20:10

Viktor Sydorenko


1 Answers

I think the proper way to fix this is to use the configuration file of PHPUnit:

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
         backupGlobals="false"
         colors="true"
         bootstrap="vendor/autoload.php"
>
    <php>
        <ini name="error_reporting" value="-1" />
        <env name="KERNEL_CLASS" value="App\Kernel" />
        <env name="APP_ENV" value="test" />
        <env name="APP_DEBUG" value="1" />
        <env name="APP_SECRET" value="s$cretf0rt3st" />
    </php>
</phpunit>
like image 131
Nek Avatar answered Oct 18 '25 10:10

Nek