You are here

public function HookDiscoveryTest::testGetDefinitions in Drupal 9

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()

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
    ->once())
    ->method('getImplementations')
    ->with('test_plugin')
    ->will($this
    ->returnValue([
    'hook_discovery_test',
    'hook_discovery_test2',
  ]));
  $this->moduleHandler
    ->expects($this
    ->exactly(2))
    ->method('invoke')
    ->willReturnMap([
    [
      'hook_discovery_test',
      'test_plugin',
      [],
      $this
        ->hookDiscoveryTestTestPlugin(),
    ],
    [
      'hook_discovery_test2',
      'test_plugin',
      [],
      $this
        ->hookDiscoveryTest2TestPlugin(),
    ],
  ]);
  $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']);
}