SymfonyExtension
SymfonyExtension copied to clipboard
Environment variable not found since symfony 5.1
Hello
Since symfony 5.1 when running vendor / bin / behat there is an error message
In EnvVarProcessor.php line 171:
Environment variable not found: "DATABASE_URL".
To reproduce the error, just install a new application in 5.1
Here are my composer depedencies :
"behatch/contexts": "3.3.0",
"friends-of-behat/mink": "^1.8",
"friends-of-behat/mink-browserkit-driver": "^1.4",
"friends-of-behat/mink-extension": "^2.4",
"friends-of-behat/symfony-extension": "^2.0",
"theofidry/alice-data-fixtures": "^1.1"`
I found the problem Symfony 5.1 delete the file config/bootstrap.php
https://github.com/symfony/symfony/pull/35308
Here's how to fix it:
In friends-of-behat/symfony-extension/src/ServiceContainer/SymfonyExtension.php Add this line in the method loadBootstrap
private function loadBootstrap(?string $bootstrap): void
{
if ($bootstrap === null) {
(new Dotenv())->bootEnv(basename(dirname(__DIR__)).'/../.env');
return;
}
require_once $bootstrap;
}
But it will have to be done more cleanly
Great! Thanks @jfsenechal Don't forget load the Dotenv class :wink:
Solution without hacking vendors:
behat.yaml
default:
extensions:
FriendsOfBehat\SymfonyExtension:
bootstrap: 'config/behat/bootstrap.php'
# ...
config/behat/bootstrap.php
<?php
(new Symfony\Component\Dotenv\Dotenv())->bootEnv(dirname(__DIR__, 2).'/.env');
Another idea I've used: the PHPUnit stuff still ships a bootstrap.php which either uses config/bootstrap.php or DotEnv loading. I've referenced this in behat.yaml and now both testing tools use the same bootstrap.php
Solution without hacking vendors:
behat.yaml
default: extensions: FriendsOfBehat\SymfonyExtension: bootstrap: 'config/behat/bootstrap.php' # ...
config/behat/bootstrap.php
<?php (new Symfony\Component\Dotenv\Dotenv())->bootEnv(dirname(__DIR__, 2).'/.env');
In my opinion, this should become a part of symfonyextension's documentation regarding handling environment variable.
Thanks BTW
Any clean solutions please ?
Pr is ready #127
Any clean solutions please ?
How would you determine that a solution is "clean"? What's wrong with the given examples?
The amount of effort to resolve a problem can easily determine if the solution if clean or not.
Regarding the given examples nothing wrong with them , they already solve the problem. thanks.
Side note here. Whilst you have the phpunit already installed, its recipe provides the test/bootstrap.php
file.
I've used it in the behat.yml
and it works
extensions:
FriendsOfBehat\SymfonyExtension:
bootstrap: tests/bootstrap.php
Why not to use the same approach and just to provide a recipe that will provide the file or just update the docs?
hi there, as today this still an issue? I am trying to run/debug Behat Scenarios using PhpStorm and I am getting the same exact error as everyone here. Also what should I do if I do not have such test/bootstrap.php
file? 🤔 I do have a config/bootstrap.php
tho, is that the same and should be used to avoid this issue?
IMHO, the best solution remains to introduce the environment variables loading in the extension loadBootstrap
function (as of #127).
This because if you did not set the APP_ENV
variable accordingly to the configuration used for the kernel in the behat.yml.dist file, you will get an incorrect configuration because you will not load the variables defined in the .env.test
file but only the .env
file.
Take care to always run Behat with:
APP_ENV=test ./vendor/bin/behat
@b2p-fred I running Behat same as in your example but for some reason, PhpStorm does not see the variables defined at .env
😐
@reypm I do not use PHPStorm to run the Behat tests. As such, sorry, I cannot help ...