Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Output / Node / EventListener / Statistics / HookStatsListener.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\Output\Statistics\HookStat;
14 use Behat\Behat\Output\Statistics\Statistics;
15 use Behat\Testwork\Call\CallResult;
16 use Behat\Testwork\EventDispatcher\Event\AfterSetup;
17 use Behat\Testwork\EventDispatcher\Event\AfterTested;
18 use Behat\Testwork\Exception\ExceptionPresenter;
19 use Behat\Testwork\Hook\Tester\Setup\HookedSetup;
20 use Behat\Testwork\Hook\Tester\Setup\HookedTeardown;
21 use Behat\Testwork\Output\Formatter;
22 use Behat\Testwork\Output\Node\EventListener\EventListener;
23 use Symfony\Component\EventDispatcher\Event;
24
25 /**
26  * Listens and records hook stats.
27  *
28  * @author Konstantin Kudryashov <ever.zet@gmail.com>
29  */
30 final class HookStatsListener implements EventListener
31 {
32     /**
33      * @var Statistics
34      */
35     private $statistics;
36     /**
37      * @var ExceptionPresenter
38      */
39     private $exceptionPresenter;
40
41     /**
42      * Initializes listener.
43      *
44      * @param Statistics         $statistics
45      * @param ExceptionPresenter $exceptionPresenter
46      */
47     public function __construct(Statistics $statistics, ExceptionPresenter $exceptionPresenter)
48     {
49         $this->statistics = $statistics;
50         $this->exceptionPresenter = $exceptionPresenter;
51     }
52
53     /**
54      * {@inheritdoc}
55      */
56     public function listenEvent(Formatter $formatter, Event $event, $eventName)
57     {
58         $this->captureHookStatsOnEvent($event);
59     }
60
61     /**
62      * Captures hook stats on hooked event.
63      *
64      * @param Event $event
65      */
66     private function captureHookStatsOnEvent(Event $event)
67     {
68         if ($event instanceof AfterSetup && $event->getSetup() instanceof HookedSetup) {
69             $this->captureBeforeHookStats($event->getSetup());
70         }
71
72         if ($event instanceof AfterTested && $event->getTeardown() instanceof HookedTeardown) {
73             $this->captureAfterHookStats($event->getTeardown());
74         }
75     }
76
77     /**
78      * Captures before hook stats.
79      *
80      * @param HookedSetup $setup
81      */
82     private function captureBeforeHookStats(HookedSetup $setup)
83     {
84         $hookCallResults = $setup->getHookCallResults();
85
86         foreach ($hookCallResults as $hookCallResult) {
87             $this->captureHookStat($hookCallResult);
88         }
89     }
90
91     /**
92      * Captures before hook stats.
93      *
94      * @param HookedTeardown $teardown
95      */
96     private function captureAfterHookStats(HookedTeardown $teardown)
97     {
98         $hookCallResults = $teardown->getHookCallResults();
99
100         foreach ($hookCallResults as $hookCallResult) {
101             $this->captureHookStat($hookCallResult);
102         }
103     }
104
105     /**
106      * Captures hook call result.
107      *
108      * @param CallResult $hookCallResult
109      */
110     private function captureHookStat(CallResult $hookCallResult)
111     {
112         $callee = $hookCallResult->getCall()->getCallee();
113         $hook = (string) $callee;
114         $path = $callee->getPath();
115         $stdOut = $hookCallResult->getStdOut();
116         $error = $hookCallResult->getException()
117             ? $this->exceptionPresenter->presentException($hookCallResult->getException())
118             : null;
119
120         $stat = new HookStat($hook, $path, $error, $stdOut);
121         $this->statistics->registerHookStat($stat);
122     }
123 }