You are here

function drupal_phpunit_populate_class_loader in Drupal 9

Same name and namespace in other branches
  1. 8 core/tests/bootstrap.php \drupal_phpunit_populate_class_loader()

Populate class loader with additional namespaces for tests.

We run this in a function to avoid setting the class loader to a global that can change. This change can cause unpredictable false positives for phpunit's global state change watcher. The class loader can be retrieved from composer at any time by requiring autoload.php.

1 call to drupal_phpunit_populate_class_loader()
bootstrap.php in core/tests/bootstrap.php
Autoloader for Drupal PHPUnit testing.

File

core/tests/bootstrap.php, line 126
Autoloader for Drupal PHPUnit testing.

Code

function drupal_phpunit_populate_class_loader() {

  /** @var \Composer\Autoload\ClassLoader $loader */
  $loader = (require __DIR__ . '/../../autoload.php');

  // Start with classes in known locations.
  $loader
    ->add('Drupal\\BuildTests', __DIR__);
  $loader
    ->add('Drupal\\Tests', __DIR__);
  $loader
    ->add('Drupal\\TestSite', __DIR__);
  $loader
    ->add('Drupal\\KernelTests', __DIR__);
  $loader
    ->add('Drupal\\FunctionalTests', __DIR__);
  $loader
    ->add('Drupal\\FunctionalJavascriptTests', __DIR__);
  $loader
    ->add('Drupal\\TestTools', __DIR__);
  if (!isset($GLOBALS['namespaces'])) {

    // Scan for arbitrary extension namespaces from core and contrib.
    $extension_roots = drupal_phpunit_contrib_extension_directory_roots();
    $dirs = array_map('drupal_phpunit_find_extension_directories', $extension_roots);
    $dirs = array_reduce($dirs, 'array_merge', []);
    $GLOBALS['namespaces'] = drupal_phpunit_get_extension_namespaces($dirs);
  }
  foreach ($GLOBALS['namespaces'] as $prefix => $paths) {
    $loader
      ->addPsr4($prefix, $paths);
  }
  return $loader;
}