Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   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  namespace Moodle\BehatExtension\Output\Printer;
  18  
  19  use Behat\Behat\Output\Node\Printer\SetupPrinter;
  20  use Behat\Testwork\Call\CallResult;
  21  use Behat\Testwork\Hook\Tester\Setup\HookedTeardown;
  22  use Behat\Testwork\Output\Formatter;
  23  use Behat\Testwork\Output\Printer\OutputPrinter;
  24  use Behat\Testwork\Tester\Setup\Setup;
  25  use Behat\Testwork\Tester\Setup\Teardown;
  26  use Moodle\BehatExtension\Driver\WebDriver;
  27  
  28  // phpcs:disable moodle.NamingConventions.ValidFunctionName.LowercaseMethod
  29  
  30  /**
  31   * Prints hooks in a pretty fashion.
  32   *
  33   * @package    core
  34   * @copyright  2016 Rajesh Taneja <rajesh@moodle.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  final class MoodleProgressPrinter implements SetupPrinter {
  38  
  39      /**
  40       * @var string Moodle directory root.
  41       */
  42      private $moodledirroot;
  43  
  44      /**
  45       * @var bool true if output is displayed.
  46       */
  47      private static $outputdisplayed;
  48  
  49      /**
  50       * Constructor.
  51       *
  52       * @param string $moodledirroot Moodle dir root.
  53       */
  54      public function __construct($moodledirroot) {
  55          $this->moodledirroot = $moodledirroot;
  56      }
  57  
  58      /**
  59       * Prints setup state.
  60       *
  61       * @param Formatter $formatter
  62       * @param Setup     $setup
  63       */
  64      public function printSetup(Formatter $formatter, Setup $setup) {
  65          if (empty(self::$outputdisplayed)) {
  66              $this->printMoodleInfo($formatter->getOutputPrinter());
  67              self::$outputdisplayed = true;
  68          }
  69      }
  70  
  71      /**
  72       * Prints teardown state.
  73       *
  74       * @param Formatter $formatter
  75       * @param Teardown  $teardown
  76       */
  77      public function printTeardown(Formatter $formatter, Teardown $teardown) {
  78          if (!$teardown instanceof HookedTeardown) {
  79              return;
  80          }
  81  
  82          foreach ($teardown->getHookCallResults() as $callresult) {
  83              $this->printTeardownHookCallResult($formatter->getOutputPrinter(), $callresult);
  84          }
  85      }
  86  
  87      /**
  88       * We print the site info + driver used and OS.
  89       *
  90       * @param Printer $printer
  91       * @return void
  92       */
  93      public function printMoodleInfo($printer) {
  94          require_once($this->moodledirroot . '/lib/behat/classes/util.php');
  95  
  96          $browser = WebDriver::getBrowserName();
  97  
  98          // Calling all directly from here as we avoid more behat framework extensions.
  99          $runinfo = \behat_util::get_site_info();
 100          $runinfo .= 'Server OS "' . PHP_OS . '"' . ', Browser: "' . $browser . '"' . PHP_EOL;
 101          $runinfo .= 'Started at ' . date('d-m-Y, H:i', time());
 102  
 103          $printer->writeln($runinfo);
 104      }
 105  
 106      /**
 107       * Prints teardown hook call result.
 108       *
 109       * @param OutputPrinter $printer
 110       * @param CallResult    $callresult
 111       */
 112      private function printTeardownHookCallResult(OutputPrinter $printer, CallResult $callresult) {
 113          // Notify dev that chained step is being used.
 114          if (\Moodle\BehatExtension\EventDispatcher\Tester\ChainedStepTester::is_chained_step_used()) {
 115              $printer->writeln();
 116              $printer->write(
 117                  "{+failed}Chained steps are deprecated. " .
 118                  "See https://moodledev.io/general/development/tools/behat/" .
 119                  "Migrating_from_Behat_2.5_to_3.x_in_Moodle#Changes_required_in_context_file{-failed}"
 120              );
 121          }
 122  
 123          if (!$callresult->hasStdOut() && !$callresult->hasException()) {
 124              return;
 125          }
 126  
 127          $hook = $callresult->getCall()->getCallee();
 128          $path = $hook->getPath();
 129  
 130          $printer->writeln($hook);
 131          $printer->writeln($path);
 132      }
 133  }