Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Output / Node / EventListener / AST / OutlineTableListener.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\AST;
12
13 use Behat\Behat\EventDispatcher\Event\AfterOutlineTested;
14 use Behat\Behat\EventDispatcher\Event\AfterScenarioSetup;
15 use Behat\Behat\EventDispatcher\Event\AfterScenarioTested;
16 use Behat\Behat\EventDispatcher\Event\AfterStepSetup;
17 use Behat\Behat\EventDispatcher\Event\AfterStepTested;
18 use Behat\Behat\EventDispatcher\Event\BeforeOutlineTested;
19 use Behat\Behat\EventDispatcher\Event\ExampleTested;
20 use Behat\Behat\EventDispatcher\Event\OutlineTested;
21 use Behat\Behat\EventDispatcher\Event\StepTested;
22 use Behat\Behat\Output\Node\Printer\ExampleRowPrinter;
23 use Behat\Behat\Output\Node\Printer\OutlineTablePrinter;
24 use Behat\Behat\Output\Node\Printer\SetupPrinter;
25 use Behat\Behat\Tester\Result\StepResult;
26 use Behat\Gherkin\Node\OutlineNode;
27 use Behat\Testwork\Output\Formatter;
28 use Behat\Testwork\Output\Node\EventListener\EventListener;
29 use Behat\Testwork\Tester\Setup\Setup;
30 use Symfony\Component\EventDispatcher\Event;
31
32 /**
33  * Listens to outline table events and calls appropriate printers.
34  *
35  * @author Konstantin Kudryashov <ever.zet@gmail.com>
36  */
37 final class OutlineTableListener implements EventListener
38 {
39     /**
40      * @var OutlineTablePrinter
41      */
42     private $tablePrinter;
43     /**
44      * @var ExampleRowPrinter
45      */
46     private $exampleRowPrinter;
47     /**
48      * @var SetupPrinter
49      */
50     private $stepSetupPrinter;
51     /**
52      * @var SetupPrinter
53      */
54     private $exampleSetupPrinter;
55     /**
56      * @var OutlineNode
57      */
58     private $outline;
59     /**
60      * @var Setup
61      */
62     private $exampleSetup;
63     /**
64      * @var Boolean
65      */
66     private $headerPrinted = false;
67     /**
68      * @var AfterStepSetup[]
69      */
70     private $stepBeforeTestedEvents = array();
71     /**
72      * @var AfterStepTested[]
73      */
74     private $stepAfterTestedEvents = array();
75
76     /**
77      * Initializes listener.
78      *
79      * @param OutlineTablePrinter $tablePrinter
80      * @param ExampleRowPrinter   $exampleRowPrinter
81      * @param SetupPrinter        $exampleSetupPrinter
82      * @param SetupPrinter        $stepSetupPrinter
83      */
84     public function __construct(
85         OutlineTablePrinter $tablePrinter,
86         ExampleRowPrinter $exampleRowPrinter,
87         SetupPrinter $exampleSetupPrinter,
88         SetupPrinter $stepSetupPrinter
89     ) {
90         $this->tablePrinter = $tablePrinter;
91         $this->exampleRowPrinter = $exampleRowPrinter;
92         $this->exampleSetupPrinter = $exampleSetupPrinter;
93         $this->stepSetupPrinter = $stepSetupPrinter;
94     }
95
96     /**
97      * {@inheritdoc}
98      */
99     public function listenEvent(Formatter $formatter, Event $event, $eventName)
100     {
101         if ($event instanceof StepTested) {
102             $this->captureStepEvent($event);
103
104             return;
105         }
106
107         $this->captureOutlineOnBeforeOutlineEvent($event);
108         $this->forgetOutlineOnAfterOutlineEvent($eventName);
109         $this->captureExampleSetupOnBeforeEvent($event);
110
111         $this->printHeaderOnAfterExampleEvent($formatter, $event, $eventName);
112         $this->printExampleRowOnAfterExampleEvent($formatter, $event, $eventName);
113         $this->printFooterOnAfterEvent($formatter, $event);
114     }
115
116     /**
117      * Captures step tested event.
118      *
119      * @param StepTested $event
120      */
121     private function captureStepEvent(StepTested $event)
122     {
123         if ($event instanceof AfterStepSetup) {
124             $this->stepBeforeTestedEvents[$event->getStep()->getLine()] = $event;
125         } else {
126             $this->stepAfterTestedEvents[$event->getStep()->getLine()] = $event;
127         }
128     }
129
130     /**
131      * Captures outline into the ivar on outline BEFORE event.
132      *
133      * @param Event $event
134      */
135     private function captureOutlineOnBeforeOutlineEvent(Event $event)
136     {
137         if (!$event instanceof BeforeOutlineTested) {
138             return;
139         }
140
141         $this->outline = $event->getOutline();
142         $this->headerPrinted = false;
143     }
144
145     /**
146      * Captures example setup on example BEFORE event.
147      *
148      * @param Event $event
149      */
150     private function captureExampleSetupOnBeforeEvent(Event $event)
151     {
152         if (!$event instanceof AfterScenarioSetup) {
153             return;
154         }
155
156         $this->exampleSetup = $event->getSetup();
157     }
158
159     /**
160      * Removes outline from the ivar on outline AFTER event.
161      *
162      * @param string $eventName
163      */
164     private function forgetOutlineOnAfterOutlineEvent($eventName)
165     {
166         if (OutlineTested::AFTER !== $eventName) {
167             return;
168         }
169
170         $this->outline = null;
171     }
172
173     /**
174      * Prints outline header (if has not been printed yet) on example AFTER event.
175      *
176      * @param Formatter $formatter
177      * @param Event     $event
178      * @param string    $eventName
179      */
180     private function printHeaderOnAfterExampleEvent(Formatter $formatter, Event $event, $eventName)
181     {
182         if (!$event instanceof AfterScenarioTested || ExampleTested::AFTER !== $eventName) {
183             return;
184         }
185
186         if ($this->headerPrinted) {
187             return;
188         }
189
190         $feature = $event->getFeature();
191         $stepTestResults = $this->getStepTestResults();
192
193         $this->tablePrinter->printHeader($formatter, $feature, $this->outline, $stepTestResults);
194         $this->headerPrinted = true;
195     }
196
197     /**
198      * Prints example row on example AFTER event.
199      *
200      * @param Formatter $formatter
201      * @param Event     $event
202      * @param string    $eventName
203      */
204     private function printExampleRowOnAfterExampleEvent(Formatter $formatter, Event $event, $eventName)
205     {
206         if (!$event instanceof AfterScenarioTested || ExampleTested::AFTER !== $eventName) {
207             return;
208         }
209
210         $example = $event->getScenario();
211
212         $this->exampleSetupPrinter->printSetup($formatter, $this->exampleSetup);
213
214         foreach ($this->stepBeforeTestedEvents as $beforeEvent) {
215             $this->stepSetupPrinter->printSetup($formatter, $beforeEvent->getSetup());
216         }
217
218         $this->exampleRowPrinter->printExampleRow($formatter, $this->outline, $example, $this->stepAfterTestedEvents);
219
220         foreach ($this->stepAfterTestedEvents as $afterEvent) {
221             $this->stepSetupPrinter->printTeardown($formatter, $afterEvent->getTeardown());
222         }
223
224         $this->exampleSetupPrinter->printTeardown($formatter, $event->getTeardown());
225
226         $this->exampleSetup = null;
227         $this->stepBeforeTestedEvents = array();
228         $this->stepAfterTestedEvents = array();
229     }
230
231     /**
232      * Prints outline footer on outline AFTER event.
233      *
234      * @param Formatter $formatter
235      * @param Event     $event
236      */
237     private function printFooterOnAfterEvent(Formatter $formatter, Event $event)
238     {
239         if (!$event instanceof AfterOutlineTested) {
240             return;
241         }
242
243         $this->tablePrinter->printFooter($formatter, $event->getTestResult());
244     }
245
246     /**
247      * Returns currently captured step events results.
248      *
249      * @return StepResult[]
250      */
251     private function getStepTestResults()
252     {
253         return array_map(
254             function (AfterStepTested $event) {
255                 return $event->getTestResult();
256             },
257             $this->stepAfterTestedEvents
258         );
259     }
260 }