This Docker Compose configuration sets up an environment for testing WordPress plugins using PHPUnit.
It is based on these two repositories:
- Brad Traversy's Docker Compose File for WordPress
- Chris Zarate's WordPress Plugin/ Theme Development Docker Compose Configuration
I've also created a YouTube video to walk through the setup process. You can find that video here: https://youtu.be/CqVV5xXydBQ
-
Clone or fork this repository.
-
Run
docker-compose up -d
to compose WordPress and MySQL. The WordPress filesystem will be added to the project as a volume within thewordpress
directory. This may take some time, but it will usually be completed within a minute. -
Open
localhost:80
in the browser and perform the famous five-second install. -
To check for errors, run
docker-compose logs
.
- Run the following command to compose the PHPUnit container and its associated database:
docker-compose -f docker-compose.yml -f docker-compose.phpunit.yml up -d
-
If you need to check for errors, run
docker-compose -f docker-compose.phpunit.yml logs
. -
Enter the PHPUnit container with bash by running the following command:
docker-compose -f docker-compose.phpunit.yml run --rm wordpress_phpunit bash
-
Once youve entered the container, change to the
/app/bin/
directory. This directory should contain a shell script calledinstall-wp-tests.sh
. -
Within
/app/bin/
in thewordpress_phpunit
container, run the following command to install the tests:
./install-wp-tests.sh wordpress_test root '' mysql_phpunit latest true
- Exit the container by running
exit
.
-
Running
docker-compose up -d
should have created awordpress
directory in the project root. This directory should contain the complete WordPress file system. Add your plugin's directory towordpress/wp-content/plugins
. -
The
tests
directory at the root of the project should contain a file calledbootstrap.php
. Within that file, you'll see this code on line 20:
require dirname( dirname( __FILE__ ) ) . '/wordpress/wp-content/plugins/my-plugin/class-my-plugin.php';
-
Change
my-plugin
to match the directory of the new plugin that you created. -
If you want the example test to pass, create a file called
class-my-plugin.php
within your new plugin directory and add the following PHP within it:
<?php
/**
* Plugin Name: My Plugin
* Plugin URI: PLUGIN SITE HERE
* Description: PLUGIN DESCRIPTION HERE
* Author: YOUR NAME HERE
* Author URI: YOUR SITE HERE
* Text Domain: my-plugin
* Domain Path: /languages
* Version: 0.1.0
*
* @package My_Plugin
*/
/**
* My Plugin class
*/
class My_Plugin {}
Run the PHPUnit tests from the project root directory using the following command:
docker-compose -f docker-compose.phpunit.yml run --rm wordpress_phpunit phpunit
This video course on writing PHPUnit tests for WordPress is a useful resource for understanding the basics. After that, the PHPUnit documentation is very helpful.
To tear down the containers created by both compose files, fun the following two commands from the project's root directory:
docker-compose down --volumes --remove-orphans
docker-compose -f docker-compose.phpunit.yml down --volumes --remove-orphans
To serve the site to a port other than localhost:80
, change the first value of services/wordpress/ports
within the docker-compose.yml
file. For example, in order to serve to port 5000, this entry could be changed to - '5000:80'
.
This repository is based on the two repositories linked below. These repositories each contain other features, which are indicated below. It is likely that these other features could be added in future development.
- Brad Traversy's Docker Compose File for WordPress
- phpMyAdmin
- Chris Zarate's WordPress Plugin/ Theme Development Docker Compose Configuration
- WP-CLI
- MariaDB rather than MySQL
- Development Proxy Server