run-tests.sh

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

Script for running tests on DrupalCI.

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

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