function TestDiscovery::getTestClasses

Same name in other branches
  1. 9 core/lib/Drupal/Core/Test/TestDiscovery.php \Drupal\Core\Test\TestDiscovery::getTestClasses()
  2. 8.9.x core/modules/simpletest/src/TestDiscovery.php \Drupal\simpletest\TestDiscovery::getTestClasses()
  3. 8.9.x core/lib/Drupal/Core/Test/TestDiscovery.php \Drupal\Core\Test\TestDiscovery::getTestClasses()
  4. 10 core/lib/Drupal/Core/Test/TestDiscovery.php \Drupal\Core\Test\TestDiscovery::getTestClasses()

Discovers all available tests in all extensions.


    $groups['block'] => [
      'Drupal\Tests\block\Functional\BlockTest' => [
        'name' => 'Drupal\Tests\block\Functional\BlockTest',
        'description' => 'Tests block UI CRUD functionality.',
        'group' => 'block',
        'groups' => ['block', 'group2', 'group3'],
      ],
    ];

@todo Remove singular grouping; retain list of groups in 'group' key.

Parameters

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

string[] $types: (optional) An array of included test types.

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

Return value

array An array of tests keyed by the group name. If a test is annotated to belong to multiple groups, it will appear under all group keys it belongs to.

See also

https://www.drupal.org/node/2296615

File

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

Class

TestDiscovery
Discovers available tests.

Namespace

Drupal\Core\Test

Code

public function getTestClasses($extension = NULL, array $types = [], ?string $directory = NULL) {
    if (!isset($extension) && empty($types)) {
        if (!empty($this->testClasses)) {
            return $this->testClasses;
        }
    }
    $list = [];
    $classmap = $this->findAllClassFiles($extension, $directory);
    // Prevent expensive class loader lookups for each reflected test class by
    // registering the complete classmap of test classes to the class loader.
    // This also ensures that test classes are loaded from the discovered
    // path names; a namespace/classname mismatch will throw an exception.
    $this->classLoader
        ->addClassMap($classmap);
    foreach ($classmap as $classname => $pathname) {
        $finder = MockFileFinder::create($pathname);
        $parser = new StaticReflectionParser($classname, $finder, TRUE);
        try {
            $info = static::getTestInfo($classname, $parser->getDocComment());
        } catch (MissingGroupException $e) {
            // If the class name ends in Test and is not a migrate table dump.
            if (str_ends_with($classname, 'Test') && !str_contains($classname, 'migrate_drupal\\Tests\\Table')) {
                throw $e;
            }
            // If the class is @group annotation just skip it. Most likely it is an
            // abstract class, trait or test fixture.
            continue;
        }
        foreach ($info['groups'] as $group) {
            $list[$group][$classname] = $info;
        }
    }
    // Sort the groups and tests within the groups by name.
    uksort($list, 'strnatcasecmp');
    foreach ($list as &$tests) {
        uksort($tests, 'strnatcasecmp');
    }
    if (!isset($extension) && empty($types)) {
        $this->testClasses = $list;
    }
    if ($types) {
        $list = NestedArray::filter($list, function ($element) use ($types) {
            return !(is_array($element) && isset($element['type']) && !in_array($element['type'], $types));
        });
    }
    return $list;
}

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