5 * Autoloader for Drupal PHPUnit testing.
7 * @see phpunit.xml.dist
10 use Drupal\Component\Assertion\Handle;
13 * Finds all valid extension directories recursively within a given directory.
15 * @param string $scan_directory
16 * The directory that should be recursively scanned.
18 * An associative array of extension directories found within the scanned
19 * directory, keyed by extension name.
21 function drupal_phpunit_find_extension_directories($scan_directory) {
23 $dirs = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($scan_directory, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS));
24 foreach ($dirs as $dir) {
25 if (strpos($dir->getPathname(), '.info.yml') !== FALSE) {
26 // Cut off ".info.yml" from the filename for use as the extension name. We
27 // use getRealPath() so that we can scan extensions represented by
29 $extensions[substr($dir->getFilename(), 0, -9)] = $dir->getPathInfo()
37 * Returns directories under which contributed extensions may exist.
40 * (optional) Path to the root of the Drupal installation.
43 * An array of directories under which contributed extensions may exist.
45 function drupal_phpunit_contrib_extension_directory_roots($root = NULL) {
47 $root = dirname(dirname(__DIR__));
50 $root . '/core/modules',
51 $root . '/core/profiles',
56 $sites_path = $root . '/sites';
57 // Note this also checks sites/../modules and sites/../profiles.
58 foreach (scandir($sites_path) as $site) {
59 if ($site[0] === '.' || $site === 'simpletest') {
62 $path = "$sites_path/$site";
63 $paths[] = is_dir("$path/modules") ? realpath("$path/modules") : NULL;
64 $paths[] = is_dir("$path/profiles") ? realpath("$path/profiles") : NULL;
65 $paths[] = is_dir("$path/themes") ? realpath("$path/themes") : NULL;
67 return array_filter($paths);
71 * Registers the namespace for each extension directory with the autoloader.
74 * An associative array of extension directories, keyed by extension name.
77 * An associative array of extension directories, keyed by their namespace.
79 function drupal_phpunit_get_extension_namespaces($dirs) {
80 $suite_names = ['Unit', 'Kernel', 'Functional', 'FunctionalJavascript'];
82 foreach ($dirs as $extension => $dir) {
83 if (is_dir($dir . '/src')) {
84 // Register the PSR-4 directory for module-provided classes.
85 $namespaces['Drupal\\' . $extension . '\\'][] = $dir . '/src';
87 $test_dir = $dir . '/tests/src';
88 if (is_dir($test_dir)) {
89 foreach ($suite_names as $suite_name) {
90 $suite_dir = $test_dir . '/' . $suite_name;
91 if (is_dir($suite_dir)) {
92 // Register the PSR-4 directory for PHPUnit-based suites.
93 $namespaces['Drupal\\Tests\\' . $extension . '\\' . $suite_name . '\\'][] = $suite_dir;
96 // Extensions can have a \Drupal\extension\Traits namespace for
97 // cross-suite trait code.
98 $trait_dir = $test_dir . '/Traits';
99 if (is_dir($trait_dir)) {
100 $namespaces['Drupal\\Tests\\' . $extension . '\\Traits\\'][] = $trait_dir;
107 // We define the COMPOSER_INSTALL constant, so that PHPUnit knows where to
108 // autoload from. This is needed for tests run in isolation mode, because
109 // phpunit.xml.dist is located in a non-default directory relative to the
110 // PHPUnit executable.
111 if (!defined('PHPUNIT_COMPOSER_INSTALL')) {
112 define('PHPUNIT_COMPOSER_INSTALL', __DIR__ . '/../../autoload.php');
116 * Populate class loader with additional namespaces for tests.
118 * We run this in a function to avoid setting the class loader to a global
119 * that can change. This change can cause unpredictable false positives for
120 * phpunit's global state change watcher. The class loader can be retrieved from
121 * composer at any time by requiring autoload.php.
123 function drupal_phpunit_populate_class_loader() {
125 /** @var \Composer\Autoload\ClassLoader $loader */
126 $loader = require __DIR__ . '/../../autoload.php';
128 // Start with classes in known locations.
129 $loader->add('Drupal\\Tests', __DIR__);
130 $loader->add('Drupal\\KernelTests', __DIR__);
131 $loader->add('Drupal\\FunctionalTests', __DIR__);
132 $loader->add('Drupal\\FunctionalJavascriptTests', __DIR__);
134 if (!isset($GLOBALS['namespaces'])) {
135 // Scan for arbitrary extension namespaces from core and contrib.
136 $extension_roots = drupal_phpunit_contrib_extension_directory_roots();
138 $dirs = array_map('drupal_phpunit_find_extension_directories', $extension_roots);
139 $dirs = array_reduce($dirs, 'array_merge', []);
140 $GLOBALS['namespaces'] = drupal_phpunit_get_extension_namespaces($dirs);
142 foreach ($GLOBALS['namespaces'] as $prefix => $paths) {
143 $loader->addPsr4($prefix, $paths);
149 // Do class loader population.
150 drupal_phpunit_populate_class_loader();
152 // Set sane locale settings, to ensure consistent string, dates, times and
154 // @see \Drupal\Core\DrupalKernel::bootEnvironment()
155 setlocale(LC_ALL, 'C');
157 // Set the default timezone. While this doesn't cause any tests to fail, PHP
158 // complains if 'date.timezone' is not set in php.ini. The Australia/Sydney
159 // timezone is chosen so all tests are run using an edge case scenario (UTC+10
160 // and DST). This choice is made to prevent timezone related regressions and
161 // reduce the fragility of the testing system in general.
162 date_default_timezone_set('Australia/Sydney');
164 // Runtime assertions. PHPUnit follows the php.ini assert.active setting for
165 // runtime assertions. By default this setting is on. Here we make a call to
166 // make PHP 5 and 7 handle assertion failures the same way, but this call does
167 // not turn runtime assertions on if they weren't on already.