Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Output / Node / EventListener / Flow / FirstBackgroundFiresFirstListener.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\Flow;
12
13 use Behat\Behat\EventDispatcher\Event\BackgroundTested;
14 use Behat\Behat\EventDispatcher\Event\ExampleTested;
15 use Behat\Behat\EventDispatcher\Event\FeatureTested;
16 use Behat\Behat\EventDispatcher\Event\OutlineTested;
17 use Behat\Behat\EventDispatcher\Event\ScenarioTested;
18 use Behat\Testwork\Output\Formatter;
19 use Behat\Testwork\Output\Node\EventListener\EventListener;
20 use Symfony\Component\EventDispatcher\Event;
21
22 /**
23  * Behat first background fires first listener.
24  *
25  * This listener catches first scenario and background events in the feature and makes sure
26  * that background event are always fired before scenario events, thus following Gherkin format.
27  *
28  * @author Konstantin Kudryashov <ever.zet@gmail.com>
29  */
30 class FirstBackgroundFiresFirstListener implements EventListener
31 {
32     /**
33      * @var \Behat\Testwork\Output\Node\EventListener\EventListener
34      */
35     private $descendant;
36     /**
37      * @var Boolean
38      */
39     private $firstBackgroundEnded = false;
40     /**
41      * @var Event[]
42      */
43     private $delayedUntilBackgroundEnd = array();
44
45     /**
46      * Initializes listener.
47      *
48      * @param EventListener $descendant
49      */
50     public function __construct(EventListener $descendant)
51     {
52         $this->descendant = $descendant;
53     }
54
55     /**
56      * {@inheritdoc}
57      */
58     public function listenEvent(Formatter $formatter, Event $event, $eventName)
59     {
60         $this->flushStatesIfBeginningOfTheFeature($eventName);
61         $this->markFirstBackgroundPrintedAfterBackground($eventName);
62
63         if ($this->isEventDelayedUntilFirstBackgroundPrinted($event)) {
64             $this->delayedUntilBackgroundEnd[] = array($event, $eventName);
65
66             return;
67         }
68
69         $this->descendant->listenEvent($formatter, $event, $eventName);
70         $this->fireDelayedEventsOnAfterBackground($formatter, $eventName);
71     }
72
73     /**
74      * Flushes state if the event is the BEFORE feature.
75      *
76      * @param string $eventName
77      */
78     private function flushStatesIfBeginningOfTheFeature($eventName)
79     {
80         if (FeatureTested::BEFORE !== $eventName) {
81             return;
82         }
83
84         $this->firstBackgroundEnded = false;
85     }
86
87     /**
88      * Marks first background printed.
89      *
90      * @param string $eventName
91      */
92     private function markFirstBackgroundPrintedAfterBackground($eventName)
93     {
94         if (BackgroundTested::AFTER !== $eventName) {
95             return;
96         }
97
98         $this->firstBackgroundEnded = true;
99     }
100
101     /**
102      * Checks if provided event should be postponed until background is printed.
103      *
104      * @param Event $event
105      *
106      * @return Boolean
107      */
108     private function isEventDelayedUntilFirstBackgroundPrinted(Event $event)
109     {
110         if (!$event instanceof ScenarioTested && !$event instanceof OutlineTested && !$event instanceof ExampleTested) {
111             return false;
112         }
113
114         return !$this->firstBackgroundEnded && $event->getFeature()->hasBackground();
115     }
116
117     /**
118      * Fires delayed events on AFTER background event.
119      *
120      * @param Formatter $formatter
121      * @param string    $eventName
122      */
123     private function fireDelayedEventsOnAfterBackground(Formatter $formatter, $eventName)
124     {
125         if (BackgroundTested::AFTER !== $eventName) {
126             return;
127         }
128
129         foreach ($this->delayedUntilBackgroundEnd as $eventInfo) {
130             list($event, $eventName) = $eventInfo;
131
132             $this->descendant->listenEvent($formatter, $event, $eventName);
133         }
134
135         $this->delayedUntilBackgroundEnd = array();
136     }
137 }