Why phpunit is not getting the correct APP_ENV as specified in phpunit.xml?
I had the same problem, .env variables seemed to be overriding the ones from my phpunit.xml, here are some of the solutions that could work:
- Specify which phpunit.xml file to use when running phpunit, it may be using a wrong one.
vendor/bin/phpunit --configuration [path to your phpunit.xml]
- Clean the config cache.
php artisan config:cache
- If you're in a Docker container, the above action may not work for you. What I did was to delete manually the
bootstrap/cache
files.
Hope it helps!
I try to answer myself with the best option I found.
If you set ENV variables at docker-compose.yml file you won't be able to overwrite them with phpunit.xml directives such as:
<env name="APP_ENV" value="testing"/>
Then you should opt for removing (like in this example) APP_ENV variable set from docker-compose.yml
And rely on .env Laravel file
APP_ENV=local
With this setup, phpunit will be able to overwrite the APP_ENV to "testing"
I'm still not 100% sure this arrangement is needed, with all docker agent versions. Another host I have with another Docker version behaves differently.
Use config('app.env')
instead. env()
values might be cached, in which case php artisan config:clear
might help, though I've also had problems with accessing env
values from command line environments.
More info here and here.
use
<env name="APP_ENV" value="testing" force="true"/>
<env name="CACHE_DRIVER" value="array" force="true"/>
<env name="SESSION_DRIVER" value="array" force="true"/>
<env name="QUEUE_DRIVER" value="sync" force="true"/>
<env name="DB_CONNECTION" value="sqlite_testing" force="true"/>
Without the force parameter, it won't work. See this issue: https://github.com/sebastianbergmann/phpunit/issues/2353 and the merged PR: https://github.com/sebastianbergmann/phpunit/pull/2723