Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
232624b1 | 5 | | CiviCRM version 4.4 | |
6a488035 TO |
6 | +--------------------------------------------------------------------+ |
7 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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-2013 | |
33 | * $Id: $ | |
34 | * | |
35 | */ | |
36 | class CRM_Utils_Hook_UnitTests extends CRM_Utils_Hook { | |
37 | ||
38 | protected $mockObject; | |
39 | protected $adhocHooks; | |
5207a7ef | 40 | protected $civiModules = NULL; |
6a488035 TO |
41 | |
42 | // Call this in CiviUnitTestCase::setUp() | |
43 | function reset() { | |
44 | $this->mockObject = NULL; | |
45 | $this->adhocHooks = array(); | |
46 | } | |
47 | ||
48 | /** | |
49 | * Use a unit-testing mock object to handle hook invocations | |
50 | * e.g. hook_civicrm_foo === $mockObject->foo() | |
51 | */ | |
52 | function setMock($mockObject) { | |
53 | $this->mockObject = $mockObject; | |
54 | } | |
55 | ||
56 | /** | |
57 | * Register a piece of code to run when invoking a hook | |
58 | */ | |
59 | function setHook($hook, $callable) { | |
60 | $this->adhocHooks[$hook] = $callable; | |
61 | } | |
62 | ||
63 | function invoke($numParams, | |
87dab4a4 | 64 | &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, |
6a488035 | 65 | $fnSuffix) { |
5207a7ef | 66 | |
87dab4a4 | 67 | $params = array( &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6); |
5207a7ef TO |
68 | |
69 | if ($this->civiModules === NULL) { | |
70 | $this->civiModules = array(); | |
71 | $this->requireCiviModules($this->civiModules); | |
72 | } | |
87dab4a4 | 73 | $this->runHooks($this->civiModules, $fnSuffix, $numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6); |
5207a7ef | 74 | |
6a488035 | 75 | if ($this->mockObject && is_callable(array($this->mockObject, $fnSuffix))) { |
87dab4a4 | 76 | call_user_func(array($this->mockObject, $fnSuffix), $arg1, $arg2, $arg3, $arg4, $arg5, $arg6); |
6a488035 TO |
77 | } |
78 | if (!empty($this->adhocHooks[$fnSuffix])) { | |
79 | call_user_func_array($this->adhocHooks[$fnSuffix], $params ); | |
80 | } | |
81 | } | |
6a488035 TO |
82 | } |
83 |