4 * This file is part of the Behat.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Behat\EventDispatcher\Tester;
13 use Behat\Behat\EventDispatcher\Event\AfterScenarioSetup;
14 use Behat\Behat\EventDispatcher\Event\AfterScenarioTested;
15 use Behat\Behat\EventDispatcher\Event\BeforeScenarioTeardown;
16 use Behat\Behat\EventDispatcher\Event\BeforeScenarioTested;
17 use Behat\Behat\Tester\ScenarioTester;
18 use Behat\Gherkin\Node\FeatureNode;
19 use Behat\Gherkin\Node\ScenarioInterface as Scenario;
20 use Behat\Testwork\Environment\Environment;
21 use Behat\Testwork\Tester\Result\TestResult;
22 use Symfony\Component\EventDispatcher\EventDispatcherInterface;
25 * Scenario tester dispatching BEFORE/AFTER events during tests.
27 * @author Konstantin Kudryashov <ever.zet@gmail.com>
29 final class EventDispatchingScenarioTester implements ScenarioTester
36 * @var EventDispatcherInterface
38 private $eventDispatcher;
42 private $beforeEventName;
46 private $afterSetupEventName;
50 private $beforeTeardownEventName;
54 private $afterEventName;
59 * @param ScenarioTester $baseTester
60 * @param EventDispatcherInterface $eventDispatcher
61 * @param string $beforeEventName
62 * @param string $afterSetupEventName
63 * @param string $beforeTeardownEventName
64 * @param string $afterEventName
66 public function __construct(
67 ScenarioTester $baseTester,
68 EventDispatcherInterface $eventDispatcher,
71 $beforeTeardownEventName,
74 $this->baseTester = $baseTester;
75 $this->eventDispatcher = $eventDispatcher;
76 $this->beforeEventName = $beforeEventName;
77 $this->afterSetupEventName = $afterSetupEventName;
78 $this->beforeTeardownEventName = $beforeTeardownEventName;
79 $this->afterEventName = $afterEventName;
85 public function setUp(Environment $env, FeatureNode $feature, Scenario $scenario, $skip)
87 $event = new BeforeScenarioTested($env, $feature, $scenario);
88 $this->eventDispatcher->dispatch($this->beforeEventName, $event);
90 $setup = $this->baseTester->setUp($env, $feature, $scenario, $skip);
92 $event = new AfterScenarioSetup($env, $feature, $scenario, $setup);
93 $this->eventDispatcher->dispatch($this->afterSetupEventName, $event);
101 public function test(Environment $env, FeatureNode $feature, Scenario $scenario, $skip)
103 return $this->baseTester->test($env, $feature, $scenario, $skip);
109 public function tearDown(Environment $env, FeatureNode $feature, Scenario $scenario, $skip, TestResult $result)
111 $event = new BeforeScenarioTeardown($env, $feature, $scenario, $result);
112 $this->eventDispatcher->dispatch($this->beforeTeardownEventName, $event);
114 $teardown = $this->baseTester->tearDown($env, $feature, $scenario, $skip, $result);
116 $event = new AfterScenarioTested($env, $feature, $scenario, $result, $teardown);
117 $this->eventDispatcher->dispatch($this->afterEventName, $event);