run-tests.sh

Same filename and directory in other branches
  1. 10 core/scripts/run-tests.sh
  2. 11.x core/scripts/run-tests.sh
  3. 9 core/scripts/run-tests.sh
  4. 8.9.x core/scripts/run-tests.sh
  5. 7.x scripts/run-tests.sh

Script for running tests on DrupalCI.

This script is intended for use only by drupal.org's testing. In general, tests should be run directly with phpunit.

@internal

File

core/scripts/run-tests.sh

View source
  1. <?php
  2. /**
  3. * @file
  4. * Script for running tests on DrupalCI.
  5. *
  6. * This script is intended for use only by drupal.org's testing. In general,
  7. * tests should be run directly with phpunit.
  8. *
  9. * @internal
  10. */
  11. use Composer\Autoload\ClassLoader;
  12. use Drupal\Component\FileSystem\FileSystem;
  13. use Drupal\Component\Utility\Environment;
  14. use Drupal\Component\Utility\Html;
  15. use Drupal\Component\Utility\Timer;
  16. use Drupal\Core\Composer\Composer;
  17. use Drupal\Core\Database\Database;
  18. use Drupal\Core\Test\EnvironmentCleaner;
  19. use Drupal\Core\Test\PhpUnitTestDiscovery;
  20. use Drupal\Core\Test\PhpUnitTestRunner;
  21. use Drupal\Core\Test\SimpletestTestRunResultsStorage;
  22. use Drupal\Core\Test\TestDatabase;
  23. use Drupal\Core\Test\TestRun;
  24. use Drupal\Core\Test\TestRunnerKernel;
  25. use Drupal\Core\Test\TestRunResultsStorageInterface;
  26. use Drupal\TestTools\TestRunner\Configuration as Config;
  27. use Drupal\TestTools\TestRunner\WorkAllocator;
  28. use PHPUnit\Framework\TestCase;
  29. use PHPUnit\Runner\Version;
  30. use Symfony\Component\Console\Helper\DescriptorHelper;
  31. use Symfony\Component\Console\Input\InputDefinition;
  32. use Symfony\Component\Console\Output\ConsoleOutput;
  33. use Symfony\Component\HttpFoundation\Request;
  34. use Symfony\Component\Process\PhpExecutableFinder;
  35. // cspell:ignore exitcode testbots wwwrun
  36. // Define some colors for display.
  37. // A nice calming green.
  38. const SIMPLETEST_SCRIPT_COLOR_PASS = 32;
  39. // An alerting Red.
  40. const SIMPLETEST_SCRIPT_COLOR_FAIL = 31;
  41. // An annoying brown.
  42. const SIMPLETEST_SCRIPT_COLOR_EXCEPTION = 33;
  43. // An appeasing yellow.
  44. const SIMPLETEST_SCRIPT_COLOR_YELLOW = 33;
  45. // A refreshing cyan.
  46. const SIMPLETEST_SCRIPT_COLOR_CYAN = 36;
  47. // A fainting gray.
  48. const SIMPLETEST_SCRIPT_COLOR_GRAY = 90;
  49. // A notable white.
  50. const SIMPLETEST_SCRIPT_COLOR_BRIGHT_WHITE = "1;97";
  51. // Restricting the chunk of queries prevents memory exhaustion.
  52. const SIMPLETEST_SCRIPT_SQLITE_VARIABLE_LIMIT = 350;
  53. const SIMPLETEST_SCRIPT_EXIT_SUCCESS = 0;
  54. const SIMPLETEST_SCRIPT_EXIT_FAILURE = 1;
  55. const SIMPLETEST_SCRIPT_EXIT_ERROR = 2;
  56. const SIMPLETEST_SCRIPT_EXIT_EXCEPTION = 3;
  57. // Setup class autoloading.
  58. $autoloader = require_once __DIR__ . '/../../autoload.php';
  59. $autoloader->addPsr4('Drupal\\TestTools\\', __DIR__ . '/../tests/Drupal/TestTools');
  60. // Setup console output.
  61. $console_output = new ConsoleOutput();
  62. // Get the configuration from the command line.
  63. $script_basename = basename($_SERVER['argv'][0]);
  64. try {
  65. Config::createFromCommandLine($_SERVER['argv']);
  66. }
  67. catch (\RuntimeException $e) {
  68. simpletest_script_print_error($e->getMessage() . ' ' . "Use the --help option for the list and usage of the options available.\n");
  69. simpletest_script_print(Config::commandLineDefinition()->getSynopsis(), SIMPLETEST_SCRIPT_COLOR_PASS);
  70. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  71. }
  72. // If --help requested, show it and exit.
  73. if (Config::get('help')) {
  74. simpletest_script_help(Config::commandLineDefinition(), $script_basename, $console_output);
  75. exit(SIMPLETEST_SCRIPT_EXIT_SUCCESS);
  76. }
  77. // Initialize script variables and bootstrap Drupal kernel.
  78. simpletest_script_init($autoloader);
  79. if (!class_exists(TestCase::class)) {
  80. echo "\nrun-tests.sh requires the PHPUnit testing framework. Use 'composer install' to ensure that it is present.\n\n";
  81. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  82. }
  83. // Defaults the PHPUnit configuration file path.
  84. if (empty(Config::get('phpunit-configuration'))) {
  85. Config::set('phpunit-configuration', \Drupal::root() . \DIRECTORY_SEPARATOR . 'core');
  86. }
  87. if (!Composer::upgradePHPUnitCheck(Version::id())) {
  88. simpletest_script_print_error("PHPUnit testing framework version 11 or greater is required when running on PHP 8.4 or greater. Run the command 'composer run-script drupal-phpunit-upgrade' in order to fix this.");
  89. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  90. }
  91. if (Config::get('list')) {
  92. // Display all available tests organized by one #[Group()] attribute.
  93. echo "\nAvailable test groups & classes\n";
  94. echo "-------------------------------\n\n";
  95. $testDiscovery = PhpUnitTestDiscovery::instance()->setConfigurationFilePath(Config::get('phpunit-configuration'));
  96. try {
  97. $groupedTestClassInfoList = $testDiscovery->getTestClasses(Config::get('module'));
  98. dump_discovery_warnings();
  99. }
  100. catch (Exception $e) {
  101. error_log((string) $e);
  102. echo (string) $e;
  103. exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
  104. }
  105. // A given class can appear in multiple groups. For historical reasons, we
  106. // need to present each test only once. The test is shown in the group that is
  107. // printed first.
  108. $printed_tests = [];
  109. foreach ($groupedTestClassInfoList as $group => $tests) {
  110. echo $group . "\n";
  111. $tests = array_diff(array_keys($tests), $printed_tests);
  112. foreach ($tests as $test) {
  113. echo " - $test\n";
  114. }
  115. $printed_tests = array_merge($printed_tests, $tests);
  116. }
  117. exit(SIMPLETEST_SCRIPT_EXIT_SUCCESS);
  118. }
  119. // List-files and list-files-json provide a way for external tools such as the
  120. // testbot to prioritize running changed tests.
  121. // @see https://www.drupal.org/node/2569585
  122. if (Config::get('list-files') || Config::get('list-files-json')) {
  123. // List all files which could be run as tests.
  124. $testDiscovery = PhpUnitTestDiscovery::instance()->setConfigurationFilePath(Config::get('phpunit-configuration'));
  125. // PhpUnitTestDiscovery::findAllClassFiles() gives us a classmap similar to a
  126. // Composer 'classmap' array.
  127. $test_classes = $testDiscovery->findAllClassFiles();
  128. // JSON output is the easiest.
  129. if (Config::get('list-files-json')) {
  130. echo json_encode($test_classes);
  131. exit(SIMPLETEST_SCRIPT_EXIT_SUCCESS);
  132. }
  133. // Output the list of files.
  134. else {
  135. foreach (array_values($test_classes) as $test_class) {
  136. echo $test_class . "\n";
  137. }
  138. }
  139. exit(SIMPLETEST_SCRIPT_EXIT_SUCCESS);
  140. }
  141. simpletest_script_setup_database();
  142. // Setup the test run results storage environment. Currently, this coincides
  143. // with the simpletest database schema.
  144. $test_run_results_storage = simpletest_script_setup_test_run_results_storage();
  145. if (Config::get('clean')) {
  146. // Clean up left-over tables and directories.
  147. $cleaner = new EnvironmentCleaner(
  148. DRUPAL_ROOT,
  149. Database::getConnection(),
  150. $test_run_results_storage,
  151. $console_output,
  152. \Drupal::service('file_system')
  153. );
  154. try {
  155. $cleaner->cleanEnvironment();
  156. }
  157. catch (Exception $e) {
  158. echo (string) $e;
  159. exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
  160. }
  161. echo "\nEnvironment cleaned.\n";
  162. // Get the status messages and print them.
  163. $messages = \Drupal::messenger()->messagesByType('status');
  164. foreach ($messages as $text) {
  165. echo " - " . $text . "\n";
  166. }
  167. exit(SIMPLETEST_SCRIPT_EXIT_SUCCESS);
  168. }
  169. echo "\n";
  170. echo "Drupal test run\n\n";
  171. echo "--------------------------------------------------------------\n";
  172. echo sprintf("Drupal Version.......: %s\n", \Drupal::VERSION);
  173. echo sprintf("PHP Version..........: %s\n", \PHP_VERSION);
  174. echo sprintf("PHP Binary...........: %s\n", (new PhpExecutableFinder())->find());
  175. echo sprintf("PHPUnit Version......: %s\n", Version::id());
  176. echo sprintf("PHPUnit configuration: %s\n", Config::get('phpunit-configuration'));
  177. if (Config::get('dburl')) {
  178. $sut_connection_info = Database::getConnectionInfo();
  179. $sut_tasks_class = $sut_connection_info['default']['namespace'] . "\\Install\\Tasks";
  180. $sut_installer = new $sut_tasks_class();
  181. $sut_connection = Database::getConnection();
  182. echo sprintf("Database.............: %s\n", (string) $sut_installer->name());
  183. echo sprintf("Database Version.....: %s\n", $sut_connection->version());
  184. }
  185. echo sprintf("Working directory....: %s\n", getcwd());
  186. echo "--------------------------------------------------------------\n";
  187. echo "\n";
  188. $groupedTestClassInfoList = simpletest_script_get_test_list();
  189. $workAllocator = new WorkAllocator(
  190. $groupedTestClassInfoList,
  191. (int) Config::get('ci-parallel-node-total'),
  192. (int) Config::get('ci-parallel-node-index'),
  193. );
  194. $test_list = array_keys($workAllocator->getAllocatedList());
  195. if (Config::get('debug-discovery')) {
  196. if ((int) Config::get('ci-parallel-node-total') > 1) {
  197. dump_bin_tests_sequence((int) Config::get('ci-parallel-node-index'), $workAllocator->getSortedList(), $workAllocator->getAllocatedList());
  198. }
  199. else {
  200. dump_tests_sequence($workAllocator->getAllocatedList());
  201. }
  202. }
  203. // Try to allocate unlimited time to run the tests.
  204. Environment::setTimeLimit(0);
  205. simpletest_script_reporter_init();
  206. $tests_to_run = [];
  207. for ($i = 0; $i < Config::get('repeat'); $i++) {
  208. $tests_to_run = array_merge($tests_to_run, $test_list);
  209. }
  210. // Execute tests.
  211. $status = simpletest_script_execute_batch($test_run_results_storage, $tests_to_run);
  212. // Stop the timer.
  213. simpletest_script_reporter_timer_stop();
  214. // Ensure all test locks are released once finished. If tests are run with a
  215. // concurrency of 1 the each test will clean up its own lock. Test locks are
  216. // not released if using a higher concurrency to ensure each test has unique
  217. // fixtures.
  218. TestDatabase::releaseAllTestLocks();
  219. // Display results before database is cleared.
  220. simpletest_script_reporter_display_results($test_run_results_storage);
  221. if (Config::get('xml')) {
  222. simpletest_script_reporter_write_xml_results($test_run_results_storage);
  223. }
  224. // Clean up all test results.
  225. if (!Config::get('keep-results')) {
  226. try {
  227. $cleaner = new EnvironmentCleaner(
  228. DRUPAL_ROOT,
  229. Database::getConnection(),
  230. $test_run_results_storage,
  231. $console_output,
  232. \Drupal::service('file_system')
  233. );
  234. $cleaner->cleanResults();
  235. }
  236. catch (Exception $e) {
  237. echo (string) $e;
  238. exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
  239. }
  240. }
  241. // Test complete, exit.
  242. exit($status);
  243. /**
  244. * Print help text.
  245. */
  246. function simpletest_script_help(InputDefinition $input_definition, string $script_basename, ConsoleOutput $console_output): void {
  247. echo <<
  248. Run Drupal tests from the shell.
  249. Usage: {$script_basename} [OPTIONS]
  250. Example: {$script_basename} Profile
  251. EOF;
  252. $helper = new DescriptorHelper();
  253. $helper->describe($console_output, $input_definition);
  254. echo <<
  255. To run this script you will normally invoke it from the root directory of your
  256. Drupal installation as the webserver user (differs per configuration), or root:
  257. sudo -u [wwwrun|www-data|etc] php ./core/scripts/{$script_basename} --url http://example.com/ --all
  258. sudo -u [wwwrun|www-data|etc] php ./core/scripts/{$script_basename} --url http://example.com/ --class Drupal\\\\Tests\\\\block\\\\Functional\\\\BlockTest
  259. Without a preinstalled Drupal site, specify a SQLite database pathname to create
  260. (for the test runner) and the default database connection info (for Drupal) to
  261. use in tests:
  262. sudo -u [wwwrun|www-data|etc] php ./core/scripts/{$script_basename}
  263. --sqlite /tmpfs/drupal/test.sqlite
  264. --dburl mysql://username:password@localhost/database
  265. --url http://example.com/ --all
  266. EOF;
  267. }
  268. /**
  269. * Initialize script variables and perform general setup requirements.
  270. *
  271. * @param \Drupal\Core\Composer\Composer $autoloader
  272. * The Composer provided PHP class loader.
  273. */
  274. function simpletest_script_init(ClassLoader $autoloader): void {
  275. // Get URL from arguments.
  276. $parsed_url = parse_url(Config::get('url'));
  277. $host = $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '');
  278. $path = isset($parsed_url['path']) ? rtrim(rtrim($parsed_url['path']), '/') : '';
  279. $port = $parsed_url['port'] ?? '80';
  280. // If the passed URL schema is 'https' then setup the $_SERVER variables
  281. // properly so that testing will run under HTTPS.
  282. if ($parsed_url['scheme'] == 'https') {
  283. $_SERVER['HTTPS'] = 'on';
  284. }
  285. $base_url = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://';
  286. $base_url .= $host;
  287. if ($path !== '') {
  288. $base_url .= $path;
  289. }
  290. putenv('SIMPLETEST_BASE_URL=' . $base_url);
  291. $_SERVER['HTTP_HOST'] = $host;
  292. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  293. $_SERVER['SERVER_ADDR'] = '127.0.0.1';
  294. $_SERVER['SERVER_PORT'] = $port;
  295. $_SERVER['SERVER_SOFTWARE'] = NULL;
  296. $_SERVER['SERVER_NAME'] = 'localhost';
  297. $_SERVER['REQUEST_URI'] = $path . '/';
  298. $_SERVER['REQUEST_METHOD'] = 'GET';
  299. $_SERVER['SCRIPT_NAME'] = $path . '/index.php';
  300. $_SERVER['SCRIPT_FILENAME'] = $path . '/index.php';
  301. $_SERVER['PHP_SELF'] = $path . '/index.php';
  302. $_SERVER['HTTP_USER_AGENT'] = 'Drupal command line';
  303. if (Config::get('concurrency') > 1) {
  304. $directory = FileSystem::getOsTemporaryDirectory();
  305. $test_symlink = @symlink(__FILE__, $directory . '/test_symlink');
  306. if (!$test_symlink) {
  307. throw new \RuntimeException('In order to use a concurrency higher than 1 the test system needs to be able to create symlinks in ' . $directory);
  308. }
  309. unlink($directory . '/test_symlink');
  310. putenv('RUN_TESTS_CONCURRENCY=' . Config::get('concurrency'));
  311. }
  312. if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
  313. // Ensure that any and all environment variables are changed to https://.
  314. foreach ($_SERVER as $key => $value) {
  315. // Some values are NULL. Non-NULL values which are falsy will not contain
  316. // text to replace.
  317. if ($value) {
  318. $_SERVER[$key] = str_replace('http://', 'https://', $value);
  319. }
  320. }
  321. }
  322. chdir(realpath(__DIR__ . '/../..'));
  323. // Prepare the kernel.
  324. try {
  325. $request = Request::createFromGlobals();
  326. $kernel = TestRunnerKernel::createFromRequest($request, $autoloader);
  327. $kernel->boot();
  328. $kernel->preHandle($request);
  329. }
  330. catch (Exception $e) {
  331. echo (string) $e;
  332. exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
  333. }
  334. }
  335. /**
  336. * Sets up database connection info for running tests.
  337. *
  338. * If this script is executed from within a real Drupal installation, then this
  339. * function essentially performs nothing (unless the --sqlite or --dburl
  340. * parameters were passed).
  341. *
  342. * Otherwise, there are three database connections of concern:
  343. * - --sqlite: The test runner connection, providing access to database tables
  344. * for recording test IDs and assertion results.
  345. * - --dburl: A database connection that is used as base connection info for all
  346. * tests; i.e., every test will spawn from this connection. In case this
  347. * connection uses e.g. SQLite, then all tests will run against SQLite. This
  348. * is exposed as $databases['default']['default'] to Drupal.
  349. * - The actual database connection used within a test. This is the same as
  350. * --dburl, but uses an additional database table prefix. This is
  351. * $databases['default']['default'] within a test environment. The original
  352. * connection is retained in
  353. * $databases['simpletest_original_default']['default'] and restored after
  354. * each test.
  355. */
  356. function simpletest_script_setup_database(): void {
  357. // If there is an existing Drupal installation that contains a database
  358. // connection info in settings.php, then $databases['default']['default'] will
  359. // hold the default database connection already. This connection is assumed to
  360. // be valid, and this connection will be used in tests, so that they run
  361. // against e.g. MySQL instead of SQLite.
  362. // However, in case no Drupal installation exists, this default database
  363. // connection can be set and/or overridden with the --dburl parameter.
  364. if (Config::get('dburl')) {
  365. // Remove a possibly existing default connection (from settings.php).
  366. Database::removeConnection('default');
  367. try {
  368. $databases['default']['default'] = Database::convertDbUrlToConnectionInfo(Config::get('dburl'), TRUE);
  369. }
  370. catch (\InvalidArgumentException $e) {
  371. simpletest_script_print_error('Invalid --dburl. Reason: ' . $e->getMessage());
  372. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  373. }
  374. }
  375. // Otherwise, use the default database connection from settings.php.
  376. else {
  377. $databases['default'] = Database::getConnectionInfo('default');
  378. }
  379. if (isset($databases['default']['default'])) {
  380. Database::addConnectionInfo('default', 'default', $databases['default']['default']);
  381. }
  382. }
  383. /**
  384. * Sets up the test runs results storage.
  385. */
  386. function simpletest_script_setup_test_run_results_storage() {
  387. $databases['default'] = Database::getConnectionInfo('default');
  388. // If no --sqlite parameter has been passed, then the test runner database
  389. // connection is the default database connection.
  390. $sqlite = Config::get('sqlite');
  391. if (!$sqlite) {
  392. $sqlite = FALSE;
  393. $databases['test-runner']['default'] = $databases['default']['default'];
  394. }
  395. // Otherwise, set up a SQLite connection for the test runner.
  396. else {
  397. if ($sqlite === ':memory:') {
  398. $sqlite = ':memory:';
  399. }
  400. elseif (is_string($sqlite) && !str_starts_with($sqlite, '/')) {
  401. $sqlite = DRUPAL_ROOT . '/' . $sqlite;
  402. }
  403. $databases['test-runner']['default'] = [
  404. 'driver' => 'sqlite',
  405. 'database' => $sqlite,
  406. 'prefix' => '',
  407. ];
  408. // Create the test runner SQLite database, unless it exists already.
  409. if ($sqlite !== ':memory:' && !file_exists($sqlite)) {
  410. if (!is_dir(dirname($sqlite))) {
  411. mkdir(dirname($sqlite));
  412. }
  413. touch($sqlite);
  414. }
  415. }
  416. // Add the test runner database connection.
  417. Database::addConnectionInfo('test-runner', 'default', $databases['test-runner']['default']);
  418. // Create the test result schema.
  419. try {
  420. $test_run_results_storage = new SimpletestTestRunResultsStorage(Database::getConnection('default', 'test-runner'));
  421. }
  422. catch (\PDOException $e) {
  423. simpletest_script_print_error($databases['test-runner']['default']['driver'] . ': ' . $e->getMessage());
  424. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  425. }
  426. if ($sqlite) {
  427. try {
  428. $test_run_results_storage->buildTestingResultsEnvironment(Config::get('keep-results-table'));
  429. }
  430. catch (Exception $e) {
  431. echo (string) $e;
  432. exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
  433. }
  434. }
  435. // Verify that the test result database schema exists by checking one table.
  436. try {
  437. if (!$test_run_results_storage->validateTestingResultsEnvironment()) {
  438. simpletest_script_print_error('Missing test result database schema. Use the --sqlite parameter.');
  439. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  440. }
  441. }
  442. catch (Exception $e) {
  443. echo (string) $e;
  444. exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
  445. }
  446. return $test_run_results_storage;
  447. }
  448. /**
  449. * Execute a batch of tests.
  450. */
  451. function simpletest_script_execute_batch(TestRunResultsStorageInterface $test_run_results_storage, $test_classes) {
  452. global $test_ids, $total_time;
  453. $total_status = SIMPLETEST_SCRIPT_EXIT_SUCCESS;
  454. $total_time = 0;
  455. $process_runner = PhpUnitTestRunner::create(\Drupal::getContainer())
  456. ->setConfigurationFilePath(Config::get('phpunit-configuration'));
  457. // Multi-process execution.
  458. $children = [];
  459. while (!empty($test_classes) || !empty($children)) {
  460. while (count($children) < Config::get('concurrency')) {
  461. if (empty($test_classes)) {
  462. break;
  463. }
  464. try {
  465. $test_run = TestRun::createNew($test_run_results_storage);
  466. }
  467. catch (Exception $e) {
  468. echo (string) $e;
  469. exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
  470. }
  471. $test_ids[] = $test_run->id();
  472. $test_class = array_shift($test_classes);
  473. // Fork a child process.
  474. try {
  475. $process = $process_runner->startPhpUnitOnSingleTestClass(
  476. $test_run,
  477. $test_class,
  478. Config::get('color'),
  479. Config::get('suppress-deprecations'),
  480. );
  481. }
  482. catch (\Throwable $e) {
  483. // PHPUnit catches exceptions already, so this is only reached when an
  484. // exception is thrown in the wrapped test runner environment.
  485. echo (string) $e;
  486. exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
  487. }
  488. // Register our new child.
  489. $children[] = [
  490. 'process' => $process,
  491. 'test_run' => $test_run,
  492. 'class' => $test_class,
  493. ];
  494. }
  495. // Wait for children every 2ms.
  496. usleep(2000);
  497. // Check if some children finished.
  498. foreach ($children as $cid => $child) {
  499. if ($child['process']->isTerminated()) {
  500. // The child exited.
  501. $child['test_run']->end(microtime(TRUE));
  502. $total_time += $child['test_run']->duration();
  503. $process_outcome = $process_runner->processPhpUnitOnSingleTestClassOutcome(
  504. $child['process'],
  505. $child['test_run'],
  506. $child['class'],
  507. );
  508. simpletest_script_reporter_display_summary(
  509. $child['class'],
  510. $process_outcome['summaries'][$child['class']],
  511. $child['test_run']->duration()
  512. );
  513. if ($process_outcome['error_output']) {
  514. echo 'ERROR: ' . implode("\n", $process_outcome['error_output']);
  515. }
  516. if (in_array($process_outcome['status'], [SIMPLETEST_SCRIPT_EXIT_FAILURE, SIMPLETEST_SCRIPT_EXIT_ERROR])) {
  517. $total_status = max($process_outcome['status'], $total_status);
  518. }
  519. elseif ($process_outcome['status']) {
  520. $message = 'FATAL ' . $child['class'] . ': test runner returned an unexpected error code (' . $process_outcome['status'] . ').';
  521. echo $message . "\n";
  522. $total_status = max(SIMPLETEST_SCRIPT_EXIT_EXCEPTION, $total_status);
  523. if (Config::get('die-on-fail')) {
  524. $test_db = new TestDatabase($child['test_run']->getDatabasePrefix());
  525. $test_directory = $test_db->getTestSitePath();
  526. echo 'Test database and files kept and test exited immediately on fail so should be reproducible if you change settings.php to use the database prefix ' . $child['test_run']->getDatabasePrefix() . ' and config directories in ' . $test_directory . "\n";
  527. Config::set('keep-results', TRUE);
  528. // Exit repeat loop immediately.
  529. Config::set('repeat', -1);
  530. }
  531. }
  532. // Remove this child.
  533. unset($children[$cid]);
  534. }
  535. }
  536. }
  537. return $total_status;
  538. }
  539. /**
  540. * Get list of tests based on arguments.
  541. *
  542. * If --all specified then return all available tests, otherwise reads list of
  543. * tests.
  544. *
  545. * @return array
  546. * List of tests.
  547. */
  548. function simpletest_script_get_test_list() {
  549. $testDiscovery = PhpUnitTestDiscovery::instance()->setConfigurationFilePath(Config::get('phpunit-configuration'));
  550. try {
  551. if (Config::get('all') || Config::get('module') || Config::get('directory')) {
  552. $groupedTestClassInfoList = $testDiscovery->getTestClasses(Config::get('module'), Config::get('types'), Config::get('directory'));
  553. }
  554. elseif (Config::get('class')) {
  555. // When --class is specified, we have to find the file of each of the
  556. // classes indicated as argument and run test discovery for it, then
  557. // merge the results.
  558. $groupedTestClassInfoList = [];
  559. foreach (Config::getTests() as $test_class) {
  560. [$class_name] = explode('::', $test_class, 2);
  561. if (class_exists($class_name)) {
  562. $fileName = (new \ReflectionClass($class_name))->getFileName();
  563. $groupedClassInfo = $testDiscovery->getTestClasses(NULL, [], $fileName);
  564. foreach (array_keys($groupedClassInfo) as $classGroupKey) {
  565. if (array_key_exists($classGroupKey, $groupedTestClassInfoList)) {
  566. $groupedTestClassInfoList[$classGroupKey] = array_merge($groupedTestClassInfoList[$classGroupKey], $groupedClassInfo[$classGroupKey]);
  567. }
  568. else {
  569. $groupedTestClassInfoList[$classGroupKey] = $groupedClassInfo[$classGroupKey];
  570. }
  571. }
  572. }
  573. else {
  574. // The class does not exist: we discover all the test classes and
  575. // suggest a possible alternative.
  576. $groupedTestClassInfoList = $testDiscovery->getTestClasses(NULL, Config::get('types'));
  577. dump_discovery_warnings();
  578. $all_classes = [];
  579. foreach ($groupedTestClassInfoList as $group) {
  580. $all_classes = array_merge($all_classes, array_keys($group));
  581. }
  582. simpletest_script_print_error('Test class not found: ' . $class_name);
  583. simpletest_script_print_alternatives($class_name, $all_classes, 6);
  584. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  585. }
  586. }
  587. }
  588. elseif (Config::get('file')) {
  589. // When --file is specified, we have to run test discovery for each of
  590. // the files indicated, then merge the results.
  591. $groupedTestClassInfoList = [];
  592. foreach (Config::getTests() as $file) {
  593. if (!file_exists($file) || is_dir($file)) {
  594. simpletest_script_print_error('File not found: ' . $file);
  595. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  596. }
  597. $groupedClassInfo = $testDiscovery->getTestClasses(NULL, [], $file);
  598. foreach (array_keys($groupedClassInfo) as $classGroupKey) {
  599. if (array_key_exists($classGroupKey, $groupedTestClassInfoList)) {
  600. $groupedTestClassInfoList[$classGroupKey] = array_merge($groupedTestClassInfoList[$classGroupKey], $groupedClassInfo[$classGroupKey]);
  601. }
  602. else {
  603. $groupedTestClassInfoList[$classGroupKey] = $groupedClassInfo[$classGroupKey];
  604. }
  605. }
  606. }
  607. }
  608. else {
  609. // When no restriction options are specified, we consider the argument as
  610. // a list of groups of tests to be executed.
  611. $groupedTestClassInfoList = [];
  612. try {
  613. $groupedTestClassInfoFullSuiteList = $testDiscovery->getTestClasses(NULL, Config::get('types'));
  614. }
  615. catch (\Exception $e) {
  616. echo (string) $e;
  617. exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
  618. }
  619. // Store all the groups so we can suggest alternatives if we need to.
  620. $all_groups = array_keys($groupedTestClassInfoFullSuiteList);
  621. // Verify that the groups exist.
  622. if (!empty($unknown_groups = array_diff(Config::getTests(), $all_groups))) {
  623. $first_group = reset($unknown_groups);
  624. simpletest_script_print_error('Test group not found: ' . $first_group);
  625. simpletest_script_print_alternatives($first_group, $all_groups);
  626. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  627. }
  628. foreach (Config::getTests() as $group_name) {
  629. $groupedTestClassInfoList[$group_name] = $groupedTestClassInfoFullSuiteList[$group_name];
  630. }
  631. // The '#slow' group is a special case, because it may not be selected in
  632. // the argument, but it must be present if any test class indicates it in
  633. // metadata, for the work allocator to prioritize its execution.
  634. foreach ($groupedTestClassInfoList as $groupName => $testClassInfoList) {
  635. foreach ($testClassInfoList as $testClass => $testClassInfo) {
  636. if (in_array('#slow', $testClassInfo['groups'])) {
  637. $groupedTestClassInfoList['#slow'][$testClass] = $testClassInfo;
  638. }
  639. }
  640. }
  641. }
  642. }
  643. catch (\Exception $e) {
  644. echo (string) $e;
  645. exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
  646. }
  647. dump_discovery_warnings();
  648. if (empty($groupedTestClassInfoList)) {
  649. simpletest_script_print_error('No valid tests were specified.');
  650. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  651. }
  652. return $groupedTestClassInfoList;
  653. }
  654. /**
  655. * Dumps the list of tests in order of execution after sorting.
  656. *
  657. * @param array $tests
  658. * The array of test class info.
  659. */
  660. function dump_tests_sequence(array $tests): void {
  661. if (!Config::get('debug-discovery')) {
  662. return;
  663. }
  664. echo "Test execution sequence\n";
  665. echo "-----------------------\n\n";
  666. echo " Seq Slow? Group Cnt Class\n";
  667. echo "-----------------------------------------\n";
  668. foreach ($tests as $testInfo) {
  669. echo sprintf(
  670. "%4d %5s %15s %4d %s\n",
  671. $testInfo['worker_sequence'],
  672. in_array('#slow', $testInfo['groups']) ? '#slow' : '',
  673. trim_with_ellipsis($testInfo['group'], 15, \STR_PAD_RIGHT),
  674. $testInfo['tests_count'],
  675. trim_with_ellipsis($testInfo['name'], 60, \STR_PAD_LEFT),
  676. );
  677. }
  678. echo "-----------------------------------------\n\n";
  679. }
  680. /**
  681. * Dumps the list of tests in order of execution for a bin.
  682. *
  683. * @param int $bin
  684. * The bin.
  685. * @param array $allTests
  686. * The list of all test classes discovered.
  687. * @param array $tests
  688. * The list of test class to run for this bin.
  689. */
  690. function dump_bin_tests_sequence(int $bin, array $allTests, array $tests): void {
  691. echo "Test execution sequence. ";
  692. echo "Tests marked *** will be executed in this PARALLEL BIN #{$bin}.\n";
  693. echo "-------------------------------------------------------------------------------------\n\n";
  694. echo " Sort Bin \n";
  695. echo "Bin Seq Seq Slow? Group Cnt Class\n";
  696. echo "-------------------------------------------------------------------------------------\n";
  697. foreach ($allTests as $testInfo) {
  698. $inBin = isset($tests[$testInfo['name']]);
  699. $message = sprintf(
  700. "%s %4d %s %5s %15s %4d %s\n",
  701. $inBin ? "***" : " ",
  702. $testInfo['sorted_sequence'],
  703. $inBin ? sprintf('%4d', $tests[$testInfo['name']]['worker_sequence']) : " ",
  704. in_array('#slow', $testInfo['groups']) ? '#slow' : '',
  705. trim_with_ellipsis($testInfo['group'], 15, \STR_PAD_RIGHT),
  706. $testInfo['tests_count'],
  707. trim_with_ellipsis($testInfo['name'], 60, \STR_PAD_LEFT),
  708. );
  709. simpletest_script_print($message, $inBin ? SIMPLETEST_SCRIPT_COLOR_BRIGHT_WHITE : SIMPLETEST_SCRIPT_COLOR_GRAY);
  710. }
  711. echo "-------------------------------------------------------------------------------------\n\n";
  712. }
  713. /**
  714. * Initialize the reporter.
  715. */
  716. function simpletest_script_reporter_init(): void {
  717. global $test_list, $results_map;
  718. $results_map = [
  719. 'pass' => 'Pass',
  720. 'fail' => 'Fail',
  721. 'error' => 'Error',
  722. 'skipped' => 'Skipped',
  723. 'cli_fail' => 'Failure',
  724. 'exception' => 'Exception',
  725. 'debug' => 'Log',
  726. ];
  727. // Tell the user about what tests are to be run.
  728. if (Config::get('all')) {
  729. echo "All tests will run.\n\n";
  730. }
  731. else {
  732. echo "Tests to be run:\n";
  733. foreach ($test_list as $class_name) {
  734. echo " - $class_name\n";
  735. }
  736. echo "\n";
  737. }
  738. echo "Test run started:\n";
  739. echo " " . date('l, F j, Y - H:i', $_SERVER['REQUEST_TIME']) . "\n";
  740. Timer::start('run-tests');
  741. echo "\n";
  742. echo "Test summary\n";
  743. echo "------------\n";
  744. echo "\n";
  745. }
  746. /**
  747. * Displays the assertion result summary for a single test class.
  748. *
  749. * @param string $class
  750. * The test class name that was run.
  751. * @param array $results
  752. * The assertion results using #pass, #fail, #exception, #debug array keys.
  753. * @param float|null $duration
  754. * The time taken for the test to complete.
  755. */
  756. function simpletest_script_reporter_display_summary($class, $results, $duration = NULL): void {
  757. // Output all test results vertically aligned.
  758. $summary = [str_pad($results['#pass'], 4, " ", STR_PAD_LEFT) . ' passed'];
  759. if ($results['#fail']) {
  760. $summary[] = $results['#fail'] . ' failed';
  761. }
  762. if ($results['#error']) {
  763. $summary[] = $results['#error'] . ' errored';
  764. }
  765. if ($results['#skipped']) {
  766. $summary[] = $results['#skipped'] . ' skipped';
  767. }
  768. if ($results['#exception']) {
  769. $summary[] = $results['#exception'] . ' exception(s)';
  770. }
  771. if ($results['#debug']) {
  772. $summary[] = $results['#debug'] . ' log(s)';
  773. }
  774. if ($results['#cli_fail']) {
  775. $summary[] = 'exit code ' . $results['#exit_code'];
  776. }
  777. // The key $results['#time'] holds the sum of the tests execution times,
  778. // without taking into account the process spawning time and the setup
  779. // times of the tests themselves. So for reporting to be consistent with
  780. // PHPUnit CLI reported execution time, we report here the overall time of
  781. // execution of the spawned process.
  782. $time = sprintf('%8.3fs', $duration);
  783. $output = vsprintf('%s %s %s', [$time, trim_with_ellipsis($class, 70, STR_PAD_LEFT), implode(', ', $summary)]);
  784. $status = ($results['#fail'] || $results['#cli_fail'] || $results['#exception'] || $results['#error'] ? 'fail' : 'pass');
  785. simpletest_script_print($output . "\n", simpletest_script_color_code($status));
  786. }
  787. /**
  788. * Display jUnit XML test results.
  789. */
  790. function simpletest_script_reporter_write_xml_results(TestRunResultsStorageInterface $test_run_results_storage): void {
  791. global $test_ids, $results_map;
  792. try {
  793. $results = simpletest_script_load_messages_by_test_id($test_run_results_storage, $test_ids);
  794. }
  795. catch (Exception $e) {
  796. echo (string) $e;
  797. exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
  798. }
  799. $test_class = '';
  800. $xml_files = [];
  801. foreach ($results as $result) {
  802. if (isset($results_map[$result->status])) {
  803. if ($result->test_class != $test_class) {
  804. // We've moved onto a new class, so write the last classes results to a
  805. // file:
  806. if (isset($xml_files[$test_class])) {
  807. file_put_contents(Config::get('xml') . '/' . str_replace('\\', '_', $test_class) . '.xml', $xml_files[$test_class]['doc']->saveXML());
  808. unset($xml_files[$test_class]);
  809. }
  810. $test_class = $result->test_class;
  811. if (!isset($xml_files[$test_class])) {
  812. $doc = new DOMDocument('1.0', 'utf-8');
  813. $root = $doc->createElement('testsuite');
  814. $root = $doc->appendChild($root);
  815. $xml_files[$test_class] = ['doc' => $doc, 'suite' => $root];
  816. }
  817. }
  818. // For convenience:
  819. $dom_document = &$xml_files[$test_class]['doc'];
  820. // Create the XML element for this test case:
  821. $case = $dom_document->createElement('testcase');
  822. $case->setAttribute('classname', $test_class);
  823. if (str_contains($result->function, '->')) {
  824. [, $name] = explode('->', $result->function, 2);
  825. }
  826. else {
  827. $name = $result->function;
  828. }
  829. $case->setAttribute('name', $name);
  830. // Passes get no further attention, but failures and exceptions get to add
  831. // more detail:
  832. if ($result->status == 'fail') {
  833. $fail = $dom_document->createElement('failure');
  834. $fail->setAttribute('type', 'failure');
  835. $fail->setAttribute('message', $result->message_group);
  836. $text = $dom_document->createTextNode($result->message);
  837. $fail->appendChild($text);
  838. $case->appendChild($fail);
  839. }
  840. elseif ($result->status == 'exception') {
  841. // In the case of an exception the $result->function may not be a class
  842. // method so we record the full function name:
  843. $case->setAttribute('name', $result->function);
  844. $fail = $dom_document->createElement('error');
  845. $fail->setAttribute('type', 'exception');
  846. $fail->setAttribute('message', $result->message_group);
  847. $full_message = $result->message . "\n\nline: " . $result->line . "\nfile: " . $result->file;
  848. $text = $dom_document->createTextNode($full_message);
  849. $fail->appendChild($text);
  850. $case->appendChild($fail);
  851. }
  852. // Append the test case XML to the test suite:
  853. $xml_files[$test_class]['suite']->appendChild($case);
  854. }
  855. }
  856. // The last test case hasn't been saved to a file yet, so do that now:
  857. if (isset($xml_files[$test_class])) {
  858. file_put_contents(Config::get('xml') . '/' . str_replace('\\', '_', $test_class) . '.xml', $xml_files[$test_class]['doc']->saveXML());
  859. unset($xml_files[$test_class]);
  860. }
  861. }
  862. /**
  863. * Stop the test timer.
  864. */
  865. function simpletest_script_reporter_timer_stop(): void {
  866. global $total_time;
  867. echo "\n";
  868. $end = Timer::stop('run-tests');
  869. $wall_seconds = $end['time'] / 1000;
  870. $formatter = \Drupal::service('date.formatter');
  871. echo "Wall time: " . $formatter->formatInterval((int) $wall_seconds) . "\n";
  872. echo "Total time: " . $formatter->formatInterval((int) $total_time) . "\n";
  873. if ($wall_seconds > 0) {
  874. echo sprintf("Speedup: %.2fx (concurrency %d)\n", $total_time / $wall_seconds, Config::get('concurrency'));
  875. }
  876. echo "\n";
  877. }
  878. /**
  879. * Display test results.
  880. */
  881. function simpletest_script_reporter_display_results(TestRunResultsStorageInterface $test_run_results_storage): void {
  882. global $test_ids, $results_map;
  883. if (Config::get('verbose')) {
  884. // Report results.
  885. echo "Detailed test results\n";
  886. echo "---------------------\n";
  887. try {
  888. $results = simpletest_script_load_messages_by_test_id($test_run_results_storage, $test_ids);
  889. }
  890. catch (Exception $e) {
  891. echo (string) $e;
  892. exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
  893. }
  894. $test_class = '';
  895. foreach ($results as $result) {
  896. if (isset($results_map[$result->status])) {
  897. if ($result->test_class != $test_class) {
  898. // Display test class every time results are for new test class.
  899. echo "\n\n---- $result->test_class ----\n\n\n";
  900. $test_class = $result->test_class;
  901. // Print table header.
  902. echo "Status Duration Info \n";
  903. echo "--------------------------------------------------------------------------------------------------------\n";
  904. }
  905. simpletest_script_format_result($result);
  906. }
  907. }
  908. }
  909. }
  910. /**
  911. * Format the result so that it fits within 80 characters.
  912. *
  913. * @param object $result
  914. * The result object to format.
  915. */
  916. function simpletest_script_format_result($result): void {
  917. global $results_map;
  918. if ($result->time == 0) {
  919. $duration = " ";
  920. }
  921. elseif ($result->time < 0.001) {
  922. $duration = " <1 ms";
  923. }
  924. else {
  925. $duration = sprintf("%9.3fs", $result->time);
  926. }
  927. $summary = sprintf("%-9.9s %s %s\n", $results_map[$result->status], $duration, trim_with_ellipsis($result->function, 80, STR_PAD_LEFT));
  928. simpletest_script_print($summary, simpletest_script_color_code($result->status));
  929. if ($result->message === '' || in_array($result->status, ['pass', 'fail', 'error'])) {
  930. return;
  931. }
  932. $message = trim(strip_tags($result->message));
  933. if (Config::get('non-html')) {
  934. $message = Html::decodeEntities($message);
  935. }
  936. $lines = explode("\n", $message);
  937. foreach ($lines as $line) {
  938. echo " $line\n";
  939. }
  940. }
  941. /**
  942. * Print error messages so the user will notice them.
  943. *
  944. * Print error message prefixed with " ERROR: " and displayed in fail color if
  945. * color output is enabled.
  946. *
  947. * @param string $message
  948. * The message to print.
  949. */
  950. function simpletest_script_print_error($message): void {
  951. simpletest_script_print(" ERROR: $message\n", SIMPLETEST_SCRIPT_COLOR_FAIL);
  952. }
  953. /**
  954. * Print a message to the console, using a color.
  955. *
  956. * @param string $message
  957. * The message to print.
  958. * @param int|string $color_code
  959. * The color code to use for coloring.
  960. */
  961. function simpletest_script_print($message, $color_code): void {
  962. try {
  963. if (Config::get('color')) {
  964. echo "\033[" . $color_code . "m" . $message . "\033[0m";
  965. }
  966. else {
  967. echo $message;
  968. }
  969. }
  970. catch (\RuntimeException) {
  971. echo $message;
  972. }
  973. }
  974. /**
  975. * Get the color code associated with the specified status.
  976. *
  977. * @param string $status
  978. * The status string to get code for. Special cases are: 'pass', 'fail', or
  979. * 'exception'.
  980. *
  981. * @return int
  982. * Color code. Returns 0 for default case.
  983. */
  984. function simpletest_script_color_code($status) {
  985. return match ($status) {
  986. 'pass' => SIMPLETEST_SCRIPT_COLOR_PASS,
  987. 'fail', 'cli_fail', 'error', 'exception' => SIMPLETEST_SCRIPT_COLOR_FAIL,
  988. 'skipped' => SIMPLETEST_SCRIPT_COLOR_YELLOW,
  989. 'debug' => SIMPLETEST_SCRIPT_COLOR_CYAN,
  990. default => 0,
  991. };
  992. }
  993. /**
  994. * Prints alternative test names.
  995. *
  996. * Searches the provided array of string values for close matches based on the
  997. * Levenshtein algorithm.
  998. *
  999. * @param string $string
  1000. * A string to test.
  1001. * @param array $array
  1002. * A list of strings to search.
  1003. * @param int $degree
  1004. * The matching strictness. Higher values return fewer matches. A value of
  1005. * 4 means that the function will return strings from $array if the candidate
  1006. * string in $array would be identical to $string by changing 1/4 or fewer of
  1007. * its characters.
  1008. *
  1009. * @see http://php.net/manual/function.levenshtein.php
  1010. */
  1011. function simpletest_script_print_alternatives($string, $array, $degree = 4): void {
  1012. $alternatives = [];
  1013. foreach ($array as $item) {
  1014. $lev = levenshtein($string, $item);
  1015. if ($lev <= strlen($item) / $degree || str_contains($string, $item)) {
  1016. $alternatives[] = $item;
  1017. }
  1018. }
  1019. if (!empty($alternatives)) {
  1020. simpletest_script_print(" Did you mean?\n", SIMPLETEST_SCRIPT_COLOR_FAIL);
  1021. foreach ($alternatives as $alternative) {
  1022. simpletest_script_print(" - $alternative\n", SIMPLETEST_SCRIPT_COLOR_FAIL);
  1023. }
  1024. }
  1025. }
  1026. /**
  1027. * Loads test result messages from the database.
  1028. *
  1029. * Messages are ordered by test class and message id.
  1030. *
  1031. * @param array $test_ids
  1032. * Array of test IDs of the messages to be loaded.
  1033. *
  1034. * @return array
  1035. * Array of test result messages from the database.
  1036. */
  1037. function simpletest_script_load_messages_by_test_id(TestRunResultsStorageInterface $test_run_results_storage, $test_ids) {
  1038. $results = [];
  1039. // Sqlite has a maximum number of variables per query. If required, the
  1040. // database query is split into chunks.
  1041. if (count($test_ids) > SIMPLETEST_SCRIPT_SQLITE_VARIABLE_LIMIT && Config::get('sqlite')) {
  1042. $test_id_chunks = array_chunk($test_ids, SIMPLETEST_SCRIPT_SQLITE_VARIABLE_LIMIT);
  1043. }
  1044. else {
  1045. $test_id_chunks = [$test_ids];
  1046. }
  1047. foreach ($test_id_chunks as $test_id_chunk) {
  1048. try {
  1049. $result_chunk = [];
  1050. foreach ($test_id_chunk as $test_id) {
  1051. $test_run = TestRun::get($test_run_results_storage, $test_id);
  1052. $result_chunk = array_merge($result_chunk, $test_run->getLogEntriesByTestClass());
  1053. }
  1054. }
  1055. catch (Exception $e) {
  1056. echo (string) $e;
  1057. exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
  1058. }
  1059. if ($result_chunk) {
  1060. $results = array_merge($results, $result_chunk);
  1061. }
  1062. }
  1063. return $results;
  1064. }
  1065. /**
  1066. * Trims a string adding a leading or trailing ellipsis.
  1067. *
  1068. * @param string $input
  1069. * The input string.
  1070. * @param int $length
  1071. * The exact trimmed string length.
  1072. * @param int $side
  1073. * Leading or trailing ellipsis.
  1074. *
  1075. * @return string
  1076. * The trimmed string.
  1077. */
  1078. function trim_with_ellipsis(string $input, int $length, int $side): string {
  1079. if (strlen($input) < $length) {
  1080. return str_pad($input, $length, ' ', \STR_PAD_RIGHT);
  1081. }
  1082. elseif (strlen($input) > $length) {
  1083. return match($side) {
  1084. \STR_PAD_RIGHT => substr($input, 0, $length - 1) . '…',
  1085. default => '…' . substr($input, -$length + 1),
  1086. };
  1087. }
  1088. return $input;
  1089. }
  1090. /**
  1091. * Outputs the discovery warning messages.
  1092. */
  1093. function dump_discovery_warnings(): void {
  1094. $warnings = PhpUnitTestDiscovery::instance()->getWarnings();
  1095. if (!empty($warnings)) {
  1096. simpletest_script_print("Test discovery warnings\n", SIMPLETEST_SCRIPT_COLOR_BRIGHT_WHITE);
  1097. simpletest_script_print("-----------------------\n", SIMPLETEST_SCRIPT_COLOR_BRIGHT_WHITE);
  1098. foreach ($warnings as $warning) {
  1099. $tmp = explode("\n", $warning);
  1100. simpletest_script_print('* ' . array_shift($tmp) . "\n", SIMPLETEST_SCRIPT_COLOR_EXCEPTION);
  1101. foreach ($tmp as $sub) {
  1102. simpletest_script_print(' ' . $sub . "\n", SIMPLETEST_SCRIPT_COLOR_EXCEPTION);
  1103. }
  1104. echo "\n";
  1105. }
  1106. }
  1107. }

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