Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Environment variable not found since symfony 5.1 #126

Open
jfsenechal opened this issue Jun 4, 2020 · 15 comments
Open

Environment variable not found since symfony 5.1 #126

jfsenechal opened this issue Jun 4, 2020 · 15 comments
Labels

Comments

@jfsenechal
Copy link

jfsenechal commented Jun 4, 2020

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
Copy link
Author

jfsenechal commented Jun 5, 2020

I found the problem
Symfony 5.1 delete the file config/bootstrap.php

symfony/symfony#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

@pamil pamil added the Bug label Jun 6, 2020
@rimoi
Copy link

rimoi commented Jun 14, 2020

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

@ymarillet
Copy link

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
Copy link

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
Copy link

kadzany commented Sep 8, 2020

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
Copy link

gdalyy commented Oct 1, 2020

Any clean solutions please ?

@jfsenechal
Copy link
Author

Pr is ready #127

@NicoHaase
Copy link

Any clean solutions please ?

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

@gdalyy
Copy link

gdalyy commented Oct 1, 2020

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
Copy link

sargath commented Oct 6, 2020

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?

mnocon added a commit to ezsystems/BehatBundle that referenced this issue Dec 22, 2020
mnocon added a commit to ezsystems/BehatBundle that referenced this issue Dec 23, 2020
@reypm
Copy link

reypm commented Jul 27, 2021

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
Copy link

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
Copy link

reypm commented Sep 24, 2021

@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
Copy link

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

@jeroendesloovere
Copy link

jeroendesloovere commented Jul 2, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests