class HookOrderTestBase

Hierarchy

Expanded class hierarchy of HookOrderTestBase

File

core/tests/Drupal/Tests/Core/Hook/HookOrderTestBase.php, line 10

Namespace

Drupal\Tests\Core\Hook
View source
abstract class HookOrderTestBase extends UnitTestCase {
    
    /**
     * The container builder.
     *
     * @var \Drupal\Core\DependencyInjection\ContainerBuilder
     */
    protected ContainerBuilder $container;
    
    /**
     * Set up three service listeners, "a", "b" and "c".
     *
     * The service id, the class name and the method name are all the same.
     *
     * @param bool $different_priority
     *   When TRUE, "c" will fire first, "b" second and "a" last. When FALSE,
     *   the priority will be set to be the same and the order is undefined.
     *
     * @return void
     */
    protected function setUpContainer(bool $different_priority) : void {
        $this->container = new ContainerBuilder();
        foreach ([
            'a',
            'b',
            'c',
        ] as $key => $name) {
            $definition = $this->container
                ->register($name, $name)
                ->setAutowired(TRUE);
            $definition->addTag('kernel.event_listener', [
                'event' => 'drupal_hook.test',
                'method' => $name,
                // Do not use $key itself to avoid a 0 priority which could potentially
                // lead to misleading results.
'priority' => $different_priority ? $key + 3 : 0,
            ]);
        }
    }
    
    /**
     * Get the priority for a service.
     */
    protected function getPriority(string $name) : int {
        $definition = $this->container
            ->getDefinition($name);
        return $definition->getTags()['kernel.event_listener'][0]['priority'];
    }

}

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.
HookOrderTestBase::$container protected property The container builder.
HookOrderTestBase::getPriority protected function Get the priority for a service. 1
HookOrderTestBase::setUpContainer protected function Set up three service listeners, "a", "b" and "c".
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.