function HookDiscoveryTest::testGetDefinitions

Same name in other branches
  1. 9 core/tests/Drupal/Tests/Core/Plugin/Discovery/HookDiscoveryTest.php \Drupal\Tests\Core\Plugin\Discovery\HookDiscoveryTest::testGetDefinitions()
  2. 8.9.x core/tests/Drupal/Tests/Core/Plugin/Discovery/HookDiscoveryTest.php \Drupal\Tests\Core\Plugin\Discovery\HookDiscoveryTest::testGetDefinitions()
  3. 11.x core/tests/Drupal/Tests/Core/Plugin/Discovery/HookDiscoveryTest.php \Drupal\Tests\Core\Plugin\Discovery\HookDiscoveryTest::testGetDefinitions()

Tests the getDefinitions() method with some plugins.

See also

\Drupal\Core\Plugin\Discovery::getDefinitions()

File

core/tests/Drupal/Tests/Core/Plugin/Discovery/HookDiscoveryTest.php, line 55

Class

HookDiscoveryTest
@coversDefaultClass \Drupal\Core\Plugin\Discovery\HookDiscovery @group Plugin

Namespace

Drupal\Tests\Core\Plugin\Discovery

Code

public function testGetDefinitions() : void {
    $this->moduleHandler
        ->expects($this->atLeastOnce())
        ->method('invokeAllWith')
        ->with('test_plugin')
        ->willReturnCallback(function (string $hook, callable $callback) {
        $callback(\Closure::fromCallable([
            $this,
            'hookDiscoveryTestTestPlugin',
        ]), 'hook_discovery_test');
        $callback(\Closure::fromCallable([
            $this,
            'hookDiscoveryTest2TestPlugin',
        ]), 'hook_discovery_test2');
    });
    $this->moduleHandler
        ->expects($this->never())
        ->method('invoke');
    $definitions = $this->hookDiscovery
        ->getDefinitions();
    $this->assertCount(3, $definitions);
    $this->assertEquals('Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Apple', $definitions['test_id_1']['class']);
    $this->assertEquals('Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Orange', $definitions['test_id_2']['class']);
    $this->assertEquals('Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Cherry', $definitions['test_id_3']['class']);
    // Ensure that the module was set.
    $this->assertEquals('hook_discovery_test', $definitions['test_id_1']['provider']);
    $this->assertEquals('hook_discovery_test', $definitions['test_id_2']['provider']);
    $this->assertEquals('hook_discovery_test2', $definitions['test_id_3']['provider']);
}

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