WPBrowser and Travis CI 05

Setting up the “I’d like this” WordPress plugin to use Travis CI, Codeception and WPBrowser.

The tools

Setting up wp-browser to support Travis CI took some time and effort but allowed me to understand the drill and develop reusable scripts in the process.
This made installing WordPress and setting up the Codeception and wp-browser environment needed to test the “I’d like this” plugin a breeze.

The plugin setup file

The travis ci configuration file in the plugin root folder is a modified copy of WPBrowser own configuration file; the main differences lie in a single site installation and in the extra step required to move the plugin inside WordPress and activating it:

sudo: required

language: php

notifications:
  email: false

php:
  - '5.6'
  - '7.0'

services:
  - mysql

cache:
  directories:
    - vendor
    - $HOME/.composer/cache

env:
  - wpDbName=test wpLoaderDbName=wploader wpDbPrefix=wp_ wpUrl=wordpress.dev wpAdminUsername=admin wpAdminPassword=admin

before_install:
  - mysql -e "create database IF NOT EXISTS $wpDbName;" -uroot
  - mysql -e "create database IF NOT EXISTS $wpLoaderDbName;" -uroot

install:
  - composer update --prefer-dist

before_script:
  # set up folders
  - mkdir -p $HOME/tools /tmp/wordpress

  # install wp-cli
  - wget https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -P /tmp/tools/
  - chmod +x /tmp/tools/wp-cli.phar && mv /tmp/tools/wp-cli.phar /tmp/tools/wp
  - export PATH=$PATH:/tmp/tools:vendor/bin

  # install Apache and WordPress setup scripts
  - git clone https://github.com/lucatume/travis-apache-setup.git /tmp/tools/travis-apache-setup
  - chmod +x /tmp/tools/travis-apache-setup/apache-setup.sh
  - chmod +x /tmp/tools/travis-apache-setup/wp-install.sh
  - ln -s /tmp/tools/travis-apache-setup/apache-setup.sh /tmp/tools/apache-setup
  - ln -s /tmp/tools/travis-apache-setup/wp-install.sh /tmp/tools/wp-install

  # download and install WordPress
  - wp-install --dir=/tmp/wordpress --dbname="$wpDbName" --dbuser="root" --dbpass="" --dbprefix=wp_ --domain="wordpress.dev" --title="Test" --admin_user=admin --admin_password=admin [email protected] --theme=twentysixteen --empty 
  - cd /tmp/wordpress

  # move the plugin into WordPress folder
  - mv $TRAVIS_BUILD_DIR /tmp/wordpress/wp-content/plugins/idlikethis
  - export PLUGIN_DIR="/tmp/wordpress/wp-content/plugins/idlikethis"

  # activate the plugin in WordPress
  - wp plugin activate idlikethis

  # flush rewrite rules
  - wp rewrite structure '%postname%' --hard

  # export a dump of the just installed database to the _data folder
  - cd /tmp/wordpress
  - wp db export $PLUGIN_DIR/tests/_data/dump.sql

  # set up Apache virtual host
  - sudo env "PATH=$PATH" apache-setup --host="127.0.0.1" --url="$wpUrl" --dir="/tmp/wordpress" 

  # Get back to the plugin dir
  - cd $PLUGIN_DIR

script:
  - codecept run

Beside unit tests that, relying on PhpUnit only requires PHP to run, the plugin tests will run acceptance, functional and integration tests using all the modules provided by the WPBrowser module.

Final thoughts

Travis CI setup proved to be easy and reusable enough not to become a chore to maintain and update and proved to be, even in this experimental phase of mine, a great tool to spot bugs and find inconsistencies.

I appreciate your input