mstnorris's avatar

Set up Laravel 5.4 with Dusk using phpunit.xml, .env.dusk.local, and an sqlite in-memory database

The title says it all. I would like to know how to properly set up a new Laravel 5.4 project with Dusk, using an in-memory SQLite database.

I can run the tests, but I get an error: "No such table: users"

  • I have created a new Laravel 5.4 project
  • Installed Dusk
  • I can run the tests, and the first one works (navigating to the /login route) but the second where it tried to log in fails.

I have added a .env.dusk.local which contains

APP_ENV=local
APP_KEY=RANDOM_STRING_HERE
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://laravel54.dev

DB_CONNECTION=sqlite
DB_DATABASE=':memory:' // I've also tried just :memory: and also adding these details to the config/database.php file but to no avail

What am I missing?

0 likes
22 replies
bobbybouwmann's avatar

I have a demo project with Laravel Dusk here: https://github.com/bobbybouwmann/dusk-demo

I think you only need to copy the .env.example file and simply replace the database credentials with the sqlite credentials and it should work out of the box.

The approach that I used is to migrate the database for each test.

If you have more questions, just let me know :)

1 like
mstnorris's avatar

That doesn't work.

It's fine when using a mysql database. But either a sqlite file or an in-memory sqlite database do not work.

1 like
bobbybouwmann's avatar

Mmmh the sqlite database isn't working me either. It looks like it's not migrating properly. I tried to migrate the database first before starting the tests and that worked fine. However the DatabaseMigrations trait should do that for us anyway..

Not sure what's going on

1 like
mstnorris's avatar

I'm glad it's not just me then. Will open an issue on GitHub.

SaeedPrez's avatar

I had same/similar problem, it would copy the content of .env.dusk.local to .env upon execution, but it would not use the values.

mstnorris's avatar

I'm not sure what it does, or meant to do. The docs aren't very clear. As mentioned in the post, I created a .env.dusk.local (and also tried a .env.dusk.testing) but it doesn't use those values, it just uses the normal .env file and as such wipes my actual database!

brandonferens's avatar

Same issue. It's almost like the .env values are cached and used instead of the copied .env.dusk.local file. I attempted to clear this with php artisan config:clear, but to no avail.

brandonferens's avatar

I finally got somewhere. In the CreatesApplication trait, add a line that forces the tests to use the proper connection like this:

public function createApplication()
{
    $app = require __DIR__ . '/../bootstrap/app.php';

    $app->make(Kernel::class)->bootstrap();

    // Force (specifically Dusk) to use the testing connection. For some reason I
    // can't get to the bottom of, Chrome will use the testing connection, but
    // the actual Dusk tests will use the mysql connection. Very irritating.
    $app['config']->set('database.default','testing');


    return $app;
}
stevejamesson's avatar

@brandonferens That line in CreatesApplication didn't seem to help in my case. (Also using sqlite locally; :memory: doesn't work, but a separate database/testing.sqlite file does seem to work.)

The docs really aren't very clear about the dusk-specific .env file.

@mstnorris

I created a .env.dusk (neither .env.dusk.local nor .env.dusk.testing worked for me). Dusk seems to consume this file regardless of my environment. In there, I've got APP_MODE=testing, DB_CONNECTION=testing, APP_URL=[project.dev], and in my config/database.php I've defined a separate sqlite connection for testing:

'connections' => [
        'testing' => [
            'driver' => 'sqlite',
            'database' => database_path('testing.sqlite'),
            'prefix' => '',
        ],
],

This setup seems to work. Migrations are run against my test database, and both phpunit tests and dusk tests are running as expected.

3 likes
mstnorris's avatar

Thank you all, I'll check out this setup and confirm.

Agreed, the docs need improving otherwise one of us would have a definitive described solution.

mstnorris's avatar

So I wasn't able to get this to work, but ran composer update Framework 5.4.6 -> 5.4.10, laravel-dusk 1.0.3 -> 1.0.5 and now the migrations are run and the normal MySQL database isn't touched.

This proves that there was initially an issue with it.

I'll try a few different configurations and report back.

stevepop's avatar

@bobbybouwmann, thanks for sharing your dusk_demo repo. It really helped me to set up my tests and now I have phpunit and dusk running perfectly.

Thanks a bunch!

dabates77@gmail.com's avatar

So I'm having a similar problem.. I have the editor open and I can see my .env file getting replaced with the .env.dusk.local, however the problem is I am using artisan serve for local development and the .env file, even though changed, is not loaded into serve. I've tried create a command to run before dusk using the ProcessBuilder, but that doesn't seem to be helping.. does anyone have any examples of using the built in server and getting the env file to get reloaded?

Lyfeway's avatar

.env.dusk, .env.dusk.local, .env.dusk.testing is ok, only use MySQL database. here is my .env.dusk:

APP_ENV=testing
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_KEY=base64:FN5Rj5dCVWTQbC4tn2BkKmkwWSKOP8DxWlHoCRdvskI=

DB_CONNECTION=mysql
DB_DATABASE=dusk_test
DB_USERNAME=homestead
DB_PASSWORD=secret
1 like
jijoel's avatar

I wanted to chime in briefly with a similar issue, because when I was trying to get dusk to work with sqlite, this was the best page that came up (and I'm sure I'll forget, and see this again, later :-) ).

I set up Dusk in Homestead, with the intention of using sqlite for dusk tests (just using the standard config/database.php file). My manual test database is in mysql. When I'd attempt to use migrations while running dusk, I'd get a 'database does not exist' error.

After way too much troubleshooting, I realized that I needed to hard-code the DB_DATABASE environment variable. So, it should look like this:

DB_CONNECTION=sqlite
DB_DATABASE=database/database.sqlite

I believe the default will not work because the DB_CONNECTION gets cached, so actually does end up existing, and pointing to the wrong spot. We need to explicitly overwrite it.

wesmahler's avatar

What @jijoel mentioned is definitely the issue and a solution. An easier solution is to hardcode the DB_DATABASE value instead of relying on the ENV variable which gets cached incorrectly.

Change it to this in the database.php file.

       'sqlite' => [
            'driver' => 'sqlite',
            'database' => database_path('database.sqlite'),
            'prefix' => '',
        ],
onicial's avatar

Hello everyone. I'm facing the same issue with setting up Dusk with a MYSQL Database. Does anybody know, how to fix this issue with using a mysql database?

My extended config/database.php file

 'dusk' => [
            'driver' => 'mysql',
            'database' => 'dusk-testing',
            'host' => '127.0.0.1',
            'port' => '3306',
            'username' => 'root',
            'password' => '',
            'prefix' => '',
        ],

My .env.dusk files (,env,dusk, .env.dusk.local, .env.dusk.testing)

DB_CONNECTION=dusk
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=dusk-testing
DB_USERNAME=root
DB_PASSWORD=

My CreateApplication.php file

public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make(Kernel::class)->bootstrap();
        
        $app['config']->set('database.default','mysql');

        return $app;
    }

Any suggestions? It's so frustrating

Cheers, Stan

Please or to participate in this conversation.