/** * Lists the available steps definitions * * @param string $type * @param string $component * @param string $filter * @return string */ public static function stepsdefinitions($type, $component, $filter) { // We don't require the test environment to be enabled to list the steps definitions // so test writers can more easily set up the environment. behat_command::check_behat_setup(); // The loaded steps depends on the component specified. behat_config_manager::update_config_file($component, false); // The Moodle\BehatExtension\HelpPrinter\MoodleDefinitionsPrinter will parse this search format. if ($type) { $filter .= '&&' . $type; } if ($filter) { $filteroption = ' -d "' . $filter . '"'; } else { $filteroption = ' -di'; } // Get steps definitions from Behat. $options = ' --config="' . behat_config_manager::get_steps_list_config_filepath() . '" ' . $filteroption; list($steps, $code) = behat_command::run($options); if ($steps) { $stepshtml = implode('', $steps); } if (empty($stepshtml)) { $stepshtml = get_string('nostepsdefinitions', 'tool_behat'); } return $stepshtml; }
// Hack to set proper dataroot and wwwroot. $behatdataroot = $CFG->behat_dataroot; $behatwwwroot = $CFG->behat_wwwroot; for ($i = 1; $i <= $parallelrun; $i++) { $CFG->behatrunprocess = $i; if (!empty($CFG->behat_parallel_run[$i - 1]['behat_wwwroot'])) { $CFG->behat_wwwroot = $CFG->behat_parallel_run[$i - 1]['behat_wwwroot']; } else { $CFG->behat_wwwroot = $behatwwwroot . "/" . BEHAT_PARALLEL_SITE_NAME . $i; } if (!empty($CFG->behat_parallel_run[$i - 1]['behat_dataroot'])) { $CFG->behat_dataroot = $CFG->behat_parallel_run[$i - 1]['behat_dataroot']; } else { $CFG->behat_dataroot = $behatdataroot . $i; } behat_config_manager::update_config_file('', true, $tags); } $CFG->behat_dataroot = $behatdataroot; $CFG->behat_wwwroot = $behatwwwroot; unset($CFG->behatrunprocess); } $cmds = array(); echo "Running " . ($options['torun'] - $options['fromrun'] + 1) . " parallel behat sites:" . PHP_EOL; for ($i = $options['fromrun']; $i <= $options['torun']; $i++) { $CFG->behatrunprocess = $i; // Options parameters to be added to each run. $myopts = !empty($options['replace']) ? str_replace($options['replace'], $i, $extraopts) : $extraopts; $behatcommand = behat_command::get_behat_command(false, false, true); $behatconfigpath = behat_config_manager::get_behat_cli_config_filepath($i); // Command to execute behat run. $cmds[BEHAT_PARALLEL_SITE_NAME . $i] = $behatcommand . ' --config ' . $behatconfigpath . " " . $myopts;
/** * Enables test mode * * It uses CFG->behat_dataroot * * Starts the test mode checking the composer installation and * the test environment and updating the available * features and steps definitions. * * Stores a file in dataroot/behat to allow Moodle to switch * to the test environment when using cli-server. * @param bool $themesuitewithallfeatures if only theme specific features need to be included in the suite. * @param string $tags comma separated tag, which will be given preference while distributing features in parallel run. * @param int $parallelruns number of parallel runs. * @param int $run current run. * @throws coding_exception * @return void */ public static function start_test_mode($themesuitewithallfeatures = false, $tags = '', $parallelruns = 0, $run = 0) { global $CFG; if (!defined('BEHAT_UTIL')) { throw new coding_exception('This method can be only used by Behat CLI tool'); } // Checks the behat set up and the PHP version. if ($errorcode = behat_command::behat_setup_problem()) { exit($errorcode); } // Check that test environment is correctly set up. self::test_environment_problem(); // Updates all the Moodle features and steps definitions. behat_config_manager::update_config_file('', true, $tags, $themesuitewithallfeatures, $parallelruns, $run); if (self::is_test_mode_enabled()) { return; } $contents = '$CFG->behat_wwwroot, $CFG->behat_prefix and $CFG->behat_dataroot' . ' are currently used as $CFG->wwwroot, $CFG->prefix and $CFG->dataroot'; $filepath = self::get_test_file_path(); if (!file_put_contents($filepath, $contents)) { behat_error(BEHAT_EXITCODE_PERMISSIONS, 'File ' . $filepath . ' can not be created'); } }
mtrace("Acceptance tests environment disabled"); } } else { if ($options['diag']) { $code = behat_util::get_behat_status(); exit($code); } else { if ($options['updatesteps']) { if (defined('BEHAT_FEATURE_STEP_FILE') && BEHAT_FEATURE_STEP_FILE) { $behatstepfile = BEHAT_FEATURE_STEP_FILE; } else { echo "BEHAT_FEATURE_STEP_FILE is not set, please ensure you set this to writable file" . PHP_EOL; exit(1); } // Rewrite config file to ensure we have all the features covered. behat_config_manager::update_config_file(); // Run behat command to get steps in feature files. $featurestepscmd = behat_command::get_behat_command(true); $featurestepscmd .= ' --config ' . behat_config_manager::get_behat_cli_config_filepath(); $featurestepscmd .= ' --dry-run --format=moodle_step_count'; $processes = cli_execute_parallel(array($featurestepscmd), __DIR__ . "/../../../../"); $status = print_update_step_output(array_pop($processes), $behatstepfile); exit($status); } else { echo $help; exit(1); } } } } }
echo PHP_EOL . PHP_EOL; } $status = (bool) $status || (bool) $exitcode; } } } else { if ($options['updatesteps']) { // Rewrite config file to ensure we have all the features covered. if (empty($options['parallel'])) { behat_config_manager::update_config_file('', true, '', $options['run-with-theme'], false, false); } else { // Update config file, ensuring we have up-to-date behat.yml. for ($i = $options['fromrun']; $i <= $options['torun']; $i++) { $CFG->behatrunprocess = $i; // Update config file for each run. behat_config_manager::update_config_file('', true, '', $options['run-with-theme'], $options['parallel'], $i); } unset($CFG->behatrunprocess); } // Do it sequentially as it's fast and need to be displayed nicely. foreach (array_chunk($cmds, 1, true) as $cmd) { $processes = cli_execute_parallel($cmd, __DIR__); print_sequential_output($processes); } exit(0); } else { // We should never reach here. echo $help; exit(1); } }
// Hack to set proper dataroot and wwwroot. $behatdataroot = $CFG->behat_dataroot; $behatwwwroot = $CFG->behat_wwwroot; for ($i = 1; $i <= $parallelrun; $i++) { $CFG->behatrunprocess = $i; if (!empty($CFG->behat_parallel_run[$i - 1]['behat_wwwroot'])) { $CFG->behat_wwwroot = $CFG->behat_parallel_run[$i - 1]['behat_wwwroot']; } else { $CFG->behat_wwwroot = $behatwwwroot . "/" . BEHAT_PARALLEL_SITE_NAME . $i; } if (!empty($CFG->behat_parallel_run[$i - 1]['behat_dataroot'])) { $CFG->behat_dataroot = $CFG->behat_parallel_run[$i - 1]['behat_dataroot']; } else { $CFG->behat_dataroot = $behatdataroot . $i; } behat_config_manager::update_config_file('', true, $tags, $options['run-with-theme'], $parallelrun); } $CFG->behat_dataroot = $behatdataroot; $CFG->behat_wwwroot = $behatwwwroot; unset($CFG->behatrunprocess); } $cmds = array(); echo "Running " . ($options['torun'] - $options['fromrun'] + 1) . " parallel behat sites:" . PHP_EOL; for ($i = $options['fromrun']; $i <= $options['torun']; $i++) { $CFG->behatrunprocess = $i; // Options parameters to be added to each run. $myopts = !empty($options['replace']) ? str_replace($options['replace'], $i, $extraopts) : $extraopts; $behatcommand = behat_command::get_behat_command(false, false, true); $behatconfigpath = behat_config_manager::get_behat_cli_config_filepath($i); // Command to execute behat run. $cmds[BEHAT_PARALLEL_SITE_NAME . $i] = $behatcommand . ' --config ' . $behatconfigpath . " " . $myopts;
echo PHP_EOL . PHP_EOL; } $status = (bool) $status || (bool) $exitcode; } } } else { if ($options['updatesteps']) { // Rewrite config file to ensure we have all the features covered. if (empty($options['parallel'])) { behat_config_manager::update_config_file('', true, '', $options['add-core-features-to-theme'], false, false); } else { // Update config file, ensuring we have up-to-date behat.yml. for ($i = $options['fromrun']; $i <= $options['torun']; $i++) { $CFG->behatrunprocess = $i; // Update config file for each run. behat_config_manager::update_config_file('', true, $options['optimize-runs'], $options['add-core-features-to-theme'], $options['parallel'], $i); } unset($CFG->behatrunprocess); } // Do it sequentially as it's fast and need to be displayed nicely. foreach (array_chunk($cmds, 1, true) as $cmd) { $processes = cli_execute_parallel($cmd, __DIR__); print_sequential_output($processes); } exit(0); } else { // We should never reach here. echo $help; exit(1); } }