-
-
Notifications
You must be signed in to change notification settings - Fork 195
Reading .env in PHPUnit versus env variables in phpunit.xml.dist #251
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
Comments
I agree. Let's avoid repetition and let's make phpunit reuse |
FYI. PHPUnit doesn't (by default) overwrite env variables that are already set. You can use the |
"dev" and "test" need very different settings to properly work. I don't think they should be shared between the envs... That could even be dangerous (eg. running tests and destroying "dev" db) |
I agree. For example, I'm used to change the database config in test to Sqlite instead of mysql, for faster tests, then I must set different env vars in the With env vars directly in |
Hmm, ok. So if we keep the current situation, we have bad DX. I'm cool with solving that however we can :). Specifically: A) There is not way to run B) There is duplication between C) The behavior is surprising: the kernel/container in the tests works different than all other kernels, including running What would I suggestion? What if we:
This would allow me to do what I've always done: use the same environment variables between |
Let's find solutions: A) Yeah, we do not often run B) Yes, there is duplication. And there may also be some duplicates when dealing with your CI (because you set env vars in your CI, not in versioned files), as well as production env. That doesn't seem like a problem to me. You don't need to duplicate it: flex recipes will add the env vars automatically in your test env, and if it's similar to your dev one, then you will probably never update it, unless you change this API key. C) Why would the kernel and container working differently than all other kernels be an issue? It's obvious: dev, test and prod are different envs, therefore kernels created with these envs shouldn't work the same way... For your 1. proposal, I'm not sure it's the best option. It would need a wrapper for the For your proposal 2, that's a bit better, but still needs a bootstrap file. Even though I think it's a nice idea to ship such file because we almost always need to setup a project before testing (database, fixtures, etc.) (unless we only do unit tests), I'm not sure implementing a new env system that would read in three places is the best for DX. Then, to debug an incorrect env vars you would have to lookup three types: xml file, env file, global vars. Too much IMO. Only using the xml & global env are enough to me, because xml is for dev, global is for CI. Much simpler, WDYT? |
On my side, the way env variables are managed by Symfony is the main issue I have since I started working with Symfony 4 + flex. First, I don't completely get why DotEnv variables are only loaded if Second, I was used to run several commands in test env before running tests (to initiate database schema, for instance) in several projects. The current implementation does not allow this ; I need to run the commands in dev mode or change the conditions in the |
I'm currently using a bash script to solve this "ENV vars in test" problem: set -o allexport # export all defined variables
source .env # load dev parameters
source .env.test # override with test parameters (only those needed)
php bin/console cache:clear -e "test" --no-debug
php bin/console doctrine:database:drop -e "test" --force --no-debug
php bin/console doctrine:database:create -e "test" --no-debug
php bin/console doctrine:migration:migrate -e "test" -n --no-debug
php bin/console doctrine:fixtures:load -e "test" -n --no-debug
# ... and more
php vendor/bin/simple-phpunit | tee tests.txt This way :
|
I just got bitten by this. Consider the following use-case: In tests, I'm importing the fixtures by running Now I was a debugging an issue with fixtures loading (caused by incorrect Doctrine mapping), so I tried to run the command directly as I did before Flex (note the
And it truncated tables and imported the fixtures into the other database (defined in I'm perfectly aware why this happens (I've read this issue few days ago), but it is still quite unexpected and surprising. It feels like a BC break (radical change in behaviour). |
I have adapted my testing script to source the EDIT And another strange behavior: it looks like behat is running in test env but still loads the The behavior I'm expecting is:
|
Fix at symfony/recipes#366 - please review it and make sure it satisfies all of your requirements! :) |
@weaverryan Great improvement! 👏 I was thinking about how to solve the use-case I mentioned three comments above. I came up with following options so far:
|
Yea, we’re discussing the environment-specific .env file idea on the linked PR too. You can share your support there if you agree (there’s no code for this in the PR yet, just a comment of mine) |
Just curious - I am encountering an error trying to run unit tests that work with repository. I am getting an error EDIT | I think I figured it out I specified the ENV DATABASE_URL inside phpunit.xml.dist and it seems to work?!? |
just stumbled upon this and it makes me wonder... WHYYY is this nowhere documented? |
I've now created a <?php
use Symfony\Component\Dotenv\Dotenv;
require __DIR__.'/../vendor/autoload.php';
if (!isset($_SERVER['APP_ENV'])) {
if (!class_exists(Dotenv::class)) {
throw new \RuntimeException('APP_ENV environment variable is not defined. You need to define environment variables for configuration or add "symfony/dotenv" as a Composer dependency to load variables from a .env file.');
}
(new Dotenv())->load(__DIR__.'/../.env');
} And in <?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="tests/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<env name="KERNEL_CLASS" value="App\Kernel" />
<env name="APP_ENV" value="test" />
<env name="APP_DEBUG" value="1" />
<env name="SHELL_VERBOSITY" value="-1" />
<!-- define your env variables for the test env here -->
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
</phpunit> Maybe this can help someone keep envvars out of git altogether. |
This is fixed in the latest recipes! Documentation (symfony/symfony-docs#10680) and a blog post coming shortly. From experience, things are MUCH nicer to work with now in the test environment :). |
@weaverryan Can you please link blog post? |
@javiereguiluz Thanks 👍 |
Hi guys!
I noticed that the
EnvConfiguration
adds environment variables to.env.dist
and alsophpunit.xml.dist
. This is because phpunit does not read.env
. Instead, you should define all of your environment variables in phpunit.xml.dist (or set real env variables).I think this is confusing (I just got bit by it). It means that my environment variables now need to be duplicated in the
.env
files and alsophpunit.xml.dist
. It also makes it difficult to run commands in the test environment, as using--env=test
isn't enough: the incorrect environment variables will be used (those from.env
instead ofphpunit.xml.dist
). I also didn't know to look inphpunit.xml.dist
: I was looking in.env
and the app wasn't seeing my changes.Obviously, this was done intentionally. But, are we sure about this behavior? I would prefer consistency with
console
andindex.php
: that.env
is read (assuming there is noAPP_ENV
) and then any environment variables inphpunit.xml.dist
override those (admittedly, I'm not exactly sure how to accomplish this).Cheers!
The text was updated successfully, but these errors were encountered: