Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are 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\EventDispatcher\Tester;
  18  
  19  // phpcs:disable moodle.NamingConventions.ValidFunctionName.LowercaseMethod
  20  
  21  use Behat\Behat\EventDispatcher\Event\AfterStepSetup;
  22  use Behat\Behat\EventDispatcher\Event\AfterStepTested;
  23  use Behat\Behat\EventDispatcher\Event\BeforeStepTeardown;
  24  use Behat\Behat\EventDispatcher\Event\BeforeStepTested;
  25  use Behat\Behat\Tester\Result\StepResult;
  26  use Behat\Behat\Tester\StepTester;
  27  use Behat\Gherkin\Node\FeatureNode;
  28  use Behat\Gherkin\Node\StepNode;
  29  use Behat\Testwork\Environment\Environment;
  30  use Behat\Testwork\EventDispatcher\TestworkEventDispatcher;
  31  use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  32  
  33  /**
  34   * Step tester dispatching BEFORE/AFTER events during tests.
  35   *
  36   * @package    core
  37   * @copyright  2016 Rajesh Taneja <rajesh@moodle.com>
  38   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  final class MoodleEventDispatchingStepTester implements StepTester {
  41  
  42      /** @var StepTester */
  43      private $basetester;
  44  
  45      /** @var EventDispatcherInterface */
  46      private $eventdispatcher;
  47  
  48      /**
  49       * Initializes tester.
  50       *
  51       * @param StepTester               $basetester
  52       * @param EventDispatcherInterface $eventdispatcher
  53       */
  54      public function __construct(StepTester $basetester, EventDispatcherInterface $eventdispatcher) {
  55          $this->basetester = $basetester;
  56          $this->eventdispatcher = $eventdispatcher;
  57      }
  58  
  59      /**
  60       * Sets up step for a test.
  61       *
  62       * @param Environment $env
  63       * @param FeatureNode $feature
  64       * @param StepNode    $step
  65       * @param bool     $skip
  66       *
  67       * @return Setup
  68       */
  69      public function setUp(Environment $env, FeatureNode $feature, StepNode $step, $skip) {
  70          $event = new BeforeStepTested($env, $feature, $step);
  71          if (TestworkEventDispatcher::DISPATCHER_VERSION === 2) {
  72              // Symfony 4.3 and up.
  73              $this->eventdispatcher->dispatch($event, $event::BEFORE);
  74          } else {
  75              // TODO: Remove when our min supported version is >= 4.3.
  76              $this->eventdispatcher->dispatch($event::BEFORE, $event);
  77          }
  78  
  79          $setup = $this->basetester->setUp($env, $feature, $step, $skip);
  80          $this->basetester->setEventDispatcher($this->eventdispatcher);
  81  
  82          $event = new AfterStepSetup($env, $feature, $step, $setup);
  83          if (TestworkEventDispatcher::DISPATCHER_VERSION === 2) {
  84              // Symfony 4.3 and up.
  85              $this->eventdispatcher->dispatch($event, $event::AFTER_SETUP);
  86          } else {
  87              // TODO: Remove when our min supported version is >= 4.3.
  88              $this->eventdispatcher->dispatch($event::AFTER_SETUP, $event);
  89          }
  90  
  91          return $setup;
  92      }
  93  
  94      /**
  95       * Tests step.
  96       *
  97       * @param Environment $env
  98       * @param FeatureNode $feature
  99       * @param StepNode    $step
 100       * @param bool     $skip
 101       * @return StepResult
 102       */
 103      public function test(Environment $env, FeatureNode $feature, StepNode $step, $skip) {
 104          return $this->basetester->test($env, $feature, $step, $skip);
 105      }
 106  
 107      /**
 108       * Tears down step after a test.
 109       *
 110       * @param Environment $env
 111       * @param FeatureNode $feature
 112       * @param StepNode    $step
 113       * @param bool     $skip
 114       * @param StepResult  $result
 115       * @return Teardown
 116       */
 117      public function tearDown(Environment $env, FeatureNode $feature, StepNode $step, $skip, StepResult $result) {
 118          $event = new BeforeStepTeardown($env, $feature, $step, $result);
 119          if (TestworkEventDispatcher::DISPATCHER_VERSION === 2) {
 120              // Symfony 4.3 and up.
 121              $this->eventdispatcher->dispatch($event, $event::BEFORE_TEARDOWN);
 122          } else {
 123              // TODO: Remove when our min supported version is >= 4.3.
 124              $this->eventdispatcher->dispatch($event::BEFORE_TEARDOWN, $event);
 125          }
 126  
 127          $teardown = $this->basetester->tearDown($env, $feature, $step, $skip, $result);
 128  
 129          $event = new AfterStepTested($env, $feature, $step, $result, $teardown);
 130          if (TestworkEventDispatcher::DISPATCHER_VERSION === 2) {
 131              // Symfony 4.3 and up.
 132              $this->eventdispatcher->dispatch($event, $event::AFTER);
 133          } else {
 134              // TODO: Remove when our min supported version is >= 4.3.
 135              $this->eventdispatcher->dispatch($event::AFTER, $event);
 136          }
 137  
 138          return $teardown;
 139      }
 140  }