class HookCollectorPassTest

Same name in this branch
  1. 11.x core/tests/Drupal/KernelTests/Core/Hook/HookCollectorPassTest.php \Drupal\KernelTests\Core\Hook\HookCollectorPassTest

@coversDefaultClass \Drupal\Core\Hook\HookCollectorPass @group Hook

Hierarchy

Expanded class hierarchy of HookCollectorPassTest

File

core/tests/Drupal/Tests/Core/Hook/HookCollectorPassTest.php, line 21

Namespace

Drupal\Tests\Core\Hook
View source
class HookCollectorPassTest extends UnitTestCase {
    use GroupIncludesTestTrait;
    
    /**
     * @covers ::collectAllHookImplementations
     * @covers ::filterIterator
     */
    public function testCollectAllHookImplementations() : void {
        vfsStream::setup('drupal_root');
        $files = [
            'modules/test_module/test_module_info.yml',
            // This creates a submodule which is not installed.
'modules/test_module/test_sub_module/test_sub_module.info.yml',
        ];
        $file_data = [];
        foreach ($files as &$filename) {
            NestedArray::setValue($file_data, explode('/', $filename), '');
        }
        vfsStream::create($file_data);
        $module_filenames = [
            'test_module' => [
                'pathname' => 'vfs://drupal_root/modules/test_module/test_module_info.yml',
            ],
        ];
        // This directory, however, should be included.
        mkdir('vfs://drupal_root/modules/test_module/includes');
        file_put_contents('vfs://drupal_root/modules/test_module/includes/test_module.inc', <<<__EOF__
<?php

function test_module_test_hook();

__EOF__
);
        // This is the not installed submodule.
        file_put_contents('vfs://drupal_root/modules/test_module/test_sub_module/test_sub_module.module', <<<__EOF__
<?php

function test_module_should_be_skipped();

__EOF__
);
        $implementations['test_hook'][ProceduralCall::class]['test_module_test_hook'] = 'test_module';
        $includes = [
            'test_module_test_hook' => 'vfs://drupal_root/modules/test_module/includes/test_module.inc',
        ];
        $container = new ContainerBuilder();
        $container->setParameter('container.modules', $module_filenames);
        $container->setDefinition('module_handler', new Definition());
        (new HookCollectorPass())->process($container);
        $this->assertSame($implementations, $container->getParameter('hook_implementations_map'));
        $this->assertSame($includes, $container->getDefinition(ProceduralCall::class)
            ->getArguments()[0]);
    }
    
    /**
     * @covers ::process
     * @covers ::collectModuleHookImplementations
     */
    public function testGroupIncludes() : void {
        $module_filenames = self::setupGroupIncludes();
        $container = new ContainerBuilder();
        $container->setParameter('container.modules', $module_filenames);
        $container->setDefinition('module_handler', new Definition());
        (new HookCollectorPass())->process($container);
        $argument = $container->getDefinition('module_handler')
            ->getArgument('$groupIncludes');
        $this->assertSame(self::GROUP_INCLUDES, $argument);
    }
    
    /**
     * @covers ::getHookAttributesInClass
     */
    public function testGetHookAttributesInClass() : void {
        
        /** @phpstan-ignore-next-line */
        $getHookAttributesInClass = fn($class) => $this->getHookAttributesInClass($class);
        $p = new HookCollectorPass();
        $getHookAttributesInClass = $getHookAttributesInClass->bindTo($p, $p);
        $x = new class  {
            function foo() : void {
            }

};
        $this->expectException(\LogicException::class);
        $hooks = $getHookAttributesInClass(get_class($x));
        $x = new class  {
            function foo() : void {
            }

};
        $hooks = $getHookAttributesInClass(get_class($x));
        $hook = reset($hooks);
        $this->assertInstanceOf(Hook::class, $hook);
        $this->assertSame('foo', $hook->hook);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overrides
ExpectDeprecationTrait::expectDeprecation public function Adds an expected deprecation.
ExpectDeprecationTrait::getCallableName private static function Returns a callable as a string suitable for inclusion in a message.
ExpectDeprecationTrait::setUpErrorHandler public function Sets up the test error handler.
ExpectDeprecationTrait::tearDownErrorHandler public function Tears down the test error handler.
GroupIncludesTestTrait::GROUP_INCLUDES constant
GroupIncludesTestTrait::setupGroupIncludes public static function
HookCollectorPassTest::testCollectAllHookImplementations public function @covers ::collectAllHookImplementations
@covers ::filterIterator
HookCollectorPassTest::testGetHookAttributesInClass public function @covers ::getHookAttributesInClass
HookCollectorPassTest::testGroupIncludes public function @covers ::process
@covers ::collectModuleHookImplementations
RandomGeneratorTrait::getRandomGenerator protected function Gets the random generator for the utility methods.
RandomGeneratorTrait::randomMachineName protected function Generates a unique random string containing letters and numbers.
RandomGeneratorTrait::randomObject public function Generates a random PHP object.
RandomGeneratorTrait::randomString public function Generates a pseudo-random string of ASCII characters of codes 32 to 126.
UnitTestCase::$root protected property The app root.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::setUp protected function 367
UnitTestCase::setUpBeforeClass public static function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.