ideasonpurpose / wp-test-stubs
A simple collection of stubs and doubles for testing WordPress code.
Requires
- php: >=8.0.0
README
WP Test Stubs
Version 0.2.0
A simple collection of stubs and doubles for testing WordPress code.
Unlike Brain Monkey or WP_Mock, this is a very dumb library.
Most functions are just empty stubs or return a matching global variable.
add_action
and add_filter
The add_action
, remove_action
, add_filter
and remove_filter
functions record calls in global $actions
or $filters
arrays. Each call pushes an associative array onto the stack containing the hook
, action
, priority
and args
. Check those global arrays to see whether the action/filter was called correctly.
Any functions which need testing should be public and capable of being tested independently.
all_added_actions
and all_added_filters
helper functions
These two helper functions return a simplified view of the global $actions
and $filters
arrays with each added hook represented as two-item, hook/action array: ['hook_name', 'method_name']
. The two parallel functions, all_removed_actions
and all_removed_filters
can be used with PHPUnit like this:
$this->assertContains(['hook_name', 'method_name'], all_added_filters());
For short-arrow and anonymous functions (Closures), test for their returned values. For example, fn() => 5
can be validated with assertContains(['hook_name', 5]
.
is_{$something} functions
All of these functions are mocked from the same pattern: Each will return the value of a global with the same name as the function. Since these functions are often used for control flow, being able to easily toggle their values makes it simple to test alternate pathways through System Under Test code without having to refactor.
To toggle any is_
function, set a value like this:
global $is_admin; $is_admin = true;
Local Development
To have Composer check out a live clone of this repo instead of downloading an archive from Packagist, add a repositories key to the root of your project's composer.json file:
{ "repositories": [ { "type": "git", "url": "https://github.com/ideasonpurpose/wp-test-stubs" } ] }
Brought to you by IOP
This project is actively developed and used in production at Ideas On Purpose.