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\Helper;
13 use Behat\Gherkin\Node\ExampleNode;
14 use Behat\Gherkin\Node\ScenarioLikeInterface as Scenario;
15 use Behat\Gherkin\Node\StepNode;
18 * Calculates width of scenario. Width of scenario = max width of scenario title and scenario step texts.
20 * @author Konstantin Kudryashov <ever.zet@gmail.com>
22 final class WidthCalculator
25 * Calculates scenario width.
27 * @param Scenario $scenario
28 * @param integer $indentation
29 * @param integer $subIndentation
33 public function calculateScenarioWidth(Scenario $scenario, $indentation, $subIndentation)
35 $length = $this->calculateScenarioHeaderWidth($scenario, $indentation);
37 foreach ($scenario->getSteps() as $step) {
38 $stepLength = $this->calculateStepWidth($step, $indentation + $subIndentation);
39 $length = max($length, $stepLength);
46 * Calculates outline examples width.
48 * @param ExampleNode $example
49 * @param integer $indentation
50 * @param integer $subIndentation
54 public function calculateExampleWidth(ExampleNode $example, $indentation, $subIndentation)
56 $length = $this->calculateScenarioHeaderWidth($example, $indentation);
58 foreach ($example->getSteps() as $step) {
59 $stepLength = $this->calculateStepWidth($step, $indentation + $subIndentation);
60 $length = max($length, $stepLength);
67 * Calculates scenario header width.
69 * @param Scenario $scenario
70 * @param integer $indentation
74 public function calculateScenarioHeaderWidth(Scenario $scenario, $indentation)
76 $indentText = str_repeat(' ', intval($indentation));
78 if ($scenario instanceof ExampleNode) {
79 $header = sprintf('%s%s', $indentText, $scenario->getTitle());
81 $title = $scenario->getTitle();
82 $lines = explode("\n", $title);
83 $header = sprintf('%s%s: %s', $indentText, $scenario->getKeyword(), array_shift($lines));
86 return mb_strlen(rtrim($header), 'utf8');
90 * Calculates step width.
92 * @param StepNode $step
93 * @param integer $indentation
97 public function calculateStepWidth(StepNode $step, $indentation)
99 $indentText = str_repeat(' ', intval($indentation));
101 $text = sprintf('%s%s %s', $indentText, $step->getKeyword(), $step->getText());
103 return mb_strlen($text, 'utf8');