How to create a test database with Laravel Sail

Search for a command to run...

No comments yet. Be the first to comment.
Introduction I run a large WhatsApp group and I often need to mention everyone in the group. But there are no real solutions for mentioning all members. WhatsApp does not natively do it, and there was a Firefox browser extension, but it was abandoned...

Introduction This past week I was toying around with the HTML5 canvas element, trying to join two images together. At first, it seemed fine, but when I tried to reload the website, it was a mess. One image would load, but the other wouldn't. Investig...

Introduction I'm going to explain how to build a free plan for your Laravel Spark Next application. I will be using Paddle as the payment gateway, but the steps are almost identical with Stripe. Best of all? No credit card required for the free plan....

Introduction Sometimes you end up deploying an application to Dokku and then realize that you want to revert the changes you made. In this tutorial we'll go over how to roll back a Dokku deployment. Before we start Preface Keep in mind that rolling ...

I have worked on projects that required the production and testing database to be the same.
This could be because some features work in MariaDB, but not in SQLite. Or some bugs appear in MySQL, but not in PostgreSQL.
When you're working in a large project, you need to know that the testing database works exactly like the one in production. You can't allow a bug to appear in production because the test database was different.
Note: If the project is small, chances are you are fine using SQLite for testing.
Today, I'm going to guide you through setting up a testing database in your current database server, thanks to Laravel Sail.
This tutorial uses PostgreSQL as the database. But the idea is the same for any other database: create a separate database in your current database server.
We will use the database server declared in the docker-compose.yml file.
In this case, PostgreSQL.
services:
pgsql:
image: 'postgres:14'
First, enter PostgreSQL's CLI
sail psql
Note: If you are using MySQL, you can use
sail mysqlinstead.
Now, create a new database
CREATE DATABASE testing;
This database is separated from the default database that your application uses.
Now, modify the phpunit.xml file to use the new database and database server.
Should end up similar to this:
<php>
<env name="DB_CONNECTION" value="pgsql"/>
<env name="DB_DATABASE" value="testing"/>
</php>
Now, you can run the tests and see if they work.
sail test --parallel
It should look similar to this:
PASS Tests\Unit\ExampleTest
✓ example
Tests: 1 passed
Time: 0.7s
That was it. It's a one-time setup, that works like a charm.
If you have found this useful, then you should follow me, I will be posting more interesting content! 🥰
Congratulations, today you have learned how to create a test database in Laravel Sail.
Without needing to modify the docker-compose.yml file or creating more services.
Let me know if the tutorial was useful to you in the comments!