A Quick Hack to Writing Testable Code

Write Testable Code

I’ll be the first to admit that I am fairly inexperienced in the practical application of unit testing, or any kind of automated testing. That’s not to say I don’t understand what these things are. I was first exposed to the concept of unit tests back in 2008 and automated browser testing in around 2012. I know the theory of how they work and what the benefits are. It’s just that I’ve never been in a situation where I had the time to learn how to implement these things, so I’ve never really been able to. It was the classic “running alongside your bike” scenario.

That being said, there is something that a very skilled developer I used to work with said to me back in 2012, which I’ve implemented ever since, that I only realised today not only applied to writing better code, but also to writing testable code:

“Every function, or class method, should only do one thing”

Shaine Gordon, CTO at Realm Digital

Let me explain that by means of an example.

Let’s say you need to write a string parser. The parser needs to take a string, convert it to lower case, strip out specific punctuation (full stop, comma and space) and return the string with the first character as upper case.

In PHP, that could look something like this:

function my_string_parser( $string ) {
	$string = strtolower( $string );
	$string = str_replace( array( ' ', '.', ',' ), '', $string );
	$string = ucfirst( $string );

	return $string;
}

Now, the perceptive amongst you might notice the bug. If you don’t see it right away, that’s OK, because that’s the point of this article.

So lets say I now decide to write a unit test to test this function (we’re not digging into TDD just yet). With PHPUnit installed, I might write a test class method that looks something like this:

function test_my_string_parser() {
	$string        = 'My test, string.';
	$parsed_string = my_string_parser( $string );
	$this->assertEquals( 'Myteststring', $parsed_string );
}

Based on the code above, this test would fail. My problem is, at what point in manipulating the string, does my function fail?

However, if I refactor my initial code a little:

function my_string_parser( $string ) {
	$string = my_string_to_lower( $string );
	$string = my_string_remove_punctuation( $string );
	$string = my_string_ucfirst( $string );

	return $string;
}

function my_string_to_lower( $string ) {
	$string = strtolower( $string );

	return $string;
}

function my_string_remove_punctuation( $string ) {
	$string = str_replace( array( ' ', '.', ',' ), '', $string );

	return $string;
}

function my_string_ucfirst( $string ) {
	$string = ucfirst( $string );

	return $string;
}

Granted, it’s a lot more code, but now I can write individual tests for each smaller ‘my_string’ function, and the failing test(s) will point to where the bugs are. I can then fix those bugs, function by function, until my individual tests pass, and then the ‘test_my_string_parser’ test will also pass.

I’m pretty sure this isn’t rocket science, or anything new, but if you’re starting your unit journey, it’s a good place to start.

Unit Tests for your WordPress plugin using WP CLI and PHPUnit

Unit Tests for your WordPress plugin using WP CLI and PHPUnit

Often when I write a blog post, part of the reason I write it is to document things that I tend to forget. This is one of those times.

Requirements:

PHPUnit: At the time of this writing, WordPress only supports the latest stable 7.x version of PHPUnit, and recommends installing it globally.

wget https://phar.phpunit.de/phpunit-7.5.9.phar
chmod +x phpunit-7.5.9.phar
sudo mv phpunit-7.5.9.phar /usr/local/bin/phpunit
phpunit --version

WP-CLI: If you’re developing on WordPress and you’re not using WP-CLI, you are missing out on some amazing tools.

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar 
sudo mv wp-cli.phar /usr/local/bin/wp

Set up

Using WP-CLI, you can quickly scaffold some unit tests files for your WordPress plugin.

Inside the WordPress directory of the site you’re building/testing on, run the following command to scaffold the unit test code for your plugin, located at /wp-content/plugins/plugin-name

wp scaffold plugin-tests plugin-name

If you don’t already have a plugin directory, because you’re building the plugin from scratch, you can just run the following command to scaffold some initial plugin code, which includes the unit test code.

wp scaffold plugin-name

Either way, you’ll end up with a ‘tests’ directory inside your plugin directory, which includes the test bootstrap file, and a sample test file.

The next step is to set up the ‘wordpress_test’ database, which the test suite will use when creating mock posts etc. Switch to the plugin directory and run the following comand, replacing ‘root’ and ‘password’ with your local MySQL servers username and password.

bash bin/install-wp-tests.sh wordpress_test root password localhost latest

And you’re done, go forth and write some unit tests for your WordPress plugin.