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\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;
23 * Prints paths for scenarios, examples, backgrounds and steps.
25 * @author Konstantin Kudryashov <ever.zet@gmail.com>
27 final class PrettyPathPrinter
30 * @var WidthCalculator
32 private $widthCalculator;
39 * Initializes printer.
41 * @param WidthCalculator $widthCalculator
42 * @param string $basePath
44 public function __construct(WidthCalculator $widthCalculator, $basePath)
46 $this->widthCalculator = $widthCalculator;
47 $this->basePath = $basePath;
51 * Prints scenario path comment.
53 * @param Formatter $formatter
54 * @param FeatureNode $feature
55 * @param Scenario $scenario
56 * @param integer $indentation
58 public function printScenarioPath(Formatter $formatter, FeatureNode $feature, Scenario $scenario, $indentation)
60 $printer = $formatter->getOutputPrinter();
62 if (!$formatter->getParameter('paths')) {
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));
73 $printer->writeln(sprintf('%s {+comment}# %s{-comment}', $spacing, $fileAndLine));
77 * Prints step path comment.
79 * @param Formatter $formatter
80 * @param Scenario $scenario
81 * @param StepNode $step
82 * @param StepResult $result
83 * @param integer $indentation
85 public function printStepPath(
92 $printer = $formatter->getOutputPrinter();
94 if (!$result instanceof DefinedStepResult || !$result->getStepDefinition() || !$formatter->getParameter('paths')) {
100 $textWidth = $this->widthCalculator->calculateStepWidth($step, $indentation);
101 $scenarioWidth = $this->widthCalculator->calculateScenarioWidth($scenario, $indentation - 2, 2);
103 $this->printDefinedStepPath($printer, $result, $scenarioWidth, $textWidth);
107 * Prints defined step path.
109 * @param OutputPrinter $printer
110 * @param DefinedStepResult $result
111 * @param integer $scenarioWidth
112 * @param integer $stepWidth
114 private function printDefinedStepPath(OutputPrinter $printer, DefinedStepResult $result, $scenarioWidth, $stepWidth)
116 $path = $result->getStepDefinition()->getPath();
117 $spacing = str_repeat(' ', max(0, $scenarioWidth - $stepWidth));
119 $printer->writeln(sprintf('%s {+comment}# %s{-comment}', $spacing, $path));
123 * Transforms path to relative.
125 * @param string $path
129 private function relativizePaths($path)
131 if (!$this->basePath) {
135 return str_replace($this->basePath . DIRECTORY_SEPARATOR, '', $path);