Merge pull request #17130 from jitendrapurohit/mail-62
[civicrm-core.git] / tests / phpunit / Civi / Core / CiviEventDispatcherTest.php
CommitLineData
926d7afc
TO
1<?php
2
3namespace Civi\Core;
4
5use Civi\Core\Event\GenericHookEvent;
6
7/**
8 * Class CiviEventDispatcherTest
9 * @package Civi\Core
10 * @group headless
11 */
12class CiviEventDispatcherTest extends \CiviUnitTestCase {
13
14 public function testDispatchPolicy_run() {
15 $d = new CiviEventDispatcher(\Civi::container());
16 $d->setDispatchPolicy([
17 'hook_civicrm_fakeRunnable' => 'run',
18 ]);
19 $calls = [];
20 $d->addListener('hook_civicrm_fakeRunnable', function() use (&$calls) {
21 $calls['hook_civicrm_fakeRunnable'] = 1;
22 });
cf2df468 23 $d->dispatch('hook_civicrm_fakeRunnable', GenericHookEvent::create([]));
926d7afc
TO
24 $this->assertEquals(1, $calls['hook_civicrm_fakeRunnable']);
25 }
26
27 public function testDispatchPolicy_drop() {
28 $d = new CiviEventDispatcher(\Civi::container());
29 $d->setDispatchPolicy([
30 '/^hook_civicrm_fakeDr/' => 'drop',
31 ]);
32 $calls = [];
33 $d->addListener('hook_civicrm_fakeDroppable', function() use (&$calls) {
34 $calls['hook_civicrm_fakeDroppable'] = 1;
35 });
cf2df468 36 $d->dispatch('hook_civicrm_fakeDroppable', GenericHookEvent::create([]));
926d7afc
TO
37 $this->assertTrue(!isset($calls['hook_civicrm_fakeDroppable']));
38 }
39
40 public function testDispatchPolicy_fail() {
41 $d = new CiviEventDispatcher(\Civi::container());
42 $d->setDispatchPolicy([
43 '/^hook_civicrm_fakeFa/' => 'fail',
44 ]);
45 try {
cf2df468 46 $d->dispatch('hook_civicrm_fakeFailure', GenericHookEvent::create([]));
926d7afc
TO
47 $this->fail('Expected exception');
48 }
49 catch (\Exception $e) {
50 $this->assertRegExp(';The dispatch policy prohibits event;', $e->getMessage());
51 }
52 }
53
54}