Setting up Automated PHP Testing on GitLab CI

I recently moved to GitLab from GitHub for private project hosting to save money. Another major deciding factor was GitLab CI, a free hosted continuous integration server which further save me more money which otherwise would have be spent on a Travis CI subscription.

In this tutorial, i will quickly go over the steps in setting up automated unit/integration testing with PHPUnit on GitLab CI.

Automated testing is running test cases where manual intervention is not required to run each one. This is usually in the form of writing test suites which have multiple test cases and a library and command line tool that runs the test suite or suites.

Note: you must have test suites written already for your project and a .gitlab-ci.yml file existing at the root of your project.

GitLab has a collection of tutorials and guides on setting up your CI pipeline depending on your project type.

For example, a typical PHP test will have the following .gitlab-ci.yml content.

# Select image from https://hub.docker.com/_/php/
image: php:5.6

# Select what we should cache
cache:
  paths:
  - vendor/

before_script:
# Install git, the php image doesn't have installed
- apt-get update -yqq
- apt-get install git -yqq

# Install mysql driver
- docker-php-ext-install pdo_mysql

# Install composer
- curl -sS https://getcomposer.org/installer | php

# Install all project dependencies
- php composer.phar install

services:
- mysql

variables:
  # Configure mysql service (https://hub.docker.com/_/mysql/)
  MYSQL_DATABASE: hello_world_test
  MYSQL_ROOT_PASSWORD: mysql

# We test PHP5.6 (the default) with MySQL
test:mysql:
  script:
  - vendor/bin/phpunit --configuration phpunit_mysql.xml --coverage-text

# We test PHP7 with MySQL, but we allow it to fail
test:php7:mysql:
  image: php:7
  script:
  - vendor/bin/phpunit --configuration phpunit_mysql.xml --coverage-text
  allow_failure: true

The above config expect that your project or one of your project dependency has a dependency on PHPUnit thus the vendor/bin/phpunit. Otherwise add the following commands in “before_script:” section before php composer.phar install to install PHPUnit.

- wget https://phar.phpunit.de/phpunit.phar
- chmod +x phpunit.phar
- mv phpunit.phar /usr/local/bin/phpunit

After which, replace vendor/bin/phpunit with just phpunit.

Information on setting up gitlab-ci.yml file for CI is available here.

With the above .gitlab-ci.yml setup done, follow the step below to activate pipeline (continous integration) in GitLab UI.

* Go to your project repository.

* Click on the pipeline top navigation memu and then click the Run pipeline button.

Gitlab pipeline

* Enter the branch name or tag to create CI builds for and then hit submit.

Create new GitLab CI pipeline

So, each time a commit is made to the branch or tag you’ve setup pipeline for, builds will be triggered to create the testing environment and run your (PHPUnit) test suites.

Below is how builds of a pipeline look when they pass.

GitLab CI builds

La Fin!

Don’t miss out!
Subscribe to My Newsletter
Invalid email address