Laravel

Using a Separate SQLite Database with Laravel Dusk

Laravel Dusk was released with Laravel 5.4 and gave developers an incredibly easy API to create and run automated browser tests. However, I came across an issue while running browser tests with SQLite.

I am not going to explain how to get started with Laravel Dusk. The documentation already does an excellent job and I would just be repeating what is already there.

The Problem

When I am developing Laravel locally, I like to use SQLite since it is easy to use and doesn’t require a database server to be run on the development machine. However, if you want to use SQLite with Dusk, it will use your development database by default. This was causing my database to be altered since I have the DatabaseMigrations trait within my tests.

The DatabaseMigrations trait will run your migrations with each test. Hence, my development database was being altered.

Why was this happening? It came down to how I had my .env file setup. Dusk was using my local .env settings which was pointing at my SQLite database.

After some digging around, I discovered Dusk will use it’s own .env file if it exists. The naming convention follows .env.dusk.{environment}, which in my case is .env.dusk.local.

But this doesn’t exactly fix the problem. Within the .env.dusk.local file, a new database configuration will need to be used to point the Dusk session at the other SQLite database.

The solution

Start by creating a new SQLite database within the database directory. I named mine dusk.sqlite.

Next, create a new database configuration within the config/database.php file.

<?php
// config/database.php
'dusk' => [
    'driver' => 'sqlite',
    'database' => env('DB_DATABASE', database_path('dusk.sqlite')),
    'prefix' => '',
],

If you haven’t created your dusk .env.dusk.local file, do so now and set the DB_CONNECTION to “dusk”. I just copied my .env file over and renamed it.

DB_CONNECTION=dusk
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=
# DB_USERNAME=homestead
# DB_PASSWORD=secret

It may also help to start the server with the dusk .env file too.

$ php artisan serve --env=dusk.local

Now start Dusk and watch it go.

Published by

rfmeier

Product developer at Rainmaker Digital and runner from Hampshire, Illinois. I work with php a lot; Spending most of my time digging into source code to see how it all works. You can find me on Twitter @rfmeier.

3 thoughts on “Using a Separate SQLite Database with Laravel Dusk”

  1. Setting:

    'database' => env('DB_DATABASE', database_path('dusk.sqlite'))

    in config/database.php for dusk connection didn’t work for me. I had to replace it with just:

    'database' => database_path('dusk.sqlite')

    and tests started running as expected.

    My assumption is that values from config file are read before dusk replaces values in .env file and so tests are run using the values from regular .env file (not .env.dusk.local)

    1. Aamdeusz,

      If DB_DATABASE had a value within your .env file, then that would have not used database_path('dusk.sqlite') as that is the default.

      Either way is fine. The only reason I attempted to read from the .env file first is to allow overriding.

Leave a Reply

Your email address will not be published. Required fields are marked *