I was rewriting one of my projects into Symfony 5. I've noticed something strange about env variables. I can't get them anymore the way I used to be able to get them in the functional tests.
Not that it matters but I have my custom ApiTestCase
which extends the existing test case shipped with Symfony 5.
In there I am doing something like that (this is not a question about the design or flow of my app, simply about how to get the variable):
<?php
namespace App\Tests;
use App\Traits\Tools\Tests\Database\UsesTestDatabase;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* Class ApiTestCase
* @package App\Tests
*/
class ApiTestCase extends WebTestCase
{
/**
* ApiTestCase constructor.
*/
public function __construct()
{
parent::__construct();
dd(getenv('TEST_ENV_VARIABLE')) // should give me 'my secret env value'
}
}
# this is .env.test file
############################################################################################################
#
# GENERAL SETTINGS
#
############################################################################################################
KERNEL_CLASS='App\Kernel'
APP_ENV=test
############################################################################################################
#
# TEST DATABASE CONFIGURATION
#
############################################################################################################
TEST_ENV_VARIABLE='my secret env value'
TEST_ENV_VARIABLE
comes back as false. Always. It doesn't matter if I put it in .env.test
or phpunit.xml.dist
or any other file I will point at. Always comes back as false
.
What is interesting - I did not have that issue in Symfony 4. I am looking at my old code and just getenv()
just works.
I did a little investigation and in config/bootstrap.php
file all my variables for test ENV are visible in $_SERVER
but they are never dragged into the ENV for some reason. Is there anything different in Symfony 5 in regards to that?
I looked at the docs but I didn't see anything that could help me (or I just missed it). Does anyone have any idea what that might be?
To be honest none of the ENV variables are present when I use getenv()
in my tests. Even the default ones shipped with Symfony.
I am using Symfony PHP unit bridge thingy and just run my tests like this:
./bin/phpunit tests/Integration/Container/Controllers/ContainerControllerTest.php
I am almost sure that this has something to do with my setup - just not sure what it might be.