function 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.

Return value

array<string<array<class-string, array{name: class-string, description: string, group: string|int, groups: list<string|int>, type: string, file: string, tests_count: positive-int}>>> 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 110

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 {
  $this->warnings = [];
  $args = [
    '--configuration',
    $this->configurationFilePath,
  ];
  if (!empty($testSuites)) {
    // Convert $testSuites from Drupal's legacy syntax to the syntax used in
    // phpunit.xml, that is necessary to PHPUnit to be able to apply the
    // test suite filter. For example, 'PHPUnit-Unit' to 'unit'.
    $tmp = [];
    foreach ($testSuites as $i) {
      if (!is_string($i)) {
        throw new \InvalidArgumentException("Test suite must be a string");
      }
      if (str_contains($i, ' ')) {
        throw new \InvalidArgumentException("Test suite name '{$i}' is invalid");
      }
      $tmp[] = $this->map[$i] ?? $i;
    }
    $args[] = '--testsuite=' . implode(',', $tmp);
  }
  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) : $this->getTestListLimitedToDirectory($phpUnitTestSuite, $extension, $testSuites);
  // 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.