Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Output / Node / EventListener / Statistics / ScenarioStatsListener.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Behat\Output\Node\EventListener\Statistics;
12
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;
21
22 /**
23  * Listens and records scenario events to the statistics.
24  *
25  * @author Konstantin Kudryashov <ever.zet@gmail.com>
26  */
27 final class ScenarioStatsListener implements EventListener
28 {
29     /**
30      * @var Statistics
31      */
32     private $statistics;
33     /**
34      * @var string
35      */
36     private $currentFeaturePath;
37
38     /**
39      * Initializes listener.
40      *
41      * @param Statistics $statistics
42      */
43     public function __construct(Statistics $statistics)
44     {
45         $this->statistics = $statistics;
46     }
47
48     /**
49      * {@inheritdoc}
50      */
51     public function listenEvent(Formatter $formatter, Event $event, $eventName)
52     {
53         $this->captureCurrentFeaturePathOnBeforeFeatureEvent($event);
54         $this->forgetCurrentFeaturePathOnAfterFeatureEvent($event);
55         $this->captureScenarioOrExampleStatsOnAfterEvent($event);
56     }
57
58     /**
59      * Captures current feature file path to the ivar on feature BEFORE event.
60      *
61      * @param Event $event
62      */
63     private function captureCurrentFeaturePathOnBeforeFeatureEvent(Event $event)
64     {
65         if (!$event instanceof BeforeFeatureTested) {
66             return;
67         }
68
69         $this->currentFeaturePath = $event->getFeature()->getFile();
70     }
71
72     /**
73      * Removes current feature file path from the ivar on feature AFTER event.
74      *
75      * @param Event $event
76      */
77     private function forgetCurrentFeaturePathOnAfterFeatureEvent($event)
78     {
79         if (!$event instanceof AfterFeatureTested) {
80             return;
81         }
82
83         $this->currentFeaturePath = null;
84     }
85
86     /**
87      * Captures scenario or example stats on their AFTER event.
88      *
89      * @param Event $event
90      */
91     private function captureScenarioOrExampleStatsOnAfterEvent(Event $event)
92     {
93         if (!$event instanceof AfterScenarioTested) {
94             return;
95         }
96
97         $scenario = $event->getScenario();
98         $title = $scenario->getTitle();
99         $path = sprintf('%s:%d', $this->currentFeaturePath, $scenario->getLine());
100         $resultCode = $event->getTestResult()->getResultCode();
101
102         $stat = new ScenarioStat($title, $path, $resultCode);
103         $this->statistics->registerScenarioStat($stat);
104     }
105 }