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\ScenarioPrinter;
14 use Behat\Gherkin\Node\FeatureNode;
15 use Behat\Gherkin\Node\ScenarioLikeInterface as Scenario;
16 use Behat\Gherkin\Node\TaggedNodeInterface;
17 use Behat\Testwork\Output\Formatter;
18 use Behat\Testwork\Output\Printer\OutputPrinter;
19 use Behat\Testwork\Tester\Result\TestResult;
22 * Prints scenario headers (with tags, keyword and long title) and footers.
24 * @author Konstantin Kudryashov <ever.zet@gmail.com>
26 final class PrettyScenarioPrinter implements ScenarioPrinter
29 * @var PrettyPathPrinter
39 private $subIndentText;
42 * Initializes printer.
44 * @param PrettyPathPrinter $pathPrinter
45 * @param integer $indentation
46 * @param integer $subIndentation
48 public function __construct(PrettyPathPrinter $pathPrinter, $indentation = 2, $subIndentation = 2)
50 $this->pathPrinter = $pathPrinter;
51 $this->indentText = str_repeat(' ', intval($indentation));
52 $this->subIndentText = $this->indentText . str_repeat(' ', intval($subIndentation));
58 public function printHeader(Formatter $formatter, FeatureNode $feature, Scenario $scenario)
60 if ($scenario instanceof TaggedNodeInterface) {
61 $this->printTags($formatter->getOutputPrinter(), $scenario->getTags());
64 $this->printKeyword($formatter->getOutputPrinter(), $scenario->getKeyword());
65 $this->printTitle($formatter->getOutputPrinter(), $scenario->getTitle());
66 $this->pathPrinter->printScenarioPath($formatter, $feature, $scenario, mb_strlen($this->indentText, 'utf8'));
67 $this->printDescription($formatter->getOutputPrinter(), $scenario->getTitle());
73 public function printFooter(Formatter $formatter, TestResult $result)
75 $formatter->getOutputPrinter()->writeln();
79 * Prints scenario tags.
81 * @param OutputPrinter $printer
82 * @param string[] $tags
84 private function printTags(OutputPrinter $printer, array $tags)
90 $tags = array_map(array($this, 'prependTagWithTagSign'), $tags);
91 $printer->writeln(sprintf('%s{+tag}%s{-tag}', $this->indentText, implode(' ', $tags)));
95 * Prints scenario keyword.
97 * @param OutputPrinter $printer
98 * @param string $keyword
100 private function printKeyword(OutputPrinter $printer, $keyword)
102 $printer->write(sprintf('%s{+keyword}%s:{-keyword}', $this->indentText, $keyword));
106 * Prints scenario title (first line of long title).
108 * @param OutputPrinter $printer
109 * @param string $longTitle
111 private function printTitle(OutputPrinter $printer, $longTitle)
113 $description = explode("\n", $longTitle);
114 $title = array_shift($description);
117 $printer->write(sprintf(' %s', $title));
122 * Prints scenario description (other lines of long title).
124 * @param OutputPrinter $printer
125 * @param string $longTitle
127 private function printDescription(OutputPrinter $printer, $longTitle)
129 $lines = explode("\n", $longTitle);
132 foreach ($lines as $line) {
133 $printer->writeln(sprintf('%s%s', $this->subIndentText, $line));
138 * Prepends tags string with tag-sign.
144 private function prependTagWithTagSign($tag)