function PhpUnitTestDiscovery::getTestClasses

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Test/PhpUnitTestDiscovery.php \Drupal\Core\Test\PhpUnitTestDiscovery::getTestClasses()

Discovers available tests.

Parameters

string|null $extension: (optional) The name of an extension to limit discovery to; e.g., 'node'.

list<string> $testSuites: (optional) An array of PHPUnit test suites to filter the discovery for.

string|null $directory: (optional) Limit discovered tests to a specific directory.

list<string> $testGroups: (optional) An array of test groups to filter the discovery for.

Return value

GroupedTestClassInfoList An array of test groups keyed by the group name. Each test group is an array of test class information arrays as returned by ::getTestClassInfo(), keyed by test class. If a test class belongs to multiple groups, it will appear under all group keys it belongs to.

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

File

core/lib/Drupal/Core/Test/PhpUnitTestDiscovery.php, line 95

Class

PhpUnitTestDiscovery
Discovers available tests using the PHPUnit API.

Namespace

Drupal\Core\Test

Code

public function getTestClasses(?string $extension = NULL, array $testSuites = [], ?string $directory = NULL, array $testGroups = []) : array {
  $this->warnings = [];
  $args = [
    '--configuration',
    $this->configurationFilePath,
  ];
  if (!empty($testSuites)) {
    foreach ($testSuites as $testSuite) {
      if (!is_string($testSuite)) {
        throw new \InvalidArgumentException("Test suite must be a string");
      }
      if (str_contains($testSuite, ' ')) {
        throw new \InvalidArgumentException("Test suite name '{$testSuite}' is invalid");
      }
    }
    $args[] = '--testsuite=' . implode(',', $testSuites);
  }
  if ($directory !== NULL) {
    $args[] = $directory;
  }
  $phpUnitConfiguration = (new Builder())->build($args);
  // TestSuiteBuilder calls the test data providers during the discovery.
  // Data providers may be changing the Drupal service container, which leads
  // to potential issues. We save the current container before running the
  // discovery, and in case a change is detected, reset it and raise
  // warnings so that developers can tune their data provider code.
  if (\Drupal::hasContainer()) {
    $container = \Drupal::getContainer();
    $containerObjectId = spl_object_id($container);
  }
  $phpUnitTestSuite = (new TestSuiteBuilder())->build($phpUnitConfiguration);
  if (isset($containerObjectId) && $containerObjectId !== spl_object_id(\Drupal::getContainer())) {
    $this->addWarning(">>> The service container was changed during the test discovery <<<\n" . "Probably, a test data provider method called \\Drupal::setContainer().\n" . "Ensure that all the data providers restore the original container before returning data.");
    assert(isset($container));
    \Drupal::setContainer($container);
  }
  $list = $directory === NULL ? $this->getTestList($phpUnitTestSuite, $extension, $testGroups) : $this->getTestListLimitedToDirectory($phpUnitTestSuite, $extension, $testSuites, $testGroups);
  // Sort the groups and tests within the groups by name.
  uksort($list, 'strnatcasecmp');
  foreach ($list as &$tests) {
    uksort($tests, 'strnatcasecmp');
  }
  return $list;
}

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