Debian Codegraph @

tests/phpunit/includes/HookContainer/HookContainerIntegrationTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\HookContainer {
4
5 use MediaWiki\Registration\ExtensionRegistry;
6 use Wikimedia\ScopedCallback;
7
8 class HookContainerIntegrationTest extends \MediaWikiIntegrationTestCase {
9
10 /**
11 * @covers \MediaWiki\HookContainer\HookContainer::run
12 */
13 public function testHookRunsWhenExtensionRegistered() {
14 $extensionRegistry = ExtensionRegistry::getInstance();
15 $numHandlersExecuted = 0;
16 $handlers = [ 'FooHook' => [ [
17 'handler' => [
18 'class' => 'FooExtension\\FooExtensionHooks',
19 'name' => 'FooExtension-FooHandler',
20 ] ] ]
21 ];
22 $reset = $extensionRegistry->setAttributeForTest( 'Hooks', $handlers );
23 $this->assertSame( 0, $numHandlersExecuted );
24
25 $this->resetServices();
26 $hookContainer = $this->getServiceContainer()->getHookContainer();
27 $hookContainer->run( 'FooHook', [ &$numHandlersExecuted ] );
28 $this->assertSame( 1, $numHandlersExecuted );
29 ScopedCallback::consume( $reset );
30 }
31
32 /**
33 * @covers \MediaWiki\HookContainer\HookContainer::run
34 * @covers \MediaWiki\HookContainer\HookContainer::scopedRegister
35 */
36 public function testHookRunsWithMultipleMixedHandlerTypes() {
37 $handlerExt = [
38 'FooHook' => [
39 [ 'handler' => [
40 'class' => 'FooExtension\\FooExtensionHooks',
41 'name' => 'FooExtension-FooHandler',
42 ]
43 ]
44 ]
45 ];
46 $resetExt = ExtensionRegistry::getInstance()->setAttributeForTest( 'Hooks', $handlerExt );
47
48 $this->resetServices();
49 $hookContainer = $this->getServiceContainer()->getHookContainer();
50
51 $numHandlersExecuted = 0;
52 $reset = $hookContainer->scopedRegister( 'FooHook', static function ( &$numHandlersRun ) {
53 $numHandlersRun++;
54 } );
55 $reset2 = $hookContainer->scopedRegister( 'FooHook', static function ( &$numHandlersRun ) {
56 $numHandlersRun++;
57 } );
58
59 $hookContainer->run( 'FooHook', [ &$numHandlersExecuted ] );
60 $this->assertEquals( 3, $numHandlersExecuted );
61
62 ScopedCallback::consume( $reset );
63 ScopedCallback::consume( $reset2 );
64 ScopedCallback::consume( $resetExt );
65 }
66
67 /**
68 * @covers \MediaWiki\HookContainer\HookContainer
69 */
70 public function testValidServiceInjection() {
71 $handler = [
72 'handler' => [
73 'name' => 'FooExtension-Mash',
74 'class' => 'FooExtension\\ServiceHooks',
75 'services' => [ 'ReadOnlyMode' ]
76 ],
77 'extensionPath' => '/path/to/extension.json'
78 ];
79 $hooks = [ 'Mash' => [ $handler ] ];
80 $reset = ExtensionRegistry::getInstance()->setAttributeForTest( 'Hooks', $hooks );
81
82 $this->resetServices();
83 $hookContainer = $this->getServiceContainer()->getHookContainer();
84
85 $arg = 0;
86 $ret = $hookContainer->run( 'Mash', [ &$arg ] );
87 $this->assertTrue( $ret );
88 $this->assertSame( 1, $arg );
89 }
90
91 /**
92 * @covers \MediaWiki\HookContainer\HookContainer
93 */
94 public function testInvalidServiceInjection() {
95 $handler = [
96 'handler' => [
97 'name' => 'FooExtension-Mash',
98 'class' => 'FooExtension\\ServiceHooks',
99 'services' => [ 'ReadOnlyMode' ]
100 ],
101 'extensionPath' => '/path/to/extension.json'
102 ];
103 $hooks = [ 'Mash' => [ $handler ] ];
104 $reset = ExtensionRegistry::getInstance()->setAttributeForTest( 'Hooks', $hooks );
105
106 $this->resetServices();
107 $hookContainer = $this->getServiceContainer()->getHookContainer();
108
109 $this->expectException( \UnexpectedValueException::class );
110 $arg = 0;
111 $hookContainer->run( 'Mash', [ &$arg ], [ 'noServices' => true ] );
112 }
113 }
114 }
115
116 namespace FooExtension {
117
118 class FooExtensionHooks {
119
120 public function onFooHook( &$numHandlersRun ) {
121 $numHandlersRun++;
122 }
123 }
124
125 class ServiceHooks {
126 public function __construct( \Wikimedia\Rdbms\ReadOnlyMode $readOnlyMode ) {
127 }
128
129 public function onMash( &$arg ) {
130 $arg++;
131 return true;
132 }
133 }
134
135 }