Merge pull request #18327 from civicrm/5.29
[civicrm-core.git] / tests / phpunit / E2E / Core / HookTest.php
CommitLineData
ecb0ae5d
TO
1<?php
2
3namespace E2E\Core;
4
5/**
6 * Class HookTest
7 * @package E2E\Core
8 * @group e2e
9 */
10class HookTest extends \CiviEndToEndTestCase {
11
12 /**
13 * This test ensures that CRM_Utils_Hook::invoke() dispatches via Symfony.
14 *
15 * This should be *in*addition* to dispatching through the UF event system,
16 * although the mechanics depend on the UF, so that part has to be tested per-UF.
17 *
18 * This uses the canonical form, `CRM_Utils_Hook::invoke(string[] $names...)`
19 */
20 public function testSymfonyListener_names() {
21 $calls = 0;
d21ceb54
TO
22 $hookExample = function ($e) use (&$calls) {
23 $calls++;
24 $e->a['foo'] = 'a.name';
25 $e->b->bar = 'b.name';
26 };
27 \Civi::dispatcher()->addListener('hook_civicrm_e2eHookExample', $hookExample);
28 try {
29 $a = [];
30 $b = new \stdClass();
31 $this->hookStub(['a', 'b'], $a, $b);
32 $this->assertEquals(1, $calls);
33 $this->assertEquals('a.name', $a['foo']);
34 $this->assertEquals('b.name', $b->bar);
35 }
36 finally {
37 \Civi::dispatcher()->removeListener('hook_civicrm_e2eHookExample', $hookExample);
38 }
ecb0ae5d
TO
39 }
40
ecb0ae5d
TO
41 /**
42 * @param mixed $names
43 * @param array $a
44 * @param \stdClass $b
45 * @return mixed
46 */
47 private function hookStub($names, &$a, $b) {
48 return \CRM_Utils_Hook::singleton()
49 ->invoke($names, $a, $b, \CRM_Utils_Hook::$_nullObject, \CRM_Utils_Hook::$_nullObject, \CRM_Utils_Hook::$_nullObject, \CRM_Utils_Hook::$_nullObject,
50 'civicrm_e2eHookExample');
51 }
52
53}