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\Pretty;
13 use Behat\Behat\Output\Node\Printer\Helper\ResultToStringConverter;
14 use Behat\Behat\Output\Node\Printer\Helper\StepTextPainter;
15 use Behat\Behat\Output\Node\Printer\StepPrinter;
16 use Behat\Behat\Tester\Result\DefinedStepResult;
17 use Behat\Behat\Tester\Result\ExecutedStepResult;
18 use Behat\Behat\Tester\Result\StepResult;
19 use Behat\Gherkin\Node\ArgumentInterface;
20 use Behat\Gherkin\Node\PyStringNode;
21 use Behat\Gherkin\Node\ScenarioLikeInterface as Scenario;
22 use Behat\Gherkin\Node\StepNode;
23 use Behat\Testwork\Exception\ExceptionPresenter;
24 use Behat\Testwork\Output\Formatter;
25 use Behat\Testwork\Output\Printer\OutputPrinter;
26 use Behat\Testwork\Tester\Result\ExceptionResult;
31 * @author Konstantin Kudryashov <ever.zet@gmail.com>
33 final class PrettyStepPrinter implements StepPrinter
36 * @var StepTextPainter
40 * @var ResultToStringConverter
42 private $resultConverter;
44 * @var PrettyPathPrinter
48 * @var ExceptionPresenter
50 private $exceptionPresenter;
58 private $subIndentText;
61 * Initializes printer.
63 * @param StepTextPainter $textPainter
64 * @param ResultToStringConverter $resultConverter
65 * @param PrettyPathPrinter $pathPrinter
66 * @param ExceptionPresenter $exceptionPresenter
67 * @param integer $indentation
68 * @param integer $subIndentation
70 public function __construct(
71 StepTextPainter $textPainter,
72 ResultToStringConverter $resultConverter,
73 PrettyPathPrinter $pathPrinter,
74 ExceptionPresenter $exceptionPresenter,
78 $this->textPainter = $textPainter;
79 $this->resultConverter = $resultConverter;
80 $this->pathPrinter = $pathPrinter;
81 $this->exceptionPresenter = $exceptionPresenter;
82 $this->indentText = str_repeat(' ', intval($indentation));
83 $this->subIndentText = $this->indentText . str_repeat(' ', intval($subIndentation));
89 public function printStep(Formatter $formatter, Scenario $scenario, StepNode $step, StepResult $result)
91 $this->printText($formatter->getOutputPrinter(), $step->getKeyword(), $step->getText(), $result);
92 $this->pathPrinter->printStepPath($formatter, $scenario, $step, $result, mb_strlen($this->indentText, 'utf8'));
93 $this->printArguments($formatter, $step->getArguments(), $result);
94 $this->printStdOut($formatter->getOutputPrinter(), $result);
95 $this->printException($formatter->getOutputPrinter(), $result);
101 * @param OutputPrinter $printer
102 * @param string $stepType
103 * @param string $stepText
104 * @param StepResult $result
106 private function printText(OutputPrinter $printer, $stepType, $stepText, StepResult $result)
108 if ($result && $result instanceof DefinedStepResult && $result->getStepDefinition()) {
109 $definition = $result->getStepDefinition();
110 $stepText = $this->textPainter->paintText($stepText, $definition, $result);
113 $style = $this->resultConverter->convertResultToString($result);
114 $printer->write(sprintf('%s{+%s}%s %s{-%s}', $this->indentText, $style, $stepType, $stepText, $style));
118 * Prints step multiline arguments.
120 * @param Formatter $formatter
121 * @param ArgumentInterface[] $arguments
122 * @param StepResult $result
124 private function printArguments(Formatter $formatter, array $arguments, StepResult $result)
126 $style = $this->resultConverter->convertResultToString($result);
128 foreach ($arguments as $argument) {
129 $text = $this->getArgumentString($argument, !$formatter->getParameter('multiline'));
131 $indentedText = implode("\n", array_map(array($this, 'subIndent'), explode("\n", $text)));
132 $formatter->getOutputPrinter()->writeln(sprintf('{+%s}%s{-%s}', $style, $indentedText, $style));
137 * Prints step output (if has one).
139 * @param OutputPrinter $printer
140 * @param StepResult $result
142 private function printStdOut(OutputPrinter $printer, StepResult $result)
144 if (!$result instanceof ExecutedStepResult || null === $result->getCallResult()->getStdOut()) {
148 $callResult = $result->getCallResult();
149 $indentedText = $this->subIndentText;
151 $pad = function ($line) use ($indentedText) {
153 '%s│ {+stdout}%s{-stdout}', $indentedText, $line
157 $printer->writeln(implode("\n", array_map($pad, explode("\n", $callResult->getStdOut()))));
161 * Prints step exception (if has one).
163 * @param OutputPrinter $printer
164 * @param StepResult $result
166 private function printException(OutputPrinter $printer, StepResult $result)
168 $style = $this->resultConverter->convertResultToString($result);
170 if (!$result instanceof ExceptionResult || !$result->hasException()) {
174 $text = $this->exceptionPresenter->presentException($result->getException());
175 $indentedText = implode("\n", array_map(array($this, 'subIndent'), explode("\n", $text)));
176 $printer->writeln(sprintf('{+%s}%s{-%s}', $style, $indentedText, $style));
180 * Returns argument string for provided argument.
182 * @param ArgumentInterface $argument
183 * @param Boolean $collapse
187 private function getArgumentString(ArgumentInterface $argument, $collapse = false)
193 if ($argument instanceof PyStringNode) {
194 $text = '"""' . "\n" . $argument . "\n" . '"""';
199 return (string) $argument;
203 * Indents text to the subIndentation level.
205 * @param string $text
209 private function subIndent($text)
211 return $this->subIndentText . $text;