4

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.

Robert
  • 1,206
  • 1
  • 17
  • 33

1 Answers1

7

With Symfony 5, the usage of putenv was deprecated in the dotenv component. You have to use either $_ENV or $_SERVER to access the environment variables.

You can find the PR here if you are interested in more details

Philip Weinke
  • 1,804
  • 1
  • 8
  • 13
  • That's a good point, Philip. Hower even thought that's deprecated it should still work and it does not. I wasn't sure if I should use `$_SERVER` or `$_ENV` but it looks like this PR explains it quite well. So that is what I am going to do. However, it also mentions that `getenv()` should work so maybe it's a bug. Who knows. Anyway, your answer makes sense to me. Thanks, Man. – Robert Aug 05 '20 at 18:28
  • 1
    Yes, it should be possible to use `getenv()` if `usePutenv` is set to true. I just had a look in a Symfony 5.1 project and the default is definitely `false` now. Changing the flag in your `bootstrap.php` should do the trick (something like `(new Dotenv())->usePutenv(true)->bootEnv(dirname(__DIR__).'/.env');`) – Philip Weinke Aug 05 '20 at 18:42
  • I did try that before it worked partially, sort of. I decided not to look into that too much as I now have a much easier and better solution. – Robert Aug 05 '20 at 18:48