Merge pull request #4871 from eileenmcnaughton/CRM-15795
[civicrm-core.git] / CRM / Utils / Hook / UnitTests.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 *
31 * @package CiviCRM_Hook
32 * @copyright CiviCRM LLC (c) 2004-2014
33 * $Id: $
34 *
35 */
36 class CRM_Utils_Hook_UnitTests extends CRM_Utils_Hook {
37
38 protected $mockObject;
39
40 /**
41 * @var array adhocHooks to call
42 */
43 protected $adhocHooks;
44 protected $civiModules = NULL;
45
46 /**
47 * Call this in CiviUnitTestCase::setUp()
48 */
49 public function reset() {
50 $this->mockObject = NULL;
51 $this->adhocHooks = array();
52 }
53
54 /**
55 * Use a unit-testing mock object to handle hook invocations
56 * e.g. hook_civicrm_foo === $mockObject->foo()
57 * @param object $mockObject
58 */
59 public function setMock($mockObject) {
60 $this->mockObject = $mockObject;
61 }
62
63 /**
64 * Register a piece of code to run when invoking a 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 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 /**
98 * @param int $numParams
99 * @param mixed $arg1
100 * @param mixed $arg2
101 * @param mixed $arg3
102 * @param mixed $arg4
103 * @param mixed $arg5
104 * @param mixed $arg6
105 * @param string $fnSuffix
106 *
107 * @return mixed
108 */
109 function invoke(
110 $numParams,
111 &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6,
112 $fnSuffix) {
113
114 $params = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6);
115
116 if ($this->civiModules === NULL) {
117 $this->civiModules = array();
118 $this->requireCiviModules($this->civiModules);
119 }
120 $this->runHooks($this->civiModules, $fnSuffix, $numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
121
122 if ($this->mockObject && is_callable(array($this->mockObject, $fnSuffix))) {
123 call_user_func(array($this->mockObject, $fnSuffix), $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
124 }
125 if (!empty($this->adhocHooks[$fnSuffix])) {
126 call_user_func_array($this->adhocHooks[$fnSuffix], $params);
127 }
128 }
129 }