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\JUnit;
13 use Behat\Behat\EventDispatcher\Event\AfterFeatureTested;
14 use Behat\Behat\EventDispatcher\Event\AfterScenarioTested;
15 use Behat\Behat\EventDispatcher\Event\AfterStepSetup;
16 use Behat\Behat\EventDispatcher\Event\AfterStepTested;
17 use Behat\Behat\EventDispatcher\Event\BeforeFeatureTested;
18 use Behat\Behat\EventDispatcher\Event\ScenarioTested;
19 use Behat\Behat\EventDispatcher\Event\StepTested;
20 use Behat\Behat\Output\Node\Printer\FeaturePrinter;
21 use Behat\Behat\Output\Node\Printer\JUnit\JUnitScenarioPrinter;
22 use Behat\Behat\Output\Node\Printer\SetupPrinter;
23 use Behat\Behat\Output\Node\Printer\StepPrinter;
24 use Behat\Gherkin\Node\FeatureNode;
25 use Behat\Testwork\EventDispatcher\Event\AfterSetup;
26 use Behat\Testwork\Output\Formatter;
27 use Behat\Testwork\Output\Node\EventListener\EventListener;
28 use Symfony\Component\EventDispatcher\Event;
31 * Listens to feature, scenario and step events and calls appropriate printers.
33 * @author Wouter J <wouter@wouterj.nl>
35 final class JUnitFeatureElementListener implements EventListener
40 private $featurePrinter;
42 * @var JUnitScenarioPrinter
44 private $scenarioPrinter;
52 private $setupPrinter;
56 private $beforeFeatureTestedEvent;
58 * @var AfterScenarioTested[]
60 private $afterScenarioTestedEvents = array();
62 * @var AfterStepTested[]
64 private $afterStepTestedEvents = array();
68 private $afterStepSetupEvents = array();
71 * Initializes listener.
73 * @param FeaturePrinter $featurePrinter
74 * @param JUnitScenarioPrinter $scenarioPrinter
75 * @param StepPrinter $stepPrinter
76 * @param SetupPrinter $setupPrinter
78 public function __construct(FeaturePrinter $featurePrinter,
79 JUnitScenarioPrinter $scenarioPrinter,
80 StepPrinter $stepPrinter,
81 SetupPrinter $setupPrinter)
83 $this->featurePrinter = $featurePrinter;
84 $this->scenarioPrinter = $scenarioPrinter;
85 $this->stepPrinter = $stepPrinter;
86 $this->setupPrinter = $setupPrinter;
92 public function listenEvent(Formatter $formatter, Event $event, $eventName)
94 if ($event instanceof ScenarioTested) {
95 $this->captureScenarioEvent($event);
98 if ($event instanceof StepTested
99 || $event instanceof AfterStepSetup
101 $this->captureStepEvent($event);
104 $this->captureFeatureOnBeforeEvent($event);
105 $this->printFeatureOnAfterEvent($formatter, $event);
109 * Captures scenario tested event.
111 * @param ScenarioTested $event
113 private function captureScenarioEvent(ScenarioTested $event)
115 if ($event instanceof AfterScenarioTested) {
116 $this->afterScenarioTestedEvents[$event->getScenario()->getLine()] = array(
118 'step_events' => $this->afterStepTestedEvents,
119 'step_setup_events' => $this->afterStepSetupEvents,
122 $this->afterStepTestedEvents = array();
123 $this->afterStepSetupEvents = array();
128 * Captures feature on BEFORE event.
130 * @param Event $event
132 private function captureFeatureOnBeforeEvent(Event $event)
134 if (!$event instanceof BeforeFeatureTested) {
138 $this->beforeFeatureTestedEvent = $event->getFeature();
142 * Captures step tested event.
144 * @param Event $event
146 private function captureStepEvent(Event $event)
148 if ($event instanceof AfterStepTested) {
149 $this->afterStepTestedEvents[$event->getStep()->getLine()] = $event;
151 if ($event instanceof AfterStepSetup) {
152 $this->afterStepSetupEvents[$event->getStep()->getLine()] = $event;
157 * Prints the feature on AFTER event.
159 * @param Formatter $formatter
160 * @param Event $event
162 public function printFeatureOnAfterEvent(Formatter $formatter, Event $event)
164 if (!$event instanceof AfterFeatureTested) {
168 $this->featurePrinter->printHeader($formatter, $this->beforeFeatureTestedEvent);
170 foreach ($this->afterScenarioTestedEvents as $afterScenario) {
171 $afterScenarioTested = $afterScenario['event'];
172 $this->scenarioPrinter->printOpenTag($formatter, $afterScenarioTested->getFeature(), $afterScenarioTested->getScenario(), $afterScenarioTested->getTestResult());
174 /** @var AfterStepSetup $afterStepSetup */
175 foreach ($afterScenario['step_setup_events'] as $afterStepSetup) {
176 $this->setupPrinter->printSetup($formatter, $afterStepSetup->getSetup());
178 foreach ($afterScenario['step_events'] as $afterStepTested) {
179 $this->stepPrinter->printStep($formatter, $afterScenarioTested->getScenario(), $afterStepTested->getStep(), $afterStepTested->getTestResult());
180 $this->setupPrinter->printTeardown($formatter, $afterStepTested->getTeardown());
184 $this->featurePrinter->printFooter($formatter, $event->getTestResult());
185 $this->afterScenarioTestedEvents = array();