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 TO |
57 | $this->adhocHooks[$hook] = $callable; |
58 | } | |
59 | ||
5bc392e6 | 60 | /** |
327eb1e7 | 61 | * Invoke standard, mock and ad hoc hooks. |
5bc392e6 | 62 | * |
77855840 TO |
63 | * @param int $numParams |
64 | * Number of parameters to pass to the hook. | |
65 | * @param mixed $arg1 | |
66 | * Parameter to be passed to the hook. | |
67 | * @param mixed $arg2 | |
68 | * Parameter to be passed to the hook. | |
69 | * @param mixed $arg3 | |
70 | * Parameter to be passed to the hook. | |
71 | * @param mixed $arg4 | |
72 | * Parameter to be passed to the hook. | |
73 | * @param mixed $arg5 | |
74 | * Parameter to be passed to the hook. | |
75 | * @param mixed $arg6 | |
76 | * Parameter to be passed to the hook. | |
77 | * @param string $fnSuffix | |
78 | * Function suffix, this is effectively the hook name. | |
5bc392e6 | 79 | * |
0a997f9f EM |
80 | * @return array|bool |
81 | * @throws \CRM_Core_Exception | |
5bc392e6 | 82 | */ |
354345c9 | 83 | public function invokeViaUF( |
a3e55d9c | 84 | $numParams, |
87dab4a4 | 85 | &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, |
6a488035 | 86 | $fnSuffix) { |
481a74f4 | 87 | $params = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6); |
241bda33 | 88 | |
0a997f9f | 89 | $fResult2 = $fResult3 = NULL; |
241bda33 | 90 | |
327eb1e7 | 91 | // run standard hooks |
5207a7ef | 92 | if ($this->civiModules === NULL) { |
be2fb01f | 93 | $this->civiModules = []; |
5207a7ef TO |
94 | $this->requireCiviModules($this->civiModules); |
95 | } | |
241bda33 TO |
96 | $fResult1 = $this->runHooks($this->civiModules, $fnSuffix, $numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6); |
97 | ||
327eb1e7 | 98 | // run mock object hooks |
be2fb01f CW |
99 | if ($this->mockObject && is_callable([$this->mockObject, $fnSuffix])) { |
100 | $fResult2 = call_user_func([$this->mockObject, $fnSuffix], $arg1, $arg2, $arg3, $arg4, $arg5, $arg6); | |
6a488035 | 101 | } |
241bda33 | 102 | |
327eb1e7 | 103 | // run adhoc hooks |
6a488035 | 104 | if (!empty($this->adhocHooks[$fnSuffix])) { |
241bda33 | 105 | $fResult3 = call_user_func_array($this->adhocHooks[$fnSuffix], $params); |
6a488035 | 106 | } |
241bda33 | 107 | |
be2fb01f CW |
108 | $result = []; |
109 | foreach ([$fResult1, $fResult2, $fResult3] as $fResult) { | |
241bda33 TO |
110 | if (!empty($fResult) && is_array($fResult)) { |
111 | $result = array_merge($result, $fResult); | |
112 | } | |
113 | } | |
114 | ||
115 | return empty($result) ? TRUE : $result; | |
6a488035 | 116 | } |
96025800 | 117 | |
6a488035 | 118 | } |