Skip to content

Commit 22bac93

Browse files
committed
Issue #3501237 by nikolay shapovalov, nicxvan: Improve HookCollectorPass test
1 parent c91c592 commit 22bac93

File tree

7 files changed

+80
-13
lines changed

7 files changed

+80
-13
lines changed

core.api.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1633,21 +1633,21 @@
16331633
* - On a method, use the attribute with the hook name:
16341634
* @code
16351635
* #[Hook('user_cancel')]
1636-
* public method userCancel(...)
1636+
* public function userCancel(...) {}
16371637
* @endcode
16381638
* - On a class, specify the method name as well as the hook name:
16391639
* @code
16401640
* #[Hook('user_cancel', method: 'userCancel')]
16411641
* class Hooks {
1642-
* method userCancel(...) {}
1642+
* public function userCancel(...) {}
16431643
* }
16441644
* @endcode
16451645
* - On a class with an __invoke method, which is taken to be the hook
16461646
* implementation:
16471647
* @code
16481648
* #[Hook('user_cancel')]
16491649
* class Hooks {
1650-
* method __invoke(...) {}
1650+
* public function __invoke(...) {}
16511651
* }
16521652
* @endcode
16531653
*

lib/Drupal/Core/Hook/Attribute/Hook.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212
* - On a method, use this attribute with the hook name:
1313
* @code
1414
* #[Hook('user_cancel')]
15-
* public method userCancel(...)
15+
* public function userCancel(...) {}
1616
* @endcode
1717
* - On a class, specifying the method name:
1818
* @code
1919
* #[Hook('user_cancel', method: 'userCancel')]
2020
* class Hooks {
21-
* method userCancel(...) {}
21+
* public function userCancel(...) {}
2222
* }
2323
* @endcode
2424
* - On a class with an __invoke method, which is taken to be the hook
2525
* implementation:
2626
* @code
2727
* #[Hook('user_cancel')]
2828
* class Hooks {
29-
* method __invoke(...) {}
29+
* public function __invoke(...) {}
3030
* }
3131
* @endcode
3232
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: 'Test Hook attribute'
2+
type: module
3+
description: 'Test Hook attribute with named arguments, and class with invoke method'
4+
package: Testing
5+
version: VERSION
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\hook_collector_hook_attribute\Hook;
6+
7+
use Drupal\Core\Hook\Attribute\Hook;
8+
9+
/**
10+
* Test Hook attribute named arguments.
11+
*/
12+
#[Hook('cache_flush')]
13+
class HookAttributeInvokeHook {
14+
15+
/**
16+
* Implements hook_cache_flush().
17+
*/
18+
public function __invoke(): void {
19+
// Set a global value we can check in test code.
20+
$GLOBALS['hook_invoke_method'] = 'hook_invoke_method';
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Drupal\hook_collector_hook_attribute\Hook;
6+
7+
use Drupal\Core\Hook\Attribute\Hook;
8+
9+
/**
10+
* Test Hook attribute named arguments.
11+
*/
12+
#[Hook(hook: 'cache_flush', method: 'flush')]
13+
class HookAttributeNamedArgumentsHook {
14+
15+
/**
16+
* Implements hook_cache_flush().
17+
*/
18+
public function flush(): void {
19+
// Set a global value we can check in test code.
20+
$GLOBALS['hook_named_arguments'] = 'hook_named_arguments';
21+
}
22+
23+
}

tests/Drupal/KernelTests/Core/Hook/HookCollectorPassTest.php

+13
Original file line numberDiff line numberDiff line change
@@ -124,4 +124,17 @@ public function testProceduralHooksSkippedWhenConfigured(): void {
124124

125125
}
126126

127+
/**
128+
* Test Hook attribute with named arguments, and class with invoke method.
129+
*/
130+
public function testHookAttribute(): void {
131+
$module_installer = $this->container->get('module_installer');
132+
$this->assertTrue($module_installer->install(['hook_collector_hook_attribute']));
133+
$this->assertFalse(isset($GLOBALS['hook_named_arguments']));
134+
$this->assertFalse(isset($GLOBALS['hook_invoke_method']));
135+
drupal_flush_all_caches();
136+
$this->assertTrue(isset($GLOBALS['hook_named_arguments']));
137+
$this->assertTrue(isset($GLOBALS['hook_invoke_method']));
138+
}
139+
127140
}

tests/Drupal/Tests/Core/Hook/HookCollectorPassTest.php

+10-7
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,7 @@ public function testGetHookAttributesInClass(): void {
9393
$getHookAttributesInClass = fn ($class) => $this->getHookAttributesInClass($class);
9494
$p = new HookCollectorPass();
9595
$getHookAttributesInClass = $getHookAttributesInClass->bindTo($p, $p);
96-
$x = new class {
97-
98-
#[Hook('install')]
99-
function foo(): void {}
10096

101-
};
102-
$this->expectException(\LogicException::class);
103-
$hooks = $getHookAttributesInClass(get_class($x));
10497
$x = new class {
10598

10699
#[Hook('foo')]
@@ -111,6 +104,16 @@ function foo(): void {}
111104
$hook = reset($hooks);
112105
$this->assertInstanceOf(Hook::class, $hook);
113106
$this->assertSame('foo', $hook->hook);
107+
108+
$x = new class {
109+
110+
#[Hook('install')]
111+
function foo(): void {}
112+
113+
};
114+
$this->expectException(\LogicException::class);
115+
// This will throw exception, and stop code execution.
116+
$getHookAttributesInClass(get_class($x));
114117
}
115118

116119
}

0 commit comments

Comments
 (0)