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\Statistics;
13 use Behat\Behat\EventDispatcher\Event\AfterFeatureTested;
14 use Behat\Behat\EventDispatcher\Event\AfterScenarioTested;
15 use Behat\Behat\EventDispatcher\Event\BeforeFeatureTested;
16 use Behat\Behat\Output\Statistics\ScenarioStat;
17 use Behat\Behat\Output\Statistics\Statistics;
18 use Behat\Testwork\Output\Formatter;
19 use Behat\Testwork\Output\Node\EventListener\EventListener;
20 use Symfony\Component\EventDispatcher\Event;
23 * Listens and records scenario events to the statistics.
25 * @author Konstantin Kudryashov <ever.zet@gmail.com>
27 final class ScenarioStatsListener implements EventListener
36 private $currentFeaturePath;
39 * Initializes listener.
41 * @param Statistics $statistics
43 public function __construct(Statistics $statistics)
45 $this->statistics = $statistics;
51 public function listenEvent(Formatter $formatter, Event $event, $eventName)
53 $this->captureCurrentFeaturePathOnBeforeFeatureEvent($event);
54 $this->forgetCurrentFeaturePathOnAfterFeatureEvent($event);
55 $this->captureScenarioOrExampleStatsOnAfterEvent($event);
59 * Captures current feature file path to the ivar on feature BEFORE event.
63 private function captureCurrentFeaturePathOnBeforeFeatureEvent(Event $event)
65 if (!$event instanceof BeforeFeatureTested) {
69 $this->currentFeaturePath = $event->getFeature()->getFile();
73 * Removes current feature file path from the ivar on feature AFTER event.
77 private function forgetCurrentFeaturePathOnAfterFeatureEvent($event)
79 if (!$event instanceof AfterFeatureTested) {
83 $this->currentFeaturePath = null;
87 * Captures scenario or example stats on their AFTER event.
91 private function captureScenarioOrExampleStatsOnAfterEvent(Event $event)
93 if (!$event instanceof AfterScenarioTested) {
97 $scenario = $event->getScenario();
98 $title = $scenario->getTitle();
99 $path = sprintf('%s:%d', $this->currentFeaturePath, $scenario->getLine());
100 $resultCode = $event->getTestResult()->getResultCode();
102 $stat = new ScenarioStat($title, $path, $resultCode);
103 $this->statistics->registerScenarioStat($stat);