function HookDiscoveryTest::testGetDefinition

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

Tests the getDefinition method with some plugins.

See also

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

File

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

Class

HookDiscoveryTest
@coversDefaultClass \Drupal\Core\Plugin\Discovery\HookDiscovery[[api-linebreak]] @group Plugin

Namespace

Drupal\Tests\Core\Plugin\Discovery

Code

public function testGetDefinition() : void {
  $this->moduleHandler
    ->expects($this->exactly(4))
    ->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->assertNull($this->hookDiscovery
    ->getDefinition('test_non_existent', FALSE));
  $plugin_definition = $this->hookDiscovery
    ->getDefinition('test_id_1');
  $this->assertEquals('Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Apple', $plugin_definition['class']);
  $this->assertEquals('hook_discovery_test', $plugin_definition['provider']);
  $plugin_definition = $this->hookDiscovery
    ->getDefinition('test_id_2');
  $this->assertEquals('Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Orange', $plugin_definition['class']);
  $this->assertEquals('hook_discovery_test', $plugin_definition['provider']);
  $plugin_definition = $this->hookDiscovery
    ->getDefinition('test_id_3');
  $this->assertEquals('Drupal\\plugin_test\\Plugin\\plugin_test\\fruit\\Cherry', $plugin_definition['class']);
  $this->assertEquals('hook_discovery_test2', $plugin_definition['provider']);
}

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