Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CiviCRM_Hook | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | class CRM_Utils_Hook_UnitTests extends CRM_Utils_Hook { | |
34 | ||
35 | protected $mockObject; | |
13884db1 EM |
36 | |
37 | /** | |
50bfb460 | 38 | * @var array $adhocHooks to call |
13884db1 | 39 | */ |
6a488035 | 40 | protected $adhocHooks; |
5207a7ef | 41 | protected $civiModules = NULL; |
6a488035 | 42 | |
13884db1 EM |
43 | /** |
44 | * Call this in CiviUnitTestCase::setUp() | |
45 | */ | |
00be9182 | 46 | public function reset() { |
6a488035 TO |
47 | $this->mockObject = NULL; |
48 | $this->adhocHooks = array(); | |
49 | } | |
50 | ||
51 | /** | |
3d469574 | 52 | * Use a unit-testing mock object to handle hook invocations. |
53 | * | |
6a488035 | 54 | * e.g. hook_civicrm_foo === $mockObject->foo() |
3d469574 | 55 | * |
13884db1 | 56 | * @param object $mockObject |
6a488035 | 57 | */ |
00be9182 | 58 | public function setMock($mockObject) { |
6a488035 TO |
59 | $this->mockObject = $mockObject; |
60 | } | |
61 | ||
62 | /** | |
fe482240 | 63 | * Register a piece of code to run when invoking a hook. |
77855840 TO |
64 | * @param string $hook |
65 | * Hook name, e.g civicrm_pre. | |
66 | * @param array $callable | |
67 | * Function to call ie array(class, method). | |
16b10e64 | 68 | * eg. array($this, mymethod) |
6a488035 | 69 | */ |
00be9182 | 70 | public function setHook($hook, $callable) { |
6a488035 TO |
71 | $this->adhocHooks[$hook] = $callable; |
72 | } | |
73 | ||
5bc392e6 | 74 | /** |
fe482240 | 75 | * Invoke hooks. |
5bc392e6 | 76 | * |
77855840 TO |
77 | * @param int $numParams |
78 | * Number of parameters to pass to the hook. | |
79 | * @param mixed $arg1 | |
80 | * Parameter to be passed to the hook. | |
81 | * @param mixed $arg2 | |
82 | * Parameter to be passed to the hook. | |
83 | * @param mixed $arg3 | |
84 | * Parameter to be passed to the hook. | |
85 | * @param mixed $arg4 | |
86 | * Parameter to be passed to the hook. | |
87 | * @param mixed $arg5 | |
88 | * Parameter to be passed to the hook. | |
89 | * @param mixed $arg6 | |
90 | * Parameter to be passed to the hook. | |
91 | * @param string $fnSuffix | |
92 | * Function suffix, this is effectively the hook name. | |
5bc392e6 EM |
93 | * |
94 | * @return mixed | |
95 | */ | |
96 | /** | |
97 | * @param int $numParams | |
98 | * @param mixed $arg1 | |
99 | * @param mixed $arg2 | |
100 | * @param mixed $arg3 | |
101 | * @param mixed $arg4 | |
102 | * @param mixed $arg5 | |
103 | * @param mixed $arg6 | |
104 | * @param string $fnSuffix | |
105 | * | |
106 | * @return mixed | |
107 | */ | |
79d7553f | 108 | public function invoke( |
a3e55d9c | 109 | $numParams, |
87dab4a4 | 110 | &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, |
6a488035 | 111 | $fnSuffix) { |
5207a7ef | 112 | |
481a74f4 | 113 | $params = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6); |
5207a7ef TO |
114 | |
115 | if ($this->civiModules === NULL) { | |
116 | $this->civiModules = array(); | |
117 | $this->requireCiviModules($this->civiModules); | |
118 | } | |
87dab4a4 | 119 | $this->runHooks($this->civiModules, $fnSuffix, $numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6); |
5207a7ef | 120 | |
6a488035 | 121 | if ($this->mockObject && is_callable(array($this->mockObject, $fnSuffix))) { |
87dab4a4 | 122 | call_user_func(array($this->mockObject, $fnSuffix), $arg1, $arg2, $arg3, $arg4, $arg5, $arg6); |
6a488035 TO |
123 | } |
124 | if (!empty($this->adhocHooks[$fnSuffix])) { | |
481a74f4 | 125 | call_user_func_array($this->adhocHooks[$fnSuffix], $params); |
6a488035 TO |
126 | } |
127 | } | |
96025800 | 128 | |
6a488035 | 129 | } |