Answers.Body: <p>An example is in <code>tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php</code>, testing that <code>hook_civicrm_tokenValues</code> works correctly on PDF letters.</p>
  
  <p>You write a <a href="https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php#L136-L140" rel="nofollow noreferrer">function using the hook</a>, but you can name it whatever you want.  Then you call <a href="https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php#L100" rel="nofollow noreferrer">$this->hookClass->setHook</a>, specifying the function name.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-04-15T14:56:27.947Z
Answers.LastActivityDate: 2019-04-15T14:56:27.947Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>I am to write a unit test for a simple extension using hooks with postprocess and formbuild. How can I do it? Please provide some example.</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-07-20T19:04:24.010Z
Comments.Text: This documentation does not explain how to use hooks only showing this: public function hook_civicrm_post($op, $objectName, $objectId, &$objectRef) {
      // listen to hook_civicrm_post
    }
Comments.UserDisplayName: Syed Murtaza Hussain Kazmi
Tags: <testing>
Title: how to write PHP Unit test for an entity using hooks
ThreadText: # how to write PHP Unit test for an entity using hooks
  
  (Asked by: username_0 on 2018-07-20)
  
  I am to write a unit test for a simple extension using hooks with postprocess and formbuild. How can I do it? Please provide some example.
  
  **Tags:** testing
  
  > **Comments:**
  > **username_0**: This documentation does not explain how to use hooks only showing this: public function hook_civicrm_post($op, $objectName, $objectId, &$objectRef) {     // listen to hook_civicrm_post   }  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-04-15. Score: 1
  
  An example is in `tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php`, testing that `hook_civicrm_tokenValues` works correctly on PDF letters.
  
  You write a [function using the hook](https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php#L136-L140), but you can name it whatever you want. Then you call [$this->hookClass->setHook](https://github.com/civicrm/civicrm-core/blob/master/tests/phpunit/CRM/Contribute/Form/Task/PDFLetterCommonTest.php#L100), specifying the function name.
  
  
  ---
  
  