Patched to Drupal 8.4.8 level. See https://www.drupal.org/sa-core-2018-004 and patch...
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Output / Node / Printer / Pretty / PrettySkippedStepPrinter.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\StepResult;
18 use Behat\Gherkin\Node\ArgumentInterface;
19 use Behat\Gherkin\Node\PyStringNode;
20 use Behat\Gherkin\Node\ScenarioLikeInterface as Scenario;
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\IntegerTestResult;
25 use Behat\Testwork\Tester\Result\TestResult;
26
27 /**
28  * Prints steps as skipped.
29  *
30  * @author Konstantin Kudryashov <ever.zet@gmail.com>
31  */
32 final class PrettySkippedStepPrinter implements StepPrinter
33 {
34     /**
35      * @var StepTextPainter
36      */
37     private $textPainter;
38     /**
39      * @var ResultToStringConverter
40      */
41     private $resultConverter;
42     /**
43      * @var PrettyPathPrinter
44      */
45     private $pathPrinter;
46     /**
47      * @var string
48      */
49     private $indentText;
50     /**
51      * @var string
52      */
53     private $subIndentText;
54
55     /**
56      * Initializes printer.
57      *
58      * @param StepTextPainter         $textPainter
59      * @param ResultToStringConverter $resultConverter
60      * @param PrettyPathPrinter       $pathPrinter
61      * @param integer                 $indentation
62      * @param integer                 $subIndentation
63      */
64     public function __construct(
65         StepTextPainter $textPainter,
66         ResultToStringConverter $resultConverter,
67         PrettyPathPrinter $pathPrinter,
68         $indentation = 4,
69         $subIndentation = 2
70     ) {
71         $this->textPainter = $textPainter;
72         $this->resultConverter = $resultConverter;
73         $this->pathPrinter = $pathPrinter;
74         $this->indentText = str_repeat(' ', intval($indentation));
75         $this->subIndentText = $this->indentText . str_repeat(' ', intval($subIndentation));
76     }
77
78     /**
79      * {@inheritdoc}
80      */
81     public function printStep(Formatter $formatter, Scenario $scenario, StepNode $step, StepResult $result)
82     {
83         $this->printText($formatter->getOutputPrinter(), $step->getKeyword(), $step->getText(), $result);
84         $this->pathPrinter->printStepPath($formatter, $scenario, $step, $result, mb_strlen($this->indentText, 'utf8'));
85         $this->printArguments($formatter, $step->getArguments());
86     }
87
88     /**
89      * Prints step text.
90      *
91      * @param OutputPrinter $printer
92      * @param string        $stepType
93      * @param string        $stepText
94      * @param StepResult    $result
95      */
96     private function printText(OutputPrinter $printer, $stepType, $stepText, StepResult $result)
97     {
98         $style = $this->resultConverter->convertResultCodeToString(TestResult::SKIPPED);
99
100         if ($result instanceof DefinedStepResult && $result->getStepDefinition()) {
101             $definition = $result->getStepDefinition();
102             $stepText = $this->textPainter->paintText(
103                 $stepText, $definition, new IntegerTestResult(TestResult::SKIPPED)
104             );
105         }
106
107         $printer->write(sprintf('%s{+%s}%s %s{-%s}', $this->indentText, $style, $stepType, $stepText, $style));
108     }
109
110     /**
111      * Prints step multiline arguments.
112      *
113      * @param Formatter           $formatter
114      * @param ArgumentInterface[] $arguments
115      */
116     private function printArguments(Formatter $formatter, array $arguments)
117     {
118         $style = $this->resultConverter->convertResultCodeToString(TestResult::SKIPPED);
119
120         foreach ($arguments as $argument) {
121             $text = $this->getArgumentString($argument, !$formatter->getParameter('multiline'));
122
123             $indentedText = implode("\n", array_map(array($this, 'subIndent'), explode("\n", $text)));
124             $formatter->getOutputPrinter()->writeln(sprintf('{+%s}%s{-%s}', $style, $indentedText, $style));
125         }
126     }
127
128     /**
129      * Returns argument string for provided argument.
130      *
131      * @param ArgumentInterface $argument
132      * @param Boolean           $collapse
133      *
134      * @return string
135      */
136     private function getArgumentString(ArgumentInterface $argument, $collapse = false)
137     {
138         if ($collapse) {
139             return '...';
140         }
141
142         if ($argument instanceof PyStringNode) {
143             $text = '"""' . "\n" . $argument . "\n" . '"""';
144
145             return $text;
146         }
147
148         return (string) $argument;
149     }
150
151     /**
152      * Indents text to the subIndentation level.
153      *
154      * @param string $text
155      *
156      * @return string
157      */
158     private function subIndent($text)
159     {
160         return $this->subIndentText . $text;
161     }
162 }