Need to write a php unit test for init().
namespace My\Space;
class MyClass{
public function init(){
add_action('my_hook','my_function',10,0);
}
}
All the test needs to do is assert that add_action() was called with the correct parameters. If add_action() was a method of another class (lets call it class WP_Functions), then an instance of that class could be passed into the constructor of MyClass and used in init() to do the add_action(). If so, Unit testing could pass in a mock of WP_Functions that asserts that add_action() be called with the desired input parameters. But now that add_action() is not a class method, what is the best solution?
Here is what I've tried:
Let
add_action()run as it is defined by WordPress. Remove actions frommy_hook, callinit()and then assert all the correct values are set in$wp_filter['my_hook']. Simple enough for the above case but gets complicated for functions that have conditions and/or add/remove many hooks/filters.Define mock version
add_action()in theMy\Spacenamespace and make it simplyvar_dump()the input parameters. Then all the test needs to do is expect thatinit()will outputs thevar_dumpfor the desired parameters. This works well but requiresadd_action()be redefined for every namespace in which its used. This becomes difficult to manage when there are many namespaces and/or many other functions similar toadd_action()that require mock versions. Also can get complicated if the callback function is member ofMyClasssuch that it is specified as[ $this, 'callback_name' ]. Plus, it utilizesvar_dump()for a non-intended purpose.Create a public method of
MyClassthat calledadd_actionthat calls the WordPressadd_action(). If so, testing can create a mock ofMyClassthat expects it'sadd_actionmethod to be called with the desired parameters. However, this requires an extra function call. Plus it would require every class that callsadd_action()to somehow have it's ownadd_actionmethod.
Is there a better solution?