Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Output / Node / Printer / Pretty / PrettySetupPrinter.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\Printer\Pretty;
12
13 use Behat\Behat\Output\Node\Printer\Helper\ResultToStringConverter;
14 use Behat\Behat\Output\Node\Printer\SetupPrinter;
15 use Behat\Testwork\Call\CallResult;
16 use Behat\Testwork\Exception\ExceptionPresenter;
17 use Behat\Testwork\Hook\Tester\Setup\HookedSetup;
18 use Behat\Testwork\Hook\Tester\Setup\HookedTeardown;
19 use Behat\Testwork\Output\Formatter;
20 use Behat\Testwork\Output\Printer\OutputPrinter;
21 use Behat\Testwork\Tester\Result\TestResult;
22 use Behat\Testwork\Tester\Setup\Setup;
23 use Behat\Testwork\Tester\Setup\Teardown;
24
25 /**
26  * Prints hooks in a pretty fashion.
27  *
28  * @author Konstantin Kudryashov <ever.zet@gmail.com>
29  */
30 final class PrettySetupPrinter implements SetupPrinter
31 {
32     /**
33      * @var ResultToStringConverter
34      */
35     private $resultConverter;
36     /**
37      * @var ExceptionPresenter
38      */
39     private $exceptionPresenter;
40     /**
41      * @var string
42      */
43     private $indentText;
44     /**
45      * @var bool
46      */
47     private $newlineBefore;
48     /**
49      * @var bool
50      */
51     private $newlineAfter;
52
53     /**
54      * Initializes printer.
55      *
56      * @param ResultToStringConverter $resultConverter
57      * @param ExceptionPresenter      $exceptionPresenter
58      * @param integer                 $indentation
59      * @param Boolean                 $newlineBefore
60      * @param Boolean                 $newlineAfter
61      */
62     public function __construct(
63         ResultToStringConverter $resultConverter,
64         ExceptionPresenter $exceptionPresenter,
65         $indentation = 0,
66         $newlineBefore = false,
67         $newlineAfter = false
68     ) {
69         $this->resultConverter = $resultConverter;
70         $this->exceptionPresenter = $exceptionPresenter;
71         $this->indentText = str_repeat(' ', intval($indentation));
72         $this->newlineBefore = $newlineBefore;
73         $this->newlineAfter = $newlineAfter;
74     }
75
76     /**
77      * {@inheritdoc}
78      */
79     public function printSetup(Formatter $formatter, Setup $setup)
80     {
81         if (!$setup instanceof HookedSetup) {
82             return;
83         }
84
85         foreach ($setup->getHookCallResults() as $callResult) {
86             $this->printSetupHookCallResult($formatter->getOutputPrinter(), $callResult);
87         }
88     }
89
90     /**
91      * {@inheritdoc}
92      */
93     public function printTeardown(Formatter $formatter, Teardown $teardown)
94     {
95         if (!$teardown instanceof HookedTeardown) {
96             return;
97         }
98
99         foreach ($teardown->getHookCallResults() as $callResult) {
100             $this->printTeardownHookCallResult($formatter->getOutputPrinter(), $callResult);
101         }
102     }
103
104     /**
105      * Prints setup hook call result.
106      *
107      * @param OutputPrinter $printer
108      * @param CallResult    $callResult
109      */
110     private function printSetupHookCallResult(OutputPrinter $printer, CallResult $callResult)
111     {
112         if (!$callResult->hasStdOut() && !$callResult->hasException()) {
113             return;
114         }
115
116         $resultCode = $callResult->hasException() ? TestResult::FAILED : TestResult::PASSED;
117         $style = $this->resultConverter->convertResultCodeToString($resultCode);
118         $hook = $callResult->getCall()->getCallee();
119         $path = $hook->getPath();
120
121         $printer->writeln(
122             sprintf('%s┌─ {+%s}@%s{-%s} {+comment}# %s{-comment}', $this->indentText, $style, $hook, $style, $path)
123         );
124
125         $printer->writeln(sprintf('%s│', $this->indentText));
126
127         $this->printHookCallStdOut($printer, $callResult, $this->indentText);
128         $this->printHookCallException($printer, $callResult, $this->indentText);
129
130         if ($this->newlineBefore) {
131             $printer->writeln();
132         }
133     }
134
135     /**
136      * Prints teardown hook call result.
137      *
138      * @param OutputPrinter $printer
139      * @param CallResult    $callResult
140      */
141     private function printTeardownHookCallResult(OutputPrinter $printer, CallResult $callResult)
142     {
143         if (!$callResult->hasStdOut() && !$callResult->hasException()) {
144             return;
145         }
146
147         $resultCode = $callResult->hasException() ? TestResult::FAILED : TestResult::PASSED;
148         $style = $this->resultConverter->convertResultCodeToString($resultCode);
149         $hook = $callResult->getCall()->getCallee();
150         $path = $hook->getPath();
151
152         $printer->writeln(sprintf('%s│', $this->indentText));
153
154         $this->printHookCallStdOut($printer, $callResult, $this->indentText);
155         $this->printHookCallException($printer, $callResult, $this->indentText);
156
157         $printer->writeln(
158             sprintf('%s└─ {+%s}@%s{-%s} {+comment}# %s{-comment}', $this->indentText, $style, $hook, $style, $path)
159         );
160
161         if ($this->newlineAfter) {
162             $printer->writeln();
163         }
164     }
165
166     /**
167      * Prints hook call output (if has some).
168      *
169      * @param OutputPrinter $printer
170      * @param CallResult    $callResult
171      * @param string        $indentText
172      */
173     private function printHookCallStdOut(OutputPrinter $printer, CallResult $callResult, $indentText)
174     {
175         if (!$callResult->hasStdOut()) {
176             return;
177         }
178
179         $pad = function ($line) use ($indentText) {
180             return sprintf(
181                 '%s│  {+stdout}%s{-stdout}', $indentText, $line
182             );
183         };
184
185         $printer->writeln(implode("\n", array_map($pad, explode("\n", $callResult->getStdOut()))));
186         $printer->writeln(sprintf('%s│', $indentText));
187     }
188
189     /**
190      * Prints hook call exception (if has some).
191      *
192      * @param OutputPrinter $printer
193      * @param CallResult    $callResult
194      * @param string        $indentText
195      */
196     private function printHookCallException(OutputPrinter $printer, CallResult $callResult, $indentText)
197     {
198         if (!$callResult->hasException()) {
199             return;
200         }
201
202         $pad = function ($l) use ($indentText) {
203             return sprintf(
204                 '%s╳  {+exception}%s{-exception}', $indentText, $l
205             );
206         };
207
208         $exception = $this->exceptionPresenter->presentException($callResult->getException());
209         $printer->writeln(implode("\n", array_map($pad, explode("\n", $exception))));
210         $printer->writeln(sprintf('%s│', $indentText));
211     }
212 }