bootstrap.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. /**
  3. * @file
  4. * Autoloader for Drupal PHPUnit testing.
  5. *
  6. * @see phpunit.xml.dist
  7. */
  8. use Drupal\Component\Assertion\Handle;
  9. use PHPUnit\Framework\AssertionFailedError;
  10. use PHPUnit\Framework\Constraint\Count;
  11. use PHPUnit\Framework\Error\Error;
  12. use PHPUnit\Framework\Error\Warning;
  13. use PHPUnit\Framework\ExpectationFailedException;
  14. use PHPUnit\Framework\Exception;
  15. use PHPUnit\Framework\MockObject\Matcher\InvokedRecorder;
  16. use PHPUnit\Framework\SkippedTestError;
  17. use PHPUnit\Framework\TestCase;
  18. use PHPUnit\Util\Test;
  19. use PHPUnit\Util\Xml;
  20. /**
  21. * Finds all valid extension directories recursively within a given directory.
  22. *
  23. * @param string $scan_directory
  24. * The directory that should be recursively scanned.
  25. * @return array
  26. * An associative array of extension directories found within the scanned
  27. * directory, keyed by extension name.
  28. */
  29. function drupal_phpunit_find_extension_directories($scan_directory) {
  30. $extensions = [];
  31. $dirs = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($scan_directory, \RecursiveDirectoryIterator::FOLLOW_SYMLINKS));
  32. foreach ($dirs as $dir) {
  33. if (strpos($dir->getPathname(), '.info.yml') !== FALSE) {
  34. // Cut off ".info.yml" from the filename for use as the extension name. We
  35. // use getRealPath() so that we can scan extensions represented by
  36. // directory aliases.
  37. $extensions[substr($dir->getFilename(), 0, -9)] = $dir->getPathInfo()
  38. ->getRealPath();
  39. }
  40. }
  41. return $extensions;
  42. }
  43. /**
  44. * Returns directories under which contributed extensions may exist.
  45. *
  46. * @param string $root
  47. * (optional) Path to the root of the Drupal installation.
  48. *
  49. * @return array
  50. * An array of directories under which contributed extensions may exist.
  51. */
  52. function drupal_phpunit_contrib_extension_directory_roots($root = NULL) {
  53. if ($root === NULL) {
  54. $root = dirname(dirname(__DIR__));
  55. }
  56. $paths = [
  57. $root . '/core/modules',
  58. $root . '/core/profiles',
  59. $root . '/modules',
  60. $root . '/profiles',
  61. $root . '/themes',
  62. ];
  63. $sites_path = $root . '/sites';
  64. // Note this also checks sites/../modules and sites/../profiles.
  65. foreach (scandir($sites_path) as $site) {
  66. if ($site[0] === '.' || $site === 'simpletest') {
  67. continue;
  68. }
  69. $path = "$sites_path/$site";
  70. $paths[] = is_dir("$path/modules") ? realpath("$path/modules") : NULL;
  71. $paths[] = is_dir("$path/profiles") ? realpath("$path/profiles") : NULL;
  72. $paths[] = is_dir("$path/themes") ? realpath("$path/themes") : NULL;
  73. }
  74. return array_filter($paths, 'file_exists');
  75. }
  76. /**
  77. * Registers the namespace for each extension directory with the autoloader.
  78. *
  79. * @param array $dirs
  80. * An associative array of extension directories, keyed by extension name.
  81. *
  82. * @return array
  83. * An associative array of extension directories, keyed by their namespace.
  84. */
  85. function drupal_phpunit_get_extension_namespaces($dirs) {
  86. $suite_names = ['Unit', 'Kernel', 'Functional', 'Build', 'FunctionalJavascript'];
  87. $namespaces = [];
  88. foreach ($dirs as $extension => $dir) {
  89. if (is_dir($dir . '/src')) {
  90. // Register the PSR-4 directory for module-provided classes.
  91. $namespaces['Drupal\\' . $extension . '\\'][] = $dir . '/src';
  92. }
  93. $test_dir = $dir . '/tests/src';
  94. if (is_dir($test_dir)) {
  95. foreach ($suite_names as $suite_name) {
  96. $suite_dir = $test_dir . '/' . $suite_name;
  97. if (is_dir($suite_dir)) {
  98. // Register the PSR-4 directory for PHPUnit-based suites.
  99. $namespaces['Drupal\\Tests\\' . $extension . '\\' . $suite_name . '\\'][] = $suite_dir;
  100. }
  101. }
  102. // Extensions can have a \Drupal\extension\Traits namespace for
  103. // cross-suite trait code.
  104. $trait_dir = $test_dir . '/Traits';
  105. if (is_dir($trait_dir)) {
  106. $namespaces['Drupal\\Tests\\' . $extension . '\\Traits\\'][] = $trait_dir;
  107. }
  108. }
  109. }
  110. return $namespaces;
  111. }
  112. // We define the COMPOSER_INSTALL constant, so that PHPUnit knows where to
  113. // autoload from. This is needed for tests run in isolation mode, because
  114. // phpunit.xml.dist is located in a non-default directory relative to the
  115. // PHPUnit executable.
  116. if (!defined('PHPUNIT_COMPOSER_INSTALL')) {
  117. define('PHPUNIT_COMPOSER_INSTALL', __DIR__ . '/../../autoload.php');
  118. }
  119. /**
  120. * Populate class loader with additional namespaces for tests.
  121. *
  122. * We run this in a function to avoid setting the class loader to a global
  123. * that can change. This change can cause unpredictable false positives for
  124. * phpunit's global state change watcher. The class loader can be retrieved from
  125. * composer at any time by requiring autoload.php.
  126. */
  127. function drupal_phpunit_populate_class_loader() {
  128. /** @var \Composer\Autoload\ClassLoader $loader */
  129. $loader = require __DIR__ . '/../../autoload.php';
  130. // Start with classes in known locations.
  131. $loader->add('Drupal\\BuildTests', __DIR__);
  132. $loader->add('Drupal\\Tests', __DIR__);
  133. $loader->add('Drupal\\TestSite', __DIR__);
  134. $loader->add('Drupal\\KernelTests', __DIR__);
  135. $loader->add('Drupal\\FunctionalTests', __DIR__);
  136. $loader->add('Drupal\\FunctionalJavascriptTests', __DIR__);
  137. $loader->add('Drupal\\TestTools', __DIR__);
  138. if (!isset($GLOBALS['namespaces'])) {
  139. // Scan for arbitrary extension namespaces from core and contrib.
  140. $extension_roots = drupal_phpunit_contrib_extension_directory_roots();
  141. $dirs = array_map('drupal_phpunit_find_extension_directories', $extension_roots);
  142. $dirs = array_reduce($dirs, 'array_merge', []);
  143. $GLOBALS['namespaces'] = drupal_phpunit_get_extension_namespaces($dirs);
  144. }
  145. foreach ($GLOBALS['namespaces'] as $prefix => $paths) {
  146. $loader->addPsr4($prefix, $paths);
  147. }
  148. return $loader;
  149. }
  150. // Do class loader population.
  151. drupal_phpunit_populate_class_loader();
  152. // Set sane locale settings, to ensure consistent string, dates, times and
  153. // numbers handling.
  154. // @see \Drupal\Core\DrupalKernel::bootEnvironment()
  155. setlocale(LC_ALL, 'C');
  156. // Set appropriate configuration for multi-byte strings.
  157. mb_internal_encoding('utf-8');
  158. mb_language('uni');
  159. // Set the default timezone. While this doesn't cause any tests to fail, PHP
  160. // complains if 'date.timezone' is not set in php.ini. The Australia/Sydney
  161. // timezone is chosen so all tests are run using an edge case scenario (UTC+10
  162. // and DST). This choice is made to prevent timezone related regressions and
  163. // reduce the fragility of the testing system in general.
  164. date_default_timezone_set('Australia/Sydney');
  165. // Runtime assertions. PHPUnit follows the php.ini assert.active setting for
  166. // runtime assertions. By default this setting is on. Ensure exceptions are
  167. // thrown if an assert fails, but this call does not turn runtime assertions on
  168. // if they weren't on already.
  169. Handle::register();
  170. // PHPUnit 4 to PHPUnit 6 bridge. Tests written for PHPUnit 4 need to work on
  171. // PHPUnit 6 with a minimum of fuss.
  172. // @todo provided for BC; remove in Drupal 9.
  173. class_alias(AssertionFailedError::class, '\PHPUnit_Framework_AssertionFailedError');
  174. class_alias(Count::class, '\PHPUnit_Framework_Constraint_Count');
  175. class_alias(Error::class, '\PHPUnit_Framework_Error');
  176. class_alias(Warning::class, '\PHPUnit_Framework_Error_Warning');
  177. class_alias(ExpectationFailedException::class, '\PHPUnit_Framework_ExpectationFailedException');
  178. class_alias(Exception::class, '\PHPUnit_Framework_Exception');
  179. class_alias(InvokedRecorder::class, '\PHPUnit_Framework_MockObject_Matcher_InvokedRecorder');
  180. class_alias(SkippedTestError::class, '\PHPUnit_Framework_SkippedTestError');
  181. class_alias(TestCase::class, '\PHPUnit_Framework_TestCase');
  182. class_alias(Test::class, '\PHPUnit_Util_Test');
  183. class_alias(Xml::class, '\PHPUnit_Util_XML');