The docs say:
You may also create a .env.testing file. This file will override the .env file when running PHPUnit tests
I can't make this work at all. I have a .env.testing file, which is a copy of my normal .env file just with APP_ENV=testing instead of local. In my (untouched from defaults) phpunit.xml file it says <server name="APP_ENV" value="testing"/>.
I have run php artisan config:clear, but that did not make any difference.
If I set APP_ENV=testing in .env, it sets the env var correctly - but that implies it's not reading .env.testing.
If I run phpunit manually with export APP_ENV=testing beforehand, it does work as expected.
In a test:
$this->assertEquals('testing', env('APP_ENV'));
and this results in:
Failed asserting that two strings are equal.
Expected :'testing'
Actual :'local'
I figured this out and I'm posting this for others to find. I am working in PHPStorm using the default phpunit configuration. With this config, PHPStorm does not explicitly load phpunit.xml for its configuration; by default phpunit is meant to look for that file itself, as its docs say:
If phpunit.xml or phpunit.xml.dist (in that order) exist in the current working directory and --configuration is not used, the configuration will be automatically read from that file.
However, in PHPStorm, if you do not specify a specific phpunit config file, it runs phpunit with a --no-configuration switch, meaning that it does not read the config file at all, so the APP_ENV value is not set as expected.
So the fix is to select your phpunit.xml file explicitly in your PHPStorm project. Personally I consider this a bug in PHPStorm, and indeed there is a 2-year old bug reporting exactly this.
how fix my phpunit.xml file explicitly in my PHPStorm project? thanks!!
Most helpful comment
I figured this out and I'm posting this for others to find. I am working in PHPStorm using the default phpunit configuration. With this config, PHPStorm does not explicitly load
phpunit.xmlfor its configuration; by default phpunit is meant to look for that file itself, as its docs say:However, in PHPStorm, if you do not specify a specific phpunit config file, it runs phpunit with a
--no-configurationswitch, meaning that it does not read the config file at all, so theAPP_ENVvalue is not set as expected.So the fix is to select your
phpunit.xmlfile explicitly in your PHPStorm project. Personally I consider this a bug in PHPStorm, and indeed there is a 2-year old bug reporting exactly this.