Laravel Sail with PHPUnit Testing
Published: 2022-09-04 03:45:53

[Laravel Sail](https://laravel.com/docs/9.x/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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mj5s4k2ypve0att4eosi.png)


## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9i4uq7uvcrevykfafebj.png)

## PHPStorm Settings
Next, you will need to do this [Container Tweak](https://github.com/lando/docs/issues/7) 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 '<the-name-of-your-container'>
to Obtain the name of your container, do 

`docker ps`

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t9euug17o1koi3uez48z.png)



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