dusk: Dusk env file not being loaded correctly
I have the following two files: .env and .env.dusk.local
.env:
APP_ENV="local"
APP_KEY=base64:358E4UyIq+Zbg+H+Pm03dYA2B/vz3ViPJ+U48ikg6qE=
APP_DEBUG=true
APP_LOG_LEVEL="debug"
APP_URL="http://localhost"
DB_CONNECTION="mysql"
DB_HOST="127.0.0.1"
DB_DATABASE="homestead"
DB_USERNAME="homestead"
DB_PASSWORD="secret"
CACHE_DRIVER="redis"
SESSION_DRIVER="redis"
QUEUE_DRIVER="sync"
.env.dusk.local
APP_ENV="local"
APP_KEY=base64:358E4UyIq+Zbg+H+Pm03dYA2B/vz3ViPJ+U48ikg6qE=
APP_DEBUG=true
APP_LOG_LEVEL="debug"
APP_URL="http://localhost"
DB_CONNECTION="sqlite"
CACHE_DRIVER="file"
SESSION_DRIVER="file"
QUEUE_DRIVER="sync"
But when I run php artisan dusk I get errors about the sqlite database not existing.
Illuminate\Database\QueryException: Database (homestead) does not exist. (SQL: select * from sqlite_master where type = 'table' and name = migrations)
So, I suspect that the env files are not correctly being copied and swapped into place. It seems they are simple being merged together, which is causing issues for tests not working.
I am currently using this on Manjaro Linux, and have not tried it on any other OS.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 22 (8 by maintainers)
@francoislevesque not necessarily. Dusk will backup your
.envand move.env.dusk.{APP_ENV}to.envso the browser can match the same environment as yours. This, however, fails to happen when usingphp artisan servebecauseservewill cache yourenvvariables the moment of starting to serve. This can be solved by runningphp artisan serve --env=dusk.local(switch local for anything you use locally). But all of you are suffering from different kinds of problem somewhat related to the same thing. If everyone were usingserve, it would make sense to implement adusk:serve, perhaps.This is still an issue
I opted for making another database array entry in
config/database.phpand used this in my
.env.dusk.local@taylorotwell Looks like
env()is being used from.envfile before dusk file is loaded. Is the environment loaded via.envbefore.env.dusk.localis copied to.env?I came by a very similar problem. I had
DB_DATABASEashomesteadin.env.dusk.localand when any of my tests accessed a route that was querying the database, I would get:I finally solved by changing:
To this:
It’s all explained in this answer.
Fixed this issue today by adding into the phpunit.dusk.xml file
UPDATE I also had to change my
.env.dusk.localto.env.dusk.testingphp artisan cache:clear --env=testingphp artisan cache:clearHaving a similar/related issue
I’m using Homestead on a Windows machine. When running
php artisan dusk, the.env’s content is replaced by.env.dusk.local’s content. However, the tests still use the “old” .env.My .env
My .env.dusk.local
config/database.php
While running the tests
LoginTest.php
The var_dump of
APP_ENVisLaravel_test, which is fine.However, the DatabaseMigrations trait is doing its migrations on the
homesteaddatabase instead oftesting.Also, in my blade file, I’m doing the following:
<h1>{{ config('app.name') }}</h1>The output on the dusk screenshot is “Laravel” instead of “Laravel_test”.
Partial Solution
Adding this line in
tests/Browser/CreatesApplication.phpto force the database change worked for me:However, it seems chrome is still using the old database connection since I still see “Laravel” instead of “Laravel_test” in the
<h1>.As @bbashy said, it looks like env() is being used from .env file before dusk file is loaded.
Edit It worked perfectly on anoter machine (Windows 10, similar specs, vagrant 1.9.2, Homestead 5.2.3). Still trying to figure out what’s wrong on the first machine.
@mustafaaloko I already did and got #249 and then I tried #250 to make it possible to easily extend, but also no luck because of the construct actions. Furthermore, I tried https://github.com/laravel/framework/pull/18973 to make #250 look better, but also no. Now I just gave up.
@bbashy I am not sure about your last assumption. Or maybe we’re talking about different issues.
But anyway, I tried running Dusk today while overriding some of the env values and as a result I don’t even have
APP_KEYfrom main.envfile loaded in tests and myAPP_DEBUGis assumed to befalsewhile.envexplicitly states it to be true. 😄Indeed, it seems like Dusk replaces the contents of .env file in it’s entirety rather than performing merge and the working workflow is as follows:
But this all sounds very confusing to me after overriding env variables in
phpunit.xml. I think it should be clearly stated in the docs how exactly Dusk processes.envfile.I had a similar issue using Homestead, which was permissions related. Dusk would dump the contents of .env.dusk.local into my .env but fail to perform the backup/replace, leaving my .env over-written with test settings.
In your case, I don’t think it can write into your .env period, which means it’ll just be using your local settings.
I believe the issue is in the code below, as well as restoreEnvironment(). For example, given you have a .env.dusk.local (returned by $this->duskFile()), the code assumes this gets written into .env, and moves on with the tests:
I would personally prefer this code to throw an exception if the backup/replace is not working.
That might be causing your issue - to verify, run a test php script from the command line which copies files in the app root, writes into an existing file, and performs an unlink. My fix was a vagrant reload, but it will depend on your setup.