Framework: APP_ENV not used when running tests

Created on 14 Jun 2019  路  2Comments  路  Source: laravel/framework

  • Laravel Version: 5.8.22
  • PHP Version: 7.3.6
  • Database Driver & Version: mysql 5.7

Description:

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.

Steps To Reproduce:

In a test:

$this->assertEquals('testing', env('APP_ENV'));

and this results in:

Failed asserting that two strings are equal.
Expected :'testing'
Actual   :'local'

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.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.

All 2 comments

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!!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

YannPl picture YannPl  路  3Comments

PhiloNL picture PhiloNL  路  3Comments

kerbylav picture kerbylav  路  3Comments

iivanov2 picture iivanov2  路  3Comments

klimentLambevski picture klimentLambevski  路  3Comments