You are here

public function DiscoveryTestBase::testDiscoveryInterface in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/Drupal/KernelTests/Core/Plugin/Discovery/DiscoveryTestBase.php \Drupal\KernelTests\Core\Plugin\Discovery\DiscoveryTestBase::testDiscoveryInterface()

Tests getDefinitions() and getDefinition().

File

core/tests/Drupal/KernelTests/Core/Plugin/Discovery/DiscoveryTestBase.php, line 40

Class

DiscoveryTestBase
Base class for plugin discovery tests.

Namespace

Drupal\KernelTests\Core\Plugin\Discovery

Code

public function testDiscoveryInterface() {

  // Ensure that getDefinitions() returns the expected definitions.
  // For the arrays to be identical (instead of only equal), they must be
  // sorted equally, which seems unnecessary here.
  // The discovered definitions may contain circular references; use a custom
  // assertion message to prevent var_export() from getting called.
  $this
    ->assertEquals($this->expectedDefinitions, $this->discovery
    ->getDefinitions(), 'Expected definitions found.');

  // Ensure that getDefinition() returns the expected definition.
  foreach ($this->expectedDefinitions as $id => $definition) {
    $this
      ->assertDefinitionIdentical($this->discovery
      ->getDefinition($id), $definition);
  }

  // Ensure that an empty array is returned if no plugin definitions are found.
  $this
    ->assertSame([], $this->emptyDiscovery
    ->getDefinitions(), 'array() returned if no plugin definitions are found.');

  // Ensure that NULL is returned as the definition of a non-existing plugin.
  $this
    ->assertNull($this->emptyDiscovery
    ->getDefinition('non_existing', FALSE), 'NULL returned as the definition of a non-existing plugin.');
}