You are here

public function TestDiscovery::registerTestNamespaces in Drupal 8

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Test/TestDiscovery.php \Drupal\Core\Test\TestDiscovery::registerTestNamespaces()

Registers test namespaces of all extensions and core test classes.

Return value

array An associative array whose keys are PSR-4 namespace prefixes and whose values are directory names.

1 call to TestDiscovery::registerTestNamespaces()
TestDiscovery::findAllClassFiles in core/lib/Drupal/Core/Test/TestDiscovery.php
Discovers all class files in all available extensions.

File

core/lib/Drupal/Core/Test/TestDiscovery.php, line 74

Class

TestDiscovery
Discovers available tests.

Namespace

Drupal\Core\Test

Code

public function registerTestNamespaces() {
  if (isset($this->testNamespaces)) {
    return $this->testNamespaces;
  }
  $this->testNamespaces = [];
  $existing = $this->classLoader
    ->getPrefixesPsr4();

  // Add PHPUnit test namespaces of Drupal core.
  $this->testNamespaces['Drupal\\Tests\\'] = [
    $this->root . '/core/tests/Drupal/Tests',
  ];
  $this->testNamespaces['Drupal\\BuildTests\\'] = [
    $this->root . '/core/tests/Drupal/BuildTests',
  ];
  $this->testNamespaces['Drupal\\KernelTests\\'] = [
    $this->root . '/core/tests/Drupal/KernelTests',
  ];
  $this->testNamespaces['Drupal\\FunctionalTests\\'] = [
    $this->root . '/core/tests/Drupal/FunctionalTests',
  ];
  $this->testNamespaces['Drupal\\FunctionalJavascriptTests\\'] = [
    $this->root . '/core/tests/Drupal/FunctionalJavascriptTests',
  ];
  $this->testNamespaces['Drupal\\TestTools\\'] = [
    $this->root . '/core/tests/Drupal/TestTools',
  ];
  $this->availableExtensions = [];
  foreach ($this
    ->getExtensions() as $name => $extension) {
    $this->availableExtensions[$extension
      ->getType()][$name] = $name;
    $base_path = $this->root . '/' . $extension
      ->getPath();

    // Add namespace of disabled/uninstalled extensions.
    if (!isset($existing["Drupal\\{$name}\\"])) {
      $this->classLoader
        ->addPsr4("Drupal\\{$name}\\", "{$base_path}/src");
    }

    // Add Simpletest test namespace.
    $this->testNamespaces["Drupal\\{$name}\\Tests\\"][] = "{$base_path}/src/Tests";

    // Add PHPUnit test namespaces.
    $this->testNamespaces["Drupal\\Tests\\{$name}\\Unit\\"][] = "{$base_path}/tests/src/Unit";
    $this->testNamespaces["Drupal\\Tests\\{$name}\\Kernel\\"][] = "{$base_path}/tests/src/Kernel";
    $this->testNamespaces["Drupal\\Tests\\{$name}\\Functional\\"][] = "{$base_path}/tests/src/Functional";
    $this->testNamespaces["Drupal\\Tests\\{$name}\\Build\\"][] = "{$base_path}/tests/src/Build";
    $this->testNamespaces["Drupal\\Tests\\{$name}\\FunctionalJavascript\\"][] = "{$base_path}/tests/src/FunctionalJavascript";

    // Add discovery for traits which are shared between different test
    // suites.
    $this->testNamespaces["Drupal\\Tests\\{$name}\\Traits\\"][] = "{$base_path}/tests/src/Traits";
  }
  foreach ($this->testNamespaces as $prefix => $paths) {
    $this->classLoader
      ->addPsr4($prefix, $paths);
  }
  return $this->testNamespaces;
}