WP-CLI allows us to quickly implement unit testing using PHPUnit. This post will guide you through the process of setting PHPUnit up to test must-use plugins in Local by Flywheel.
By default WordPress only allows single-file plugins inside the mu-plugins directory. Bedrock implements an autoloader for mu-plugins in directories. Check out https://github.com/wemakecustom/wp-mu-loader if you aren’t using Bedrock.
1. Install PHPUnit
Install through Composer:
composer require phpunit/phpunit "^7.0"
2. Scaffold Plugin tests
The first step is to right-click your website in Local, and select “Open site shell”. From your WP-directory, use following command:
wp scaffold plugin-tests --dir=app/mu-plugins/custom-plugin
The dir-flag should be relative to your current working directory.
3. Initialize the testing environment
Change your working directory to your mu-plugin:
Then initialize the environment:
bash bin/install-wp-tests.sh local-tests root root localhost:/Users/tombroucke/Library/ApplicationSupport/Local/run/Nv9pAB3wp/mysql/mysqld.sock
You will have to replace the socket location by the location you find in Local > Site > Database.
You will find the socket location inside Local > Site > Database. It is important to know that I have a symlink from “ApplicationSupport” to “Application Support”. In fact iTerm 2 has created this symlink for me.
4. Start testing
Add some tests to the tests directory and run PHPUnit