4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.6 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2014 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
31 * @package CiviCRM_Hook
32 * @copyright CiviCRM LLC (c) 2004-2014
36 class CRM_Utils_Hook_UnitTests
extends CRM_Utils_Hook
{
38 protected $mockObject;
41 * @var array adhocHooks to call
43 protected $adhocHooks;
44 protected $civiModules = NULL;
47 * Call this in CiviUnitTestCase::setUp()
49 public function reset() {
50 $this->mockObject
= NULL;
51 $this->adhocHooks
= array();
55 * Use a unit-testing mock object to handle hook invocations
56 * e.g. hook_civicrm_foo === $mockObject->foo()
57 * @param object $mockObject
59 public function setMock($mockObject) {
60 $this->mockObject
= $mockObject;
64 * Register a piece of code to run when invoking a 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)
71 public function setHook($hook, $callable) {
72 $this->adhocHooks
[$hook] = $callable;
78 * @param int $numParams
79 * Number of parameters to pass to the hook.
81 * Parameter to be passed to the hook.
83 * Parameter to be passed to the hook.
85 * Parameter to be passed to the hook.
87 * Parameter to be passed to the hook.
89 * Parameter to be passed to the hook.
91 * Parameter to be passed to the hook.
92 * @param string $fnSuffix
93 * Function suffix, this is effectively the hook name.
98 * @param int $numParams
105 * @param string $fnSuffix
111 &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6,
114 $params = array(&$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6);
116 if ($this->civiModules
=== NULL) {
117 $this->civiModules
= array();
118 $this->requireCiviModules($this->civiModules
);
120 $this->runHooks($this->civiModules
, $fnSuffix, $numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
122 if ($this->mockObject
&& is_callable(array($this->mockObject
, $fnSuffix))) {
123 call_user_func(array($this->mockObject
, $fnSuffix), $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
125 if (!empty($this->adhocHooks
[$fnSuffix])) {
126 call_user_func_array($this->adhocHooks
[$fnSuffix], $params);