Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.8 .env.testing file is not working

Tags:

I created a .env.testing file with my credentials, everything the same as .env besides a different table name for the database.

I tried php artisan config:clear which deletes the cached config file in bootstrap/cache/config.php and it broke the database connection. If I re-cache the file by running php artisan config:cache the cached file is back, but without the credentials in the .env.testing file. When I rerun PHPUnit, it connects to the wrong DB, the DB name that is stored in .env not .env.testing.

Is this for real? Did the latest Laravel release break test environments?

Here is what the docs read: (found here: https://laravel.com/docs/5.8/testing)

"You are free to define other testing environment configuration values as necessary. The testing environment variables can be configured in the phpunit.xml file, but make sure to clear your configuration cache using the config:clear Artisan command before running your tests!

Also, you may create a .env.testing file in the root of your project. This file will override the .env file when running PHPUnit tests or executing Artisan commands with the --env=testing option."

UPDATE

I finally was able to override the database that is stated in the .env by adding the database override in the phpunit.xml file; the .env.testing is still not working. The docs are misleading at the least.

like image 556
zeros-and-ones Avatar asked Jun 14 '19 21:06

zeros-and-ones


People also ask

Where is the Laravel .env file?

env file must be in the root directory of your Laravel app.

What is .env file in Laravel?

. env file, as its name suggest, is a local where you put all your environment setup, such as database credentials, cache drivers and etc. Everything that is about the server that the project is running, and may have different values for different servers, are setup here.


2 Answers

I was having this same problem with Laravel v5.6. I set up a .env.testing file with a different username, password, and database but my tests were always running in the main .env database.

It looks like you need to specify the test environment when running the config cache command. After running this command, it worked as described in the docs:

php artisan config:cache --env=testing 
like image 180
rachelderp Avatar answered Sep 20 '22 17:09

rachelderp


This is what worked for me:

In phpunit.xml I had to define <env name="APP_ENV" value="testing"/> inside the <php> block to make PhpUnit load .env.testing instead of .env


It seems like the Laravel docs are wrong and .env.testing is not hard-coded anywhere in Laravel or PhpUnit, it reads the environment file for whatever APP_ENV is specified in phpunit.xml. You could even rename this to .env.phpunit or anything else if you define it in phpunit.xml in this format: <env name="APP_ENV" value="phpunit"/>

like image 42
Denes Papp Avatar answered Sep 20 '22 17:09

Denes Papp