[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/behat/classes/ -> behat_command.php (source)

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Behat command utils
  19   *
  20   * @package    core
  21   * @category   test
  22   * @copyright  2012 David Monllaó
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  require_once (__DIR__ . '/../lib.php');
  29  
  30  /**
  31   * Behat command related utils
  32   *
  33   * @package    core
  34   * @category   test
  35   * @copyright  2013 David Monllaó
  36   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class behat_command {
  39  
  40      /**
  41       * Docs url
  42       */
  43      const DOCS_URL = 'http://docs.moodle.org/dev/Acceptance_testing';
  44  
  45      /**
  46       * Ensures the behat dir exists in moodledata
  47       * @param int $runprocess run process for which behat dir is returned.
  48       * @return string Full path
  49       */
  50      public static function get_behat_dir($runprocess = 0) {
  51          global $CFG;
  52  
  53          // If not set then return empty string.
  54          if (!isset($CFG->behat_dataroot)) {
  55              return "";
  56          }
  57  
  58          if (empty($runprocess)) {
  59              $behatdir = $CFG->behat_dataroot . '/behat';
  60          } else if (isset($CFG->behat_parallel_run[$runprocess - 1]['behat_dataroot'])) {
  61              $behatdir = $CFG->behat_parallel_run[$runprocess - 1]['behat_dataroot'] . '/behat';;
  62          } else {
  63              $behatdir = $CFG->behat_dataroot . $runprocess . '/behat';
  64          }
  65  
  66          if (!is_dir($behatdir)) {
  67              if (!mkdir($behatdir, $CFG->directorypermissions, true)) {
  68                  behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Directory ' . $behatdir . ' can not be created');
  69              }
  70          }
  71  
  72          if (!is_writable($behatdir)) {
  73              behat_error(BEHAT_EXITCODE_PERMISSIONS, 'Directory ' . $behatdir . ' is not writable');
  74          }
  75  
  76          return $behatdir;
  77      }
  78  
  79      /**
  80       * Returns the executable path
  81       *
  82       * Allows returning a customized command for cygwin when the
  83       * command is just displayed, when using exec(), system() and
  84       * friends we stay with DIRECTORY_SEPARATOR as they use the
  85       * normal cmd.exe (in Windows).
  86       *
  87       * @param  bool $custombyterm  If the provided command should depend on the terminal where it runs
  88       * @param bool $parallelrun If parallel run is installed.
  89       * @param bool $absolutepath return command with absolute path.
  90       * @return string
  91       */
  92      public final static function get_behat_command($custombyterm = false, $parallerun = false, $absolutepath = false) {
  93  
  94          $separator = DIRECTORY_SEPARATOR;
  95          $exec = 'behat';
  96  
  97          // Cygwin uses linux-style directory separators.
  98          if ($custombyterm && testing_is_cygwin()) {
  99              $separator = '/';
 100  
 101              // MinGW can not execute .bat scripts.
 102              if (!testing_is_mingw()) {
 103                  $exec = 'behat.bat';
 104              }
 105          }
 106  
 107          // If relative path then prefix relative path.
 108          if ($absolutepath) {
 109              $pathprefix = testing_cli_argument_path('/') . $separator;
 110          } else {
 111              $pathprefix = '';
 112          }
 113  
 114          if (!$parallerun) {
 115              $command = $pathprefix . 'vendor' . $separator . 'bin' . $separator . $exec;
 116          } else {
 117              $command = 'php ' . $pathprefix . 'admin' . $separator . 'tool' . $separator . 'behat' . $separator . 'cli'
 118                  . $separator . 'run.php';
 119          }
 120          return $command;
 121      }
 122  
 123      /**
 124       * Runs behat command with provided options
 125       *
 126       * Execution continues when the process finishes
 127       *
 128       * @param  string $options  Defaults to '' so tests would be executed
 129       * @return array            CLI command outputs [0] => string, [1] => integer
 130       */
 131      public final static function run($options = '') {
 132          global $CFG;
 133  
 134          $currentcwd = getcwd();
 135          chdir($CFG->dirroot);
 136          exec(self::get_behat_command() . ' ' . $options, $output, $code);
 137          chdir($currentcwd);
 138  
 139          return array($output, $code);
 140      }
 141  
 142      /**
 143       * Checks if behat is set up and working
 144       *
 145       * Notifies failures both from CLI and web interface.
 146       *
 147       * It checks behat dependencies have been installed and runs
 148       * the behat help command to ensure it works as expected
 149       *
 150       * @return int Error code or 0 if all ok
 151       */
 152      public static function behat_setup_problem() {
 153          global $CFG;
 154  
 155          // Moodle setting.
 156          if (!self::are_behat_dependencies_installed()) {
 157  
 158              // Returning composer error code to avoid conflicts with behat and moodle error codes.
 159              self::output_msg(get_string('errorcomposer', 'tool_behat'));
 160              return TESTING_EXITCODE_COMPOSER;
 161          }
 162  
 163          // Behat test command.
 164          list($output, $code) = self::run(' --help');
 165  
 166          if ($code != 0) {
 167  
 168              // Returning composer error code to avoid conflicts with behat and moodle error codes.
 169              self::output_msg(get_string('errorbehatcommand', 'tool_behat', self::get_behat_command()));
 170              return TESTING_EXITCODE_COMPOSER;
 171          }
 172  
 173          // No empty values.
 174          if (empty($CFG->behat_dataroot) || empty($CFG->behat_prefix) || empty($CFG->behat_wwwroot)) {
 175              self::output_msg(get_string('errorsetconfig', 'tool_behat'));
 176              return BEHAT_EXITCODE_CONFIG;
 177  
 178          }
 179  
 180          // Not repeated values.
 181          // We only need to check this when the behat site is not running as
 182          // at this point, when it is running, all $CFG->behat_* vars have
 183          // already been copied to $CFG->dataroot, $CFG->prefix and $CFG->wwwroot.
 184          if (!defined('BEHAT_SITE_RUNNING') &&
 185                  ($CFG->behat_prefix == $CFG->prefix ||
 186                  $CFG->behat_dataroot == $CFG->dataroot ||
 187                  $CFG->behat_wwwroot == $CFG->wwwroot ||
 188                  (!empty($CFG->phpunit_prefix) && $CFG->phpunit_prefix == $CFG->behat_prefix) ||
 189                  (!empty($CFG->phpunit_dataroot) && $CFG->phpunit_dataroot == $CFG->behat_dataroot)
 190                  )) {
 191              self::output_msg(get_string('erroruniqueconfig', 'tool_behat'));
 192              return BEHAT_EXITCODE_CONFIG;
 193          }
 194  
 195          // Checking behat dataroot existence otherwise echo about admin/tool/behat/cli/init.php.
 196          if (!empty($CFG->behat_dataroot)) {
 197              $CFG->behat_dataroot = realpath($CFG->behat_dataroot);
 198          }
 199          if (empty($CFG->behat_dataroot) || !is_dir($CFG->behat_dataroot) || !is_writable($CFG->behat_dataroot)) {
 200              self::output_msg(get_string('errordataroot', 'tool_behat'));
 201              return BEHAT_EXITCODE_CONFIG;
 202          }
 203  
 204          return 0;
 205      }
 206  
 207      /**
 208       * Has the site installed composer.
 209       * @return bool
 210       */
 211      public static function are_behat_dependencies_installed() {
 212          if (!is_dir(__DIR__ . '/../../../vendor/behat')) {
 213              return false;
 214          }
 215          return true;
 216      }
 217  
 218      /**
 219       * Outputs a message.
 220       *
 221       * Used in CLI + web UI methods. Stops the
 222       * execution in web.
 223       *
 224       * @param string $msg
 225       * @return void
 226       */
 227      protected static function output_msg($msg) {
 228          global $CFG, $PAGE;
 229  
 230          // If we are using the web interface we want pretty messages.
 231          if (!CLI_SCRIPT) {
 232  
 233              $renderer = $PAGE->get_renderer('tool_behat');
 234              echo $renderer->render_error($msg);
 235  
 236              // Stopping execution.
 237              exit(1);
 238  
 239          } else {
 240  
 241              // We continue execution after this.
 242              $clibehaterrorstr = "Ensure you set \$CFG->behat_* vars in config.php " .
 243                  "and you ran admin/tool/behat/cli/init.php.\n" .
 244                  "More info in " . self::DOCS_URL . "#Installation\n\n";
 245  
 246              echo 'Error: ' . $msg . "\n\n" . $clibehaterrorstr;
 247          }
 248      }
 249  
 250  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1