MyTestCasethat is assumed very similar to the one coded in the WordPress / Setup docs section.
did_action(), it doesn't exist any
did_action()for filters, Brain Monkey has a method accessible via
Brain\Monkey\Filters\applied()that does what you might expect.
Filters\applied()return the number of times an action or a filter has been triggered, just like
did_action()does in WordPress, but there's no way to use them to check which arguments were passed to the fired hook.
Filters\applied()are fine for simple tests, mostly because using them you don't need to recall Brain Monkey methods, but they are not very powerful: arguments checking and, above all, the ability to respond to fired hooks are pivotal tasks to proper test WordPress code.
MyClassabove in this page, it can be tested with:
with(), validates hook arguments, not function arguments, it means what is passed to
apply_filtersexcluding hook name itself
apply_filtersby default returns the first argument passed to it, just like WordPress function does when no callback is added to the filter.
andReturnUsing()expectation methods that can be used to make a filter return anything.
andReturnFirstArg()that can be used to make a filter expectation behave like WordPress does: return first argument received:
andReturnFirstArg()comes useful again, to set a "catch all" expectation:
nullso, if Brain Monkey would allow a mocked returning value for
do_action()expectations, it would be in contrast with real WordPress code, with disastrous effects on tests.
Actions\expectDone()because it will throw an exception.
do_action(), like WordPress actually does.
whenHappen()method for action expectations. The method takes a callback to be ran when an action is fired.
current_filter()correctly resolves to the correct hook during the execution of any callback added to respond to hooks.
current_filter()returns the right hook just like it does in WordPress. Note this will also work with any callback passed to
doing_filter()works as well!