14. Dezember 2019

PHP Matrix Testing for WordPress Plugins with GitHub Actions

For maintainers of public plugins it is essential that our plugins also run on older versions of WordPress. Nowadays it is not as bad as WordPress is dropping support for older PHP versions but nevertheless it is crucial that our plugins work with versions that are probably older than the fancy shiny new version we developers tend to use. How can we reliably be sure that our plugins also work in older versions of PHP? Since we don’t want to install every PHP version ourselves and tediously test everything by hand there needs to be a better way to do it. And there is.

The best thing to make sure your code works are unit tests. When the source code has a high enough test coverage every code gets executed and we can be pretty sure that when we run the unit tests on an older PHP version the plugin also runs on this version.

How can we achieve this? First we need some kind of infrastructure our code can run on. There are many providers out there like Circle Ci, Travis or several others. While this works perfectly there is also a new player in the game of automated tasks: GitHub Actions. GitHub Actions can run on whatever event you want to listen to: Pushs, Issues Created, etc. Hence we can also run our CI (Continuous Integration) tasks on it.

Matrix testing describes the task of testing a software on different version of the programming language or other factors. For our case we want to run our tests on multiple versions of PHP. We can achieve that with the following config:

name: CI

on: [push]

    runs-on: ${{ matrix.operating-system }}
        operating-system: [ubuntu-latest]
        php-versions: ['7.2', '7.3']
    name: PHP ${{ matrix.php-versions }} Test on ${{ matrix.operating-system }}
    - name: Checkout
      uses: actions/checkout@v1

    - name: Setup PHP
      uses: shivammathur/setup-php@v1
        php-version: ${{ matrix.php-versions }}
        extension-csv: mbstring, intl #optional, setup extensions
        ini-values-csv: post_max_size=256M, short_open_tag=On #optional, setup php.ini configuration
        coverage: xdebug #optional, setup coverage driver
        pecl: false #optional, setup PECL

    - name: Check PHP Version
      run: php -v

    - name: Composer install
      run: composer install --optimize-autoloader --prefer-dist
    - name: Install WP Tests
      run: bash bin/install-wp-tests.sh wordpress_test root root localhost latest
    - name: phpunit tests
      run: ./vendor/bin/phpunit

This config assumes that you have setup your unit tests with wp scaffold plugin-tests a very helpful command to get your plugin started with unit tests. What it does is to run 2 times when you push your code, once for each PHP version set in the key php-versions in the config file. You need to place this config file under the folder .github/workflows/anynamehere.yml in your plugin repository. When you now push your code to GitHub the tests in your tests directory run automatically and you will see if anything fails or not.

On the left you can see the different tested PHP versions

GitHub Actions are a very good way to unit test your plugin and you can also do PHP version compatibility testing for free if your unit tests have a high enoug test coverage.

Did you like this article? Drop me a line on twitter @revenwo