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\FeaturePrinter;
14 use Behat\Gherkin\Node\FeatureNode;
15 use Behat\Gherkin\Node\TaggedNodeInterface;
16 use Behat\Testwork\Output\Formatter;
17 use Behat\Testwork\Output\Printer\OutputPrinter;
18 use Behat\Testwork\Tester\Result\TestResult;
21 * Prints feature header and footer.
23 * @author Konstantin Kudryashov <ever.zet@gmail.com>
25 final class PrettyFeaturePrinter implements FeaturePrinter
34 private $subIndentText;
37 * Initializes printer.
39 * @param integer $indentation
40 * @param integer $subIndentation
42 public function __construct($indentation = 0, $subIndentation = 2)
44 $this->indentText = str_repeat(' ', intval($indentation));
45 $this->subIndentText = $this->indentText . str_repeat(' ', intval($subIndentation));
51 public function printHeader(Formatter $formatter, FeatureNode $feature)
53 if ($feature instanceof TaggedNodeInterface) {
54 $this->printTags($formatter->getOutputPrinter(), $feature->getTags());
57 $this->printTitle($formatter->getOutputPrinter(), $feature);
58 $this->printDescription($formatter->getOutputPrinter(), $feature);
64 public function printFooter(Formatter $formatter, TestResult $result)
69 * Prints feature tags.
71 * @param OutputPrinter $printer
72 * @param string[] $tags
74 private function printTags(OutputPrinter $printer, array $tags)
80 $tags = array_map(array($this, 'prependTagWithTagSign'), $tags);
81 $printer->writeln(sprintf('%s{+tag}%s{-tag}', $this->indentText, implode(' ', $tags)));
85 * Prints feature title using provided printer.
87 * @param OutputPrinter $printer
88 * @param FeatureNode $feature
90 private function printTitle(OutputPrinter $printer, FeatureNode $feature)
92 $printer->write(sprintf('%s{+keyword}%s:{-keyword}', $this->indentText, $feature->getKeyword()));
94 if ($title = $feature->getTitle()) {
95 $printer->write(sprintf(' %s', $title));
102 * Prints feature description using provided printer.
104 * @param OutputPrinter $printer
105 * @param FeatureNode $feature
107 private function printDescription(OutputPrinter $printer, FeatureNode $feature)
109 if (!$feature->getDescription()) {
115 foreach (explode("\n", $feature->getDescription()) as $descriptionLine) {
116 $printer->writeln(sprintf('%s%s', $this->subIndentText, $descriptionLine));
123 * Prepends tags string with tag-sign.
129 private function prependTagWithTagSign($tag)