Testing under OS X with Travis CI

In preparation for the release of Sculpin 3 (the upcoming version of the static site generator that runs this blog and many others), I recently spent some time exploring how to tests for PHP projects on non-Linux operating systems using TravisCI.

I realized while working on the upcoming release that I was always running the app under OS X but the CI tests were always running under Linux. This was not ideal for a tool that was intended to run on any OS.

Originally I wanted to ensure that Windows usage was working, but that proved more challenging than expected. (I ended up having to manually verify that on a VM.) However, Travis was able to handle OS X, and others have already used my findings to optimize their configurations.

Anyway, Travis lets you define a "Matrix" of configurations for tests. I took the dist and language parameters that were being set project-wide, and migrated them into the Matrix definition. This allowed me to have granular control over those properties.

I also learned that Travis supports an addons key, which I used to tell it to install php and composer under OS X using Homebrew. This was very helpful for keeping the configuration clean and simple.

Before (partial example):

language: php

dist: trusty
sudo: false

matrix:
  include:
    - php: 7.2
      env:
        - DEPS=latest

before_install:
  - phpenv config-rm xdebug.ini || true

script:
  - if [[ $TEST_COVERAGE != 'true' ]]; then composer test ; fi

After (also a partial example):

addons:
  homebrew:
    packages:
    - php
    - composer

matrix:
  allow_failures:
    - os: windows
      language: bash
  include:
    - os: linux
      dist: trusty
      language: php
      php: 7.2
      env:
        - DEPS=latest
    - os: linux
      dist: trusty
      language: php
      php: 7.3
      env:
        - DEPS=latest
    - os: osx
      env:
        - DEPS=latest

before_install:
  - if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then phpenv config-rm xdebug.ini || true; fi

script:
  - if [[ $TEST_COVERAGE != 'true' ]]; then composer test ; fi

A lot of extra stuff has been sliced out of this example, if you want more detail you can view the full diff on github.

On OS X, language: php and the phpenv tool aren't necessary because of the Homebrew settings. However, this does leave the Travis interface reporting OS X as a "ruby" environment. Misleading, but currently unavoidable.

I'll have to take another pass at the Windows matrix entry at some point in the future. For now, I've flagged it as "allowed to fail" as seen at the top of the matrix YAML block in the "After" example.

Advice is always welcome if you know of a cleaner way to organize this or to get PHP tests with Composer working nicely under Windows on Travis. Please leave a comment using Disqus or file a PR for the Sculpin project on GitHub.

Thanks for reading! :)


Published: December 10, 2018

Categories: howto

Tags: dev, development, coding, php, howto, sculpin, testing

comments powered by Disqus