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\Context\ContextClass;
  18  
  19  use Behat\Behat\Context\ContextClass\ClassResolver as Resolver;
  20  
  21  // phpcs:disable moodle.NamingConventions.ValidFunctionName.LowercaseMethod
  22  
  23  /**
  24   * Moodle behat context class resolver.
  25   *
  26   * Resolves arbitrary context strings into a context classes.
  27   *
  28   * @see ContextEnvironmentHandler
  29   *
  30   * @package    core
  31   * @copyright  2104 Rajesh Taneja <rajesh@moodle.com>
  32   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  33   */
  34  final class ClassResolver implements Resolver {
  35  
  36      /** @var array keep list of all behat contexts in moodle. */
  37      private $moodlebehatcontexts = null;
  38  
  39      /**
  40       * Constructor for ClassResolver class.
  41       *
  42       * @param array $parameters list of params provided to moodle.
  43       */
  44      public function __construct($parameters) {
  45          $this->moodlebehatcontexts = $parameters['steps_definitions'];
  46      }
  47      /**
  48       * Checks if resolvers supports provided class.
  49       * Moodle behat context class starts with behat_
  50       *
  51       * @param string $contextstring
  52       * @return Boolean
  53       */
  54      public function supportsClass($contextstring) {
  55          return (strpos($contextstring, 'behat_') === 0);
  56      }
  57  
  58      /**
  59       * Resolves context class.
  60       *
  61       * @param string $contextclass
  62       * @return string context class.
  63       */
  64      public function resolveClass($contextclass) {
  65          if (!is_array($this->moodlebehatcontexts)) {
  66              throw new \RuntimeException('There are no Moodle context with steps definitions');
  67          }
  68  
  69          // Using the key as context identifier load context class.
  70          if (
  71              !empty($this->moodlebehatcontexts[$contextclass]) &&
  72              (file_exists($this->moodlebehatcontexts[$contextclass]))
  73          ) {
  74              require_once($this->moodlebehatcontexts[$contextclass]);
  75          } else {
  76              throw new \RuntimeException('Moodle behat context "' . $contextclass . '" not found');
  77          }
  78          return $contextclass;
  79      }
  80  }