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\Testwork\Exception\ServiceContainer\ExceptionExtension;
  20  use Behat\Testwork\Output\ServiceContainer\Formatter\FormatterFactory;
  21  use Behat\Testwork\Output\ServiceContainer\OutputExtension;
  22  use Behat\Testwork\ServiceContainer\ServiceProcessor;
  23  use Behat\Testwork\Translator\ServiceContainer\TranslatorExtension;
  24  use Symfony\Component\DependencyInjection\ContainerBuilder;
  25  use Symfony\Component\DependencyInjection\Definition;
  26  use Symfony\Component\DependencyInjection\Reference;
  27  
  28  // phpcs:disable moodle.NamingConventions.ValidFunctionName.LowercaseMethod
  29  
  30  /**
  31   * Moodle behat context class resolver.
  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  class MoodleProgressFormatterFactory implements FormatterFactory {
  38      /** @var ServiceProcessor */
  39      private $processor;
  40  
  41      /** @var string moodle progress ID */
  42      const ROOT_LISTENER_ID_MOODLE = 'output.node.listener.moodleprogress';
  43  
  44      /** @var string moodle printer ID */
  45      const RESULT_TO_STRING_CONVERTER_ID_MOODLE = 'output.node.printer.result_to_string';
  46  
  47      /** @var string Available extension points */
  48      const ROOT_LISTENER_WRAPPER_TAG_MOODLE = 'output.node.listener.moodleprogress.wrapper';
  49  
  50      /**
  51       * Initializes extension.
  52       *
  53       * @param null|ServiceProcessor $processor
  54       */
  55      public function __construct(ServiceProcessor $processor = null) {
  56          $this->processor = $processor ? : new ServiceProcessor();
  57      }
  58  
  59      /**
  60       * Builds formatter configuration.
  61       *
  62       * @param ContainerBuilder $container
  63       */
  64      public function buildFormatter(ContainerBuilder $container) {
  65          $this->loadRootNodeListener($container);
  66          $this->loadCorePrinters($container);
  67          $this->loadPrinterHelpers($container);
  68          $this->loadFormatter($container);
  69      }
  70  
  71      /**
  72       * Processes formatter configuration.
  73       *
  74       * @param ContainerBuilder $container
  75       */
  76      public function processFormatter(ContainerBuilder $container) {
  77          $this->processListenerWrappers($container);
  78      }
  79  
  80      /**
  81       * Loads progress formatter node event listener.
  82       *
  83       * @param ContainerBuilder $container
  84       */
  85      protected function loadRootNodeListener(ContainerBuilder $container) {
  86          $definition = new Definition('Behat\Behat\Output\Node\EventListener\AST\StepListener', [
  87              new Reference('output.node.printer.moodleprogress.step')
  88          ]);
  89          $container->setDefinition(self::ROOT_LISTENER_ID_MOODLE, $definition);
  90      }
  91  
  92      /**
  93       * Loads formatter itself.
  94       *
  95       * @param ContainerBuilder $container
  96       */
  97      protected function loadFormatter(ContainerBuilder $container) {
  98  
  99          $definition = new Definition('Behat\Behat\Output\Statistics\TotalStatistics');
 100          $container->setDefinition('output.moodleprogress.statistics', $definition);
 101  
 102          $moodleconfig = $container->getParameter('behat.moodle.parameters');
 103  
 104          $definition = new Definition(
 105              'Moodle\BehatExtension\Output\Printer\MoodleProgressPrinter',
 106              [$moodleconfig['moodledirroot']]
 107          );
 108          $container->setDefinition('moodle.output.node.printer.moodleprogress.printer', $definition);
 109  
 110          $definition = new Definition('Behat\Testwork\Output\NodeEventListeningFormatter', [
 111              'moodle_progress',
 112              'Prints information about then run followed by one character per step.',
 113              [
 114                  'timer' => true
 115              ],
 116              $this->createOutputPrinterDefinition(),
 117              new Definition('Behat\Testwork\Output\Node\EventListener\ChainEventListener', [
 118                  [
 119                      new Reference(self::ROOT_LISTENER_ID_MOODLE),
 120                      new Definition('Behat\Behat\Output\Node\EventListener\Statistics\StatisticsListener', [
 121                          new Reference('output.moodleprogress.statistics'),
 122                          new Reference('output.node.printer.moodleprogress.statistics')
 123                      ]),
 124                      new Definition('Behat\Behat\Output\Node\EventListener\Statistics\ScenarioStatsListener', [
 125                          new Reference('output.moodleprogress.statistics')
 126                      ]),
 127                      new Definition('Behat\Behat\Output\Node\EventListener\Statistics\StepStatsListener', [
 128                          new Reference('output.moodleprogress.statistics'),
 129                          new Reference(ExceptionExtension::PRESENTER_ID)
 130                      ]),
 131                      new Definition('Behat\Behat\Output\Node\EventListener\Statistics\HookStatsListener', [
 132                          new Reference('output.moodleprogress.statistics'),
 133                          new Reference(ExceptionExtension::PRESENTER_ID)
 134                      ]),
 135                      new Definition('Behat\Behat\Output\Node\EventListener\AST\SuiteListener', [
 136                          new Reference('moodle.output.node.printer.moodleprogress.printer')
 137                      ])
 138                  ]
 139              ])
 140          ]);
 141          $definition->addTag(OutputExtension::FORMATTER_TAG, ['priority' => 1]);
 142          $container->setDefinition(OutputExtension::FORMATTER_TAG . '.moodleprogress', $definition);
 143      }
 144  
 145      /**
 146       * Loads printer helpers.
 147       *
 148       * @param ContainerBuilder $container
 149       */
 150      protected function loadPrinterHelpers(ContainerBuilder $container) {
 151          $definition = new Definition('Behat\Behat\Output\Node\Printer\Helper\ResultToStringConverter');
 152          $container->setDefinition(self::RESULT_TO_STRING_CONVERTER_ID_MOODLE, $definition);
 153      }
 154  
 155      /**
 156       * Loads feature, scenario and step printers.
 157       *
 158       * @param ContainerBuilder $container
 159       */
 160      protected function loadCorePrinters(ContainerBuilder $container) {
 161          $definition = new Definition('Behat\Behat\Output\Node\Printer\CounterPrinter', [
 162              new Reference(self::RESULT_TO_STRING_CONVERTER_ID_MOODLE),
 163              new Reference(TranslatorExtension::TRANSLATOR_ID),
 164          ]);
 165          $container->setDefinition('output.node.moodle.printer.counter', $definition);
 166  
 167          $definition = new Definition('Behat\Behat\Output\Node\Printer\ListPrinter', [
 168              new Reference(self::RESULT_TO_STRING_CONVERTER_ID_MOODLE),
 169              new Reference(ExceptionExtension::PRESENTER_ID),
 170              new Reference(TranslatorExtension::TRANSLATOR_ID),
 171              '%paths.base%'
 172          ]);
 173          $container->setDefinition('output.node.moodle.printer.list', $definition);
 174  
 175          $definition = new Definition('Behat\Behat\Output\Node\Printer\Progress\ProgressStepPrinter', [
 176              new Reference(self::RESULT_TO_STRING_CONVERTER_ID_MOODLE)
 177          ]);
 178          $container->setDefinition('output.node.printer.moodleprogress.step', $definition);
 179  
 180          $definition = new Definition('Behat\Behat\Output\Node\Printer\Progress\ProgressStatisticsPrinter', [
 181              new Reference('output.node.moodle.printer.counter'),
 182              new Reference('output.node.moodle.printer.list')
 183          ]);
 184          $container->setDefinition('output.node.printer.moodleprogress.statistics', $definition);
 185      }
 186  
 187      /**
 188       * Creates output printer definition.
 189       *
 190       * @return Definition
 191       */
 192      protected function createOutputPrinterDefinition() {
 193          return new Definition('Behat\Testwork\Output\Printer\StreamOutputPrinter', [
 194              new Definition('Behat\Behat\Output\Printer\ConsoleOutputFactory'),
 195          ]);
 196      }
 197  
 198      /**
 199       * Processes all registered pretty formatter node listener wrappers.
 200       *
 201       * @param ContainerBuilder $container
 202       */
 203      protected function processListenerWrappers(ContainerBuilder $container) {
 204          $this->processor->processWrapperServices(
 205              $container,
 206              self::ROOT_LISTENER_ID_MOODLE,
 207              self::ROOT_LISTENER_WRAPPER_TAG_MOODLE
 208          );
 209      }
 210  }