Laravel 8 Setup Php Unit Testing With Sqlite Database These are the steps I take to set up Laravel 8 ready for fresh unit tests.

Laravel PHP Unit testing

Once I was ready to begin unit testing with Laravel 8 I found that the advice often offered on Stackoverflow wasn't as accurate as expected. This was the same for numerous tutorials found in any of the search engines.

What worked for an earlier version of the Laravel framework wasn't working for a later version.

A key piece of information was missing. Following the general approach to setting up with the SQLite database my early experience was the same of that of many others.

What would happen was any migration would impact on the MySql database, or the default database.

Setup the SQLite database

Here are the steps I use each time with a new Laravel installation. The first step is to set your configuration. Put this configuration, below, in the ./config/database.php file.

'connections' => [

    'sqlite_testing' => [
        'driver'   => 'sqlite',
        'database' => storage_path().'/database.sqlite', 
        'prefix'   => '',
    ], // ...

If you opt to store your database in a file, you'll only need to migrate your database once only. However if you decide to store your database in memory then you'll need to migrate your database in your test setUp() and tearDown() methods.

I decide to store the database on the file system; therefore there is no need to use the various traits available.

Setup PHPUnit's XML file

The next step is to make a slight change to the phpunit.xml file. In between the <server name="CACHE_DRIVER" value="array"/> and <server name="MAIL_MAILER" value="array"/> settings, copy and paste the following below and save the changes.

<server name="TEST_DB_CONNECTION" value="sqlite_testing"/> <server name="TEST_DB_DATABASE" value="./database.sqlite"/>

Nearly there. The third step is to now define those two constants in your .ENV file. In your .ENV file, locate your default database settings. Below those, paste in the following and save the changes.

TEST_DB_CONNECTION=sqlite TEST_DB_DATABASE=/storage/database.sqlite

If you are determined to use memory as your storage method then use the following instead.

TEST_DB_DATABASE=:memory:

The next step is very important. This was the key piece of missing information for me to get the set up working as expected without wiping clean my default database.

Complete your Setup

Look under the ./tests/ root directory for the CreatesApplication.php file. Found it? Inside the public function createApplication and before $app = require __DIR__.'/../bootstrap/app.php'; you put the following below first.

putenv('DB_CONNECTION=sqlite_testing');

This will ensure that running your unit tests will not destroy your existing default database. Then it is simply run the following artisan commands and your all ready to go.

php artisan cache:clear php artisan config:clear php artisan migrate --database=sqlite_testing

One more thing. With any TEXT data column type, SQLite will complain if the column is NOT NULL. In your Laravel migration's use $table->text('column')->nullable(); for a happy life.