-
Notifications
You must be signed in to change notification settings - Fork 24
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
Symfony: .env.local
and .env.test
are ignored
#99
Comments
@ThomasLandauer Can you fix it? |
Same here |
I have similar issue, it happens because Codeception neither loads environment directly nor uses Symfony's way to do that (specifically, does not include My solution is the following snippet in params:
- .env.test Maybe it will be better to modify |
the main problem is, that my
so phpunit normally loads my workaround is to rebuild the behavior of
ad 1.
ad 2.
ad 3. Add this to your codeception.yml to load the new tests/_bootstrap.php you just created - see https://codeception.com/docs/reference/Configuration#Global-Configuration:
edit: updated comment with comment from @ThomasLandauer https://github.com/Codeception/Codeception/issues/5411#issuecomment-585279655 |
@c33s solution works for me (Symfony 4.4) - there's just one step missing:
|
I'm using Symfony 5 in my current project and I had this issue too. Adding env.test file to params section worked for me, but it would definitely be better if Symfony module did it automatically. Including config/bootstrap in An alternative approach is to add Currently I prefer modifying recipe file. |
So I wanted to commit my code and realized that Comment at the top of
Comment above
Secrets must go to Updating recipe looks like bad option now and it would be better to include bootstrap.php |
Maybe a stupid question, but which secrets do you have in test environment? |
Same issue here, isn't possible to pass in testing credentials without hacking the config. |
why hack the config? can you explain the problem? do you have "real" secrets in your tests? i have dummy secrets for my tests and the are in |
@c33s Yes we do have "real" secrets because we test production APIs and third party API / bundle integrations periodically, I don't think that's the point though... |
I tried the approach suggested by @c33s but it doesn't seem to work for me. Controller always uses I removed all Any idea what I may be missing ? |
@dant89 ok, i get it, you have real secrets in your file, so my question would be, what are you trying to archive? |
@callmebob2016 do you have a minimal example for me? for me it sounds really stange, if you define symfony initiates the dotenv class in
first check if the dotenv is instanced with or it can have something to do with the phpconfig general note: stuff defined in how the dotenv component has changed and how it's working: |
btw i am online in slack (symfony & codeception) |
@c33s thanks for quick reply. Please have a look at this example: https://github.com/callmebob2016/env_swap_test This is basically symfony5 + codeception + phpbrowser. Everything should be as per your original description. However, controller always returns |
@callmebob2016 you are using acceptance tests and not functional tests, also you are using phpbrowser and not the symfony module. so your setup behave differently. see my PR at your project, i added the symfony module and i am not using phpbrowser. in the config you see, that the symfony module also has the hardcoded value in which environment the kernel should be booted. then the other environment variables should be correctly loaded from your the edit: for future reference:
|
👎
👍
👎
yes. but secrets in the tests? if you have secrets they should got to real environment variables but as i wrote above, if you use codeception to monitor your production application, it's not testing, its monitoring and then we should write a real prod symfony app (lets call it monitoring), which is doing the calls to the other symfony app (lets call it app-app). not sure if codeception is the right way for it. if codeception is just the tool to easily access the prod pages, i am sure the code of codeception can directly be used in symfony but then it should be called in prod environment.
no, the app environment must be correctly set and the
👍 |
Brilliant, thanks a lot. One more question: why do you see using acceptance test as incorrect here? I thought (perhaps I am wrong here), that under the hood they are the same code. The difference apart from the conceptual is modules are in their config files. Also I can see in your PR, that both work now. What am I missing ? |
@callmebob2016 i absolutly don't see it as incorrect, it's just something i noticed. which test type you use for what is your decision or the "decision" of best practices you are following. i personally just connect acceptance tests with real remote controlled browser or phantom (which is/may be totally wrong). https://stackoverflow.com/questions/3370334/difference-between-acceptance-test-and-functional-test you miss nothing, both work just fine now :) |
@c33s fyi: .env().local for symfony are not only for storing secrets. They are also meant to store settings that are dependent where the tests are run. E.g. paths go obviously also very often into the .env().local config file. Every developer in a team will have e.g. another path to a local folder where certificates/thumbnails/cache-things are stored for dev/test environment when using symfony. |
Symfony dropped the
<?php
$_ENV['APP_ENV'] = 'test';
(new Symfony\Component\Dotenv\Dotenv())->bootEnv(dirname(__DIR__).'/.env'); And I'm with the others here: I prefer to have the tests handle |
@TavoNiievez Shouldn't this be moved to https://github.com/Codeception/module-symfony/issues too? |
@ThomasLandauer yeap. Done. Is #88 the same issue? |
@TavoNiievez via slack:
to sum things up: codeception should never automatically load any .env files via params (if a user want to load it, they should load it via manual config). the reason behind this is that symfony is loading the dotenv files in a specific way which also differs between symfony versions. to have a correct behavior it should be always loaded like symfony is loading it. https://symfony.com/blog/improvements-to-the-handling-of-env-files-for-all-symfony-versions https://symfony.com/doc/current/configuration/dot-env-changes.html#updating-my-application for older symfony versions this means we have to include the to give a little insignt this is one variant of if (is_array($env = @include dirname(__DIR__).'/.env.local.php') && (!isset($env['APP_ENV']) || ($_SERVER['APP_ENV'] ?? $_ENV['APP_ENV'] ?? $env['APP_ENV']) === $env['APP_ENV'])) {
foreach ($env as $k => $v) {
$_ENV[$k] = $_ENV[$k] ?? (isset($_SERVER[$k]) && 0 !== strpos($k, 'HTTP_') ? $_SERVER[$k] : $v);
}
} elseif (!class_exists(Dotenv::class)) {
throw new RuntimeException('Please run "composer require symfony/dotenv" to load the ".env" files configuring the application.');
} else {
// load all the .env files
(new Dotenv(false))->loadEnv(dirname(__DIR__).'/.env');
} but this code also differs between different symfony versions. in older versions the dotenv component was initialized without a parameter, then the parameters was added. in the past the dotenv code was inside of the so the code in #4 should be adapted to handle this. |
Is there any update on this? Just started trying to use codeception and got stung by this unexpected dotenv behaviour - wasted a few hours trying to figure it out, which almost put me off using codeception altogether... |
No updates. I haven't used this module since I posted my comment 3 years ago. |
No updates from my side either. |
For anyone interested, especially @ThomasLandauer and @c33s I opened a new PR for this: #190 |
Starting in November 2018, Symfony has changed the way
.env
files are working: https://symfony.com/doc/current/configuration.html#the-env-file-environment-variablesThese changes are not realized in Codeception.
To assert this, I created a controller which just outputs an environment variable:
When I access the url with codeception (
codecept_debug($I->grabPageSource());
), I always get what I have in.env
(should be.env.local
or.env.test
).However, when I open the page in the browser in DEV environment, I get the content of
.env.dev
, when in TEST environment,.env.test
; this is the expected behavior.Details
The text was updated successfully, but these errors were encountered: