You are here

public static function TestDiscovery::getTestInfo in Drupal 9

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

Retrieves information about a test class for UI purposes.

Parameters

string $classname: The test classname.

string $doc_comment: (optional) The class PHPDoc comment. If not passed in reflection will be used but this is very expensive when parsing all the test classes.

Return value

array An associative array containing:

  • name: The test class name.
  • description: The test (PHPDoc) summary.
  • group: The test's first @group (parsed from PHPDoc annotations).
  • groups: All of the test's @group annotations, as an array (parsed from PHPDoc annotations).
  • requires: An associative array containing test requirements parsed from PHPDoc annotations:

    • module: List of Drupal module extension names the test depends on.

Throws

\Drupal\simpletest\Exception\MissingGroupException If the class does not have a @group annotation.

5 calls to TestDiscovery::getTestInfo()
TestDiscovery::getTestClasses in core/lib/Drupal/Core/Test/TestDiscovery.php
Discovers all available tests in all extensions.
TestDiscoveryTest::testGetTestInfoEmptyDocblock in core/tests/Drupal/Tests/Core/Test/TestDiscoveryTest.php
Ensure that classes are not reflected when the docblock is empty.
TestDiscoveryTest::testTestInfoParser in core/tests/Drupal/Tests/Core/Test/TestDiscoveryTest.php
@covers ::getTestInfo @dataProvider infoParserProvider
TestDiscoveryTest::testTestInfoParserMissingGroup in core/tests/Drupal/Tests/Core/Test/TestDiscoveryTest.php
@covers ::getTestInfo
TestDiscoveryTest::testTestInfoParserMissingSummary in core/tests/Drupal/Tests/Core/Test/TestDiscoveryTest.php
@covers ::getTestInfo

File

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

Class

TestDiscovery
Discovers available tests.

Namespace

Drupal\Core\Test

Code

public static function getTestInfo($classname, $doc_comment = NULL) {
  if ($doc_comment === NULL) {
    $reflection = new \ReflectionClass($classname);
    $doc_comment = $reflection
      ->getDocComment();
  }
  $info = [
    'name' => $classname,
  ];
  $annotations = [];

  // Look for annotations, allow an arbitrary amount of spaces before the
  // * but nothing else.
  preg_match_all('/^[ ]*\\* \\@([^\\s]*) (.*$)/m', $doc_comment, $matches);
  if (isset($matches[1])) {
    foreach ($matches[1] as $key => $annotation) {

      // For historical reasons, there is a single-value 'group' result key
      // and a 'groups' key as an array.
      if ($annotation === 'group') {
        $annotations['groups'][] = $matches[2][$key];
      }
      if (!empty($annotations[$annotation])) {

        // Only @group is allowed to have more than one annotation, in the
        // 'groups' key. Other annotations only have one value per key.
        continue;
      }
      $annotations[$annotation] = $matches[2][$key];
    }
  }
  if (empty($annotations['group'])) {

    // Concrete tests must have a group.
    throw new MissingGroupException(sprintf('Missing @group annotation in %s', $classname));
  }
  $info['group'] = $annotations['group'];
  $info['groups'] = $annotations['groups'];

  // Sort out PHPUnit-runnable tests by type.
  if ($testsuite = static::getPhpunitTestSuite($classname)) {
    $info['type'] = 'PHPUnit-' . $testsuite;
  }
  else {
    $info['type'] = 'Simpletest';
  }
  if (!empty($annotations['coversDefaultClass'])) {
    $info['description'] = 'Tests ' . $annotations['coversDefaultClass'] . '.';
  }
  else {
    $info['description'] = static::parseTestClassSummary($doc_comment);
  }
  if (isset($annotations['dependencies'])) {
    $info['requires']['module'] = array_map('trim', explode(',', $annotations['dependencies']));
  }
  return $info;
}