You are here

public function HookDiscoveryTest::testGetDefinitions in Drupal 10

Same name and namespace in other branches
  1. 8 core/tests/Drupal/Tests/Core/Plugin/Discovery/HookDiscoveryTest.php \Drupal\Tests\Core\Plugin\Discovery\HookDiscoveryTest::testGetDefinitions()
  2. 9 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 56

Class

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

Namespace

Drupal\Tests\Core\Plugin\Discovery

Code

public function testGetDefinitions() {
  $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']);
}