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.
CiviCRM Stack Exchange is a question and answer site for administrators and users of the CiviCRM Constituent Relationship Management software. It only takes a minute to sign up.
Sign up to join this communityI 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.
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)