3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
30 * @package CiviCRM_Hook
31 * @copyright CiviCRM LLC (c) 2004-2019
33 class CRM_Utils_Hook_UnitTests
extends CRM_Utils_Hook
{
35 protected $mockObject;
40 protected $adhocHooks;
41 protected $civiModules = NULL;
44 * Call this in CiviUnitTestCase::setUp()
46 public function reset() {
47 $this->mockObject
= NULL;
48 $this->adhocHooks
= [];
52 * Use a unit-testing mock object to handle hook invocations.
54 * e.g. hook_civicrm_foo === $mockObject->foo()
55 * Mocks with a magic `__call()` method are called for every hook invokation.
57 * @param object $mockObject
59 public function setMock($mockObject) {
60 $this->mockObject
= $mockObject;
64 * Register a function to run when invoking a specific hook.
66 * Hook name, e.g civicrm_pre.
67 * @param array $callable
68 * Function to call ie array(class, method).
69 * eg. array($this, mymethod)
71 public function setHook($hook, $callable) {
72 $this->adhocHooks
[$hook] = $callable;
76 * Invoke standard, mock and ad hoc hooks.
78 * @param int $numParams
79 * Number of parameters to pass to the hook.
81 * Parameter to be passed to the hook.
83 * Parameter to be passed to the hook.
85 * Parameter to be passed to the hook.
87 * Parameter to be passed to the hook.
89 * Parameter to be passed to the hook.
91 * Parameter to be passed to the hook.
92 * @param string $fnSuffix
93 * Function suffix, this is effectively the hook name.
97 public function invokeViaUF(
99 &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6,
101 $params = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6);
103 $fResult1 = $fResult2 = $fResult3 = NULL;
105 // run standard hooks
106 if ($this->civiModules
=== NULL) {
107 $this->civiModules
= [];
108 $this->requireCiviModules($this->civiModules
);
110 $fResult1 = $this->runHooks($this->civiModules
, $fnSuffix, $numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
112 // run mock object hooks
113 if ($this->mockObject
&& is_callable([$this->mockObject
, $fnSuffix])) {
114 $fResult2 = call_user_func([$this->mockObject
, $fnSuffix], $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
118 if (!empty($this->adhocHooks
[$fnSuffix])) {
119 $fResult3 = call_user_func_array($this->adhocHooks
[$fnSuffix], $params);
123 foreach ([$fResult1, $fResult2, $fResult3] as $fResult) {
124 if (!empty($fResult) && is_array($fResult)) {
125 $result = array_merge($result, $fResult);
129 return empty($result) ?
TRUE : $result;