Merge pull request #11703 from eileenmcnaughton/export
[civicrm-core.git] / CRM / Utils / Hook / UnitTests.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CiviCRM_Hook
31 * @copyright CiviCRM LLC (c) 2004-2018
32 */
33 class CRM_Utils_Hook_UnitTests extends CRM_Utils_Hook {
34
35 protected $mockObject;
36
37 /**
38 * @var array $adhocHooks to call
39 */
40 protected $adhocHooks;
41 protected $civiModules = NULL;
42
43 /**
44 * Call this in CiviUnitTestCase::setUp()
45 */
46 public function reset() {
47 $this->mockObject = NULL;
48 $this->adhocHooks = array();
49 }
50
51 /**
52 * Use a unit-testing mock object to handle hook invocations.
53 *
54 * e.g. hook_civicrm_foo === $mockObject->foo()
55 * Mocks with a magic `__call()` method are called for every hook invokation.
56 *
57 * @param object $mockObject
58 */
59 public function setMock($mockObject) {
60 $this->mockObject = $mockObject;
61 }
62
63 /**
64 * Register a function to run when invoking a specific hook.
65 * @param string $hook
66 * Hook name, e.g civicrm_pre.
67 * @param array $callable
68 * Function to call ie array(class, method).
69 * eg. array($this, mymethod)
70 */
71 public function setHook($hook, $callable) {
72 $this->adhocHooks[$hook] = $callable;
73 }
74
75 /**
76 * Invoke standard, mock and ad hoc hooks.
77 *
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.
94 *
95 * @return mixed
96 */
97 public function invokeViaUF(
98 $numParams,
99 &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6,
100 $fnSuffix) {
101 $params = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6);
102
103 $fResult1 = $fResult2 = $fResult3 = NULL;
104
105 // run standard hooks
106 if ($this->civiModules === NULL) {
107 $this->civiModules = array();
108 $this->requireCiviModules($this->civiModules);
109 }
110 $fResult1 = $this->runHooks($this->civiModules, $fnSuffix, $numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
111
112 // run mock object hooks
113 if ($this->mockObject && is_callable(array($this->mockObject, $fnSuffix))) {
114 $fResult2 = call_user_func(array($this->mockObject, $fnSuffix), $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
115 }
116
117 // run adhoc hooks
118 if (!empty($this->adhocHooks[$fnSuffix])) {
119 $fResult3 = call_user_func_array($this->adhocHooks[$fnSuffix], $params);
120 }
121
122 $result = array();
123 foreach (array($fResult1, $fResult2, $fResult3) as $fResult) {
124 if (!empty($fResult) && is_array($fResult)) {
125 $result = array_merge($result, $fResult);
126 }
127 }
128
129 return empty($result) ? TRUE : $result;
130 }
131
132 }