minor tidy ups
[civicrm-core.git] / tests / phpunit / CRM / Utils / HookTest.php
1 <?php
2 require_once 'CiviTest/CiviUnitTestCase.php';
3
4 /**
5 * Class CRM_Utils_HookTest
6 */
7 class CRM_Utils_HookTest extends CiviUnitTestCase {
8
9 static $activeTest = NULL;
10
11 var $fakeModules;
12
13 var $log;
14
15 function setUp() {
16 parent::setUp();
17 $this->fakeModules = array(
18 'hooktesta',
19 'hooktestb',
20 'hooktestc',
21 'hooktestd',
22 'hookteste',
23 );
24 // our goal is to test a helper in CRM_Utils_Hook, but we need a concrete class
25 $this->hook = new CRM_Utils_Hook_UnitTests();
26 $this->log = array();
27 self::$activeTest = $this;
28 }
29
30 function tearDown() {
31 self::$activeTest = $this;
32 parent::tearDown();
33 }
34
35 /**
36 * Verify that runHooks() is reentrant by invoking one hook which calls another hooks
37 */
38 function testRunHooks_reentrancy() {
39 $arg1 = 'whatever';
40 $this->hook->runHooks($this->fakeModules, 'civicrm_testRunHooks_outer', 1, $arg1, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject);
41 $this->assertEquals(
42 array(
43 'a-outer',
44 'b-outer-1',
45 'a-inner',
46 'b-inner',
47 'b-outer-2',
48 'c-outer',
49 ),
50 $this->log
51 );
52 }
53
54 /**
55 * Verify that the results of runHooks() are correctly merged
56 */
57 function testRunHooks_merge() {
58 $result = $this->hook->runHooks($this->fakeModules, 'civicrm_testRunHooks_merge', 0, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject);
59 $this->assertEquals(
60 array(
61 'from-module-a1',
62 'from-module-a2',
63 'from-module-e',
64 ),
65 $result
66 );
67 }
68 }
69
70 /* --- Library of test hook implementations ---- */
71
72 function hooktesta_civicrm_testRunHooks_outer() {
73 $test = CRM_Utils_HookTest::$activeTest;
74 $test->log[] = 'a-outer';
75 }
76
77 function hooktestb_civicrm_testRunHooks_outer() {
78 $test = CRM_Utils_HookTest::$activeTest;
79 $test->log[] = 'b-outer-1';
80 $test->hook->runHooks($test->fakeModules, 'civicrm_testRunHooks_inner', 0, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject, CRM_Utils_Hook::$_nullObject);
81 $test->log[] = 'b-outer-2';
82 }
83
84 function hooktestc_civicrm_testRunHooks_outer() {
85 $test = CRM_Utils_HookTest::$activeTest;
86 $test->log[] = 'c-outer';
87 }
88
89 function hooktesta_civicrm_testRunHooks_inner() {
90 $test = CRM_Utils_HookTest::$activeTest;
91 $test->log[] = 'a-inner';
92 }
93
94 function hooktestb_civicrm_testRunHooks_inner() {
95 $test = CRM_Utils_HookTest::$activeTest;
96 $test->log[] = 'b-inner';
97 }
98
99 function hooktesta_civicrm_testRunHooks_merge() {
100 return array('from-module-a1', 'from-module-a2');
101 }
102
103 // OMIT: function hooktestb_civicrm_testRunHooks_merge
104
105 function hooktestc_civicrm_testRunHooks_merge() {
106 return array();
107 }
108
109 function hooktestd_civicrm_testRunHooks_merge() {
110 return NULL;
111 }
112
113 function hookteste_civicrm_testRunHooks_merge() {
114 return array('from-module-e');
115 }