Adding Unit Tests for Mindbody API

The mz-mindbody-api wordpress plugin has grown over the years and it’s getting difficult to keep track of all the various parameters and features.

It’s probably ready for a refactoring, but in the meantime, I aim to get some phpunit tests installed.

I’ve been using MAMP a lot for development and wanting to move toward a more DevOps approach, but don’t like the memory issues that come with Vagrant and VirtualBox.

So have decided to play around with Docker for WP development.

Followed this tutorial, then this one and got WP running beautifully. Using wpcli to alias docker-compose run --rm my-wpcli I can run wp-cli commands like:

wp core install --url="localhost:8080" --title="WordPress" --admin_user="admin" --admin_password="prollynotadmin" --admin_email="admin@email.com"

And: wpcli post list.

So removed the containers with docker-compose rm -v, added phpunit to the docker-compose.yml file (gist here) and ran docker-compose up -d again.

Now I copied the plugin repo to the plugins directory, and following the Pippin’s Plugins tutorial ran: wpcli scaffold plugin-tests mz-mindbody-api

docker-compose run --rm my-phpunit /usr/local/bin/phpunit --help

or simpler:

docker-compose run --rm my-phpunit phpunit --help

The following confused me at first. When I run docker ps or docker-compose ps I see a list of “containers” the names of which consist of name from dockerfile, prefixed by the project name (default: directory name) and suffixed by the number one. When referencing the container in docker-compose, you just reference the name from the dockerfile. With straight Docker commands you use the whole name.

So to install the WP test suite for phpunit:

docker exec wp_my-wp_1 bash wp-content/plugins/mz-mindbody-api/bin/install-wp-tests.sh wordpress_test root 'password_from_docker_compose' localhost latest

And:

$ docker exec wp_my-wp_1 bash wp-content/plugins/mz-mindbody-api/bin/install-wp-tests.sh wordpress_test root 'dockeradmin' localhost latest
+ install_wp
+ '[' -d /tmp/wordpress/ ']'
+ mkdir -p /tmp/wordpress/
+ [[ latest == \n\i\g\h\t\l\y ]]
+ [[ latest == \t\r\u\n\k ]]
+ '[' latest == latest ']'
+ local ARCHIVE_NAME=latest
+ download https://wordpress.org/latest.tar.gz /tmp/wordpress.tar.gz
++ which curl
+ '[' /usr/bin/curl ']'
+ curl -s https://wordpress.org/latest.tar.gz
+ tar --strip-components=1 -zxmf /tmp/wordpress.tar.gz -C /tmp/wordpress/
+ download https://raw.github.com/markoheijnen/wp-mysqli/master/db.php /tmp/wordpress//wp-content/db.php
++ which curl
+ '[' /usr/bin/curl ']'
+ curl -s https://raw.github.com/markoheijnen/wp-mysqli/master/db.php
+ install_test_suite
++ uname -s
+ [[ Linux == \D\a\r\w\i\n ]]
+ local ioption=-i
+ '[' '!' -d /tmp/wordpress-tests-lib ']'
+ mkdir -p /tmp/wordpress-tests-lib
+ svn co --quiet https://develop.svn.wordpress.org/tags/4.7.2/tests/phpunit/includes/ /tmp/wordpress-tests-lib/includes
wp-content/plugins/mz-mindbody-api/bin/install-wp-tests.sh: line 81: svn: command not found

So

docker exec wp_my-wp_1 apt-get update 
docker exec wp_my-wp_1 apt-get install -y wget git curl zip vim
docker exec wp_my-wp_1 apt-get install -y apache2 subversion libapache2-svn libsvn-perl

Run again and get: mysqladmin: command not found.
Seems it’s a different container that runs the database.

docker exec wp_my-wpdb_1 mysqladmin create wordpress_test --user=root --password=docker_pass --host=localhost --protocol=tcp

Coming along!

$ docker exec wp_my-wp_1 phpunit
rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:247: starting container process caused "exec: \"phpunit\": executable file not found in $PATH"

OK. At this point, I had come accross a very robust full stack wrapper: https://github.com/chriszarate/docker-compose-wordpress. And am using it.

Note, if you already have wp-cli installed and want to remove: https://danielbachhuber.com/tip/delete-wp-cli/