You are here

public function DiscoveryTestBase::testDiscoveryInterface in Drupal 8

Same name and namespace in other branches
  1. 9 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
    ->assertEqual($this->discovery
    ->getDefinitions(), $this->expectedDefinitions, '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
    ->assertIdentical($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
    ->assertIdentical($this->emptyDiscovery
    ->getDefinition('non_existing', FALSE), NULL, 'NULL returned as the definition of a non-existing plugin.');
}