Skip to content

Setting up PHPUnit for PHPStorm

Arnaud Broes edited this page May 23, 2019 · 4 revisions

PHPUnit (based on xUnit) is the official unit testing framework for WordPress. Here is the official documentation - https://make.wordpress.org/core/handbook/testing/automated-testing/phpunit/.

At the moment of writing this article, WordPress is only compatible with the 7.x release tree of PHPUnit.

Required files

In order to set up PHPUnit for All in One SEO Pack, first download a Subversion (SVN) client such as TortoiseSVN and checkout the following repository - https://develop.svn.wordpress.org/trunk/. Alternatively, you can download the repository via Git here - https://github.com/WordPress/wordpress-develop

This repository contains most of the files we need to get started and will act as the global installation of PHPUnit for WordPress.

You also need to have Composer installed.

Configuring WAMP

We'll need to add a new virtual host in our AMP stack application. In our case, this is the WAMP software.

Apart from a virtual host entry and empty directory, we also need to add an empty MySQL database through phpMyAdmin.

We now need to rename the "wp-tests-config-sample.php" file in the root folder of the repository we just downloaded by removing the "sample" part.

Then, configure it by specifying the target database and relevant user login (default for WAMP is "root" with a blank password) and define all security keys by randomly generating them through the included link.

This configuration file will be used by PHPUnit to set up a fresh install of WordPress to run the tests on.

Configuring PHPStorm

In PHPStorm we'll need to do two things: add a new Debug Configuration and add a new Test Framework.

Debug Configuration

Create a new Debug Configuration through the debug dropdown in the top right of the screen. Select the "PHPUnit" template and follow these two steps:

  1. Select the "Defined in the configuration file" radial, then check "Use alternative configuration file" and select the "phpunit.xml.dist" file found in All in One SEO Pack's root directory.
  2. Add "WP_TESTS_DIR" as an environment variable. The path (value) should be set to the "tests/phpunit" subdirectory of the repository we downloaded at the beginning of this tutorial.

Now our Debug Configuration is complete.

Test Framework

In order to add a Test Framework, we'll need to download PHPUnit 7.x again through Composer so that we can target the "autoload.php" file. Here is the official "Getting Started" page -
https://phpunit.de/getting-started/phpunit-7.html.

Personally, I downloaded the package (see "vendor" folder) to the same directory where I previously cloned the repository from wordpress.org, but you can really add it anywhere you like. Use the following command:

composer require --dev phpunit/phpunit ^7

If all went well, you should have gotten a pop-up to add a Test Framework after you completed adding the Debug Configuration. Otherwise you can find this section under Languages & Frameworks > PHP > Test Frameworks. Select the "PHPUnit Local template" and follow these two steps:

  1. Select the "Use Composer autoloader" radial (which should be the default to begin with) and target the autoload.php file at the root of the "vendor" directory.
  2. Then, check "Default configuration file" and again target the "phpunit.xml.dist" file in All in One SEO Pack's root directory.

You should now be able to run unit tests for All in One SEO Pack by running the Debug Configuration you added earlier.

Debug All in One SEO Pack during unit tests

In order to debug All in One SEO Pack during unit testing, go to Settings > Language & Frameworks > PHP and include the path to the repository we downloaded earlier.

Some useful resources