Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
f299f7db | 6 | | Copyright CiviCRM LLC (c) 2004-2020 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CiviCRM_Hook | |
f299f7db | 31 | * @copyright CiviCRM LLC (c) 2004-2020 |
6a488035 TO |
32 | */ |
33 | class CRM_Utils_Hook_UnitTests extends CRM_Utils_Hook { | |
34 | ||
35 | protected $mockObject; | |
13884db1 EM |
36 | |
37 | /** | |
6714d8d2 | 38 | * @var array |
13884db1 | 39 | */ |
6a488035 | 40 | protected $adhocHooks; |
5207a7ef | 41 | protected $civiModules = NULL; |
6a488035 | 42 | |
13884db1 EM |
43 | /** |
44 | * Call this in CiviUnitTestCase::setUp() | |
45 | */ | |
00be9182 | 46 | public function reset() { |
6a488035 | 47 | $this->mockObject = NULL; |
be2fb01f | 48 | $this->adhocHooks = []; |
6a488035 TO |
49 | } |
50 | ||
51 | /** | |
3d469574 | 52 | * Use a unit-testing mock object to handle hook invocations. |
53 | * | |
6a488035 | 54 | * e.g. hook_civicrm_foo === $mockObject->foo() |
327eb1e7 | 55 | * Mocks with a magic `__call()` method are called for every hook invokation. |
3d469574 | 56 | * |
13884db1 | 57 | * @param object $mockObject |
6a488035 | 58 | */ |
00be9182 | 59 | public function setMock($mockObject) { |
6a488035 TO |
60 | $this->mockObject = $mockObject; |
61 | } | |
62 | ||
63 | /** | |
327eb1e7 | 64 | * Register a function to run when invoking a specific hook. |
77855840 TO |
65 | * @param string $hook |
66 | * Hook name, e.g civicrm_pre. | |
67 | * @param array $callable | |
68 | * Function to call ie array(class, method). | |
16b10e64 | 69 | * eg. array($this, mymethod) |
6a488035 | 70 | */ |
00be9182 | 71 | public function setHook($hook, $callable) { |
6a488035 TO |
72 | $this->adhocHooks[$hook] = $callable; |
73 | } | |
74 | ||
5bc392e6 | 75 | /** |
327eb1e7 | 76 | * Invoke standard, mock and ad hoc hooks. |
5bc392e6 | 77 | * |
77855840 TO |
78 | * @param int $numParams |
79 | * Number of parameters to pass to the hook. | |
80 | * @param mixed $arg1 | |
81 | * Parameter to be passed to the hook. | |
82 | * @param mixed $arg2 | |
83 | * Parameter to be passed to the hook. | |
84 | * @param mixed $arg3 | |
85 | * Parameter to be passed to the hook. | |
86 | * @param mixed $arg4 | |
87 | * Parameter to be passed to the hook. | |
88 | * @param mixed $arg5 | |
89 | * Parameter to be passed to the hook. | |
90 | * @param mixed $arg6 | |
91 | * Parameter to be passed to the hook. | |
92 | * @param string $fnSuffix | |
93 | * Function suffix, this is effectively the hook name. | |
5bc392e6 EM |
94 | * |
95 | * @return mixed | |
96 | */ | |
354345c9 | 97 | public function invokeViaUF( |
a3e55d9c | 98 | $numParams, |
87dab4a4 | 99 | &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, |
6a488035 | 100 | $fnSuffix) { |
481a74f4 | 101 | $params = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6); |
241bda33 TO |
102 | |
103 | $fResult1 = $fResult2 = $fResult3 = NULL; | |
104 | ||
327eb1e7 | 105 | // run standard hooks |
5207a7ef | 106 | if ($this->civiModules === NULL) { |
be2fb01f | 107 | $this->civiModules = []; |
5207a7ef TO |
108 | $this->requireCiviModules($this->civiModules); |
109 | } | |
241bda33 TO |
110 | $fResult1 = $this->runHooks($this->civiModules, $fnSuffix, $numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6); |
111 | ||
327eb1e7 | 112 | // run mock object hooks |
be2fb01f CW |
113 | if ($this->mockObject && is_callable([$this->mockObject, $fnSuffix])) { |
114 | $fResult2 = call_user_func([$this->mockObject, $fnSuffix], $arg1, $arg2, $arg3, $arg4, $arg5, $arg6); | |
6a488035 | 115 | } |
241bda33 | 116 | |
327eb1e7 | 117 | // run adhoc hooks |
6a488035 | 118 | if (!empty($this->adhocHooks[$fnSuffix])) { |
241bda33 | 119 | $fResult3 = call_user_func_array($this->adhocHooks[$fnSuffix], $params); |
6a488035 | 120 | } |
241bda33 | 121 | |
be2fb01f CW |
122 | $result = []; |
123 | foreach ([$fResult1, $fResult2, $fResult3] as $fResult) { | |
241bda33 TO |
124 | if (!empty($fResult) && is_array($fResult)) { |
125 | $result = array_merge($result, $fResult); | |
126 | } | |
127 | } | |
128 | ||
129 | return empty($result) ? TRUE : $result; | |
6a488035 | 130 | } |
96025800 | 131 | |
6a488035 | 132 | } |