MDL-37046 behat: Steps definitions loading
authorDavid Monllao <davidm@moodle.com>
Thu, 18 Oct 2012 03:05:31 +0000 (11:05 +0800)
committerDavid Monllao <davidm@moodle.com>
Tue, 29 Jan 2013 00:40:36 +0000 (08:40 +0800)
admin/tool/behat/locallib.php
lib/testing/classes/tests_finder.php

index 7bd00fe..e8bf9b1 100644 (file)
@@ -37,6 +37,8 @@ require_once($CFG->libdir . '/phpunit/classes/tests_finder.php');
  */
 class tool_behat {
 
+    private static $behat_tests_path = '/tests/behat';
+
     /**
      * Displays basic info about acceptance tests
      */
@@ -151,8 +153,6 @@ class tool_behat {
 
         self::update_config_file();
 
-        echo self::get_header();
-
         @set_time_limit(0);
 
         // Priority to the one specified as argument.
@@ -176,18 +176,28 @@ class tool_behat {
         self::enable_test_environment();
         $currentcwd = getcwd();
 
-        // Outputting runner form and tests results.
-        if (!CLI_SCRIPT) {
-            echo self::get_run_tests_form($tags);
-        }
-
         chdir($CFG->behatpath);
-        passthru('bin/behat ' . $tagsoption . ' ' .$extra, $code);
+        ob_start();
+        passthru('bin/behat --ansi ' . $tagsoption . ' ' .$extra, $code);
+        $output = ob_get_contents();
+        ob_end_clean();
 
-        // Switching back to regular environment
+        // Switching back to regular environment.
         self::disable_test_environment();
         chdir($currentcwd);
 
+        // Output.
+        echo self::get_header();
+        if (!CLI_SCRIPT) {
+            echo self::get_run_tests_form($tags);
+        }
+        echo $output;
+
+        // Dirty hack to avoid behat bad HTML structure when test execution throws an exception and there are skipped steps.
+        if (strstr($output, 'class="skipped"') != false) {
+            echo '</ol></div></div></div></body></html>';
+        }
+
         echo self::get_footer();
     }
 
@@ -197,25 +207,57 @@ class tool_behat {
      */
     private static function update_config_file() {
 
-        // Gets all the components with features
+        $contents = '';
+
+        // Gets all the components with features.
         $components = tests_finder::get_components_with_tests('features');
         if ($components) {
-            $contents = 'features:' . PHP_EOL;
+            $contents .= 'features:' . PHP_EOL;
             foreach ($components as $componentname => $path) {
+                $path = self::clean_path($path) . self::$behat_tests_path;
                 $contents .= '  - ' . $path . PHP_EOL;
             }
         }
 
-        // Gets all the components with steps definitions
-        // TODO
+        // Gets all the components with steps definitions.
+        $components = tests_finder::get_components_with_tests('stepsdefinitions');
+        if ($components) {
+            $contents .= 'steps_definitions:' . PHP_EOL;
+            foreach ($components as $componentname => $filepath) {
+                $filepath = self::clean_path($path) . self::$behat_tests_path . '/behat_' . $componentname . '.php';
+                $contents .= '  ' . $componentname . ': ' . $filepath . PHP_EOL;
+            }
+        }
 
-        // Stores the file
+        // Stores the file.
         $fullfilepath = self::get_behat_dir() . '/config.yml';
         if (!file_put_contents($fullfilepath, $contents)) {
             throw new file_exception('cannotcreatefile', $fullfilepath);
         }
     }
 
+
+    /**
+     * Cleans the path returned by get_components_with_tests() to standarize it
+     *
+     * {@see tests_finder::get_all_directories_with_tests()} it returns the path including /tests/
+     * @param string $path
+     * @return string The string without the last /tests part
+     */
+    private static function clean_path($path) {
+
+        $path = rtrim($path, '/');
+
+        $parttoremove = '/tests';
+
+        $substr = substr($path, strlen($path) - strlen($parttoremove));
+        if ($substr == $parttoremove) {
+            $path = substr($path, 0, strlen($path) - strlen($parttoremove));
+        }
+
+        return rtrim($path, '/');
+    }
+
     /**
      * Checks whether the test database and dataroot is ready
      * Stops execution if something went wrong
index 0a89cfa..9bc6bda 100644 (file)
@@ -181,7 +181,7 @@ class tests_finder {
                 $regexp = '|'.$sep.'tests'.$sep.'behat'.$sep.'.*\.feature$|';
                 break;
             case 'stepsdefinitions':
-                $regexp = '|'.$sep.'tests'.$sep.'behat'.$sep.'.*\.php$|';
+                $regexp = '|'.$sep.'tests'.$sep.'behat'.$sep.'behat_.*\.php$|';
                 break;
         }