Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Behat / Output / Node / Printer / Pretty / PrettyPathPrinter.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\WidthCalculator;
14 use Behat\Behat\Tester\Result\DefinedStepResult;
15 use Behat\Behat\Tester\Result\StepResult;
16 use Behat\Gherkin\Node\FeatureNode;
17 use Behat\Gherkin\Node\ScenarioLikeInterface as Scenario;
18 use Behat\Gherkin\Node\StepNode;
19 use Behat\Testwork\Output\Formatter;
20 use Behat\Testwork\Output\Printer\OutputPrinter;
21
22 /**
23  * Prints paths for scenarios, examples, backgrounds and steps.
24  *
25  * @author Konstantin Kudryashov <ever.zet@gmail.com>
26  */
27 final class PrettyPathPrinter
28 {
29     /**
30      * @var WidthCalculator
31      */
32     private $widthCalculator;
33     /**
34      * @var string
35      */
36     private $basePath;
37
38     /**
39      * Initializes printer.
40      *
41      * @param WidthCalculator $widthCalculator
42      * @param string          $basePath
43      */
44     public function __construct(WidthCalculator $widthCalculator, $basePath)
45     {
46         $this->widthCalculator = $widthCalculator;
47         $this->basePath = $basePath;
48     }
49
50     /**
51      * Prints scenario path comment.
52      *
53      * @param Formatter   $formatter
54      * @param FeatureNode $feature
55      * @param Scenario    $scenario
56      * @param integer     $indentation
57      */
58     public function printScenarioPath(Formatter $formatter, FeatureNode $feature, Scenario $scenario, $indentation)
59     {
60         $printer = $formatter->getOutputPrinter();
61
62         if (!$formatter->getParameter('paths')) {
63             $printer->writeln();
64
65             return;
66         }
67
68         $fileAndLine = sprintf('%s:%s', $this->relativizePaths($feature->getFile()), $scenario->getLine());
69         $headerWidth = $this->widthCalculator->calculateScenarioHeaderWidth($scenario, $indentation);
70         $scenarioWidth = $this->widthCalculator->calculateScenarioWidth($scenario, $indentation, 2);
71         $spacing = str_repeat(' ', max(0, $scenarioWidth - $headerWidth));
72
73         $printer->writeln(sprintf('%s {+comment}# %s{-comment}', $spacing, $fileAndLine));
74     }
75
76     /**
77      * Prints step path comment.
78      *
79      * @param Formatter  $formatter
80      * @param Scenario   $scenario
81      * @param StepNode   $step
82      * @param StepResult $result
83      * @param integer    $indentation
84      */
85     public function printStepPath(
86         Formatter $formatter,
87         Scenario $scenario,
88         StepNode $step,
89         StepResult $result,
90         $indentation
91     ) {
92         $printer = $formatter->getOutputPrinter();
93
94         if (!$result instanceof DefinedStepResult || !$result->getStepDefinition() || !$formatter->getParameter('paths')) {
95             $printer->writeln();
96
97             return;
98         }
99
100         $textWidth = $this->widthCalculator->calculateStepWidth($step, $indentation);
101         $scenarioWidth = $this->widthCalculator->calculateScenarioWidth($scenario, $indentation - 2, 2);
102
103         $this->printDefinedStepPath($printer, $result, $scenarioWidth, $textWidth);
104     }
105
106     /**
107      * Prints defined step path.
108      *
109      * @param OutputPrinter     $printer
110      * @param DefinedStepResult $result
111      * @param integer           $scenarioWidth
112      * @param integer           $stepWidth
113      */
114     private function printDefinedStepPath(OutputPrinter $printer, DefinedStepResult $result, $scenarioWidth, $stepWidth)
115     {
116         $path = $result->getStepDefinition()->getPath();
117         $spacing = str_repeat(' ', max(0, $scenarioWidth - $stepWidth));
118
119         $printer->writeln(sprintf('%s {+comment}# %s{-comment}', $spacing, $path));
120     }
121
122     /**
123      * Transforms path to relative.
124      *
125      * @param string $path
126      *
127      * @return string
128      */
129     private function relativizePaths($path)
130     {
131         if (!$this->basePath) {
132             return $path;
133         }
134
135         return str_replace($this->basePath . DIRECTORY_SEPARATOR, '', $path);
136     }
137 }