function ModuleHandlerTest::testHasImplementations

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php \Drupal\Tests\Core\Extension\ModuleHandlerTest::testHasImplementations()
  2. 10 core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php \Drupal\Tests\Core\Extension\ModuleHandlerTest::testHasImplementations()

Tests hasImplementations.

@covers ::getHookListeners @covers ::hasImplementations

File

core/tests/Drupal/Tests/Core/Extension/ModuleHandlerTest.php, line 343

Class

ModuleHandlerTest
@coversDefaultClass \Drupal\Core\Extension\ModuleHandler @runTestsInSeparateProcesses

Namespace

Drupal\Tests\Core\Extension

Code

public function testHasImplementations() : void {
    $c = new class  {
        function some_method() : void {
        }

};
    $implementations['some_hook'][get_class($c)]['some_method'] = 'some_module';
    $module_handler = $this->getModuleHandler($implementations);
    $module_handler->setModuleList([
        'some_module' => TRUE,
    ]);
    $r = new \ReflectionObject($module_handler);
    // Set up some synthetic results.
    $this->eventDispatcher
        ->expects($this->once())
        ->method('getListeners')
        ->with('drupal_hook.some_hook')
        ->willReturn([
        [
            $c,
            'some_method',
        ],
    ]);
    $this->assertNotEmpty($module_handler->hasImplementations('some_hook'));
    $listeners = $r->getProperty('invokeMap')
        ->getValue($module_handler)['some_hook']['some_module'];
    // Anonymous class doesn't work with assertSame() so assert it piecemeal.
    $this->assertSame(count($listeners), 1);
    foreach ($listeners as $listener) {
        $this->assertSame(get_class($c), get_class($listener[0]));
        $this->assertSame('some_method', $listener[1]);
    }
}

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