Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
bc77d7c0 TO |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | | |
8 | | and copyright information, see https://civicrm.org/licensing | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * @package CiviCRM_Hook | |
ca5cec67 | 15 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
16 | */ |
17 | class CRM_Utils_Hook_UnitTests extends CRM_Utils_Hook { | |
18 | ||
19 | protected $mockObject; | |
13884db1 EM |
20 | |
21 | /** | |
6714d8d2 | 22 | * @var array |
13884db1 | 23 | */ |
6a488035 | 24 | protected $adhocHooks; |
0a997f9f | 25 | protected $civiModules; |
6a488035 | 26 | |
13884db1 EM |
27 | /** |
28 | * Call this in CiviUnitTestCase::setUp() | |
29 | */ | |
0a997f9f | 30 | public function reset(): void { |
6a488035 | 31 | $this->mockObject = NULL; |
be2fb01f | 32 | $this->adhocHooks = []; |
6a488035 TO |
33 | } |
34 | ||
35 | /** | |
3d469574 | 36 | * Use a unit-testing mock object to handle hook invocations. |
37 | * | |
6a488035 | 38 | * e.g. hook_civicrm_foo === $mockObject->foo() |
0a997f9f | 39 | * Mocks with a magic `__call()` method are called for every hook invocation. |
3d469574 | 40 | * |
0a997f9f | 41 | * @param PHPUnit\Framework\MockObject\MockBuilder $mockObject |
6a488035 | 42 | */ |
0a997f9f | 43 | public function setMock($mockObject): void { |
6a488035 TO |
44 | $this->mockObject = $mockObject; |
45 | } | |
46 | ||
47 | /** | |
327eb1e7 | 48 | * Register a function to run when invoking a specific hook. |
0a997f9f | 49 | * |
77855840 TO |
50 | * @param string $hook |
51 | * Hook name, e.g civicrm_pre. | |
0a997f9f | 52 | * @param callable|array $callable |
77855840 | 53 | * Function to call ie array(class, method). |
0a997f9f | 54 | * eg. array($this, myMethod) |
6a488035 | 55 | */ |
0a997f9f | 56 | public function setHook(string $hook, $callable): void { |
6a488035 | 57 | $this->adhocHooks[$hook] = $callable; |
15ed06b4 EM |
58 | if (strpos($hook, 'token') !== FALSE) { |
59 | unset(Civi::$statics['CRM_Contact_Tokens']['hook_tokens']); | |
60 | } | |
6a488035 TO |
61 | } |
62 | ||
5bc392e6 | 63 | /** |
327eb1e7 | 64 | * Invoke standard, mock and ad hoc hooks. |
5bc392e6 | 65 | * |
77855840 TO |
66 | * @param int $numParams |
67 | * Number of parameters to pass to the hook. | |
68 | * @param mixed $arg1 | |
69 | * Parameter to be passed to the hook. | |
70 | * @param mixed $arg2 | |
71 | * Parameter to be passed to the hook. | |
72 | * @param mixed $arg3 | |
73 | * Parameter to be passed to the hook. | |
74 | * @param mixed $arg4 | |
75 | * Parameter to be passed to the hook. | |
76 | * @param mixed $arg5 | |
77 | * Parameter to be passed to the hook. | |
78 | * @param mixed $arg6 | |
79 | * Parameter to be passed to the hook. | |
80 | * @param string $fnSuffix | |
81 | * Function suffix, this is effectively the hook name. | |
5bc392e6 | 82 | * |
0a997f9f EM |
83 | * @return array|bool |
84 | * @throws \CRM_Core_Exception | |
5bc392e6 | 85 | */ |
354345c9 | 86 | public function invokeViaUF( |
a3e55d9c | 87 | $numParams, |
87dab4a4 | 88 | &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, |
6a488035 | 89 | $fnSuffix) { |
481a74f4 | 90 | $params = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6); |
241bda33 | 91 | |
0a997f9f | 92 | $fResult2 = $fResult3 = NULL; |
241bda33 | 93 | |
327eb1e7 | 94 | // run standard hooks |
5207a7ef | 95 | if ($this->civiModules === NULL) { |
be2fb01f | 96 | $this->civiModules = []; |
5207a7ef TO |
97 | $this->requireCiviModules($this->civiModules); |
98 | } | |
241bda33 TO |
99 | $fResult1 = $this->runHooks($this->civiModules, $fnSuffix, $numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6); |
100 | ||
327eb1e7 | 101 | // run mock object hooks |
be2fb01f CW |
102 | if ($this->mockObject && is_callable([$this->mockObject, $fnSuffix])) { |
103 | $fResult2 = call_user_func([$this->mockObject, $fnSuffix], $arg1, $arg2, $arg3, $arg4, $arg5, $arg6); | |
6a488035 | 104 | } |
241bda33 | 105 | |
327eb1e7 | 106 | // run adhoc hooks |
6a488035 | 107 | if (!empty($this->adhocHooks[$fnSuffix])) { |
241bda33 | 108 | $fResult3 = call_user_func_array($this->adhocHooks[$fnSuffix], $params); |
6a488035 | 109 | } |
241bda33 | 110 | |
be2fb01f CW |
111 | $result = []; |
112 | foreach ([$fResult1, $fResult2, $fResult3] as $fResult) { | |
241bda33 TO |
113 | if (!empty($fResult) && is_array($fResult)) { |
114 | $result = array_merge($result, $fResult); | |
115 | } | |
116 | } | |
117 | ||
118 | return empty($result) ? TRUE : $result; | |
6a488035 | 119 | } |
96025800 | 120 | |
6a488035 | 121 | } |