This script runs Drupal tests from command line.

File

scripts/run-tests.sh
View source
  1. /**
  2. * @file
  3. * This script runs Drupal tests from command line.
  4. */
  5. define('SIMPLETEST_SCRIPT_COLOR_PASS', 32); // Green.
  6. define('SIMPLETEST_SCRIPT_COLOR_FAIL', 31); // Red.
  7. define('SIMPLETEST_SCRIPT_COLOR_EXCEPTION', 33); // Brown.
  8. define('SIMPLETEST_SCRIPT_EXIT_SUCCESS', 0);
  9. define('SIMPLETEST_SCRIPT_EXIT_FAILURE', 1);
  10. define('SIMPLETEST_SCRIPT_EXIT_EXCEPTION', 2);
  11. // Set defaults and get overrides.
  12. list($args, $count) = simpletest_script_parse_args();
  13. if ($args['help'] || $count == 0) {
  14. simpletest_script_help();
  15. exit(($count == 0) ? SIMPLETEST_SCRIPT_EXIT_FAILURE : SIMPLETEST_SCRIPT_EXIT_SUCCESS);
  16. }
  17. if ($args['execute-test']) {
  18. // Masquerade as Apache for running tests.
  19. simpletest_script_init("Apache");
  20. simpletest_script_run_one_test($args['test-id'], $args['execute-test']);
  21. }
  22. else {
  23. // Run administrative functions as CLI.
  24. simpletest_script_init(NULL);
  25. }
  26. // Bootstrap to perform initial validation or other operations.
  27. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  28. if (!module_exists('simpletest')) {
  29. simpletest_script_print_error("The simpletest module must be enabled before this script can run.");
  30. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  31. }
  32. if ($args['clean']) {
  33. // Clean up left-over times and directories.
  34. simpletest_clean_environment();
  35. echo "\nEnvironment cleaned.\n";
  36. // Get the status messages and print them.
  37. $messages = drupal_get_messages('status');
  38. foreach ($messages['status'] as $text) {
  39. echo " - " . $text . "\n";
  40. }
  41. exit(SIMPLETEST_SCRIPT_EXIT_SUCCESS);
  42. }
  43. // Load SimpleTest files.
  44. $groups = simpletest_test_get_all();
  45. $all_tests = array();
  46. foreach ($groups as $group => $tests) {
  47. $all_tests = array_merge($all_tests, array_keys($tests));
  48. }
  49. $test_list = array();
  50. if ($args['list']) {
  51. // Display all available tests.
  52. echo "\nAvailable test groups & classes\n";
  53. echo "-------------------------------\n\n";
  54. foreach ($groups as $group => $tests) {
  55. echo $group . "\n";
  56. foreach ($tests as $class => $info) {
  57. echo " - " . $info['name'] . ' (' . $class . ')' . "\n";
  58. }
  59. }
  60. exit(SIMPLETEST_SCRIPT_EXIT_SUCCESS);
  61. }
  62. $test_list = simpletest_script_get_test_list();
  63. // Try to allocate unlimited time to run the tests.
  64. drupal_set_time_limit(0);
  65. simpletest_script_reporter_init();
  66. // Setup database for test results.
  67. $test_id = db_insert('simpletest_test_id')->useDefaults(array('test_id'))->execute();
  68. // Execute tests.
  69. $status = simpletest_script_execute_batch($test_id, simpletest_script_get_test_list());
  70. // Retrieve the last database prefix used for testing and the last test class
  71. // that was run from. Use the information to read the lgo file in case any
  72. // fatal errors caused the test to crash.
  73. list($last_prefix, $last_test_class) = simpletest_last_test_get($test_id);
  74. simpletest_log_read($test_id, $last_prefix, $last_test_class);
  75. // Stop the timer.
  76. simpletest_script_reporter_timer_stop();
  77. // Display results before database is cleared.
  78. simpletest_script_reporter_display_results();
  79. if ($args['xml']) {
  80. simpletest_script_reporter_write_xml_results();
  81. }
  82. // Cleanup our test results.
  83. simpletest_clean_results_table($test_id);
  84. // Test complete, exit.
  85. exit($status);
  86. /**
  87. * Print help text.
  88. */
  89. function simpletest_script_help() {
  90. global $args;
  91. echo <<
  92. Run Drupal tests from the shell.
  93. Usage: {$args['script']} [OPTIONS]
  94. Example: {$args['script']} Profile
  95. All arguments are long options.
  96. --help Print this page.
  97. --list Display all available test groups.
  98. --clean Cleans up database tables or directories from previous, failed,
  99. tests and then exits (no tests are run).
  100. --url Immediately precedes a URL to set the host and path. You will
  101. need this parameter if Drupal is in a subdirectory on your
  102. localhost and you have not set \$base_url in settings.php. Tests
  103. can be run under SSL by including https:// in the URL.
  104. --php The absolute path to the PHP executable. Usually not needed.
  105. --concurrency [num]
  106. Run tests in parallel, up to [num] tests at a time.
  107. --all Run all available tests.
  108. --class Run tests identified by specific class names, instead of group names.
  109. A specific test method can be added, for example,
  110. 'UserAccountLinksUnitTests::testDisabledAccountLink'.
  111. --file Run tests identified by specific file names, instead of group names.
  112. Specify the path and the extension (i.e. 'modules/user/user.test').
  113. --directory Run all tests found within the specified file directory.
  114. --xml
  115. If provided, test results will be written as xml files to this path.
  116. --color Output text format results with color highlighting.
  117. --verbose Output detailed assertion messages in addition to summary.
  118. --fail-only When paired with --verbose, do not print the detailed messages
  119. for passing tests.
  120. --cache (Experimental) Cache result of setUp per installation profile.
  121. This will create one cache entry per profile and is generally safe
  122. to use.
  123. To clear all cache entries use --clean.
  124. --cache-modules
  125. (Experimental) Cache result of setUp per installation profile and
  126. installed modules. This will create one copy of the database
  127. tables per module-combination and therefore this option should not
  128. be used when running all tests. This is most useful for local
  129. development of individual test cases. This option implies --cache.
  130. To clear all cache entries use --clean.
  131. --ci-parallel-node-index
  132. The index of the job in the job set.
  133. --ci-parallel-node-total
  134. The total number of instances of this job running in parallel.
  135. [,[, ...]]
  136. One or more tests to be run. By default, these are interpreted
  137. as the names of test groups as shown at
  138. ?q=admin/config/development/testing.
  139. These group names typically correspond to module names like "User"
  140. or "Profile" or "System", but there is also a group "XML-RPC".
  141. If --class is specified then these are interpreted as the names of
  142. specific test classes whose test methods will be run. Tests must
  143. be separated by commas. Ignored if --all is specified.
  144. To run this script you will normally invoke it from the root directory of your
  145. Drupal installation as the webserver user (differs per configuration), or root:
  146. sudo -u [wwwrun|www-data|etc] php ./scripts/{$args['script']}
  147. --url http://example.com/ --all
  148. sudo -u [wwwrun|www-data|etc] php ./scripts/{$args['script']}
  149. --url http://example.com/ --class BlockTestCase
  150. \n
  151. EOF;
  152. }
  153. /**
  154. * Parse execution argument and ensure that all are valid.
  155. *
  156. * @return The list of arguments.
  157. */
  158. function simpletest_script_parse_args() {
  159. // Set default values.
  160. $args = array(
  161. 'script' => '',
  162. 'help' => FALSE,
  163. 'list' => FALSE,
  164. 'clean' => FALSE,
  165. 'url' => '',
  166. 'php' => '',
  167. 'concurrency' => 1,
  168. 'all' => FALSE,
  169. 'class' => FALSE,
  170. 'file' => FALSE,
  171. 'directory' => '',
  172. 'color' => FALSE,
  173. 'verbose' => FALSE,
  174. 'cache' => FALSE,
  175. 'cache-modules' => FALSE,
  176. 'test_names' => array(),
  177. 'fail-only' => FALSE,
  178. // Used internally.
  179. 'test-id' => 0,
  180. 'execute-test' => '',
  181. 'xml' => '',
  182. 'ci-parallel-node-index' => 1,
  183. 'ci-parallel-node-total' => 1,
  184. );
  185. // Override with set values.
  186. $args['script'] = basename(array_shift($_SERVER['argv']));
  187. $count = 0;
  188. while ($arg = array_shift($_SERVER['argv'])) {
  189. if (preg_match('/--(\S+)/', $arg, $matches)) {
  190. // Argument found.
  191. if (array_key_exists($matches[1], $args)) {
  192. // Argument found in list.
  193. $previous_arg = $matches[1];
  194. if (is_bool($args[$previous_arg])) {
  195. $args[$matches[1]] = TRUE;
  196. }
  197. else {
  198. $args[$matches[1]] = array_shift($_SERVER['argv']);
  199. }
  200. // Clear extraneous values.
  201. $args['test_names'] = array();
  202. $count++;
  203. }
  204. else {
  205. // Argument not found in list.
  206. simpletest_script_print_error("Unknown argument '$arg'.");
  207. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  208. }
  209. }
  210. else {
  211. // Values found without an argument should be test names.
  212. $args['test_names'] += explode(',', $arg);
  213. $count++;
  214. }
  215. }
  216. // Validate the concurrency argument
  217. if (!is_numeric($args['concurrency']) || $args['concurrency'] <= 0) {
  218. simpletest_script_print_error("--concurrency must be a strictly positive integer.");
  219. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  220. }
  221. return array($args, $count);
  222. }
  223. /**
  224. * Initialize script variables and perform general setup requirements.
  225. */
  226. function simpletest_script_init($server_software) {
  227. global $args, $php;
  228. $host = 'localhost';
  229. $path = '';
  230. // Determine location of php command automatically, unless a command line argument is supplied.
  231. if (!empty($args['php'])) {
  232. $php = $args['php'];
  233. }
  234. elseif ($php_env = getenv('_')) {
  235. // '_' is an environment variable set by the shell. It contains the command that was executed.
  236. $php = $php_env;
  237. }
  238. elseif (defined('PHP_BINARY') && $php_env = PHP_BINARY) {
  239. // 'PHP_BINARY' specifies the PHP binary path during script execution. Available since PHP 5.4.
  240. $php = $php_env;
  241. }
  242. elseif ($sudo = getenv('SUDO_COMMAND')) {
  243. // 'SUDO_COMMAND' is an environment variable set by the sudo program.
  244. // Extract only the PHP interpreter, not the rest of the command.
  245. list($php, ) = explode(' ', $sudo, 2);
  246. }
  247. else {
  248. simpletest_script_print_error('Unable to automatically determine the path to the PHP interpreter. Supply the --php command line argument.');
  249. simpletest_script_help();
  250. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  251. }
  252. // Get URL from arguments.
  253. if (!empty($args['url'])) {
  254. $parsed_url = parse_url($args['url']);
  255. $host = $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : '');
  256. $path = isset($parsed_url['path']) ? $parsed_url['path'] : '';
  257. // If the passed URL schema is 'https' then setup the $_SERVER variables
  258. // properly so that testing will run under HTTPS.
  259. if ($parsed_url['scheme'] == 'https') {
  260. $_SERVER['HTTPS'] = 'on';
  261. }
  262. }
  263. $_SERVER['HTTP_HOST'] = $host;
  264. $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
  265. $_SERVER['SERVER_ADDR'] = '127.0.0.1';
  266. $_SERVER['SERVER_SOFTWARE'] = $server_software;
  267. $_SERVER['SERVER_NAME'] = 'localhost';
  268. $_SERVER['REQUEST_URI'] = $path .'/';
  269. $_SERVER['REQUEST_METHOD'] = 'GET';
  270. $_SERVER['SCRIPT_NAME'] = $path .'/index.php';
  271. $_SERVER['PHP_SELF'] = $path .'/index.php';
  272. $_SERVER['HTTP_USER_AGENT'] = 'Drupal command line';
  273. if (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') {
  274. // Ensure that any and all environment variables are changed to https://.
  275. foreach ($_SERVER as $key => $value) {
  276. // The first time this script runs $_SERVER['SERVER_SOFTWARE'] will be
  277. // NULL, so avoid errors from str_replace().
  278. if (!empty($_SERVER[$key])) {
  279. $_SERVER[$key] = str_replace('http://', 'https://', $_SERVER[$key]);
  280. }
  281. }
  282. }
  283. chdir(realpath(dirname(__FILE__) . '/..'));
  284. define('DRUPAL_ROOT', getcwd());
  285. require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
  286. }
  287. /**
  288. * Execute a batch of tests.
  289. */
  290. function simpletest_script_execute_batch($test_id, $test_classes) {
  291. global $args;
  292. $total_status = SIMPLETEST_SCRIPT_EXIT_SUCCESS;
  293. // Multi-process execution.
  294. $children = array();
  295. while (!empty($test_classes) || !empty($children)) {
  296. while (count($children) < $args['concurrency']) {
  297. if (empty($test_classes)) {
  298. break;
  299. }
  300. // Fork a child process.
  301. $test_class = array_shift($test_classes);
  302. $command = simpletest_script_command($test_id, $test_class);
  303. $process = proc_open($command, array(), $pipes, NULL, NULL, array('bypass_shell' => TRUE));
  304. if (!is_resource($process)) {
  305. echo "Unable to fork test process. Aborting.\n";
  306. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  307. }
  308. // Register our new child.
  309. $children[] = array(
  310. 'process' => $process,
  311. 'class' => $test_class,
  312. 'pipes' => $pipes,
  313. );
  314. }
  315. // Wait for children every 200ms.
  316. usleep(200000);
  317. // Check if some children finished.
  318. foreach ($children as $cid => $child) {
  319. $status = proc_get_status($child['process']);
  320. if (empty($status['running'])) {
  321. // The child exited, unregister it.
  322. proc_close($child['process']);
  323. if ($status['exitcode'] == SIMPLETEST_SCRIPT_EXIT_FAILURE) {
  324. if ($status['exitcode'] > $total_status) {
  325. $total_status = $status['exitcode'];
  326. }
  327. }
  328. elseif ($status['exitcode']) {
  329. $total_status = $status['exitcode'];
  330. echo 'FATAL ' . $test_class . ': test runner returned a non-zero error code (' . $status['exitcode'] . ').' . "\n";
  331. }
  332. // Remove this child.
  333. unset($children[$cid]);
  334. }
  335. }
  336. }
  337. return $total_status;
  338. }
  339. /**
  340. * Bootstrap Drupal and run a single test.
  341. */
  342. function simpletest_script_run_one_test($test_id, $test_class) {
  343. global $args;
  344. try {
  345. // Bootstrap Drupal.
  346. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  347. simpletest_classloader_register();
  348. if (strpos($test_class, '::') > 0) {
  349. list($class_name, $method) = explode('::', $test_class, 2);
  350. $methods = array($method);
  351. }
  352. else {
  353. $class_name = $test_class;
  354. // Use empty array to run all the test methods.
  355. $methods = array();
  356. }
  357. $test = new $class_name($test_id);
  358. $test->useSetupInstallationCache = !empty($args['cache']);
  359. $test->useSetupModulesCache = !empty($args['cache-modules']);
  360. $test->run($methods);
  361. $info = $test->getInfo();
  362. $had_fails = (isset($test->results['#fail']) && $test->results['#fail'] > 0);
  363. $had_exceptions = (isset($test->results['#exception']) && $test->results['#exception'] > 0);
  364. $status = ($had_fails || $had_exceptions ? 'fail' : 'pass');
  365. simpletest_script_print($info['name'] . ' ' . _simpletest_format_summary_line($test->results) . "\n", simpletest_script_color_code($status));
  366. // Finished, kill this runner.
  367. if ($had_fails || $had_exceptions) {
  368. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  369. }
  370. exit(SIMPLETEST_SCRIPT_EXIT_SUCCESS);
  371. }
  372. catch (Exception $e) {
  373. echo (string) $e;
  374. exit(SIMPLETEST_SCRIPT_EXIT_EXCEPTION);
  375. }
  376. }
  377. /**
  378. * Return a command used to run a test in a separate process.
  379. *
  380. * @param $test_id
  381. * The current test ID.
  382. * @param $test_class
  383. * The name of the test class to run.
  384. */
  385. function simpletest_script_command($test_id, $test_class) {
  386. global $args, $php;
  387. $command = escapeshellarg($php) . ' ' . escapeshellarg('./scripts/' . $args['script']) . ' --url ' . escapeshellarg($args['url']);
  388. if ($args['color']) {
  389. $command .= ' --color';
  390. }
  391. if ($args['cache-modules']) {
  392. $command .= ' --cache --cache-modules';
  393. }
  394. elseif ($args['cache']) {
  395. $command .= ' --cache';
  396. }
  397. $command .= " --php " . escapeshellarg($php) . " --test-id $test_id --execute-test " . escapeshellarg($test_class);
  398. return $command;
  399. }
  400. /**
  401. * Get list of tests based on arguments. If --all specified then
  402. * returns all available tests, otherwise reads list of tests.
  403. *
  404. * Will print error and exit if no valid tests were found.
  405. *
  406. * @return List of tests.
  407. */
  408. function simpletest_script_get_test_list() {
  409. global $args, $all_tests, $groups;
  410. $test_list = array();
  411. if ($args['all']) {
  412. $test_list = $all_tests;
  413. }
  414. else {
  415. if ($args['class']) {
  416. // Check for valid class names.
  417. $test_list = array();
  418. foreach ($args['test_names'] as $test_class) {
  419. list($class_name, $method) = explode('::', $test_class, 2);
  420. if (class_exists($class_name)) {
  421. if (empty($method) || method_exists($class_name, $method)) {
  422. $test_list[] = $test_class;
  423. } else {
  424. $all_methods = get_class_methods($class_name);
  425. simpletest_script_print_error('Test method not found: ' . $test_class);
  426. simpletest_script_print_alternatives($method, $all_methods, 6);
  427. exit(1);
  428. }
  429. }
  430. else {
  431. $groups = simpletest_test_get_all();
  432. $all_classes = array();
  433. foreach ($groups as $group) {
  434. $all_classes = array_merge($all_classes, array_keys($group));
  435. }
  436. simpletest_script_print_error('Test class not found: ' . $class_name);
  437. simpletest_script_print_alternatives($class_name, $all_classes, 6);
  438. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  439. }
  440. }
  441. }
  442. elseif ($args['file']) {
  443. $files = array();
  444. foreach ($args['test_names'] as $file) {
  445. $files[drupal_realpath($file)] = 1;
  446. }
  447. // Check for valid class names.
  448. foreach ($all_tests as $class_name) {
  449. $refclass = new ReflectionClass($class_name);
  450. $file = $refclass->getFileName();
  451. if (isset($files[$file])) {
  452. $test_list[] = $class_name;
  453. }
  454. }
  455. }
  456. elseif ($args['directory']) {
  457. // Extract test case class names from specified directory.
  458. // Find all tests in the PSR-X structure; Drupal\$extension\Tests\*.php
  459. // Since we do not want to hard-code too many structural file/directory
  460. // assumptions about PSR-0/4 files and directories, we check for the
  461. // minimal conditions only; i.e., a '*.php' file that has '/Tests/' in
  462. // its path.
  463. // Ignore anything from third party vendors, and ignore template files used in tests.
  464. // And any api.php files.
  465. $ignore = array('nomask' => '/vendor|\.tpl\.php|\.api\.php/');
  466. $files = array();
  467. if ($args['directory'][0] === '/') {
  468. $directory = $args['directory'];
  469. }
  470. else {
  471. $directory = DRUPAL_ROOT . "/" . $args['directory'];
  472. }
  473. $file_list = file_scan_directory($directory, '/\.php|\.test$/', $ignore);
  474. foreach ($file_list as $file) {
  475. // '/Tests/' can be contained anywhere in the file's path (there can be
  476. // sub-directories below /Tests), but must be contained literally.
  477. // Case-insensitive to match all Simpletest and PHPUnit tests:
  478. // ./lib/Drupal/foo/Tests/Bar/Baz.php
  479. // ./foo/src/Tests/Bar/Baz.php
  480. // ./foo/tests/Drupal/foo/Tests/FooTest.php
  481. // ./foo/tests/src/FooTest.php
  482. // $file->filename doesn't give us a directory, so we use $file->uri
  483. // Strip the drupal root directory and trailing slash off the URI
  484. $filename = substr($file->uri, strlen(DRUPAL_ROOT)+1);
  485. if (stripos($filename, '/Tests/')) {
  486. $files[drupal_realpath($filename)] = 1;
  487. } else if (stripos($filename, '.test')){
  488. $files[drupal_realpath($filename)] = 1;
  489. }
  490. }
  491. // Check for valid class names.
  492. foreach ($all_tests as $class_name) {
  493. $refclass = new ReflectionClass($class_name);
  494. $classfile = $refclass->getFileName();
  495. if (isset($files[$classfile])) {
  496. $test_list[] = $class_name;
  497. }
  498. }
  499. }
  500. else {
  501. // Check for valid group names and get all valid classes in group.
  502. foreach ($args['test_names'] as $group_name) {
  503. if (isset($groups[$group_name])) {
  504. $test_list = array_merge($test_list, array_keys($groups[$group_name]));
  505. }
  506. else {
  507. simpletest_script_print_error('Test group not found: ' . $group_name);
  508. simpletest_script_print_alternatives($group_name, array_keys($groups));
  509. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  510. }
  511. }
  512. }
  513. }
  514. if (empty($test_list)) {
  515. simpletest_script_print_error('No valid tests were specified.');
  516. exit(SIMPLETEST_SCRIPT_EXIT_FAILURE);
  517. }
  518. if ((int) $args['ci-parallel-node-total'] > 1) {
  519. $tests_per_job = ceil(count($test_list) / $args['ci-parallel-node-total']);
  520. $test_list = array_slice($test_list, ($args['ci-parallel-node-index'] - 1) * $tests_per_job, $tests_per_job);
  521. }
  522. return $test_list;
  523. }
  524. /**
  525. * Initialize the reporter.
  526. */
  527. function simpletest_script_reporter_init() {
  528. global $args, $all_tests, $test_list, $results_map;
  529. $results_map = array(
  530. 'pass' => 'Pass',
  531. 'fail' => 'Fail',
  532. 'exception' => 'Exception'
  533. );
  534. echo "\n";
  535. echo "Drupal test run\n";
  536. echo "---------------\n";
  537. echo "\n";
  538. // Tell the user about what tests are to be run.
  539. if ($args['all']) {
  540. echo "All tests will run.\n\n";
  541. }
  542. else {
  543. echo "Tests to be run:\n";
  544. foreach ($test_list as $test_name) {
  545. if (strpos($test_name, '::') > 0) {
  546. list($test_class, $method) = explode('::', $test_name, 2);
  547. $info = call_user_func(array($test_class, 'getInfo'));
  548. } else {
  549. $info = call_user_func(array($test_name, 'getInfo'));
  550. }
  551. echo " - " . $info['name'] . ' (' . $test_name . ')' . "\n";
  552. }
  553. echo "\n";
  554. }
  555. echo "Test run started:\n";
  556. echo " " . format_date($_SERVER['REQUEST_TIME'], 'long') . "\n";
  557. timer_start('run-tests');
  558. echo "\n";
  559. echo "Test summary\n";
  560. echo "------------\n";
  561. echo "\n";
  562. }
  563. /**
  564. * Display jUnit XML test results.
  565. */
  566. function simpletest_script_reporter_write_xml_results() {
  567. global $args, $test_id, $results_map;
  568. $results = db_query("SELECT * FROM {simpletest} WHERE test_id = :test_id ORDER BY test_class, message_id", array(':test_id' => $test_id));
  569. $test_class = '';
  570. $xml_files = array();
  571. foreach ($results as $result) {
  572. if (isset($results_map[$result->status])) {
  573. if ($result->test_class != $test_class) {
  574. // We've moved onto a new class, so write the last classes results to a file:
  575. if (isset($xml_files[$test_class])) {
  576. file_put_contents($args['xml'] . '/' . $test_class . '.xml', $xml_files[$test_class]['doc']->saveXML());
  577. unset($xml_files[$test_class]);
  578. }
  579. $test_class = $result->test_class;
  580. if (!isset($xml_files[$test_class])) {
  581. $doc = new DomDocument('1.0');
  582. $root = $doc->createElement('testsuite');
  583. $root = $doc->appendChild($root);
  584. $xml_files[$test_class] = array('doc' => $doc, 'suite' => $root);
  585. }
  586. }
  587. // For convenience:
  588. $dom_document = &$xml_files[$test_class]['doc'];
  589. // Create the XML element for this test case:
  590. $case = $dom_document->createElement('testcase');
  591. $case->setAttribute('classname', $test_class);
  592. list($class, $name) = explode('->', $result->function, 2);
  593. $case->setAttribute('name', $name);
  594. // Passes get no further attention, but failures and exceptions get to add more detail:
  595. if ($result->status == 'fail') {
  596. $fail = $dom_document->createElement('failure');
  597. $fail->setAttribute('type', 'failure');
  598. $fail->setAttribute('message', $result->message_group);
  599. $text = $dom_document->createTextNode($result->message);
  600. $fail->appendChild($text);
  601. $case->appendChild($fail);
  602. }
  603. elseif ($result->status == 'exception') {
  604. // In the case of an exception the $result->function may not be a class
  605. // method so we record the full function name:
  606. $case->setAttribute('name', $result->function);
  607. $fail = $dom_document->createElement('error');
  608. $fail->setAttribute('type', 'exception');
  609. $fail->setAttribute('message', $result->message_group);
  610. $full_message = $result->message . "\n\nline: " . $result->line . "\nfile: " . $result->file;
  611. $text = $dom_document->createTextNode($full_message);
  612. $fail->appendChild($text);
  613. $case->appendChild($fail);
  614. }
  615. // Append the test case XML to the test suite:
  616. $xml_files[$test_class]['suite']->appendChild($case);
  617. }
  618. }
  619. // The last test case hasn't been saved to a file yet, so do that now:
  620. if (isset($xml_files[$test_class])) {
  621. file_put_contents($args['xml'] . '/' . $test_class . '.xml', $xml_files[$test_class]['doc']->saveXML());
  622. unset($xml_files[$test_class]);
  623. }
  624. }
  625. /**
  626. * Stop the test timer.
  627. */
  628. function simpletest_script_reporter_timer_stop() {
  629. echo "\n";
  630. $end = timer_stop('run-tests');
  631. echo "Test run duration: " . format_interval($end['time'] / 1000);
  632. echo "\n\n";
  633. }
  634. /**
  635. * Display test results.
  636. */
  637. function simpletest_script_reporter_display_results() {
  638. global $args, $test_id, $results_map;
  639. if ($args['verbose']) {
  640. // Report results.
  641. echo "Detailed test results\n";
  642. echo "---------------------\n";
  643. $results = db_query("SELECT * FROM {simpletest} WHERE test_id = :test_id ORDER BY test_class, message_id", array(':test_id' => $test_id));
  644. $test_class = '';
  645. foreach ($results as $result) {
  646. if (isset($results_map[$result->status]) && (!$args['fail-only'] || $result->status !== 'pass')) {
  647. if ($result->test_class != $test_class) {
  648. // Display test class every time results are for new test class.
  649. echo "\n\n---- $result->test_class ----\n\n\n";
  650. $test_class = $result->test_class;
  651. // Print table header.
  652. echo "Status Group Filename Line Function \n";
  653. echo "--------------------------------------------------------------------------------\n";
  654. }
  655. simpletest_script_format_result($result);
  656. }
  657. }
  658. }
  659. }
  660. /**
  661. * Format the result so that it fits within the default 80 character
  662. * terminal size.
  663. *
  664. * @param $result The result object to format.
  665. */
  666. function simpletest_script_format_result($result) {
  667. global $results_map, $color;
  668. $summary = sprintf("%-9.9s %-10.10s %-17.17s %4.4s %-35.35s\n",
  669. $results_map[$result->status], $result->message_group, basename($result->file), $result->line, $result->function);
  670. simpletest_script_print($summary, simpletest_script_color_code($result->status));
  671. $lines = explode("\n", wordwrap(trim(strip_tags($result->message)), 76));
  672. foreach ($lines as $line) {
  673. echo " $line\n";
  674. }
  675. }
  676. /**
  677. * Print error message prefixed with " ERROR: " and displayed in fail color
  678. * if color output is enabled.
  679. *
  680. * @param $message The message to print.
  681. */
  682. function simpletest_script_print_error($message) {
  683. simpletest_script_print(" ERROR: $message\n", SIMPLETEST_SCRIPT_COLOR_FAIL);
  684. }
  685. /**
  686. * Print a message to the console, if color is enabled then the specified
  687. * color code will be used.
  688. *
  689. * @param $message The message to print.
  690. * @param $color_code The color code to use for coloring.
  691. */
  692. function simpletest_script_print($message, $color_code) {
  693. global $args;
  694. if (!empty($args['color'])) {
  695. echo "\033[" . $color_code . "m" . $message . "\033[0m";
  696. }
  697. else {
  698. echo $message;
  699. }
  700. }
  701. /**
  702. * Get the color code associated with the specified status.
  703. *
  704. * @param $status The status string to get code for.
  705. * @return Color code.
  706. */
  707. function simpletest_script_color_code($status) {
  708. switch ($status) {
  709. case 'pass':
  710. return SIMPLETEST_SCRIPT_COLOR_PASS;
  711. case 'fail':
  712. return SIMPLETEST_SCRIPT_COLOR_FAIL;
  713. case 'exception':
  714. return SIMPLETEST_SCRIPT_COLOR_EXCEPTION;
  715. }
  716. return 0; // Default formatting.
  717. }
  718. /**
  719. * Prints alternative test names.
  720. *
  721. * Searches the provided array of string values for close matches based on the
  722. * Levenshtein algorithm.
  723. *
  724. * @see http://php.net/manual/en/function.levenshtein.php
  725. *
  726. * @param string $string
  727. * A string to test.
  728. * @param array $array
  729. * A list of strings to search.
  730. * @param int $degree
  731. * The matching strictness. Higher values return fewer matches. A value of
  732. * 4 means that the function will return strings from $array if the candidate
  733. * string in $array would be identical to $string by changing 1/4 or fewer of
  734. * its characters.
  735. */
  736. function simpletest_script_print_alternatives($string, $array, $degree = 4) {
  737. $alternatives = array();
  738. foreach ($array as $item) {
  739. $lev = levenshtein($string, $item);
  740. if ($lev <= strlen($item) / $degree || FALSE !== strpos($string, $item)) {
  741. $alternatives[] = $item;
  742. }
  743. }
  744. if (!empty($alternatives)) {
  745. simpletest_script_print(" Did you mean?\n", SIMPLETEST_SCRIPT_COLOR_FAIL);
  746. foreach ($alternatives as $alternative) {
  747. simpletest_script_print(" - $alternative\n", SIMPLETEST_SCRIPT_COLOR_FAIL);
  748. }
  749. }
  750. }