Using github actions to execute your PHP tests after every push
Tags: ci, php, phpunit, symfony
Published: 2023-02-25 11:09:54

I've been developing my own symfony bundle and I would like to share with you how I'm driving continuos integration to execute my unit tests. 
For testing purposes I'm using [phpunit](https://phpunit.de/) and for continous integration i've choosen [github actions](https://github.com/features/actions).

> This post deals with an easy way of driving tests and continous integration but i think it can serve as a basis for more advanced configurations.

### Adding tests

From your project root, install phpunit using composer as you can see below:

```
   composer require --dev symfony/test-pack
```

After installing phpunit, you already can write your tests under _tests/_ directory which will be automatically created after phpunit installation. 

After writing tests, we are ready to use Github actions so tests will be executed for every push. 

### 1.- Create continous integration file

First of all, you have to create directory _.github/workflows_ under root directory. Then, create a yaml file, with a name of your choose, where we will set continous integration configuration.

### 2.- Add content to continuos integration file

Let me show you how my ci file looks like:

```yaml
name: CI

on: [push]

jobs:
  build-test:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - run: echo "The ${{ github.repository }} repository has been cloned to the runner."
      - uses: php-actions/composer@v6
      - run: echo "Composer dependencies have been installed"
      - run: vendor/bin/phpunit
```

Let's describe it line by line:

- **name**: Identifies pipeline name (as pipeline we understand all jobs we're going to execute)

- **on**: Event which will trigger pipeline (in my case on every push)

- **jobs**: Jobs we are going to execute. Each job contains one or more steps. As we can see, mi ci file executes one job named _build-test_

- **runs-on**: Specifies the operating system on which job will run. In my case the latest ubuntu version

- **steps**: List all steps to execute in order

Now, let's describe each step:

#### Checkout repository

This step checkouts code from repository. To do it, it uses predefined action "_actions/checkout@v3_" so we have not run any git command.

After checkout, we show a message using _echo_ linux command. Variable _github.repository_ is an available var which holds the repository name. To use it we have to format it like we can see in the file string.

#### Install composer dependencies

Now we want to install composer dependencies before executing tests, otherwise required libraries wouldn't be available. As we did in first step, we use a predefined action "_php-actions/composer@v6_" which do the work for us. 

After composer finishes, we show a message which informs that composer dependencies have been installed.

#### Run tests

As composer dependencies are now installed, we are ready to run phpunit tests. To do it we only have to tell ci file to execute them using _run: vendor/bin/phpunit_.

#### Visualizing pipeline

After pushing code, we can login into our github account, go to our repository and click over Actions tab.


![Repository actions list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3k9abf0ezh61kno38awz.png)

If we click over the last pipeline executed we will see all steps have been executed successfully.


![Repository pipeline steps](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2k8f2udf0a141v75xlpt.png)

Finally, if we click on the phpunit step, we will see step logs and also tests have passed successfully.


![Github phpunit step](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bozmqa5p7nw79sr5t9ka.png)

As yo can see, we've built a simple pipeline (github refers them as workflows) where, on every push, unit tests are executed to ensure code consistency. 

You can see the entire code on [my github](https://github.com/icolomina/api_one_endpoint): 

> If you enjoy my content and like the Symfony framework, consider reading my book: [Building an Operation-Oriented Api using PHP and the Symfony Framework: A step-by-step guide](https://amzn.eu/d/3eO1DDi)