Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Output / Node / Printer / Pretty / PrettyStepPrinter.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\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\ExecutedStepResult;
18 use Behat\Behat\Tester\Result\StepResult;
19 use Behat\Gherkin\Node\ArgumentInterface;
20 use Behat\Gherkin\Node\PyStringNode;
21 use Behat\Gherkin\Node\ScenarioLikeInterface as Scenario;
22 use Behat\Gherkin\Node\StepNode;
23 use Behat\Testwork\Exception\ExceptionPresenter;
24 use Behat\Testwork\Output\Formatter;
25 use Behat\Testwork\Output\Printer\OutputPrinter;
26 use Behat\Testwork\Tester\Result\ExceptionResult;
27
28 /**
29  * Prints step.
30  *
31  * @author Konstantin Kudryashov <ever.zet@gmail.com>
32  */
33 final class PrettyStepPrinter implements StepPrinter
34 {
35     /**
36      * @var StepTextPainter
37      */
38     private $textPainter;
39     /**
40      * @var ResultToStringConverter
41      */
42     private $resultConverter;
43     /**
44      * @var PrettyPathPrinter
45      */
46     private $pathPrinter;
47     /**
48      * @var ExceptionPresenter
49      */
50     private $exceptionPresenter;
51     /**
52      * @var string
53      */
54     private $indentText;
55     /**
56      * @var string
57      */
58     private $subIndentText;
59
60     /**
61      * Initializes printer.
62      *
63      * @param StepTextPainter         $textPainter
64      * @param ResultToStringConverter $resultConverter
65      * @param PrettyPathPrinter       $pathPrinter
66      * @param ExceptionPresenter      $exceptionPresenter
67      * @param integer                 $indentation
68      * @param integer                 $subIndentation
69      */
70     public function __construct(
71         StepTextPainter $textPainter,
72         ResultToStringConverter $resultConverter,
73         PrettyPathPrinter $pathPrinter,
74         ExceptionPresenter $exceptionPresenter,
75         $indentation = 4,
76         $subIndentation = 2
77     ) {
78         $this->textPainter = $textPainter;
79         $this->resultConverter = $resultConverter;
80         $this->pathPrinter = $pathPrinter;
81         $this->exceptionPresenter = $exceptionPresenter;
82         $this->indentText = str_repeat(' ', intval($indentation));
83         $this->subIndentText = $this->indentText . str_repeat(' ', intval($subIndentation));
84     }
85
86     /**
87      * {@inheritdoc}
88      */
89     public function printStep(Formatter $formatter, Scenario $scenario, StepNode $step, StepResult $result)
90     {
91         $this->printText($formatter->getOutputPrinter(), $step->getKeyword(), $step->getText(), $result);
92         $this->pathPrinter->printStepPath($formatter, $scenario, $step, $result, mb_strlen($this->indentText, 'utf8'));
93         $this->printArguments($formatter, $step->getArguments(), $result);
94         $this->printStdOut($formatter->getOutputPrinter(), $result);
95         $this->printException($formatter->getOutputPrinter(), $result);
96     }
97
98     /**
99      * Prints step text.
100      *
101      * @param OutputPrinter $printer
102      * @param string        $stepType
103      * @param string        $stepText
104      * @param StepResult    $result
105      */
106     private function printText(OutputPrinter $printer, $stepType, $stepText, StepResult $result)
107     {
108         if ($result && $result instanceof DefinedStepResult && $result->getStepDefinition()) {
109             $definition = $result->getStepDefinition();
110             $stepText = $this->textPainter->paintText($stepText, $definition, $result);
111         }
112
113         $style = $this->resultConverter->convertResultToString($result);
114         $printer->write(sprintf('%s{+%s}%s %s{-%s}', $this->indentText, $style, $stepType, $stepText, $style));
115     }
116
117     /**
118      * Prints step multiline arguments.
119      *
120      * @param Formatter           $formatter
121      * @param ArgumentInterface[] $arguments
122      * @param StepResult          $result
123      */
124     private function printArguments(Formatter $formatter, array $arguments, StepResult $result)
125     {
126         $style = $this->resultConverter->convertResultToString($result);
127
128         foreach ($arguments as $argument) {
129             $text = $this->getArgumentString($argument, !$formatter->getParameter('multiline'));
130
131             $indentedText = implode("\n", array_map(array($this, 'subIndent'), explode("\n", $text)));
132             $formatter->getOutputPrinter()->writeln(sprintf('{+%s}%s{-%s}', $style, $indentedText, $style));
133         }
134     }
135
136     /**
137      * Prints step output (if has one).
138      *
139      * @param OutputPrinter $printer
140      * @param StepResult    $result
141      */
142     private function printStdOut(OutputPrinter $printer, StepResult $result)
143     {
144         if (!$result instanceof ExecutedStepResult || null === $result->getCallResult()->getStdOut()) {
145             return;
146         }
147
148         $callResult = $result->getCallResult();
149         $indentedText = $this->subIndentText;
150
151         $pad = function ($line) use ($indentedText) {
152             return sprintf(
153                 '%s│ {+stdout}%s{-stdout}', $indentedText, $line
154             );
155         };
156
157         $printer->writeln(implode("\n", array_map($pad, explode("\n", $callResult->getStdOut()))));
158     }
159
160     /**
161      * Prints step exception (if has one).
162      *
163      * @param OutputPrinter $printer
164      * @param StepResult    $result
165      */
166     private function printException(OutputPrinter $printer, StepResult $result)
167     {
168         $style = $this->resultConverter->convertResultToString($result);
169
170         if (!$result instanceof ExceptionResult || !$result->hasException()) {
171             return;
172         }
173
174         $text = $this->exceptionPresenter->presentException($result->getException());
175         $indentedText = implode("\n", array_map(array($this, 'subIndent'), explode("\n", $text)));
176         $printer->writeln(sprintf('{+%s}%s{-%s}', $style, $indentedText, $style));
177     }
178
179     /**
180      * Returns argument string for provided argument.
181      *
182      * @param ArgumentInterface $argument
183      * @param Boolean           $collapse
184      *
185      * @return string
186      */
187     private function getArgumentString(ArgumentInterface $argument, $collapse = false)
188     {
189         if ($collapse) {
190             return '...';
191         }
192
193         if ($argument instanceof PyStringNode) {
194             $text = '"""' . "\n" . $argument . "\n" . '"""';
195
196             return $text;
197         }
198
199         return (string) $argument;
200     }
201
202     /**
203      * Indents text to the subIndentation level.
204      *
205      * @param string $text
206      *
207      * @return string
208      */
209     private function subIndent($text)
210     {
211         return $this->subIndentText . $text;
212     }
213 }