WordPress Testing Tools
The sole ability to mocking functions is a great help on testing WordPress code.
All WordPress functions can be mocked and tested using the techniques described in the PHP Functions section, they are PHP functions, after all.
However, to test WordPress code in isolation, without a bunch of bootstrap code for every test, a more fine grained control of plugin API functions is required.
This is exactly what Brain Monkey offers.
Following functions are defined by Brain Monkey when it is loaded for tests:
If your code uses any of these functions, and very likely it does, you don't need to define (or mock) them to avoid fatal errors during tests.
Note that the returning value of those functions (most of the times) will work out of the box as you might expect.
For example, if your code contains:
do_action('my_custom_action'); // something in the middle $did = did_action('my_custom_action');
the value of
$did will be correctly
did_action() in WordPress returns the number an action was done).
Or if your code contains:
$post = [ 'post_title' => 'My Title' ]; $title = apply_filters('the_title', $post['post_title']);
the value of
$title will be
'My Title', without the need of any intervention.
But, of course, that's not enough. To proper test WordPress code you will probably desire to:
- test if an action or a filter has been added, how many times that happen and with which arguments
- test if an action or a filter has been fired, how many times that happen and with which arguments
- perform some callback when an action is fired, being able to access passed arguments
- perform some callback when an filter is applied, being able to access passed arguments and to return specific values
Guess what, Brain Monkey allows to do all of this and even more.
And it does that using its straightforward and human readable syntax.