class TestDiscovery

Same name in this branch
  1. 8.9.x core/lib/Drupal/Core/Test/TestDiscovery.php \Drupal\Core\Test\TestDiscovery
Same name in other branches
  1. 9 core/lib/Drupal/Core/Test/TestDiscovery.php \Drupal\Core\Test\TestDiscovery
  2. 10 core/lib/Drupal/Core/Test/TestDiscovery.php \Drupal\Core\Test\TestDiscovery
  3. 11.x core/lib/Drupal/Core/Test/TestDiscovery.php \Drupal\Core\Test\TestDiscovery

Discovers available tests.

This class provides backwards compatibility for code which uses the legacy \Drupal\simpletest\TestDiscovery.

Hierarchy

Expanded class hierarchy of TestDiscovery

Deprecated

in drupal:8.8.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Test\TestDiscovery instead.

See also

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

9 files declare their use of TestDiscovery
simpletest.module in core/modules/simpletest/simpletest.module
Provides testing functionality.
SimpletestDeprecationTest.php in core/modules/simpletest/tests/src/Kernel/SimpletestDeprecationTest.php
SimpletestResultsForm.php in core/modules/simpletest/src/Form/SimpletestResultsForm.php
SimpletestTestForm.php in core/modules/simpletest/src/Form/SimpletestTestForm.php
TestDeprecatedTestHooks.php in core/modules/simpletest/tests/src/Kernel/TestDeprecatedTestHooks.php

... See full list

1 string reference to 'TestDiscovery'
simpletest.services.yml in core/modules/simpletest/simpletest.services.yml
core/modules/simpletest/simpletest.services.yml
1 service uses TestDiscovery
test_discovery in core/modules/simpletest/simpletest.services.yml
Drupal\simpletest\TestDiscovery

File

core/modules/simpletest/src/TestDiscovery.php, line 25

Namespace

Drupal\simpletest
View source
class TestDiscovery extends CoreTestDiscovery {
    
    /**
     * The module handler.
     *
     * @var \Drupal\Core\Extension\ModuleHandlerInterface
     */
    protected $moduleHandler;
    
    /**
     * Constructs a new test discovery.
     *
     * @param string $root
     *   The app root.
     * @param $class_loader
     *   The class loader. Normally Composer's ClassLoader, as included by the
     *   front controller, but may also be decorated; e.g.,
     *   \Symfony\Component\ClassLoader\ApcClassLoader.
     * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
     *   The module handler.
     */
    public function __construct($root, $class_loader, ModuleHandlerInterface $module_handler) {
        parent::__construct($root, $class_loader);
        $this->moduleHandler = $module_handler;
    }
    
    /**
     * Discovers all available tests in all extensions.
     *
     * This method is a near-duplicate of
     * \Drupal\Core\Tests\TestDiscovery::getTestClasses(). It exists so that we
     * can provide a BC invocation of hook_simpletest_alter().
     *
     * @param string $extension
     *   (optional) The name of an extension to limit discovery to; e.g., 'node'.
     * @param string[] $types
     *   An array of included test types.
     *
     * @return 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.
     * @code
     *     $groups['block'] => array(
     *       'Drupal\Tests\block\Functional\BlockTest' => array(
     *         'name' => 'Drupal\Tests\block\Functional\BlockTest',
     *         'description' => 'Tests block UI CRUD functionality.',
     *         'group' => 'block',
     *         'groups' => ['block', 'group2', 'group3'],
     *       ),
     *     );
     * @endcode
     */
    public function getTestClasses($extension = NULL, array $types = []) {
        if (!isset($extension) && empty($types)) {
            if (!empty($this->testClasses)) {
                return $this->testClasses;
            }
        }
        $list = [];
        $classmap = $this->findAllClassFiles($extension);
        // 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
        // pathnames; 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 (preg_match('/Test$/', $classname) && strpos($classname, 'migrate_drupal\\Tests\\Table') === FALSE) {
                    throw $e;
                }
                // If the class is @group annotation just skip it. Most likely it is an
                // abstract class, trait or test fixture.
                continue;
            }
            // Skip this test class if it is a Simpletest-based test and requires
            // unavailable modules. TestDiscovery should not filter out module
            // requirements for PHPUnit-based test classes.
            // @todo Move this behavior to \Drupal\simpletest\TestBase so tests can be
            //       marked as skipped, instead.
            // @see https://www.drupal.org/node/1273478
            if ($info['type'] == 'Simpletest') {
                if (!empty($info['requires']['module'])) {
                    if (array_diff($info['requires']['module'], $this->availableExtensions['module'])) {
                        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');
        }
        // Allow modules extending core tests to disable originals.
        $this->moduleHandler
            ->alterDeprecated('Convert your test to a PHPUnit-based one and implement test listeners. See: https://www.drupal.org/node/2939892', 'simpletest', $list);
        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;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
TestDiscovery::$availableExtensions protected property Cached list of all available extension names, keyed by extension type.
TestDiscovery::$classLoader protected property The class loader.
TestDiscovery::$moduleHandler protected property The module handler.
TestDiscovery::$root protected property The app root.
TestDiscovery::$testClasses protected property Statically cached list of test classes.
TestDiscovery::$testNamespaces protected property Cached map of all test namespaces to respective directories.
TestDiscovery::findAllClassFiles public function Discovers all class files in all available extensions.
TestDiscovery::getExtensions protected function Returns all available extensions.
TestDiscovery::getPhpunitTestSuite public static function Determines the phpunit testsuite for a given classname, based on namespace.
TestDiscovery::getTestClasses public function Discovers all available tests in all extensions. Overrides TestDiscovery::getTestClasses
TestDiscovery::getTestInfo public static function Retrieves information about a test class for UI purposes.
TestDiscovery::parseTestClassAnnotations public static function Parses annotations in the phpDoc of a test class.
TestDiscovery::parseTestClassSummary public static function Parses the phpDoc summary line of a test class.
TestDiscovery::registerTestNamespaces public function Registers test namespaces of all extensions and core test classes.
TestDiscovery::scanDirectory public static function Scans a given directory for class files.
TestDiscovery::__construct public function Constructs a new test discovery. Overrides TestDiscovery::__construct

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