Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Output / Node / Printer / Pretty / PrettyScenarioPrinter.php
1 <?php
2
3 /*
4  * This file is part of the Behat.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Behat\Output\Node\Printer\Pretty;
12
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;
20
21 /**
22  * Prints scenario headers (with tags, keyword and long title) and footers.
23  *
24  * @author Konstantin Kudryashov <ever.zet@gmail.com>
25  */
26 final class PrettyScenarioPrinter implements ScenarioPrinter
27 {
28     /**
29      * @var PrettyPathPrinter
30      */
31     private $pathPrinter;
32     /**
33      * @var string
34      */
35     private $indentText;
36     /**
37      * @var string
38      */
39     private $subIndentText;
40
41     /**
42      * Initializes printer.
43      *
44      * @param PrettyPathPrinter $pathPrinter
45      * @param integer           $indentation
46      * @param integer           $subIndentation
47      */
48     public function __construct(PrettyPathPrinter $pathPrinter, $indentation = 2, $subIndentation = 2)
49     {
50         $this->pathPrinter = $pathPrinter;
51         $this->indentText = str_repeat(' ', intval($indentation));
52         $this->subIndentText = $this->indentText . str_repeat(' ', intval($subIndentation));
53     }
54
55     /**
56      * {@inheritdoc}
57      */
58     public function printHeader(Formatter $formatter, FeatureNode $feature, Scenario $scenario)
59     {
60         if ($scenario instanceof TaggedNodeInterface) {
61             $this->printTags($formatter->getOutputPrinter(), $scenario->getTags());
62         }
63
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());
68     }
69
70     /**
71      * {@inheritdoc}
72      */
73     public function printFooter(Formatter $formatter, TestResult $result)
74     {
75         $formatter->getOutputPrinter()->writeln();
76     }
77
78     /**
79      * Prints scenario tags.
80      *
81      * @param OutputPrinter $printer
82      * @param string[]      $tags
83      */
84     private function printTags(OutputPrinter $printer, array $tags)
85     {
86         if (!count($tags)) {
87             return;
88         }
89
90         $tags = array_map(array($this, 'prependTagWithTagSign'), $tags);
91         $printer->writeln(sprintf('%s{+tag}%s{-tag}', $this->indentText, implode(' ', $tags)));
92     }
93
94     /**
95      * Prints scenario keyword.
96      *
97      * @param OutputPrinter $printer
98      * @param string        $keyword
99      */
100     private function printKeyword(OutputPrinter $printer, $keyword)
101     {
102         $printer->write(sprintf('%s{+keyword}%s:{-keyword}', $this->indentText, $keyword));
103     }
104
105     /**
106      * Prints scenario title (first line of long title).
107      *
108      * @param OutputPrinter $printer
109      * @param string        $longTitle
110      */
111     private function printTitle(OutputPrinter $printer, $longTitle)
112     {
113         $description = explode("\n", $longTitle);
114         $title = array_shift($description);
115
116         if ('' !== $title) {
117             $printer->write(sprintf(' %s', $title));
118         }
119     }
120
121     /**
122      * Prints scenario description (other lines of long title).
123      *
124      * @param OutputPrinter $printer
125      * @param string        $longTitle
126      */
127     private function printDescription(OutputPrinter $printer, $longTitle)
128     {
129         $lines = explode("\n", $longTitle);
130         array_shift($lines);
131
132         foreach ($lines as $line) {
133             $printer->writeln(sprintf('%s%s', $this->subIndentText, $line));
134         }
135     }
136
137     /**
138      * Prepends tags string with tag-sign.
139      *
140      * @param string $tag
141      *
142      * @return string
143      */
144     private function prependTagWithTagSign($tag)
145     {
146         return '@' . $tag;
147     }
148 }