INFRA-132 - Change "else if" to "elseif"
[civicrm-core.git] / CRM / Utils / Hook / UnitTests.php
CommitLineData
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 */
36class 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
77855840
TO
65 * @param string $hook
66 * Hook name, e.g civicrm_pre.
67 * @param array $callable
68 * Function to call ie array(class, method).
13884db1 69 * eg. array($this, mymethod)
6a488035 70 */
00be9182 71 public function setHook($hook, $callable) {
6a488035
TO
72 $this->adhocHooks[$hook] = $callable;
73 }
74
5bc392e6 75 /**
e7292422 76 * Invoke hooks
5bc392e6 77 *
77855840
TO
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.
5bc392e6
EM
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 */
a3e55d9c
TO
109 function invoke(
110 $numParams,
87dab4a4 111 &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6,
6a488035 112 $fnSuffix) {
5207a7ef 113
87dab4a4 114 $params = array( &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6);
5207a7ef
TO
115
116 if ($this->civiModules === NULL) {
117 $this->civiModules = array();
118 $this->requireCiviModules($this->civiModules);
119 }
87dab4a4 120 $this->runHooks($this->civiModules, $fnSuffix, $numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
5207a7ef 121
6a488035 122 if ($this->mockObject && is_callable(array($this->mockObject, $fnSuffix))) {
87dab4a4 123 call_user_func(array($this->mockObject, $fnSuffix), $arg1, $arg2, $arg3, $arg4, $arg5, $arg6);
6a488035
TO
124 }
125 if (!empty($this->adhocHooks[$fnSuffix])) {
126 call_user_func_array($this->adhocHooks[$fnSuffix], $params );
127 }
128 }
6a488035 129}