DEV Community

Nacho Colomina Torregrosa
Nacho Colomina Torregrosa

Posted on

Using github actions to execute your PHP tests after every push

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 and for continous integration i've choosen github 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
Enter fullscreen mode Exit fullscreen mode

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:

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
Enter fullscreen mode Exit fullscreen mode

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

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

Repository pipeline steps

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

Github phpunit step

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:

Top comments (0)