4 * This file is part of the Behat.
5 * (c) Konstantin Kudryashov <ever.zet@gmail.com>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 namespace Behat\Behat\Output\Node\Printer;
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;
26 * @author Konstantin Kudryashov <ever.zet@gmail.com>
28 final class ListPrinter
31 * @var ResultToStringConverter
33 private $resultConverter;
35 * @var ExceptionPresenter
37 private $exceptionPresenter;
39 * @var TranslatorInterface
48 * Initializes printer.
50 * @param ResultToStringConverter $resultConverter
51 * @param ExceptionPresenter $exceptionPresenter
52 * @param TranslatorInterface $translator
53 * @param string $basePath
55 public function __construct(
56 ResultToStringConverter $resultConverter,
57 ExceptionPresenter $exceptionPresenter,
58 TranslatorInterface $translator,
61 $this->resultConverter = $resultConverter;
62 $this->exceptionPresenter = $exceptionPresenter;
63 $this->translator = $translator;
64 $this->basePath = $basePath;
68 * Prints scenarios list.
70 * @param OutputPrinter $printer
71 * @param string $intro
72 * @param integer $resultCode
73 * @param ScenarioStat[] $scenarioStats
75 public function printScenariosList(OutputPrinter $printer, $intro, $resultCode, array $scenarioStats)
77 if (!count($scenarioStats)) {
81 $style = $this->resultConverter->convertResultCodeToString($resultCode);
82 $intro = $this->translator->trans($intro, array(), 'output');
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));
96 * @param OutputPrinter $printer
97 * @param string $intro
98 * @param integer $resultCode
99 * @param StepStat[] $stepStats
101 public function printStepList(OutputPrinter $printer, $intro, $resultCode, array $stepStats)
103 if (!count($stepStats)) {
107 $style = $this->resultConverter->convertResultCodeToString($resultCode);
108 $intro = $this->translator->trans($intro, array(), 'output');
110 $printer->writeln(sprintf('--- {+%s}%s{-%s}' . PHP_EOL, $style, $intro, $style));
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());
122 * Prints failed hooks list.
124 * @param OutputPrinter $printer
125 * @param string $intro
126 * @param HookStat[] $failedHookStats
128 public function printFailedHooksList(OutputPrinter $printer, $intro, array $failedHookStats)
130 if (!count($failedHookStats)) {
134 $style = $this->resultConverter->convertResultCodeToString(TestResult::FAILED);
135 $intro = $this->translator->trans($intro, array(), 'output');
137 $printer->writeln(sprintf('--- {+%s}%s{-%s}' . PHP_EOL, $style, $intro, $style));
138 foreach ($failedHookStats as $hookStat) {
139 $this->printHookStat($printer, $hookStat, $style);
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
153 * @deprecated Remove in 4.0
155 private function printStat(OutputPrinter $printer, $name, $path, $style, $stdOut, $error)
157 $path = $this->relativizePaths($path);
158 $printer->writeln(sprintf(' {+%s}%s{-%s} {+comment}# %s{-comment}', $style, $name, $style, $path));
160 $pad = function ($line) { return ' ' . $line; };
162 if (null !== $stdOut) {
163 $padText = function ($line) { return ' │ ' . $line; };
164 $stdOutString = array_map($padText, explode("\n", $stdOut));
165 $printer->writeln(implode("\n", $stdOutString));
169 $exceptionString = implode("\n", array_map($pad, explode("\n", $error)));
170 $printer->writeln(sprintf('{+%s}%s{-%s}', $style, $exceptionString, $style));
179 * @param OutputPrinter $printer
180 * @param HookStat $hookStat
181 * @param string $style
183 private function printHookStat(OutputPrinter $printer, HookStat $hookStat, $style)
186 sprintf(' {+%s}%s{-%s} {+comment}# %s{-comment}',
187 $style, $hookStat->getName(), $style, $this->relativizePaths($hookStat->getPath())
191 $pad = function ($line) { return ' ' . $line; };
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));
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));
210 * @param OutputPrinter $printer
211 * @param integer $number
212 * @param StepStatV2 $stat
213 * @param string $style
215 private function printStepStat(OutputPrinter $printer, $number, StepStatV2 $stat, $style)
217 $maxLength = max(mb_strlen($stat->getScenarioText(), 'utf8'), mb_strlen($stat->getStepText(), 'utf8') + 2) + 1;
220 sprintf('%03d {+%s}%s{-%s}%s{+comment}# %s{-comment}',
223 $stat->getScenarioText(),
225 str_pad(' ', $maxLength - mb_strlen($stat->getScenarioText(), 'utf8')),
226 $this->relativizePaths($stat->getScenarioPath())
231 sprintf(' {+%s}%s{-%s}%s{+comment}# %s{-comment}',
233 $stat->getStepText(),
235 str_pad(' ', $maxLength - mb_strlen($stat->getStepText(), 'utf8') - 2),
236 $this->relativizePaths($stat->getStepPath())
240 $pad = function ($line) { return ' ' . $line; };
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));
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));
257 * Transforms path to relative.
259 * @param string $path
263 private function relativizePaths($path)
265 if (!$this->basePath) {
269 return str_replace($this->basePath . DIRECTORY_SEPARATOR, '', $path);