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; |
5207a7ef | 25 | protected $civiModules = NULL; |
6a488035 | 26 | |
13884db1 EM |
27 | /** |
28 | * Call this in CiviUnitTestCase::setUp() | |
29 | */ | |
00be9182 | 30 | public function reset() { |
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() |
327eb1e7 | 39 | * Mocks with a magic `__call()` method are called for every hook invokation. |
3d469574 | 40 | * |
13884db1 | 41 | * @param object $mockObject |
6a488035 | 42 | */ |
00be9182 | 43 | public function setMock($mockObject) { |
6a488035 TO |
44 | $this->mockObject = $mockObject; |
45 | } | |
46 | ||
47 | /** | |
327eb1e7 | 48 | * Register a function to run when invoking a specific hook. |
77855840 TO |
49 | * @param string $hook |
50 | * Hook name, e.g civicrm_pre. | |
51 | * @param array $callable | |
52 | * Function to call ie array(class, method). | |
16b10e64 | 53 | * eg. array($this, mymethod) |
6a488035 | 54 | */ |
00be9182 | 55 | public function setHook($hook, $callable) { |
6a488035 TO |
56 | $this->adhocHooks[$hook] = $callable; |
57 | } | |
58 | ||
5bc392e6 | 59 | /** |
327eb1e7 | 60 | * Invoke standard, mock and ad hoc hooks. |
5bc392e6 | 61 | * |
77855840 TO |
62 | * @param int $numParams |
63 | * Number of parameters to pass to the hook. | |
64 | * @param mixed $arg1 | |
65 | * Parameter to be passed to the hook. | |
66 | * @param mixed $arg2 | |
67 | * Parameter to be passed to the hook. | |
68 | * @param mixed $arg3 | |
69 | * Parameter to be passed to the hook. | |
70 | * @param mixed $arg4 | |
71 | * Parameter to be passed to the hook. | |
72 | * @param mixed $arg5 | |
73 | * Parameter to be passed to the hook. | |
74 | * @param mixed $arg6 | |
75 | * Parameter to be passed to the hook. | |
76 | * @param string $fnSuffix | |
77 | * Function suffix, this is effectively the hook name. | |
5bc392e6 EM |
78 | * |
79 | * @return mixed | |
80 | */ | |
354345c9 | 81 | public function invokeViaUF( |
a3e55d9c | 82 | $numParams, |
87dab4a4 | 83 | &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, |
6a488035 | 84 | $fnSuffix) { |
481a74f4 | 85 | $params = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6); |
241bda33 TO |
86 | |
87 | $fResult1 = $fResult2 = $fResult3 = NULL; | |
88 | ||
327eb1e7 | 89 | // run standard hooks |
5207a7ef | 90 | if ($this->civiModules === NULL) { |
be2fb01f | 91 | $this->civiModules = []; |
5207a7ef TO |
92 | $this->requireCiviModules($this->civiModules); |
93 | } | |
241bda33 TO |
94 | $fResult1 = $this->runHooks($this->civiModules, $fnSuffix, $numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6); |
95 | ||
327eb1e7 | 96 | // run mock object hooks |
be2fb01f CW |
97 | if ($this->mockObject && is_callable([$this->mockObject, $fnSuffix])) { |
98 | $fResult2 = call_user_func([$this->mockObject, $fnSuffix], $arg1, $arg2, $arg3, $arg4, $arg5, $arg6); | |
6a488035 | 99 | } |
241bda33 | 100 | |
327eb1e7 | 101 | // run adhoc hooks |
6a488035 | 102 | if (!empty($this->adhocHooks[$fnSuffix])) { |
241bda33 | 103 | $fResult3 = call_user_func_array($this->adhocHooks[$fnSuffix], $params); |
6a488035 | 104 | } |
241bda33 | 105 | |
be2fb01f CW |
106 | $result = []; |
107 | foreach ([$fResult1, $fResult2, $fResult3] as $fResult) { | |
241bda33 TO |
108 | if (!empty($fResult) && is_array($fResult)) { |
109 | $result = array_merge($result, $fResult); | |
110 | } | |
111 | } | |
112 | ||
113 | return empty($result) ? TRUE : $result; | |
6a488035 | 114 | } |
96025800 | 115 | |
6a488035 | 116 | } |