Travis CI WordPress configuration and setup for PHP 5.6 and 5.5 for composer and PHP Unit tests. Read more about Continuous Integration from Martin Fowler.
- With this setup, WordPress is fully loaded before tests are run.
- Disable
wp_mail
to prevent tests from failing - Any WordPress hooks will not be implemented - like
add_action
andadd_filter
. You can configure it to be otherwise but for my needs, I have not (no instructions for this). - This configuration is only setup for composer package projects (using composer). Edit the
composer.json
to your specification and needs.
Composer's default is PHP Unit 4.8 because I need compatibility with PHP 5.5.9. If you do not have these requirements update the composer.json
file accordingly.
Use this project as your base configuration and you are ready to go. Otherwise, copy the files and settings you need.
When you push to GitHub with Travis CI integrated your Tests in the tests
folder will be run as expected.
Here is the Travis CI documentation you will care most about:
- Configuring PHP
- Including MySQL
- Default Environment Variables
- Speed Up Tests
- Complex Builds (don't set
script: ./scripts/run-tests.sh
like in the docs. That will bypass thephpunit.xml
file and test nothing - only do this if you are sure it is what you want. You will want to use .sh files for provisioning)
When doing local tests I use Laravel Homestead vagrant box add laravel/homestead
. Then from the project directory like ~/Code/project
I run a few command on the VM to download and install WordPress. Finally I use PHPStorm to run tests with coverage.
Create the wordpress
database on the guest machine Homestead:
mysql -e "SET NAMES utf8; create database IF NOT EXISTS wordpress;" -uroot
Download and install WordPress with wp-cli.phar
:
curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
./wp-cli.phar core download --allow-root --path=wordpress
./wp-cli.phar core config --allow-root --dbname=wordpress --dbuser=homestead --dbpass=secret --dbhost=127.0.0.1 --path=wordpress
./wp-cli.phar core install --allow-root --admin_name=admin --admin_password=admin --admin_email=admin@example.com --url=http://tests.wp --title=WordPress --path=wordpress
In the Homestead.yaml
file I add the wordpress site:
sites:
- map: tests.wp
to: /home/vagrant/Code/project/wordpress
In the host computers hosts
file I point tests.wp
to the Homestead IP. In my Homestead.yaml
file it is:
ip: "192.168.10.10"
So, in my hosts
file on my Mac using Hostbuddy I add a new host and flush the DNS cache on my machine:
192.168.10.10 tests.wp
If you are running tests form the host computer then be sure to edit the wp-config.php
file to that configuration. Here is what I do because the MySQL port is forwarded on the host computer:
/** MySQL hostname */
if( php_sapi_name() !== 'cli' || gethostname() == 'homestead' ) {
define('DB_HOST', 'localhost');
} else {
define('DB_HOST', '127.0.0.1:33060');
}
If you are running tests on the guest machine, Homestead, you do not need to do this.
If you need help with Circle CI this is a great resource http://blog.wppusher.com/continuous-integration-with-wordpress-and-circleci/