| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | CiviCRM version 4.6 | |
| 6 | +--------------------------------------------------------------------+ |
| 7 | | Copyright CiviCRM LLC (c) 2004-2014 | |
| 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-2014 |
| 33 | * $Id: $ |
| 34 | * |
| 35 | */ |
| 36 | class CRM_Utils_Hook_UnitTests extends CRM_Utils_Hook { |
| 37 | |
| 38 | protected $mockObject; |
| 39 | protected $adhocHooks; |
| 40 | protected $civiModules = NULL; |
| 41 | |
| 42 | // Call this in CiviUnitTestCase::setUp() |
| 43 | public 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 | public function setMock($mockObject) { |
| 53 | $this->mockObject = $mockObject; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Register a piece of code to run when invoking a hook |
| 58 | */ |
| 59 | public function setHook($hook, $callable) { |
| 60 | $this->adhocHooks[$hook] = $callable; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | *Invoke hooks |
| 65 | * |
| 66 | * @param int $numParams Number of parameters to pass to the hook |
| 67 | * @param mixed $arg1 parameter to be passed to the hook |
| 68 | * @param mixed $arg2 parameter to be passed to the hook |
| 69 | * @param mixed $arg3 parameter to be passed to the hook |
| 70 | * @param mixed $arg4 parameter to be passed to the hook |
| 71 | * @param mixed $arg5 parameter to be passed to the hook |
| 72 | * @param mixed $arg6 parameter to be passed to the hook |
| 73 | * @param string $fnSuffix function suffix, this is effectively the hook name |
| 74 | * |
| 75 | * @return mixed |
| 76 | */ |
| 77 | /** |
| 78 | * @param int $numParams |
| 79 | * @param mixed $arg1 |
| 80 | * @param mixed $arg2 |
| 81 | * @param mixed $arg3 |
| 82 | * @param mixed $arg4 |
| 83 | * @param mixed $arg5 |
| 84 | * @param mixed $arg6 |
| 85 | * @param string $fnSuffix |
| 86 | * |
| 87 | * @return mixed |
| 88 | */ |
| 89 | function invoke($numParams, |
| 90 | &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6, |
| 91 | $fnSuffix) { |
| 92 | |
| 93 | $params = array( &$arg1, &$arg2, &$arg3, &$arg4, &$arg5, &$arg6); |
| 94 | |
| 95 | if ($this->civiModules === NULL) { |
| 96 | $this->civiModules = array(); |
| 97 | $this->requireCiviModules($this->civiModules); |
| 98 | } |
| 99 | $this->runHooks($this->civiModules, $fnSuffix, $numParams, $arg1, $arg2, $arg3, $arg4, $arg5, $arg6); |
| 100 | |
| 101 | if ($this->mockObject && is_callable(array($this->mockObject, $fnSuffix))) { |
| 102 | call_user_func(array($this->mockObject, $fnSuffix), $arg1, $arg2, $arg3, $arg4, $arg5, $arg6); |
| 103 | } |
| 104 | if (!empty($this->adhocHooks[$fnSuffix])) { |
| 105 | call_user_func_array($this->adhocHooks[$fnSuffix], $params ); |
| 106 | } |
| 107 | } |
| 108 | } |