Skip to content
Snippets Groups Projects
Unverified Commit b66ca108 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3501237 by nikolay shapovalov, nicxvan: Improve HookCollectorPass test

parent 868e8468
No related branches found
No related tags found
2 merge requests!2964Issue #2865710 : Dependencies from only one instance of a widget are used in display modes,!10223132456: Fix issue where views instances are emptied before an ajax request is complete
Pipeline #424598 passed with warnings
Pipeline: drupal

#424609

    Pipeline: drupal

    #424606

      Pipeline: drupal

      #424604

        ......@@ -1633,13 +1633,13 @@
        * - On a method, use the attribute with the hook name:
        * @code
        * #[Hook('user_cancel')]
        * public method userCancel(...)
        * public function userCancel(...) {}
        * @endcode
        * - On a class, specify the method name as well as the hook name:
        * @code
        * #[Hook('user_cancel', method: 'userCancel')]
        * class Hooks {
        * method userCancel(...) {}
        * public function userCancel(...) {}
        * }
        * @endcode
        * - On a class with an __invoke method, which is taken to be the hook
        ......@@ -1647,7 +1647,7 @@
        * @code
        * #[Hook('user_cancel')]
        * class Hooks {
        * method __invoke(...) {}
        * public function __invoke(...) {}
        * }
        * @endcode
        *
        ......
        ......@@ -12,13 +12,13 @@
        * - On a method, use this attribute with the hook name:
        * @code
        * #[Hook('user_cancel')]
        * public method userCancel(...)
        * public function userCancel(...) {}
        * @endcode
        * - On a class, specifying the method name:
        * @code
        * #[Hook('user_cancel', method: 'userCancel')]
        * class Hooks {
        * method userCancel(...) {}
        * public function userCancel(...) {}
        * }
        * @endcode
        * - On a class with an __invoke method, which is taken to be the hook
        ......@@ -26,7 +26,7 @@
        * @code
        * #[Hook('user_cancel')]
        * class Hooks {
        * method __invoke(...) {}
        * public function __invoke(...) {}
        * }
        * @endcode
        *
        ......
        name: 'Test Hook attribute'
        type: module
        description: 'Test Hook attribute with named arguments, and class with invoke method'
        package: Testing
        version: VERSION
        <?php
        declare(strict_types=1);
        namespace Drupal\hook_collector_hook_attribute\Hook;
        use Drupal\Core\Hook\Attribute\Hook;
        /**
        * Test Hook attribute named arguments.
        */
        #[Hook('cache_flush')]
        class HookAttributeInvokeHook {
        /**
        * Implements hook_cache_flush().
        */
        public function __invoke(): void {
        // Set a global value we can check in test code.
        $GLOBALS['hook_invoke_method'] = 'hook_invoke_method';
        }
        }
        <?php
        declare(strict_types=1);
        namespace Drupal\hook_collector_hook_attribute\Hook;
        use Drupal\Core\Hook\Attribute\Hook;
        /**
        * Test Hook attribute named arguments.
        */
        #[Hook(hook: 'cache_flush', method: 'flush')]
        class HookAttributeNamedArgumentsHook {
        /**
        * Implements hook_cache_flush().
        */
        public function flush(): void {
        // Set a global value we can check in test code.
        $GLOBALS['hook_named_arguments'] = 'hook_named_arguments';
        }
        }
        ......@@ -124,4 +124,17 @@ public function testProceduralHooksSkippedWhenConfigured(): void {
        }
        /**
        * Test Hook attribute with named arguments, and class with invoke method.
        */
        public function testHookAttribute(): void {
        $module_installer = $this->container->get('module_installer');
        $this->assertTrue($module_installer->install(['hook_collector_hook_attribute']));
        $this->assertFalse(isset($GLOBALS['hook_named_arguments']));
        $this->assertFalse(isset($GLOBALS['hook_invoke_method']));
        drupal_flush_all_caches();
        $this->assertTrue(isset($GLOBALS['hook_named_arguments']));
        $this->assertTrue(isset($GLOBALS['hook_invoke_method']));
        }
        }
        ......@@ -93,14 +93,7 @@ public function testGetHookAttributesInClass(): void {
        $getHookAttributesInClass = fn ($class) => $this->getHookAttributesInClass($class);
        $p = new HookCollectorPass();
        $getHookAttributesInClass = $getHookAttributesInClass->bindTo($p, $p);
        $x = new class {
        #[Hook('install')]
        function foo(): void {}
        };
        $this->expectException(\LogicException::class);
        $hooks = $getHookAttributesInClass(get_class($x));
        $x = new class {
        #[Hook('foo')]
        ......@@ -111,6 +104,16 @@ function foo(): void {}
        $hook = reset($hooks);
        $this->assertInstanceOf(Hook::class, $hook);
        $this->assertSame('foo', $hook->hook);
        $x = new class {
        #[Hook('install')]
        function foo(): void {}
        };
        $this->expectException(\LogicException::class);
        // This will throw exception, and stop code execution.
        $getHookAttributesInClass(get_class($x));
        }
        }
        0% Loading or .
        You are about to add 0 people to the discussion. Proceed with caution.
        Finish editing this message first!
        Please register or to comment