Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.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\Definition\Cli;
  18  
  19  use Behat\Behat\Definition\DefinitionWriter;
  20  use Behat\Behat\Definition\Printer\ConsoleDefinitionListPrinter;
  21  use Behat\Testwork\Cli\Controller;
  22  use Behat\Testwork\Suite\SuiteRepository;
  23  use Moodle\BehatExtension\Definition\Printer\ConsoleDefinitionInformationPrinter;
  24  use Symfony\Component\Console\Command\Command;
  25  use Symfony\Component\Console\Input\InputInterface;
  26  use Symfony\Component\Console\Input\InputOption;
  27  use Symfony\Component\Console\Output\OutputInterface;
  28  
  29  /**
  30   * Available definition controller, for calling moodle information printer.
  31   *
  32   * @package    core
  33   * @copyright  2016 Rajesh Taneja <rajesh@moodle.com>
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  final class AvailableDefinitionsController implements Controller {
  37      /** @var SuiteRepository */
  38      private $suiterepository;
  39  
  40      /** @var DefinitionWriter */
  41      private $writer;
  42  
  43      /** @var ConsoleDefinitionListPrinter */
  44      private $listprinter;
  45  
  46      /** @var ConsoleDefinitionInformationPrinter */
  47      private $infoprinter;
  48  
  49      /**
  50       * Initializes controller.
  51       *
  52       * @param SuiteRepository                     $suiterepository
  53       * @param DefinitionWriter                    $writer
  54       * @param ConsoleDefinitionListPrinter        $listprinter
  55       * @param ConsoleDefinitionInformationPrinter $infoprinter
  56       */
  57      public function __construct(
  58          SuiteRepository $suiterepository,
  59          DefinitionWriter $writer,
  60          ConsoleDefinitionListPrinter $listprinter,
  61          ConsoleDefinitionInformationPrinter $infoprinter
  62      ) {
  63          $this->suiterepository = $suiterepository;
  64          $this->writer = $writer;
  65          $this->listprinter = $listprinter;
  66          $this->infoprinter = $infoprinter;
  67      }
  68  
  69      /**
  70       * Configures command to be executable by the controller.
  71       *
  72       * @param Command $command
  73       */
  74      public function configure(Command $command) {
  75          $command->addOption('--definitions', '-d', InputOption::VALUE_REQUIRED,
  76              "Print all available step definitions:" . PHP_EOL .
  77              "- use <info>--definitions l</info> to just list definition expressions." . PHP_EOL .
  78              "- use <info>--definitions i</info> to show definitions with extended info." . PHP_EOL .
  79              "- use <info>--definitions 'needle'</info> to find specific definitions." . PHP_EOL .
  80              "Use <info>--lang</info> to see definitions in specific language."
  81          );
  82      }
  83  
  84      /**
  85       * Executes controller.
  86       *
  87       * @param InputInterface  $input
  88       * @param OutputInterface $output
  89       *
  90       * @return null|integer
  91       */
  92      public function execute(InputInterface $input, OutputInterface $output) {
  93          if (null === $argument = $input->getOption('definitions')) {
  94              return null;
  95          }
  96  
  97          $printer = $this->getdefinitionPrinter($argument);
  98          foreach ($this->suiterepository->getSuites() as $suite) {
  99              $this->writer->printSuiteDefinitions($printer, $suite);
 100          }
 101  
 102          return 0;
 103      }
 104  
 105      /**
 106       * Returns definition printer for provided option argument.
 107       *
 108       * @param string $argument
 109       *
 110       * @return \Behat\Behat\Definition\Printer\DefinitionPrinter
 111       */
 112      private function getdefinitionprinter($argument) {
 113          if ('l' === $argument) {
 114              return $this->listprinter;
 115          }
 116  
 117          if ('i' !== $argument) {
 118              $this->infoprinter->setSearchCriterion($argument);
 119          }
 120  
 121          return $this->infoprinter;
 122      }
 123  }