class TestDiscovery (View source)

Discovers available tests.

Properties

protected ClassLoader $classLoader

The class loader.

protected array $testClasses

Statically cached list of test classes.

protected array $testNamespaces

Cached map of all test namespaces to respective directories.

protected array $availableExtensions

Cached list of all available extension names, keyed by extension type.

protected string $root

The app root.

Methods

__construct(string $root, $class_loader)

Constructs a new test discovery.

array
registerTestNamespaces()

Registers test namespaces of all extensions and core test classes.

array
getTestClasses(string $extension = NULL, array $types = [])

Discovers all available tests in all extensions.

array
findAllClassFiles(string $extension = NULL)

Discovers all class files in all available extensions.

static array
scanDirectory(string $namespace_prefix, string $path)

Scans a given directory for class files.

static array
getTestInfo(string $classname, string $doc_comment = NULL)

Retrieves information about a test class for UI purposes.

static string
parseTestClassSummary(string $doc_comment)

Parses the phpDoc summary line of a test class.

static array
parseTestClassAnnotations(ReflectionClass $class)

Parses annotations in the phpDoc of a test class.

static string|false
getPhpunitTestSuite(string $classname)

Determines the phpunit testsuite for a given classname, based on namespace.

getExtensions()

Returns all available extensions.

Details

__construct(string $root, $class_loader)

Constructs a new test discovery.

Parameters

string $root

The app root.

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

array registerTestNamespaces()

Registers test namespaces of all extensions and core test classes.

Return Value

array

An associative array whose keys are PSR-4 namespace prefixes and whose values are directory names.

array getTestClasses(string $extension = NULL, array $types = [])

Discovers all available tests in all extensions.

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

array $types

An array of included test types.

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

array findAllClassFiles(string $extension = NULL)

Discovers all class files in all available extensions.

Parameters

string $extension

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

Return Value

array

A classmap containing all discovered class files; i.e., a map of fully-qualified classnames to pathnames.

static array scanDirectory(string $namespace_prefix, string $path)

Scans a given directory for class files.

Limit to '*Test.php' files (~10% less files to reflect/introspect).

Parameters

string $namespace_prefix

The namespace prefix to use for discovered classes. Must contain a trailing namespace separator (backslash). For example: 'Drupal\node\Tests\'

string $path

The directory path to scan. For example: '/path/to/drupal/core/modules/node/tests/src'

Return Value

array

An associative array whose keys are fully-qualified class names and whose values are corresponding filesystem pathnames.

Exceptions

InvalidArgumentException

See also

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

static array getTestInfo(string $classname, string $doc_comment = NULL)

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.

Exceptions

MissingGroupException

static string parseTestClassSummary(string $doc_comment)

Parses the phpDoc summary line of a test class.

Parameters

string $doc_comment

The documentation comment.

Return Value

string

The parsed phpDoc summary line. An empty string is returned if no summary line can be parsed.

static array parseTestClassAnnotations(ReflectionClass $class)

Parses annotations in the phpDoc of a test class.

Parameters

ReflectionClass $class

The reflected test class.

Return Value

array

An associative array that contains all annotations on the test class; typically including:

  • group: A list of @group values.
  • requires: An associative array of @requires values; e.g.:
    • module: A list of Drupal module dependencies that are required to exist.

See also

\PHPUnit\Util\Test::parseTestMethodAnnotations()
http://phpunit.de/manual/current/en/incomplete-and-skipped-tests.html#incomplete-and-skipped-tests.skipping-tests-using-requires

static string|false getPhpunitTestSuite(string $classname)

Determines the phpunit testsuite for a given classname, based on namespace.

Parameters

string $classname

The test classname.

Return Value

string|false

The testsuite name or FALSE if its not a phpunit test.

protected Extension[] getExtensions()

Returns all available extensions.

Return Value

Extension[]

An array of Extension objects, keyed by extension name.