2

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. – Nicolas at cividesk Mar 2 '17 at 6:15
2

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:

$this->hookClass->setMock($this);

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, privacy policy and cookie policy

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