Merge in 5.20
[civicrm-core.git] / CRM / Utils / Hook / UnitTests.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CiviCRM_Hook
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Utils_Hook_UnitTests extends CRM_Utils_Hook {
18
19 protected $mockObject;
20
21 /**
22 * @var array
23 */
24 protected $adhocHooks;
25 protected $civiModules = NULL;
26
27 /**
28 * Call this in CiviUnitTestCase::setUp()
29 */
30 public function reset() {
31 $this->mockObject = NULL;
32 $this->adhocHooks = [];
33 }
34
35 /**
36 * Use a unit-testing mock object to handle hook invocations.
37 *
38 * e.g. hook_civicrm_foo === $mockObject->foo()
39 * Mocks with a magic `__call()` method are called for every hook invokation.
40 *
41 * @param object $mockObject
42 */
43 public function setMock($mockObject) {
44 $this->mockObject = $mockObject;
45 }
46
47 /**
48 * Register a function to run when invoking a specific hook.
49 * @param string $hook
50 * Hook name, e.g civicrm_pre.
51 * @param array $callable
52 * Function to call ie array(class, method).
53 * eg. array($this, mymethod)
54 */
55 public function setHook($hook, $callable) {
56 $this->adhocHooks[$hook] = $callable;
57 }
58
59 /**
60 * Invoke standard, mock and ad hoc hooks.
61 *
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.
78 *
79 * @return mixed
80 */
81 public function invokeViaUF(
82 $numParams,
83 &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6,
84 $fnSuffix) {
85 $params = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6);
86
87 $fResult1 = $fResult2 = $fResult3 = NULL;
88
89 // run standard hooks
90 if ($this->civiModules === NULL) {
91 $this->civiModules = [];
92 $this->requireCiviModules($this->civiModules);
93 }
94 $fResult1 = $this->runHooks($this->civiModules, $fnSuffix, $numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
95
96 // run mock object hooks
97 if ($this->mockObject && is_callable([$this->mockObject, $fnSuffix])) {
98 $fResult2 = call_user_func([$this->mockObject, $fnSuffix], $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
99 }
100
101 // run adhoc hooks
102 if (!empty($this->adhocHooks[$fnSuffix])) {
103 $fResult3 = call_user_func_array($this->adhocHooks[$fnSuffix], $params);
104 }
105
106 $result = [];
107 foreach ([$fResult1, $fResult2, $fResult3] as $fResult) {
108 if (!empty($fResult) && is_array($fResult)) {
109 $result = array_merge($result, $fResult);
110 }
111 }
112
113 return empty($result) ? TRUE : $result;
114 }
115
116 }