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\StepResult;
18 use Behat\Gherkin\Node\ArgumentInterface;
19 use Behat\Gherkin\Node\PyStringNode;
20 use Behat\Gherkin\Node\ScenarioLikeInterface as Scenario;
21 use Behat\Gherkin\Node\StepNode;
22 use Behat\Testwork\Output\Formatter;
23 use Behat\Testwork\Output\Printer\OutputPrinter;
24 use Behat\Testwork\Tester\Result\IntegerTestResult;
25 use Behat\Testwork\Tester\Result\TestResult;
28 * Prints steps as skipped.
30 * @author Konstantin Kudryashov <ever.zet@gmail.com>
32 final class PrettySkippedStepPrinter implements StepPrinter
35 * @var StepTextPainter
39 * @var ResultToStringConverter
41 private $resultConverter;
43 * @var PrettyPathPrinter
53 private $subIndentText;
56 * Initializes printer.
58 * @param StepTextPainter $textPainter
59 * @param ResultToStringConverter $resultConverter
60 * @param PrettyPathPrinter $pathPrinter
61 * @param integer $indentation
62 * @param integer $subIndentation
64 public function __construct(
65 StepTextPainter $textPainter,
66 ResultToStringConverter $resultConverter,
67 PrettyPathPrinter $pathPrinter,
71 $this->textPainter = $textPainter;
72 $this->resultConverter = $resultConverter;
73 $this->pathPrinter = $pathPrinter;
74 $this->indentText = str_repeat(' ', intval($indentation));
75 $this->subIndentText = $this->indentText . str_repeat(' ', intval($subIndentation));
81 public function printStep(Formatter $formatter, Scenario $scenario, StepNode $step, StepResult $result)
83 $this->printText($formatter->getOutputPrinter(), $step->getKeyword(), $step->getText(), $result);
84 $this->pathPrinter->printStepPath($formatter, $scenario, $step, $result, mb_strlen($this->indentText, 'utf8'));
85 $this->printArguments($formatter, $step->getArguments());
91 * @param OutputPrinter $printer
92 * @param string $stepType
93 * @param string $stepText
94 * @param StepResult $result
96 private function printText(OutputPrinter $printer, $stepType, $stepText, StepResult $result)
98 $style = $this->resultConverter->convertResultCodeToString(TestResult::SKIPPED);
100 if ($result instanceof DefinedStepResult && $result->getStepDefinition()) {
101 $definition = $result->getStepDefinition();
102 $stepText = $this->textPainter->paintText(
103 $stepText, $definition, new IntegerTestResult(TestResult::SKIPPED)
107 $printer->write(sprintf('%s{+%s}%s %s{-%s}', $this->indentText, $style, $stepType, $stepText, $style));
111 * Prints step multiline arguments.
113 * @param Formatter $formatter
114 * @param ArgumentInterface[] $arguments
116 private function printArguments(Formatter $formatter, array $arguments)
118 $style = $this->resultConverter->convertResultCodeToString(TestResult::SKIPPED);
120 foreach ($arguments as $argument) {
121 $text = $this->getArgumentString($argument, !$formatter->getParameter('multiline'));
123 $indentedText = implode("\n", array_map(array($this, 'subIndent'), explode("\n", $text)));
124 $formatter->getOutputPrinter()->writeln(sprintf('{+%s}%s{-%s}', $style, $indentedText, $style));
129 * Returns argument string for provided argument.
131 * @param ArgumentInterface $argument
132 * @param Boolean $collapse
136 private function getArgumentString(ArgumentInterface $argument, $collapse = false)
142 if ($argument instanceof PyStringNode) {
143 $text = '"""' . "\n" . $argument . "\n" . '"""';
148 return (string) $argument;
152 * Indents text to the subIndentation level.
154 * @param string $text
158 private function subIndent($text)
160 return $this->subIndentText . $text;