2023-03-28

Adding SQLite In-Memory Database for PHPUnit

Laravel by default uses MySQL for it's database setup, but I typically like to use SQLite when possible for simplicity and ease of use. When bootstrapping a new app with Sail and no database configured your PHPUnit config can use a quick tweak to use SQLite in memory.

Update your database.php inside the config directory to include a new option under the connections array.

'testing' => [
    'driver' => 'sqlite',
    'database' => ':memory:'
]

Then simply update the default phpunit.xml to use the new option.

- <env name="DB_DATABASE" value="testing"/>
+ <env name="DB_CONNECTION" value="testing"/>

You can run a sail artisan config:clear and now your tests will run with an in-memory SQLite database.