Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Output / Node / Printer / ListPrinter.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;
12
13 use Behat\Behat\Output\Node\Printer\Helper\ResultToStringConverter;
14 use Behat\Behat\Output\Statistics\HookStat;
15 use Behat\Behat\Output\Statistics\ScenarioStat;
16 use Behat\Behat\Output\Statistics\StepStatV2;
17 use Behat\Behat\Output\Statistics\StepStat;
18 use Behat\Testwork\Exception\ExceptionPresenter;
19 use Behat\Testwork\Output\Printer\OutputPrinter;
20 use Behat\Testwork\Tester\Result\TestResult;
21 use Symfony\Component\Translation\TranslatorInterface;
22
23 /**
24  * Behat list printer.
25  *
26  * @author Konstantin Kudryashov <ever.zet@gmail.com>
27  */
28 final class ListPrinter
29 {
30     /**
31      * @var ResultToStringConverter
32      */
33     private $resultConverter;
34     /**
35      * @var ExceptionPresenter
36      */
37     private $exceptionPresenter;
38     /**
39      * @var TranslatorInterface
40      */
41     private $translator;
42     /**
43      * @var string
44      */
45     private $basePath;
46
47     /**
48      * Initializes printer.
49      *
50      * @param ResultToStringConverter $resultConverter
51      * @param ExceptionPresenter      $exceptionPresenter
52      * @param TranslatorInterface     $translator
53      * @param string                  $basePath
54      */
55     public function __construct(
56         ResultToStringConverter $resultConverter,
57         ExceptionPresenter $exceptionPresenter,
58         TranslatorInterface $translator,
59         $basePath
60     ) {
61         $this->resultConverter = $resultConverter;
62         $this->exceptionPresenter = $exceptionPresenter;
63         $this->translator = $translator;
64         $this->basePath = $basePath;
65     }
66
67     /**
68      * Prints scenarios list.
69      *
70      * @param OutputPrinter  $printer
71      * @param string         $intro
72      * @param integer        $resultCode
73      * @param ScenarioStat[] $scenarioStats
74      */
75     public function printScenariosList(OutputPrinter $printer, $intro, $resultCode, array $scenarioStats)
76     {
77         if (!count($scenarioStats)) {
78             return;
79         }
80
81         $style = $this->resultConverter->convertResultCodeToString($resultCode);
82         $intro = $this->translator->trans($intro, array(), 'output');
83
84         $printer->writeln(sprintf('--- {+%s}%s{-%s}' . PHP_EOL, $style, $intro, $style));
85         foreach ($scenarioStats as $stat) {
86             $path = $this->relativizePaths((string) $stat);
87             $printer->writeln(sprintf('    {+%s}%s{-%s}', $style, $path, $style));
88         }
89
90         $printer->writeln();
91     }
92
93     /**
94      * Prints step list.
95      *
96      * @param OutputPrinter $printer
97      * @param string        $intro
98      * @param integer       $resultCode
99      * @param StepStat[]    $stepStats
100      */
101     public function printStepList(OutputPrinter $printer, $intro, $resultCode, array $stepStats)
102     {
103         if (!count($stepStats)) {
104             return;
105         }
106
107         $style = $this->resultConverter->convertResultCodeToString($resultCode);
108         $intro = $this->translator->trans($intro, array(), 'output');
109
110         $printer->writeln(sprintf('--- {+%s}%s{-%s}' . PHP_EOL, $style, $intro, $style));
111
112         foreach ($stepStats as $num => $stepStat) {
113             if ($stepStat instanceof StepStatV2) {
114                 $this->printStepStat($printer, $num + 1, $stepStat, $style);
115             } elseif ($stepStat instanceof StepStat) {
116                 $this->printStat($printer, $stepStat->getText(), $stepStat->getPath(), $style, $stepStat->getStdOut(), $stepStat->getError());
117             }
118         }
119     }
120
121     /**
122      * Prints failed hooks list.
123      *
124      * @param OutputPrinter $printer
125      * @param string        $intro
126      * @param HookStat[]    $failedHookStats
127      */
128     public function printFailedHooksList(OutputPrinter $printer, $intro, array $failedHookStats)
129     {
130         if (!count($failedHookStats)) {
131             return;
132         }
133
134         $style = $this->resultConverter->convertResultCodeToString(TestResult::FAILED);
135         $intro = $this->translator->trans($intro, array(), 'output');
136
137         $printer->writeln(sprintf('--- {+%s}%s{-%s}' . PHP_EOL, $style, $intro, $style));
138         foreach ($failedHookStats as $hookStat) {
139             $this->printHookStat($printer, $hookStat, $style);
140         }
141     }
142
143     /**
144      * Prints hook stat.
145      *
146      * @param OutputPrinter $printer
147      * @param string        $name
148      * @param string        $path
149      * @param string        $style
150      * @param null|string   $stdOut
151      * @param null|string   $error
152      *
153      * @deprecated Remove in 4.0
154      */
155     private function printStat(OutputPrinter $printer, $name, $path, $style, $stdOut, $error)
156     {
157         $path = $this->relativizePaths($path);
158         $printer->writeln(sprintf('    {+%s}%s{-%s} {+comment}# %s{-comment}', $style, $name, $style, $path));
159
160         $pad = function ($line) { return '      ' . $line; };
161
162         if (null !== $stdOut) {
163             $padText = function ($line) { return '      │ ' . $line; };
164             $stdOutString = array_map($padText, explode("\n", $stdOut));
165             $printer->writeln(implode("\n", $stdOutString));
166         }
167
168         if ($error) {
169             $exceptionString = implode("\n", array_map($pad, explode("\n", $error)));
170             $printer->writeln(sprintf('{+%s}%s{-%s}', $style, $exceptionString, $style));
171         }
172
173         $printer->writeln();
174     }
175
176     /**
177      * Prints hook stat.
178      *
179      * @param OutputPrinter $printer
180      * @param HookStat      $hookStat
181      * @param string        $style
182      */
183     private function printHookStat(OutputPrinter $printer, HookStat $hookStat, $style)
184     {
185         $printer->writeln(
186             sprintf('    {+%s}%s{-%s} {+comment}# %s{-comment}',
187                 $style, $hookStat->getName(), $style, $this->relativizePaths($hookStat->getPath())
188             )
189         );
190
191         $pad = function ($line) { return '      ' . $line; };
192
193         if (null !== $hookStat->getStdOut()) {
194             $padText = function ($line) { return '      │ ' . $line; };
195             $stdOutString = array_map($padText, explode("\n", $hookStat->getStdOut()));
196             $printer->writeln(implode("\n", $stdOutString));
197         }
198
199         if ($hookStat->getError()) {
200             $exceptionString = implode("\n", array_map($pad, explode("\n", $hookStat->getError())));
201             $printer->writeln(sprintf('{+%s}%s{-%s}', $style, $exceptionString, $style));
202         }
203
204         $printer->writeln();
205     }
206
207     /**
208      * Prints hook stat.
209      *
210      * @param OutputPrinter $printer
211      * @param integer       $number
212      * @param StepStatV2    $stat
213      * @param string        $style
214      */
215     private function printStepStat(OutputPrinter $printer, $number, StepStatV2 $stat, $style)
216     {
217         $maxLength = max(mb_strlen($stat->getScenarioText(), 'utf8'), mb_strlen($stat->getStepText(), 'utf8') + 2) + 1;
218
219         $printer->writeln(
220             sprintf('%03d {+%s}%s{-%s}%s{+comment}# %s{-comment}',
221                 $number,
222                 $style,
223                 $stat->getScenarioText(),
224                 $style,
225                 str_pad(' ', $maxLength - mb_strlen($stat->getScenarioText(), 'utf8')),
226                 $this->relativizePaths($stat->getScenarioPath())
227             )
228         );
229
230         $printer->writeln(
231             sprintf('      {+%s}%s{-%s}%s{+comment}# %s{-comment}',
232                 $style,
233                 $stat->getStepText(),
234                 $style,
235                 str_pad(' ', $maxLength - mb_strlen($stat->getStepText(), 'utf8') - 2),
236                 $this->relativizePaths($stat->getStepPath())
237             )
238         );
239
240         $pad = function ($line) { return '        ' . $line; };
241
242         if (null !== $stat->getStdOut()) {
243             $padText = function ($line) { return '        │ ' . $line; };
244             $stdOutString = array_map($padText, explode("\n", $stat->getStdOut()));
245             $printer->writeln(implode("\n", $stdOutString));
246         }
247
248         if ($stat->getError()) {
249             $exceptionString = implode("\n", array_map($pad, explode("\n", $stat->getError())));
250             $printer->writeln(sprintf('{+%s}%s{-%s}', $style, $exceptionString, $style));
251         }
252
253         $printer->writeln();
254     }
255
256     /**
257      * Transforms path to relative.
258      *
259      * @param string $path
260      *
261      * @return string
262      */
263     private function relativizePaths($path)
264     {
265         if (!$this->basePath) {
266             return $path;
267         }
268
269         return str_replace($this->basePath . DIRECTORY_SEPARATOR, '', $path);
270     }
271 }