FriendsOfBehat / SymfonyExtension

:musical_score: Extension integrating Behat with Symfony.
MIT License
470 stars 62 forks source link

Environment variable not found since symfony 5.1 #126

Open jfsenechal opened 4 years ago

jfsenechal commented 4 years ago

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 commented 4 years ago

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

rimoi commented 4 years ago

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

ymarillet commented 4 years ago

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');
NicoHaase commented 3 years ago

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

kadzany commented 3 years ago

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

gdalyy commented 3 years ago

Any clean solutions please ?

jfsenechal commented 3 years ago

Pr is ready #127

NicoHaase commented 3 years ago

Any clean solutions please ?

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

gdalyy commented 3 years ago

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.

sargath commented 3 years ago

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?

reypm commented 2 years ago

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?

b2p-fred commented 2 years ago

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
reypm commented 2 years ago

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

b2p-fred commented 2 years ago

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

jeroendesloovere commented 2 weeks ago

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

You can set it in once in behat.yaml

default:
    extensions:
        FriendsOfBehat\SymfonyExtension:
            # the "bootstrap" line is a fix for error: Environment variable not found: "DATABASE_URL".
            bootstrap: 'tests/bootstrap.php'
            kernel:
                environment: 'test'
                debug: true