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\OutlinePrinter;
15 use Behat\Behat\Output\Node\Printer\ScenarioPrinter;
16 use Behat\Behat\Output\Node\Printer\StepPrinter;
17 use Behat\Behat\Tester\Result\UndefinedStepResult;
18 use Behat\Gherkin\Node\ExampleTableNode;
19 use Behat\Gherkin\Node\FeatureNode;
20 use Behat\Gherkin\Node\OutlineNode;
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\TestResult;
27 * Prints outline header with outline steps and table header.
29 * @author Konstantin Kudryashov <ever.zet@gmail.com>
31 final class PrettyOutlinePrinter implements OutlinePrinter
34 * @var ScenarioPrinter
36 private $scenarioPrinter;
42 * @var ResultToStringConverter
44 private $resultConverter;
52 private $subIndentText;
55 * @param ScenarioPrinter $scenarioPrinter
56 * @param StepPrinter $stepPrinter
57 * @param ResultToStringConverter $resultConverter
58 * @param integer $indentation
59 * @param integer $subIndentation
61 public function __construct(
62 ScenarioPrinter $scenarioPrinter,
63 StepPrinter $stepPrinter,
64 ResultToStringConverter $resultConverter,
68 $this->scenarioPrinter = $scenarioPrinter;
69 $this->stepPrinter = $stepPrinter;
70 $this->resultConverter = $resultConverter;
71 $this->indentText = str_repeat(' ', intval($indentation));
72 $this->subIndentText = $this->indentText . str_repeat(' ', intval($subIndentation));
78 public function printHeader(Formatter $formatter, FeatureNode $feature, OutlineNode $outline)
80 $this->scenarioPrinter->printHeader($formatter, $feature, $outline);
82 $this->printExamplesSteps($formatter, $outline, $outline->getSteps());
83 $this->printExamplesTableHeader($formatter->getOutputPrinter(), $outline->getExampleTable());
89 public function printFooter(Formatter $formatter, TestResult $result)
91 $formatter->getOutputPrinter()->writeln();
95 * Prints outline steps.
97 * @param Formatter $formatter
98 * @param OutlineNode $outline
99 * @param StepNode[] $steps
101 private function printExamplesSteps(Formatter $formatter, OutlineNode $outline, array $steps)
103 foreach ($steps as $step) {
104 $this->stepPrinter->printStep($formatter, $outline, $step, new UndefinedStepResult());
107 $formatter->getOutputPrinter()->writeln();
111 * Prints examples table header.
113 * @param OutputPrinter $printer
114 * @param ExampleTableNode $table
116 private function printExamplesTableHeader(OutputPrinter $printer, ExampleTableNode $table)
118 $printer->writeln(sprintf('%s{+keyword}%s:{-keyword}', $this->indentText, $table->getKeyword()));
121 $wrapper = $this->getWrapperClosure();
122 $row = $table->getRowAsStringWithWrappedValues($rowNum, $wrapper);
124 $printer->writeln(sprintf('%s%s', $this->subIndentText, $row));
128 * Creates wrapper-closure for the example header.
132 private function getWrapperClosure()
134 $style = $this->resultConverter->convertResultCodeToString(TestResult::SKIPPED);
136 return function ($col) use ($style) {
137 return sprintf('{+%s_param}%s{-%s_param}', $style, $col, $style);