Mocking WordPress Hooks
The hooks and filters of the WordPress Plugin API are common (and preferred) entry points for third-party scripts, and WP_Mock makes it easy to test that these are being registered and executed within your code.
Ensuring actions and filters are registered
Rather than attempting to mock add_action()
or add_filter()
, WP_Mock has built-in support for both of these functions: instead, use WP_Mock::expectActionAdded()
and WP_Mock::expectFilterAdded()
, respectively.
In the following example, our expectations will fail if the MyClass::addHooks()
method do not call add_action('save_post', [$this, 'myActionCallback'], 10, 2 )
and add_action('the_content', [$this, 'myFilterCallback'])
:
It's important to note that the $priority
and $parameter_count
arguments (parameters 3 and 4 for both add_action()
and add_filter()
) are significant. If in our example our code used a different priority or a different number of arguments when setting the callbacks, the test would have failed.
If the actual instance of an expected class cannot be passed, AnyInstance
can be used:
Asserting that closures have been added as hook callbacks
Sometimes it's handy to add a Closure as a WordPress hook instead of defining a function in the global namespace. To assert that such a hook has been added, you can perform assertions referencing the Closure class or a callable
type:
Asserting that actions and filters are applied
Now that we're testing if we are adding actions and/or filters, the next step is to ensure our code is calling those hooks when expected.
For actions, we'll want to listen for do_action()
to be called for our action name, so we'll use WP_Mock::expectAction()
:
This test will fail if MyClass::myMethod()
does not call do_action('my_action')
. In situations where your code needs to trigger actions, this assertion makes sure the appropriate hooks are being triggered.
For filters, we can inject our own response to apply_filters()
using WP_Mock::onFilter()
.
Take the code below, for example:
We can test that the filter is being applied by using WP_Mock::onFilter()
:
Alternatively, there is a method WP_Mock::expectFilter()
that will add a bare assertion that the filter will be applied without changing the value:
And then the test:
Last updated