Tuesday, 1 November 2016

Unit Testing Custom WP-CLI Commands


Recently I’ve been spending pretty much all my time working on Mergebot for Delicious Brains, which mainly consists of building the WordPress plugin part of the project. I’ve also been spending a good deal of time refactoring code, making code testable, and generally implementing best practices to ensure code is maintainable and has high unit test coverage. Yesterday I sat down to further increase that coverage by adding unit tests around the three custom WP-CLI commands the plugin has, and hit a roadblock. Let me show you what I mean. Our custom WP-CLI commands allow you to do things like start/stop recording of queries, view and discard the current changeset, and connect your current site to the Mergebot application. The commands look pretty much like this:
namespace DeliciousBrains\Mergebot\CLI;

class CLI_Recordings extends \WP_CLI_Command {

}
The classes also have a constructor that take instances of other classes as dependencies, this makes the class decoupled and easier to test as those dependencies can be mocked in PHPUnit. However, the issue arises when you try to instantiate CLI_Recordings in the testcase:
Fatal error: Class ‘WP_CLI_Command’ not found in /src/mergebot/includes/cli/cli-recordings.php
Source: https://managewp.org/articles/13734/unit-testing-custom-wp-cli-commands




source https://williechiu40.wordpress.com/2016/11/01/unit-testing-custom-wp-cli-commands/

No comments:

Post a Comment