function PhpUnitTestDiscovery::getTestClassInfo

Returns the test class information.

Parameters

\PHPUnit\Framework\Test $testClass: The test class.

string $testSuite: The test suite of this test class.

Return value

array{name: class-string, description: string, group: string|int, groups: list<string|int>, type: string, file: string, tests_count: positive-int} The test class information.

2 calls to PhpUnitTestDiscovery::getTestClassInfo()
PhpUnitTestDiscovery::getTestList in core/lib/Drupal/Core/Test/PhpUnitTestDiscovery.php
Returns a list of tests from a TestSuite object.
PhpUnitTestDiscovery::getTestListLimitedToDirectory in core/lib/Drupal/Core/Test/PhpUnitTestDiscovery.php
Returns a list of tests from a TestSuite object limited to a directory.

File

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

Class

PhpUnitTestDiscovery
Discovers available tests using the PHPUnit API.

Namespace

Drupal\Core\Test

Code

private function getTestClassInfo(Test $testClass, string $testSuite) : array {
  $reflection = new \ReflectionClass($testClass->name());
  // Let PHPUnit API return the groups, as it will deal transparently with
  // annotations or attributes, but skip groups generated by PHPUnit
  // internally and starting with a double underscore prefix.
  if (RunnerVersion::getMajor() < 11) {
    $groups = array_filter($testClass->groups(), function (string $value) : bool {
      return !str_starts_with($value, '__phpunit');
    });
  }
  else {
    // In PHPUnit 11+, we need to coalesce the groups from individual tests
    // as they may not be available from the test class level (when tests are
    // backed by data providers).
    $tmp = [];
    foreach ($testClass as $test) {
      if ($test instanceof DataProviderTestSuite) {
        foreach ($test as $testWithData) {
          $tmp = array_merge($tmp, $testWithData->groups());
        }
      }
      else {
        $tmp = array_merge($tmp, $test->groups());
      }
    }
    $groups = array_filter(array_unique($tmp), function (string $value) : bool {
      return !str_starts_with($value, '__phpunit');
    });
  }
  if (empty($groups)) {
    throw new MissingGroupException(sprintf('Missing group metadata in test class %s', $testClass->name()));
  }
  // Let PHPUnit API return the class coverage information.
  $test = $testClass;
  while (!$test instanceof TestCase) {
    $test = $test->tests()[0];
  }
  if (($metadata = $test->valueObjectForEvents()
    ->metadata()
    ->isCoversClass()) && $metadata->isNotEmpty()) {
    $description = sprintf('Tests %s.', $metadata->asArray()[0]
      ->className());
  }
  elseif (($metadata = $test->valueObjectForEvents()
    ->metadata()
    ->isCoversDefaultClass()) && $metadata->isNotEmpty()) {
    $description = sprintf('Tests %s.', $metadata->asArray()[0]
      ->className());
  }
  else {
    $description = TestDiscovery::parseTestClassSummary($reflection->getDocComment());
  }
  // Find the test cases count.
  $count = 0;
  foreach ($testClass->tests() as $testCase) {
    if ($testCase instanceof TestCase) {
      // If it's a straight test method, counts 1.
      $count++;
    }
    else {
      // It's a data provider test suite, count 1 per data set provided.
      $count += count($testCase->tests());
    }
  }
  return [
    'name' => $testClass->name(),
    'group' => $groups[0],
    'groups' => $groups,
    'type' => $testSuite,
    'description' => $description,
    'file' => $reflection->getFileName(),
    'tests_count' => $count,
  ];
}

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