SymfonyExtension icon indicating copy to clipboard operation
SymfonyExtension copied to clipboard

Environment variable not found since symfony 5.1

Open jfsenechal opened this issue 4 years ago • 15 comments

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"`

jfsenechal avatar Jun 04 '20 19:06 jfsenechal

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

jfsenechal avatar Jun 05 '20 15:06 jfsenechal

Great! Thanks @jfsenechal Don't forget load the Dotenv class :wink:

rimoi avatar Jun 14 '20 09:06 rimoi

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');

ymarillet avatar Jun 30 '20 20:06 ymarillet

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

NicoHaase avatar Aug 03 '20 10:08 NicoHaase

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

kadzany avatar Sep 08 '20 06:09 kadzany

Any clean solutions please ?

gdalyy avatar Oct 01 '20 03:10 gdalyy

Pr is ready #127

jfsenechal avatar Oct 01 '20 06:10 jfsenechal

Any clean solutions please ?

How would you determine that a solution is "clean"? What's wrong with the given examples?

NicoHaase avatar Oct 01 '20 14:10 NicoHaase

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.

gdalyy avatar Oct 01 '20 23:10 gdalyy

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?

sargath avatar Oct 06 '20 12:10 sargath

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?

reypm avatar Jul 27 '21 15:07 reypm

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 avatar Sep 24 '21 06:09 b2p-fred

@b2p-fred I running Behat same as in your example but for some reason, PhpStorm does not see the variables defined at .env 😐

reypm avatar Sep 24 '21 12:09 reypm

@reypm I do not use PHPStorm to run the Behat tests. As such, sorry, I cannot help ...

b2p-fred avatar Sep 29 '21 08:09 b2p-fred