Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Output / Node / EventListener / Statistics / StepStatsListener.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\AfterStepTested;
14 use Behat\Behat\EventDispatcher\Event\BeforeFeatureTested;
15 use Behat\Behat\EventDispatcher\Event\BeforeScenarioTested;
16 use Behat\Behat\EventDispatcher\Event\FeatureTested;
17 use Behat\Behat\EventDispatcher\Event\ScenarioTested;
18 use Behat\Behat\Output\Statistics\StepStatV2;
19 use Behat\Behat\Output\Statistics\Statistics;
20 use Behat\Behat\Output\Statistics\StepStat;
21 use Behat\Behat\Tester\Exception\PendingException;
22 use Behat\Behat\Tester\Result\ExecutedStepResult;
23 use Behat\Behat\Tester\Result\StepResult;
24 use Behat\Testwork\Exception\ExceptionPresenter;
25 use Behat\Testwork\Output\Formatter;
26 use Behat\Testwork\Output\Node\EventListener\EventListener;
27 use Behat\Testwork\Tester\Result\ExceptionResult;
28 use Exception;
29 use Symfony\Component\EventDispatcher\Event;
30
31 /**
32  * Listens and records step events to statistics.
33  *
34  * @author Konstantin Kudryashov <ever.zet@gmail.com>
35  */
36 final class StepStatsListener implements EventListener
37 {
38     /**
39      * @var Statistics
40      */
41     private $statistics;
42     /**
43      * @var string
44      */
45     private $currentFeaturePath;
46     /**
47      * @var ExceptionPresenter
48      */
49     private $exceptionPresenter;
50     /**
51      * @var string
52      */
53     private $scenarioTitle;
54     /**
55      * @var string
56      */
57     private $scenarioPath;
58
59     /**
60      * Initializes listener.
61      *
62      * @param Statistics         $statistics
63      * @param ExceptionPresenter $exceptionPresenter
64      */
65     public function __construct(Statistics $statistics, ExceptionPresenter $exceptionPresenter)
66     {
67         $this->statistics = $statistics;
68         $this->exceptionPresenter = $exceptionPresenter;
69     }
70
71     /**
72      * {@inheritdoc}
73      */
74     public function listenEvent(Formatter $formatter, Event $event, $eventName)
75     {
76         $this->captureCurrentFeaturePathOnBeforeFeatureEvent($event);
77         $this->forgetCurrentFeaturePathOnAfterFeatureEvent($eventName);
78         $this->captureScenarioOnBeforeFeatureEvent($event);
79         $this->forgetScenarioOnAfterFeatureEvent($eventName);
80         $this->captureStepStatsOnAfterEvent($event);
81     }
82
83     /**
84      * Captures current feature file path to the ivar on feature BEFORE event.
85      *
86      * @param Event $event
87      */
88     private function captureCurrentFeaturePathOnBeforeFeatureEvent(Event $event)
89     {
90         if (!$event instanceof BeforeFeatureTested) {
91             return;
92         }
93
94         $this->currentFeaturePath = $event->getFeature()->getFile();
95     }
96
97     /**
98      * Removes current feature file path from the ivar on feature AFTER event.
99      *
100      * @param string $eventName
101      */
102     private function forgetCurrentFeaturePathOnAfterFeatureEvent($eventName)
103     {
104         if (FeatureTested::AFTER !== $eventName) {
105             return;
106         }
107
108         $this->currentFeaturePath = null;
109     }
110
111     /**
112      * Captures current scenario title and path on scenario BEFORE event.
113      *
114      * @param Event $event
115      */
116     private function captureScenarioOnBeforeFeatureEvent(Event $event)
117     {
118         if (!$event instanceof BeforeScenarioTested) {
119             return;
120         }
121
122         $this->scenarioTitle = sprintf('%s: %s', $event->getScenario()->getKeyword(), $event->getScenario()->getTitle());
123         $this->scenarioPath = sprintf('%s:%s', $this->currentFeaturePath, $event->getScenario()->getLine());
124     }
125
126     private function forgetScenarioOnAfterFeatureEvent($eventName)
127     {
128         if (ScenarioTested::AFTER !== $eventName) {
129             return;
130         }
131
132         $this->scenarioTitle = $this->scenarioPath = null;
133     }
134
135     /**
136      * Captures step stats on step AFTER event.
137      *
138      * @param Event $event
139      */
140     private function captureStepStatsOnAfterEvent(Event $event)
141     {
142         if (!$event instanceof AfterStepTested) {
143             return;
144         }
145
146         $result = $event->getTestResult();
147         $step = $event->getStep();
148         $text = sprintf('%s %s', $step->getKeyword(), $step->getText());
149         $exception = $this->getStepException($result);
150
151         $path = $this->getStepPath($event, $exception);
152         $error = $exception ? $this->exceptionPresenter->presentException($exception) : null;
153         $stdOut = $result instanceof ExecutedStepResult ? $result->getCallResult()->getStdOut() : null;
154
155         $resultCode = $result->getResultCode();
156         $stat = new StepStatV2($this->scenarioTitle, $this->scenarioPath, $text, $path, $resultCode, $error, $stdOut);
157
158         $this->statistics->registerStepStat($stat);
159     }
160
161     /**
162      * Gets exception from the step test results.
163      *
164      * @param StepResult $result
165      *
166      * @return null|Exception
167      */
168     private function getStepException(StepResult $result)
169     {
170         if ($result instanceof ExceptionResult) {
171             return $result->getException();
172         }
173
174         return null;
175     }
176
177     /**
178      * Gets step path from the AFTER test event and exception.
179      *
180      * @param AfterStepTested $event
181      * @param null|Exception  $exception
182      *
183      * @return string
184      */
185     private function getStepPath(AfterStepTested $event, Exception $exception = null)
186     {
187         $path = sprintf('%s:%d', $this->currentFeaturePath, $event->getStep()->getLine());
188
189         if ($exception && $exception instanceof PendingException) {
190             $path = $event->getTestResult()->getStepDefinition()->getPath();
191         }
192
193         return $path;
194     }
195 }