30 Days of Automated Testing:Using PHPUnit【D02】

Beginner: Setting up the Environment

WilliamP
3 min readJan 3, 2023

Download and set up Laradock

First, let’s download Laradock in the Home folder:

cd ~ && git clone <https://github.com/Laradock/laradock.git> Laradock

Next, let’s enter the laradock directory and copy the example environment file:

cd ~/Laradock
cp .env.example .env

Then open the .env file and pay attention to a few key values:

APP_CODE_PATH_HOST: This is the project folder and points to our project directory. In this case, we will input ../PHPUnit-test (where we will create the Laravel project later).

# Point to the path of your applications code on your host
APP_CODE_PATH_HOST=../PHPUnit-test

DATA_PATH_HOST: This is the data path, and when the Laradock database container is started, it will create related files and directories under this folder. The default value is ~/.laradock/data, and it is generally best to keep the default value.

# Choose storage path on your machine. For all storage systems
DATA_PATH_HOST=~/.laradock/data

PHP_VERSION: This is the PHP version number, and the default value is 8.0. We will keep the default value here.

# Choose storage path on your machine. For all storage systems
DATA_PATH_HOST=~/.laradock/data

In the ~/Laravel directory, enter the following command to start the workspace container:

#~/Laradock
docker-compose up -d workspace

The workspace container is a core container in Laradock and is used to run composer. We will also run PHPUnit in this container.

When starting the workspace container, it will also create the folder specified in the APP_CODE_PATH_HOST key value in the .env file (if it does not already exist). This allows you to easily access the code for your Laravel application from within the workspace container.

Enter The Workspace Container

After starting the workspace container, we need to enter the container to perform subsequent actions.

The default name of the newly started workspace container is laradock-workspace-1. Let's enter this container:

docker exec -it laradock-workspace-1 bash

If you get an error “Error: No such container:” when running the above command, it means that the container cannot be found. To list the running Docker containers, use the following command:

docker ps -a

Initialize The Laravel Project Folder

After entering the workspace container, let's initialize the project folder! (Note that when you enter the container, you will see that the current location is /var/www, which corresponds to the local path of ~/PHPUnit-test, which is the path pointed to by APP_CODE_PATH_HOST earlier)

Next, we will use composer to initialize the Laravel project folder:

composer create-project laravel/laravel .

This will start installing Laravel into the ~/PHPUnit-test folder. This may take a while, so feel free to go make a cup of coffee.

After the coffee is ready, the installation should be complete:

PHPUnit is installed along with Laravel during initialization. Let’s test it out:

./vendor/bin/phpunit

PHPUnit comes with two test cases pre-written, so you should see something like the following screenshot:

With this, we have completed the environment setup. Give yourself a pat on the back!

In the next article, we will write our first test.

If you liked this article or found it helpful, feel free to give it some claps and follow the author!

Reference

  1. https://laradock.io/
  2. https://laravel.com/docs/9.x

Articles of This Series

--

--