Merge pull request #21164 from civicrm/5.41
[civicrm-core.git] / Civi / Test / HookInterface.php
CommitLineData
09e1f1e3
TO
1<?php
2
3namespace Civi\Test;
4
5/**
6 * Interface HookInterface
7 * @package Civi\Test
8 *
9 * This interface allows you to subscribe to hooks as part of the test.
10 * Simply create an eponymous hook function (e.g. `hook_civicrm_post()`).
11 *
0b882a86 12 * ```
09e1f1e3
TO
13 * class MyTest extends \PHPUnit_Framework_TestCase implements \Civi\Test\HookInterface {
14 * public function hook_civicrm_post($op, $objectName, $objectId, &$objectRef) {
15 * echo "Running hook_civicrm_post\n";
16 * }
17 * }
0b882a86 18 * ```
09e1f1e3 19 *
df2b1bc5
TO
20 * Similarly, to subscribe using Symfony-style listeners, create function with the 'on_' prefix:
21 *
22 * ```
23 * class MyTest extends \PHPUnit_Framework_TestCase implements \Civi\Test\HookInterface {
e435f178 24 * public function on_civi_api_authorize(AuthorizeEvent $e): void {
df2b1bc5
TO
25 * echo "Running civi.api.authorize\n";
26 * }
e435f178 27 * public function on_hook_civicrm_post(GenericHookEvent $e): void {
df2b1bc5
TO
28 * echo "Running hook_civicrm_post\n";
29 * }
30 * }
31 * ```
32 *
09e1f1e3
TO
33 * At time of writing, there are a few limitations in how HookInterface is handled
34 * by CiviTestListener:
35 *
36 * - The test must execute in-process (aka HeadlessInterface; aka CIVICRM_UF==UnitTests).
37 * End-to-end tests (multi-process tests) are not supported.
38 * - Early bootstrap hooks (e.g. hook_civicrm_config) are not supported.
df2b1bc5
TO
39 * - This does not support priorities or registering multiple listeners.
40 *
41 * If you need more advanced registration abilities, consider using `Civi::dispatcher()`
42 * or `EventDispatcherInterface`.
09e1f1e3
TO
43 *
44 * @see CiviTestListener
45 */
46interface HookInterface {
47}