// now get cli options
list($options, $unrecognized) = cli_get_params(
array(
- 'help ' => false,
+ 'help' => false,
'stepsdefinitions' => false,
'buildconfigfile' => false,
- 'runtests' => false
+ 'runtests' => false,
+ 'filter' => false,
+ 'tags' => false,
+ 'extra' => false,
),
array(
'h' => 'help'
Ensure the user who executes the action has permissions over behat installation
Options:
---stepsdefinitions Displays the available steps definitions
+--stepsdefinitions Displays the available steps definitions (accepts --filter=\"\" option to restrict the list to the matching definitions)
--buildconfigfile Updates the Moodle components config file
---runtests Runs the tests
+--runtests Runs the tests (accepts --tags=\"\" option to execute only the matching tests and --extra=\"\" to specify extra behat options)
-h, --help Print out this help
Example from Moodle root directory:
-\$ php admin/tool/behat/cli/util --stepsdefinitions
+\$ php admin/tool/behat/cli/util.php --runtests --tags=\"tool_behat\" --extra=\"--format junit --out /path/report.html\"
";
if (!empty($options['help'])) {
exit(0);
}
-call_user_func('tool_behat::' . $action);
+switch ($action) {
+
+ case 'stepsdefinitions':
+ tool_behat::stepsdefinitions($options['filter']);
+ break;
+
+ case 'runtests':
+ tool_behat::runtests($options['tags'], $options['extra']);
+ break;
+
+ case 'buildconfigfile':
+ tool_behat::buildconfigfile();
+ break;
+}
+
mtrace(get_string('finished', 'tool_behat'));
require_once($CFG->libdir . '/phpunit/bootstraplib.php');
require_once($CFG->libdir . '/phpunit/classes/tests_finder.php');
+/**
+ * Behat commands manager
+ *
+ * CLI + web execution
+ *
+ * @package tool_behat
+ * @copyright 2012 David Monllaó
+ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
+ */
class tool_behat {
/**
echo $html;
}
-
/**
* Lists the available steps definitions
+ * @param string $filter Keyword to filter the list of steps definitions availables
*/
- public static function stepsdefinitions() {
+ public static function stepsdefinitions($filter = false) {
global $CFG;
if (!CLI_SCRIPT) {
}
self::check_behat_setup();
- if ($filter = optional_param('filter', false, PARAM_ALPHANUMEXT)) {
+ // Priority to the one specified as argument.
+ if (!$filter) {
+ $filter = optional_param('filter', false, PARAM_ALPHANUMEXT);
+ }
+
+ if ($filter) {
$filteroption = ' -d ' . $filter;
} else {
$filteroption = ' -di';
/**
* Switches from and to the regular environment to the testing environment
+ * @param string $testenvironment enable|disable
*/
- public static function switchenvironment() {
+ public static function switchenvironment($testenvironment = false) {
global $CFG;
if (!CLI_SCRIPT) {
confirm_sesskey();
}
- $testenvironment = optional_param('testenvironment', 'enable', PARAM_ALPHA);
+
+ // Priority to the one specified as argument.
+ if (!$testenvironment) {
+ $testenvironment = optional_param('testenvironment', 'enable', PARAM_ALPHA);
+ }
+
if ($testenvironment == 'enable') {
self::enable_test_environment();
} else if ($testenvironment == 'disable') {
/**
* Runs the acceptance tests
+ * @param string $tags Restricts the executed tests to the ones that matches the tags
+ * @param string $extra Extra CLI behat options
*/
- public static function runtests() {
+ public static function runtests($tags = false, $extra = false) {
global $CFG;
if (!CLI_SCRIPT) {
@set_time_limit(0);
+ // Priority to the one specified as argument.
+ if (!$tags) {
+ $tags = optional_param('tags', false, PARAM_ALPHANUMEXT);
+ }
+ // $extra only passed as CLI option (to simplify web runner usage).
+
$tagsoption = '';
- if ($tags = optional_param('tags', false, PARAM_ALPHANUMEXT)) {
+ if ($tags) {
$tagsoption = ' --tags ' . $tags;
}
+ if (!$extra && !CLI_SCRIPT) {
+ $extra = ' --format html';
+ } else if(!$extra && CLI_SCRIPT) {
+ $extra = '';
+ }
+
// Switching database and dataroot to test environment.
self::enable_test_environment();
$currentcwd = getcwd();
// Outputting runner form and tests results.
- echo self::get_run_tests_form($tags);
+ if (!CLI_SCRIPT) {
+ echo self::get_run_tests_form($tags);
+ }
chdir($CFG->behatpath);
- passthru('bin/behat --format html' . $tagsoption, $code);
+ passthru('bin/behat ' . $tagsoption . ' ' .$extra, $code);
// Switching back to regular environment
self::disable_test_environment();
return false;
}
-
/**
* Returns true if Moodle is currently running with the test database and dataroot
* @return bool
return $html;
}
-
}