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\BeforeOutlineTested;
14 use Behat\Behat\Output\Node\Printer\SuitePrinter;
15 use Behat\Gherkin\Node\ExampleNode;
16 use Behat\Gherkin\Node\OutlineNode;
17 use Behat\Testwork\EventDispatcher\Event\AfterSuiteTested;
18 use Behat\Testwork\EventDispatcher\Event\BeforeSuiteTested;
19 use Behat\Testwork\Output\Formatter;
20 use Behat\Testwork\Output\Node\EventListener\EventListener;
21 use Symfony\Component\EventDispatcher\Event;
24 * Listens for Outline events store the current one
26 * @author James Watson <james@sitepulse.org>
28 final class JUnitOutlineStoreListener implements EventListener
34 private $suitePrinter;
39 private $lineScenarioMap = array();
42 * Initializes listener.
44 * @param SuitePrinter $suitePrinter
46 public function __construct(SuitePrinter $suitePrinter)
48 $this->suitePrinter = $suitePrinter;
54 public function listenEvent(Formatter $formatter, Event $event, $eventName)
56 $this->captureOutlineOnBeforeOutlineEvent($event);
58 $this->printHeaderOnBeforeSuiteTestedEvent($formatter, $event);
59 $this->printFooterOnAfterSuiteTestedEvent($formatter, $event);
63 * Captures outline into the ivar on outline BEFORE event.
67 private function captureOutlineOnBeforeOutlineEvent(Event $event)
69 if (!$event instanceof BeforeOutlineTested) {
73 $outline = $event->getOutline();
74 foreach ($outline->getExamples() as $example) {
75 $this->lineScenarioMap[$example->getLine()] = $outline;
80 * @param Formatter $formatter
83 private function printHeaderOnBeforeSuiteTestedEvent(Formatter $formatter, Event $event)
85 if (!$event instanceof BeforeSuiteTested) {
88 $this->suitePrinter->printHeader($formatter, $event->getSuite());
92 * @param Formatter $formatter
95 private function printFooterOnAfterSuiteTestedEvent(Formatter $formatter, Event $event)
97 if (!$event instanceof AfterSuiteTested) {
100 $this->suitePrinter->printFooter($formatter, $event->getSuite());
104 * @param ExampleNode $scenario
105 * @return OutlineNode
107 public function getCurrentOutline(ExampleNode $scenario)
109 return $this->lineScenarioMap[$scenario->getLine()];