I found this example but it doesn't look like it would generalise to all hooks.

The use case is for API4 - basically whenever any action is called I think we should be able to verify that particular hooks (and only those hooks) are called.

  • +1 on this, as I wanted to test that hook inputs correspond to the specs and did not find a good way to do it.
    – cividesk
    Mar 2, 2017 at 6:15

1 Answer 1


So I figured this out. The specific hook engine for unit testing, very conveniently, comes with an injectable mock property.

After grabbing the hook engine with $this->hookClass = \CRM_Utils_Hook::singleton() you just need to inject a mock object that has a magic __call() method with somthing like:


The hook engine will then call your __call($name, $arguments) method with every hook invocation.

You can see a fuller example here (please ignore the reporting stuff going on - I doubt that is best practice! Also the use of the unit test class as the mock is a bit lazy - its probably best to make a new class to provide the mock)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.