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\Output\Node\EventListener\AST;
13 use Behat\Behat\EventDispatcher\Event\AfterStepSetup;
14 use Behat\Behat\EventDispatcher\Event\AfterStepTested;
15 use Behat\Behat\EventDispatcher\Event\ExampleTested;
16 use Behat\Behat\EventDispatcher\Event\ScenarioLikeTested;
17 use Behat\Behat\EventDispatcher\Event\ScenarioTested;
18 use Behat\Behat\Output\Node\Printer\SetupPrinter;
19 use Behat\Behat\Output\Node\Printer\StepPrinter;
20 use Behat\Gherkin\Node\ScenarioLikeInterface;
21 use Behat\Testwork\Output\Formatter;
22 use Behat\Testwork\Output\Node\EventListener\EventListener;
23 use Symfony\Component\EventDispatcher\Event;
26 * Listens to step events and call appropriate printers.
28 * @author Konstantin Kudryashov <ever.zet@gmail.com>
30 final class StepListener implements EventListener
37 * @var ScenarioLikeInterface
41 * @var null|SetupPrinter
43 private $setupPrinter;
46 * Initializes listener.
48 * @param StepPrinter $stepPrinter
49 * @param null|SetupPrinter $setupPrinter
51 public function __construct(StepPrinter $stepPrinter, SetupPrinter $setupPrinter = null)
53 $this->stepPrinter = $stepPrinter;
54 $this->setupPrinter = $setupPrinter;
60 public function listenEvent(Formatter $formatter, Event $event, $eventName)
62 $this->captureScenarioOnScenarioEvent($event);
63 $this->forgetScenarioOnAfterEvent($eventName);
64 $this->printStepSetupOnBeforeEvent($formatter, $event);
65 $this->printStepOnAfterEvent($formatter, $event);
69 * Captures scenario into the ivar on scenario/background/example BEFORE event.
73 private function captureScenarioOnScenarioEvent(Event $event)
75 if (!$event instanceof ScenarioLikeTested) {
79 $this->scenario = $event->getScenario();
83 * Removes scenario from the ivar on scenario/background/example AFTER event.
85 * @param string $eventName
87 private function forgetScenarioOnAfterEvent($eventName)
89 if (!in_array($eventName, array(ScenarioTested::AFTER, ExampleTested::AFTER))) {
93 $this->scenario = null;
96 private function printStepSetupOnBeforeEvent(Formatter $formatter, Event $event)
98 if (!$event instanceof AfterStepSetup) {
102 if ($this->setupPrinter) {
103 $this->setupPrinter->printSetup($formatter, $event->getSetup());
108 * Prints step on AFTER event.
110 * @param Formatter $formatter
111 * @param Event $event
113 private function printStepOnAfterEvent(Formatter $formatter, Event $event)
115 if (!$event instanceof AfterStepTested) {
119 $this->stepPrinter->printStep($formatter, $this->scenario, $event->getStep(), $event->getTestResult());
121 if ($this->setupPrinter) {
122 $this->setupPrinter->printTeardown($formatter, $event->getTeardown());