PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 23, 2022

[FIXED] Symfony 5 Functional Tests - Environment Variables missing, getenv() returns false

 January 23, 2022     environment-variables, functional-testing, integration-testing, symfony, symfony5     No comments   

Issue

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.


Solution

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



Answered By - Philip Weinke
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing