Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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\Printer;
  18  
  19  use Behat\Behat\Definition\Printer\ConsoleDefinitionPrinter;
  20  use Behat\Testwork\Suite\Suite;
  21  
  22  // phpcs:disable moodle.NamingConventions.ValidFunctionName.LowercaseMethod
  23  
  24  /**
  25   * Moodle console definition information printer.
  26   *
  27   * Used in moodle for definition printing.
  28   *
  29   * @package    core
  30   * @copyright  2016 Rajesh Taneja <rajesh@moodle.com>
  31   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  32   */
  33  final class ConsoleDefinitionInformationPrinter extends ConsoleDefinitionPrinter {
  34      /** @var null|string */
  35      private $searchcriterion;
  36  
  37      /**
  38       * Sets search criterion.
  39       *
  40       * @param string $criterion
  41       */
  42      public function setSearchCriterion($criterion) {
  43          $this->searchcriterion = $criterion;
  44      }
  45  
  46      /**
  47       * Prints definition.
  48       *
  49       * @param Suite        $suite
  50       * @param Definition[] $definitions
  51       */
  52      public function printDefinitions(Suite $suite, $definitions) {
  53          $template = <<<TPL
  54  <div class="step"><div class="stepdescription">{description}</div>
  55  <div class="stepcontent"><span class="steptype">{type}</span><span class="stepregex">{regex}</span></div>
  56  <div class="stepapipath">{apipath}</div>
  57  </div>
  58  TPL;
  59  
  60          $search = $this->searchcriterion;
  61  
  62          // If there is a specific type (given, when or then) required.
  63          if (strpos($search, '&&') !== false) {
  64              list($search, $type) = explode('&&', $search);
  65          }
  66  
  67          foreach ($definitions as $definition) {
  68              $definition = $this->translateDefinition($suite, $definition);
  69  
  70              if (!empty($type) && strtolower($definition->getType()) != $type) {
  71                  continue;
  72              }
  73  
  74              $pattern = $definition->getPattern();
  75  
  76              if ($search && !preg_match('/' . str_replace(' ', '.*', preg_quote($search, '/') . '/'), $pattern)) {
  77                  continue;
  78              }
  79  
  80              $description = $definition->getDescription();
  81  
  82              // Removing beginning and end.
  83              $pattern = substr($pattern, 2, strlen($pattern) - 4);
  84  
  85              // Replacing inline regex for expected info string.
  86              $pattern = preg_replace_callback(
  87                  '/"\(\?P<([^>]*)>(.*?)"( |$)/',
  88                  function ($matches) {
  89                      return '"' . strtoupper($matches[1]) . '" ';
  90                  },
  91                  $pattern
  92              );
  93  
  94              $definitiontoprint[] = strtr($template, [
  95                  '{regex}'       => $pattern,
  96                  '{type}'        => str_pad($definition->getType(), 5, ' ', STR_PAD_LEFT),
  97                  '{description}' => $description ? $description : '',
  98                  '{apipath}'     => $definition->getPath()
  99              ]);
 100  
 101              $this->write(implode("\n", $definitiontoprint));
 102              unset($definitiontoprint);
 103          }
 104      }
 105  }