Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
39de6fd5 | 5 | | CiviCRM version 4.6 | |
6a488035 | 6 | +--------------------------------------------------------------------+ |
06b69b18 | 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | |
06b69b18 | 32 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
33 | * $Id: $ |
34 | * | |
35 | */ | |
36 | class CRM_Utils_Hook_UnitTests extends CRM_Utils_Hook { | |
37 | ||
38 | protected $mockObject; | |
13884db1 EM |
39 | |
40 | /** | |
41 | * @var array adhocHooks to call | |
42 | */ | |
6a488035 | 43 | protected $adhocHooks; |
5207a7ef | 44 | protected $civiModules = NULL; |
6a488035 | 45 | |
13884db1 EM |
46 | /** |
47 | * Call this in CiviUnitTestCase::setUp() | |
48 | */ | |
00be9182 | 49 | public function reset() { |
6a488035 TO |
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() | |
13884db1 | 57 | * @param object $mockObject |
6a488035 | 58 | */ |
00be9182 | 59 | public function setMock($mockObject) { |
6a488035 TO |
60 | $this->mockObject = $mockObject; |
61 | } | |
62 | ||
63 | /** | |
64 | * Register a piece of code to run when invoking a hook | |
13884db1 EM |
65 | * @param string $hook hook name, e.g civicrm_pre |
66 | * @param array $callable function to call ie array(class, method) | |
67 | * eg. array($this, mymethod) | |
6a488035 | 68 | */ |
00be9182 | 69 | public function setHook($hook, $callable) { |
6a488035 TO |
70 | $this->adhocHooks[$hook] = $callable; |
71 | } | |
72 | ||
5bc392e6 EM |
73 | /** |
74 | *Invoke hooks | |
75 | * | |
76 | * @param int $numParams Number of parameters to pass to the hook | |
77 | * @param mixed $arg1 parameter to be passed to the hook | |
78 | * @param mixed $arg2 parameter to be passed to the hook | |
79 | * @param mixed $arg3 parameter to be passed to the hook | |
80 | * @param mixed $arg4 parameter to be passed to the hook | |
81 | * @param mixed $arg5 parameter to be passed to the hook | |
82 | * @param mixed $arg6 parameter to be passed to the hook | |
83 | * @param string $fnSuffix function suffix, this is effectively the hook name | |
84 | * | |
85 | * @return mixed | |
86 | */ | |
87 | /** | |
88 | * @param int $numParams | |
89 | * @param mixed $arg1 | |
90 | * @param mixed $arg2 | |
91 | * @param mixed $arg3 | |
92 | * @param mixed $arg4 | |
93 | * @param mixed $arg5 | |
94 | * @param mixed $arg6 | |
95 | * @param string $fnSuffix | |
96 | * | |
97 | * @return mixed | |
98 | */ | |
6a488035 | 99 | function invoke($numParams, |
87dab4a4 | 100 | &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, |
6a488035 | 101 | $fnSuffix) { |
5207a7ef | 102 | |
87dab4a4 | 103 | $params = array( &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6); |
5207a7ef TO |
104 | |
105 | if ($this->civiModules === NULL) { | |
106 | $this->civiModules = array(); | |
107 | $this->requireCiviModules($this->civiModules); | |
108 | } | |
87dab4a4 | 109 | $this->runHooks($this->civiModules, $fnSuffix, $numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6); |
5207a7ef | 110 | |
6a488035 | 111 | if ($this->mockObject && is_callable(array($this->mockObject, $fnSuffix))) { |
87dab4a4 | 112 | call_user_func(array($this->mockObject, $fnSuffix), $arg1, $arg2, $arg3, $arg4, $arg5, $arg6); |
6a488035 TO |
113 | } |
114 | if (!empty($this->adhocHooks[$fnSuffix])) { | |
115 | call_user_func_array($this->adhocHooks[$fnSuffix], $params ); | |
116 | } | |
117 | } | |
6a488035 | 118 | } |