I’ve begun using PHPUnit, WordPress, and MAMP to introduce unit testing to my theme and plugin development. Obviously, this requires an installation of PHPUnit and the WordPress testing framework. Because I use MAMP for local development, I had to do some additional customization to get the frameworks setup.
To install PHPUnit, you’ll need to use Pear. It’s included with MAMP, but you’ll likely need to update it. In a terminal session, issue the following command:
$ cd /Applications/MAMP/bin/php/php5.3.6/bin $ sudo ./pear upgrade pear
In the comments, Japh noted that the received an error when trying to run Pear. If you experience this, run the following command:
$ mv /Applications/MAMP/bin/php/php5.3.6/conf/pear.conf /Applications/MAMP/bin/php/php5.3.6/conf/pear.conf.old
The problem is that Pear’s configuration file is out of date – this just moves it to a file with a different name.
Next, tell Pear to use repositories that we specify in the command line:
$ sudo /Applications/MAMP/bin/php/php5.3.6/bin/pear config-set auto_discover 1
Finally, install PHPUnit:
$ sudo /Applications/MAMP/bin/php/php5.3.6/bin/pear install pear.phpunit.de/PHPUnit
If you’re interested in creating a symbolic link to the pear installation, run the following command. This will allow you to run ‘phpunit’ in any terminal session:
$ sudo ln -s /Applications/MAMP/bin/php/php5.3.6/bin/phpunit /usr/local/bin/phpunit
Install The WordPress Tests
Installing the WordPress Tests is relatively easy. You can grab a copy of the code from the Subversion repository and either clone or extract a downloaded archive. The easiest way to setup the rests is to drop the wordpress-tests directory in the root of the WordPress installation directory.
After that, update the wp-tests-config-sample.php just as you would a standard wp-config.php file. Rename it to wp-tests-config.php.
Make sure that you use a database that you don’t mind destroying as the tests will create and empty tables during the course of their execution.
Depending on where you’ve installed the WordPress Tests, you may need to update the path to WordPress. For example, if they are running in the root of the WordPress installation then you’re good to go, but if you have them running in, say, a theme directory, you’ll need to locate the following line in wp-tests-config.php:
define( 'ABSPATH', dirname( __FILE__ ) . '/wordpress/' );
And change it to:
define( 'ABSPATH', '../../../../' );
Once done, you can begin issuing commands to execute the WordPress tests. Assuming that you’ve setup the symbolic link to PHPUnit as covered above, you can test, for example, the user capabilities by running:
$ phpunit tests/test_user_capabilities.php
If you happen to get an error, it’s likely because the WordPress Tests attempt to use the mysql socket located in /var/mysql on the local machine, but if you’re using MAMP, then the location of the socket doesn’t exist.
To fix this, create a symbolic link between the socket in /var/mysql to the socket provided my MAMP:
$ sudo mkdir /var/mysql $ sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /var/mysql/mysql.sock
After that, you should be good to go.