Last Updated: February 25, 2016
·
1.299K
· groovecoder

quick WordPress plugin unit testing

If you want to "unit-test" your WordPress plugin, you don't have to set up all the WordPress plumbing like described at http://stackoverflow.com/q/9138215/571420

For most of the WP functions you can stub them with a simple return true.

function add_filter( $hook_point, $hook_callback, $mode ) { 
    return true;
}
function add_action( $hook_point, $hook_callback ) {
    return true;
}
function register_activation_hook( $file, $callback ) {
    return true;
}

Or if you need to control the stub behavior, go ahead and abuse the global namespace

$feed = false;
function is_feed() {
    global $feed;
    if $( feed ) return true;
    return false;

Now you can test your plugin's logic independent of all the WordPress hooks and calls.

Example: https://github.com/groovecoder/wp-promote-mdn/blob/master/tests/PromoteMDNTest.php

1 Response
Add your response