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.

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.

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 my updates.
Subscribe to Receive Programming, Web development, Git and WordPress Tutorials & all things tech.