You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Environment variables not loaded into putenv via ParamsLoader #6776
I am writing tests for a Craft 4 project using codeception/codeception and codeception/module-yii2 and found an issue while running functional tests where environment variables are unavailable when an internal request is made.
My project uses symfony/dotenv to handle loading of environment variables.
In my Craft config files I use environment variables to alter functionality, eg a config/site.php:
<?php
use craft\helpers\App;
$myEnvVar = App::env('VARIABLE_NAME');
Running functional tests like the following resulted in unexpected behaviour and errors on the site caused by missing environment variables.
<?php
namespace functional;
use FunctionalTester;
class LoginCest
{
public function tryLoggingIn(FunctionalTester $I)
{
$I->amOnPage('/account/login');
}
}
The value of $myEnvVar should always be what I set in the environment variable.
What do you get instead?
When I breakpoint my config file and Codeception first starts the $myEnvVar is whatever I provided in the .env however after the functional test starts and an internal request is made this $myEnvVar is now null.
The workaround was to load the environment variables in my own Codeception bootstrap file and enable usePutenv for Dotenv:
$symfonyDotEnv = new SymfonyDotenv();
$symfonyDotEnv->usePutenv(true);
$values = $symfonyDotEnv->parse(self::getFileContents($file), $file);
$symfonyDotEnv->populate($values);
If I bypassed my own bootstrap file and altered the Codeception ParamsLoader to the above the functional tests worked as expected because the variables were kept in PHP's putenv and unaffected by the Yii2 connector reseting $_SERVER variables.
Should the ParamsLoader be using usePutenv(true) by default or would it be possible to add an option in the codeception.yml to enable this behaviour as a workaround for frameworks that do not use the $_ENV for environment variables?
Alternatively it might be good to add a notice on the Codeception documentation advising developers that this could be an issue when implementing functional tests in some frameworks and how to work around it.
Activity