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.
   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\Formatter;
  18  
  19  use Behat\Behat\EventDispatcher\Event\AfterFeatureTested;
  20  use Behat\Behat\EventDispatcher\Event\AfterStepTested;
  21  use Behat\Behat\EventDispatcher\Event\BeforeFeatureTested;
  22  use Behat\Testwork\Output\Formatter;
  23  use Behat\Testwork\Output\Printer\OutputPrinter;
  24  
  25  // phpcs:disable moodle.NamingConventions.ValidFunctionName.LowercaseMethod
  26  
  27  /**
  28   * Feature step counter for distributing features between parallel runs.
  29   *
  30   * Use it with --dry-run (and any other selectors combination) to
  31   * get the results quickly.
  32   *
  33   * @package core
  34   * @copyright  2016 onwards Rajesh Taneja
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class MoodleStepcountFormatter implements Formatter {
  38  
  39      /** @var int Number of steps executed in feature file. */
  40      private static $stepcount = 0;
  41  
  42      /** @var OutputPrinter */
  43      private $printer;
  44  
  45      /** @var array */
  46      private $parameters;
  47  
  48      /** @var string */
  49      private $name;
  50  
  51      /** @var string */
  52      private $description;
  53  
  54      /**
  55       * Initializes formatter.
  56       *
  57       * @param string        $name
  58       * @param string        $description
  59       * @param array         $parameters
  60       * @param OutputPrinter $printer
  61       */
  62      public function __construct($name, $description, array $parameters, OutputPrinter $printer) {
  63          $this->name = $name;
  64          $this->description = $description;
  65          $this->parameters = $parameters;
  66          $this->printer = $printer;
  67      }
  68  
  69      /**
  70       * Returns an array of event names this subscriber wants to listen to.
  71       *
  72       * @return array The event names to listen to
  73       */
  74      public static function getSubscribedEvents() {
  75          return [
  76              'tester.feature_tested.before'     => 'beforeFeature',
  77              'tester.feature_tested.after'      => 'afterFeature',
  78              'tester.step_tested.after'         => 'afterStep',
  79          ];
  80      }
  81  
  82      /**
  83       * Returns formatter name.
  84       *
  85       * @return string
  86       */
  87      public function getName() {
  88          return $this->name;
  89      }
  90  
  91      /**
  92       * Returns formatter description.
  93       *
  94       * @return string
  95       */
  96      public function getDescription() {
  97          return $this->description;
  98      }
  99  
 100      /**
 101       * Returns formatter output printer.
 102       *
 103       * @return OutputPrinter
 104       */
 105      public function getOutputPrinter() {
 106          return $this->printer;
 107      }
 108  
 109      /**
 110       * Sets formatter parameter.
 111       *
 112       * @param string $name
 113       * @param mixed  $value
 114       */
 115      public function setParameter($name, $value) {
 116          $this->parameters[$name] = $value;
 117      }
 118  
 119      /**
 120       * Returns parameter name.
 121       *
 122       * @param string $name
 123       * @return mixed
 124       */
 125      public function getParameter($name) {
 126          return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
 127      }
 128  
 129      /**
 130       * Listens to "feature.before" event.
 131       *
 132       * @param BeforeFeatureTested $event
 133       */
 134      public function beforeFeature(BeforeFeatureTested $event) {
 135          self::$stepcount = 0;
 136      }
 137  
 138      /**
 139       * Listens to "feature.after" event.
 140       *
 141       * @param AfterFeatureTested $event
 142       */
 143      public function afterFeature(AfterFeatureTested $event) {
 144          $this->printer->writeln($event->getFeature()->getFile() . '::' . self::$stepcount);
 145      }
 146  
 147      /**
 148       * Listens to "step.after" event.
 149       *
 150       * @param AfterStepTested $event
 151       */
 152      public function afterStep(AfterStepTested $event) {
 153          self::$stepcount++;
 154      }
 155  }