DEV Community

Paul Preibisch
Paul Preibisch

Posted on

Laravel Sail with PHPUnit Testing

Laravel Sail is an interface that is supposed to be an easy way to get started with using Docker and Laravel. Once it works it works great! But there were a few extra steps needed to get it running on my dev setup. While setting up sail, I had a difficult time getting PHPUnit working. Hopefully this post will help if you experience the same difficulties.

From Laravel's documentation, installing Sail is easy:

composer require laravel/sail --dev
php artisan sail:install

Before you running the next step however, you need to pay special attention to the a few .env file settings

Configure .env

1) Understanding DB_HOST
If you open the docker-compose.yml, which Sail installs, you will notice that they have named the mysql container 'mysql' (its under services). Please note, THIS is the name you need to use for DB_HOST

so set:
DB_HOST=mysql

2) Set FORWARD_DB_PORT=3309
Understanding the difference between FORWARD_DB_PORT, and DB_PORT is important. FORWARD_DB_PORT is what YOU will use to connect to your db while developing. Ie: if you set it to 3309 like I did, this is what you will put in your PHPStorm settings to access the DB. The Container however, uses the DB_PORT setting to connect to the db. So leave DB_HOST=3306.
If you are working on multiple Laravel projects at once, set each App's FORWARD_DB_PORT differently, ie: 3307,3308,3309 ...
and you can keep each Apps DB_HOST to 3306

3) Set the APP_PORT=8909
Next, set the APP_PORT to something custom in each Laravel APP. This is what you will use to access your web app locally
ie: 127.0.0.1:8909

Docker-compose.yml

4) Take a look at docker-compose.yml
Also make sure to look at the docker-compose.yml. This is the configuration docker will use to create the containers. In my case, my client was using php 7.4, so I had to change a few params. I replaced 8.1 with 7.4

Image description

Lets bring the Sail Up!

Ok, Once the above is done, type

sail up now, docker will build the containers for your app.

If you get a command not found error,
add this to ~/.bash_aliases

alias sail="bash ./vendor/bin/sail"

then reload your aliases using
source ~/.bash_aliases

Now try to run 'sail up' again

Finally, in order to use xDebug to debug your php, you will need to set your cli interpreter to point to your container:

Image description

PHPStorm Settings

Next, you will need to do this Container Tweak in case you are getting this error:

laravel SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: Name or service not known

First, get the container name in PHPStorm by clicking on the folder icon next to 'Docker Container ', next, change the Network mode from 'bridge' to '
to Obtain the name of your container, do

docker ps

Image description

I hope this helps you get on your way with Sail!
It's a great way to isolate your Laravel Projects!

Top comments (2)

Collapse
 
paulpreibisch profile image
Paul Preibisch • Edited

One other thing I would mention, is to duplicate the .env file, and save it as .env.testing, then change:

APP_ENV=testing
DB_DATABASE=testing
Enter fullscreen mode Exit fullscreen mode

then you can run:

sail artisan migrate --env=testing
Enter fullscreen mode Exit fullscreen mode

this will allow you to have the testing tables set up inside the testing db

Collapse
 
daviidy profile image
David • Edited

You should also add the username and password in your testing env

DB_USERNAME=<username>
DB_PASSWORD=<password>
Enter fullscreen mode Exit fullscreen mode