diff --git a/src/Report/Text.php b/src/Report/Text.php index 56b237465..a307aa4b0 100644 --- a/src/Report/Text.php +++ b/src/Report/Text.php @@ -10,9 +10,13 @@ namespace SebastianBergmann\CodeCoverage\Report; use const PHP_EOL; +use function array_map; use function date; use function ksort; +use function max; use function sprintf; +use function str_pad; +use function strlen; use SebastianBergmann\CodeCoverage\CodeCoverage; use SebastianBergmann\CodeCoverage\Node\File; use SebastianBergmann\CodeCoverage\Util\Percentage; @@ -156,28 +160,31 @@ public function process(CodeCoverage $coverage, bool $showColors = false): strin $report->numberOfExecutableLines(), ); + $padding = max(array_map('strlen', [$classes, $methods, $lines])); + if ($this->showOnlySummary) { - $title = 'Code Coverage Report Summary:'; + $title = 'Code Coverage Report Summary:'; + $padding = max($padding, strlen($title)); - $output .= $this->format($colors['header'], $title); + $output .= $this->format($colors['header'], $padding, $title); } else { $date = date(' Y-m-d H:i:s'); $title = 'Code Coverage Report:'; - $output .= $this->format($colors['header'], $title); - $output .= $this->format($colors['header'], $date); - $output .= $this->format($colors['header'], ''); - $output .= $this->format($colors['header'], ' Summary:'); + $output .= $this->format($colors['header'], $padding, $title); + $output .= $this->format($colors['header'], $padding, $date); + $output .= $this->format($colors['header'], $padding, ''); + $output .= $this->format($colors['header'], $padding, ' Summary:'); } - $output .= $this->format($colors['classes'], $classes); - $output .= $this->format($colors['methods'], $methods); + $output .= $this->format($colors['classes'], $padding, $classes); + $output .= $this->format($colors['methods'], $padding, $methods); if ($hasBranchCoverage) { - $output .= $this->format($colors['paths'], $paths); - $output .= $this->format($colors['branches'], $branches); + $output .= $this->format($colors['paths'], $padding, $paths); + $output .= $this->format($colors['branches'], $padding, $branches); } - $output .= $this->format($colors['lines'], $lines); + $output .= $this->format($colors['lines'], $padding, $lines); if ($this->showOnlySummary) { return $output . PHP_EOL; @@ -297,10 +304,12 @@ private function printCoverageCounts(int $numberOfCoveredElements, int $totalNum sprintf($format, $totalNumberOfElements) . ')'; } - private function format(string $color, false|string $string): string + private function format(string $color, int $padding, false|string $string): string { - $reset = $color ? self::COLOR_RESET : ''; + if ($color === '') { + return (string) $string . PHP_EOL; + } - return $color . (string) $string . $reset . PHP_EOL; + return $color . str_pad((string) $string, $padding) . self::COLOR_RESET . PHP_EOL; } } diff --git a/tests/_files/NamespacedBankAccount-text-with-colors.txt b/tests/_files/NamespacedBankAccount-text-with-colors.txt new file mode 100644 index 000000000..83f3901e1 --- /dev/null +++ b/tests/_files/NamespacedBankAccount-text-with-colors.txt @@ -0,0 +1,14 @@ + + +Code Coverage Report:  + %s  +  + Summary:  + Classes: 0.00% (0/1) + Methods: 75.00% (3/4) + Lines: 62.50% (5/8) + +SomeNamespace\BankAccount + Methods: ( 0/ 0) Lines: ( 0/ 0) +SomeNamespace\BankAccountTrait + Methods: 75.00% ( 3/ 4) Lines: 62.50% ( 5/ 8) diff --git a/tests/tests/Report/TextTest.php b/tests/tests/Report/TextTest.php index 16a99d369..8cefd57c9 100644 --- a/tests/tests/Report/TextTest.php +++ b/tests/tests/Report/TextTest.php @@ -57,6 +57,16 @@ public function testTextForNamespacedBankAccountTest(): void ); } + public function testTextForNamespacedBankAccountTestWhenColorsAreEnabled(): void + { + $text = new Text(Thresholds::default(), true, false); + + $this->assertStringMatchesFormatFile( + TEST_FILES_PATH . 'NamespacedBankAccount-text-with-colors.txt', + str_replace(PHP_EOL, "\n", $text->process($this->getLineCoverageForNamespacedBankAccount(), true)), + ); + } + public function testTextForFileWithIgnoredLines(): void { $text = new Text(Thresholds::default(), false, false);