Answers.Body: <p>So I figured this out. The specific hook engine for unit testing, very conveniently, comes with an injectable mock property.</p>
  
  <p>After grabbing the hook engine with <code>$this-&gt;hookClass = \CRM_Utils_Hook::singleton()</code> you just need to inject a mock object that has a magic <code>__call()</code> method with somthing like:</p>
  
  <p><code>
  $this-&gt;hookClass-&gt;setMock($this);
  </code></p>
  
  <p>The hook engine will then call your <code>__call($name, $arguments)</code> method with every hook invocation.</p>
  
  <p>You can see a fuller example <a href="https://github.com/ErichBSchulz/api4/commit/6c9e2e0676969ac8cedfe23e036815092d2ca814" rel="nofollow noreferrer">here</a> (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)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-03-05T09:55:06.093Z
Answers.LastActivityDate: 2017-03-05T10:08:03.883Z
Answers.LastEditDate: 2017-03-05T10:08:03.883Z
Answers.UserDisplayName: ErichBSchulz
Body: <p>I found <a href="https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/Civi/Test/ExampleHookTest.php" rel="nofollow noreferrer">this example</a> but it doesn't look like it would generalise to all hooks.</p>
  
  <p>The use case is for <a href="https://github.com/civicrm/api4/pull/47" rel="nofollow noreferrer">API4</a> - basically whenever any action is called I think we should be able to verify that particular hooks (and only those hooks) are called.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2017-03-02T06:15:31.123Z
Comments.Text: +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.
Comments.UserDisplayName: cividesk
Tags: <hooks><unit-testing><phpunit>
Title: How do I write unit tests that verify a hook is being fired?
ThreadText: # How do I write unit tests that verify a hook is being fired?
  
  (Asked by: username_0 on 2017-02-11)
  
  I found [this example](https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/Civi/Test/ExampleHookTest.php) but it doesn't look like it would generalise to all hooks.
  
  The use case is for [API4](https://github.com/civicrm/api4/pull/47) - basically whenever any action is called I think we should be able to verify that particular hooks (and only those hooks) are called.
  
  **Tags:** hooks, unit-testing, phpunit
  
  > **Comments:**
  > **username_1**: +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.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-03-05. Score: 2 | Accepted answer
  
  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](https://github.com/username_0/api4/commit/6c9e2e0676969ac8cedfe23e036815092d2ca814) (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)
  
  
  ---
  
  