#!/usr/bin/env php useDefaults(array('test_id'))->execute(); // Execute tests. simpletest_script_command($args['concurrency'], $test_id, implode(",", $test_list)); // Display results before database is cleared. simpletest_script_reporter_display_results(); // Cleanup our test results. simpletest_clean_results_table($test_id); /** * Parse execution argument and ensure that all are valid. * * @return The list of arguments. */ function simpletest_script_parse_args() { // Set default values. $args = array( 'script' => '', 'help' => FALSE, 'list' => FALSE, 'clean' => FALSE, 'url' => '', 'php' => '', 'concurrency' => 1, 'all' => FALSE, 'class' => FALSE, 'file' => FALSE, 'color' => FALSE, 'verbose' => FALSE, 'test_names' => array(), // Used internally. 'test-id' => NULL, 'execute-batch' => FALSE, 'basepath' => '', ); // Override with set values. $args['script'] = basename(array_shift($_SERVER['argv'])); $count = 0; while ($arg = array_shift($_SERVER['argv'])) { if (preg_match('/--(\S+)/', $arg, $matches)) { // Argument found. if (array_key_exists($matches[1], $args)) { // Argument found in list. $previous_arg = $matches[1]; if (is_bool($args[$previous_arg])) { $args[$matches[1]] = TRUE; } else { $args[$matches[1]] = array_shift($_SERVER['argv']); } // Clear extraneous values. $args['test_names'] = array(); $count++; } else { // Argument not found in list. simpletest_script_print_error("Unknown argument '$arg'."); exit; } } else { // Values found without an argument should be test names. $args['test_names'] += explode(',', $arg); $count++; } } // Validate the concurrency argument if (!is_numeric($args['concurrency']) || $args['concurrency'] <= 0) { simpletest_script_print_error("--concurrency must be a strictly positive integer."); exit; } elseif ($args['concurrency'] > 1 && !function_exists('pcntl_fork')) { simpletest_script_print_error("Parallel test execution requires the Process Control extension to be compiled in PHP. Please see http://php.net/manual/en/intro.pcntl.php for more information."); exit; } // Validate the presence of the basepath argument. if ( ! isset($args['basepath'])) { simpletest_script_print_error('This script cannot proceed without the basepath argument'); exit; } return array($args, $count); } /** * Initialize script variables and perform general setup requirements. */ function simpletest_script_init() { global $args; $host = 'localhost'; $path = ''; // Get url from arguments. if (!empty($args['url'])) { $parsed_url = parse_url($args['url']); $host = $parsed_url['host'] . (isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''); $path = $parsed_url['path']; } $_SERVER['HTTP_HOST'] = $host; $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; $_SERVER['SERVER_ADDR'] = '127.0.0.1'; $_SERVER['SERVER_SOFTWARE'] = 'Apache'; $_SERVER['SERVER_NAME'] = 'localhost'; $_SERVER['REQUEST_URI'] = $path .'/'; $_SERVER['REQUEST_METHOD'] = 'GET'; $_SERVER['SCRIPT_NAME'] = $path .'/index.php'; $_SERVER['PHP_SELF'] = $path .'/index.php'; $_SERVER['HTTP_USER_AGENT'] = 'Drupal command line'; chdir($args['basepath']); define('DRUPAL_ROOT', getcwd()); require_once DRUPAL_ROOT . '/includes/bootstrap.inc'; } /** * Execute a batch of tests. */ function simpletest_script_execute_batch() { global $args; if (is_null($args['test-id'])) { simpletest_script_print_error("--execute-batch should not be called interactively."); exit; } if ($args['concurrency'] == 1) { // Fallback to mono-threaded execution. if (count($args['test_names']) > 1) { foreach ($args['test_names'] as $test_class) { // Execute each test in its separate Drupal environment. simpletest_script_command(1, $args['test-id'], $test_class); } exit; } else { // Execute an individual test. $test_class = array_shift($args['test_names']); drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); simpletest_script_run_one_test($args['test-id'], $test_class); exit; } } else { // Multi-threaded execution. $children = array(); while (!empty($args['test_names']) || !empty($children)) { // Fork children safely since Drupal is not bootstrapped yet. while (count($children) < $args['concurrency']) { if (empty($args['test_names'])) break; $child = array(); $child['test_class'] = $test_class = array_shift($args['test_names']); $child['pid'] = pcntl_fork(); if (!$child['pid']) { // This is the child process, bootstrap and execute the test. drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); simpletest_script_run_one_test($args['test-id'], $test_class); exit; } else { // Register our new child. $children[] = $child; } } // Wait for children every 200ms. usleep(200000); // Check if some children finished. foreach ($children as $cid => $child) { if (pcntl_waitpid($child['pid'], $status, WUNTRACED | WNOHANG)) { // This particular child exited. unset($children[$cid]); } } } exit; } } /** * Run a single test (assume a Drupal bootstrapped environment). */ function simpletest_script_run_one_test($test_id, $test_class) { simpletest_get_all_tests(); $test = new $test_class($test_id); $test->run(); $info = $test->getInfo(); $status = ((isset($test->results['#fail']) && $test->results['#fail'] > 0) || (isset($test->results['#exception']) && $test->results['#exception'] > 0) ? 'fail' : 'pass'); $group_name = check_plain($info['group']); $case_name = check_plain($info['name']); echo << $group_name $case_name {$test->results['#pass']} {$test->results['#fail']} {$test->results['#exception']} EOXML; } /** * Execute a command to run batch of tests in separate process. */ function simpletest_script_command($concurrency, $test_id, $tests) { global $args, $argv; $command = "{$argv[0]} --url {$args['url']} --basepath {$args['basepath']}"; $command .= " --concurrency $concurrency --test-id $test_id --execute-batch $tests"; passthru($command); } /** * Get list of tests based on arguments. If --all specified then * returns all available tests, otherwise reads list of tests. * * Will print error and exit if no valid tests were found. * * @return List of tests. */ function simpletest_script_get_test_list() { global $args, $all_tests, $groups; $test_list = array(); if ($args['all']) { $test_list = $all_tests; } else { if ($args['class']) { // Check for valid class names. foreach ($args['test_names'] as $class_name) { if (in_array($class_name, $all_tests)) { $test_list[] = $class_name; } } } elseif ($args['file']) { $files = array(); foreach ($args['test_names'] as $file) { $files[realpath($file)] = 1; } // Check for valid class names. foreach ($all_tests as $class_name => $info) { $refclass = new ReflectionClass($class_name); $file = $refclass->getFileName(); if (isset($files[$file])) { $test_list[] = $class_name; } } } else { // Check for valid group names and get all valid classes in group. foreach ($args['test_names'] as $group_name) { if (isset($groups[$group_name])) { foreach($groups[$group_name] as $class_name => $info) { $test_list[] = $class_name; } } } } } if (empty($test_list)) { simpletest_script_print_error('No valid tests were specified.'); exit; } return $test_list; } /** * Initialize the reporter. */ function simpletest_script_reporter_init() { echo '', PHP_EOL; echo ' ', PHP_EOL; } /** * Display test results. */ function simpletest_script_reporter_display_results() { echo ' ', PHP_EOL; echo '', PHP_EOL; }