MDL-37046 behat: Adding CLI options
authorDavid Monllao <davidm@moodle.com>
Mon, 15 Oct 2012 06:11:55 +0000 (14:11 +0800)
committerDavid Monllao <davidm@moodle.com>
Tue, 29 Jan 2013 00:40:36 +0000 (08:40 +0800)
admin/tool/behat/cli/util.php
admin/tool/behat/locallib.php

index 2583266..1600837 100644 (file)
@@ -31,10 +31,13 @@ require_once($CFG->dirroot . '/' . $CFG->admin . '/tool/behat/locallib.php');
 // 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'
@@ -47,14 +50,14 @@ Behat tool
 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'])) {
@@ -80,6 +83,20 @@ if (empty($action)) {
     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'));
index 7a717c4..ec3e8a0 100644 (file)
@@ -26,6 +26,15 @@ require_once($CFG->libdir . '/filestorage/file_exceptions.php');
 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 {
 
     /**
@@ -44,11 +53,11 @@ 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) {
@@ -56,7 +65,12 @@ class tool_behat {
         }
         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';
@@ -94,14 +108,20 @@ class tool_behat {
 
     /**
      * 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') {
@@ -145,8 +165,10 @@ class tool_behat {
 
     /**
      * 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) {
@@ -158,20 +180,34 @@ class tool_behat {
 
         @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();
@@ -301,7 +337,6 @@ class tool_behat {
         return false;
     }
 
-
     /**
      * Returns true if Moodle is currently running with the test database and dataroot
      * @return bool
@@ -513,5 +548,4 @@ class tool_behat {
 
         return $html;
     }
-
 }