Automated PHPUnit testing with WordPress using GitLab

The short version of this entire tutorial would be, simply clone

https://gitlab.com/reusable-tools/barebone-plugin

and replace the classes with your own classes and test cases with the ones you need in your plugin.

For quite some time I had been using PHP Unit testing, but it used to be manual, on my own system. Not good if you’re committing a lot of code on a lot of different projects everyday.

So I decided to switch to GitLab’s in built CI and shared runner support, since Github’s travis is free only for public repos whereas GitLab also gets the job done, without having you to make all your repos public.

Using wp-cli’s scaffold you can add PHP Unit Scaffold based on the plugin/theme you’re working on, only one drawback, it works fine for PHP 5.3 and PHP 5.6 but not with PHP 7.0 or 7.1. WordPress has changed its requirements for quite some time and have shifted to PHP 7.0 so thats the version we’d like to test our code with.

By default it adds docker image tetraweb/php:5.6 and if you try to change it to tetraweb/php:7.0 you’ll run into errors, because tetraweb is using latest version of PHPUnit and you need to downgrade it to work with WordPress, since all WordPress test cases and extension classes are written for an older stable version of PHP Unit.

Then I decided to tweak that docker image a bit, as described in this GitHub repo:

https://github.com/gagan0123/wp-testing-docker

and created gagan0123/php:7.0 and gagan0123/php:7.1 with older version of PHP Unit in place and it started working.

This will work fine with both the self hosted GitLab installations and GitLab.com accounts.

Leave a Reply

Your email address will not be published. Required fields are marked *