Skip to content

Commit c765b5f

Browse files
committed
feat(phpunit): add ConsoleOutput enum and ConsoleCoverageRenderer
Introduce ConsoleOutput string-backed enum with DEFAULT, ALL, and UNCOVERED_ONLY cases plus a resolve() factory that handles env var > parameter > default priority. Add ConsoleCoverageRenderer with a static render() method that produces console coverage output supporting the three display modes. Ref #27
1 parent 9b36db5 commit c765b5f

2 files changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Studio\OpenApiContractTesting\PHPUnit;
6+
7+
use function round;
8+
use function str_repeat;
9+
10+
/**
11+
* @phpstan-type CoverageResult array{covered: string[], uncovered: string[], total: int, coveredCount: int}
12+
*/
13+
final class ConsoleCoverageRenderer
14+
{
15+
/**
16+
* @param array<string, CoverageResult> $results
17+
*/
18+
public static function render(array $results, ConsoleOutput $consoleOutput = ConsoleOutput::DEFAULT): string
19+
{
20+
if ($results === []) {
21+
return '';
22+
}
23+
24+
$output = "\n\n";
25+
$output .= "OpenAPI Contract Test Coverage\n";
26+
$output .= str_repeat('=', 50) . "\n";
27+
28+
foreach ($results as $spec => $result) {
29+
$percentage = self::percentage($result['coveredCount'], $result['total']);
30+
$uncoveredCount = $result['total'] - $result['coveredCount'];
31+
32+
$output .= "\n[{$spec}] {$result['coveredCount']}/{$result['total']} endpoints ({$percentage}%)\n";
33+
$output .= str_repeat('-', 50) . "\n";
34+
35+
$output .= self::renderCovered($result, $consoleOutput, $uncoveredCount);
36+
$output .= self::renderUncovered($result, $consoleOutput, $uncoveredCount);
37+
}
38+
39+
$output .= "\n";
40+
41+
return $output;
42+
}
43+
44+
/**
45+
* @param CoverageResult $result
46+
*/
47+
private static function renderCovered(array $result, ConsoleOutput $consoleOutput, int $uncoveredCount): string
48+
{
49+
if ($result['covered'] === []) {
50+
return '';
51+
}
52+
53+
if ($consoleOutput === ConsoleOutput::UNCOVERED_ONLY) {
54+
return "Covered: {$result['coveredCount']} endpoints\n";
55+
}
56+
57+
$output = "Covered:\n";
58+
59+
foreach ($result['covered'] as $endpoint) {
60+
$output .= "{$endpoint}\n";
61+
}
62+
63+
return $output;
64+
}
65+
66+
/**
67+
* @param CoverageResult $result
68+
*/
69+
private static function renderUncovered(array $result, ConsoleOutput $consoleOutput, int $uncoveredCount): string
70+
{
71+
if ($uncoveredCount <= 0) {
72+
return '';
73+
}
74+
75+
if ($consoleOutput === ConsoleOutput::DEFAULT) {
76+
return "Uncovered: {$uncoveredCount} endpoints\n";
77+
}
78+
79+
$output = "Uncovered:\n";
80+
81+
foreach ($result['uncovered'] as $endpoint) {
82+
$output .= "{$endpoint}\n";
83+
}
84+
85+
return $output;
86+
}
87+
88+
private static function percentage(int $covered, int $total): float|int
89+
{
90+
return $total > 0 ? round($covered / $total * 100, 1) : 0;
91+
}
92+
}

src/PHPUnit/ConsoleOutput.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Studio\OpenApiContractTesting\PHPUnit;
6+
7+
use function getenv;
8+
use function mb_strtolower;
9+
use function trim;
10+
11+
enum ConsoleOutput: string
12+
{
13+
case DEFAULT = 'default';
14+
case ALL = 'all';
15+
case UNCOVERED_ONLY = 'uncovered_only';
16+
17+
/**
18+
* Resolve the console output mode from environment variable and/or phpunit.xml parameter.
19+
*
20+
* Priority: environment variable > parameter > DEFAULT.
21+
*/
22+
public static function resolve(?string $parameterValue): self
23+
{
24+
$envValue = getenv('OPENAPI_CONSOLE_OUTPUT');
25+
26+
if ($envValue !== false && trim($envValue) !== '') {
27+
$resolved = self::tryFrom(mb_strtolower(trim($envValue)));
28+
29+
return $resolved ?? self::DEFAULT;
30+
}
31+
32+
if ($parameterValue !== null && trim($parameterValue) !== '') {
33+
$resolved = self::tryFrom(mb_strtolower(trim($parameterValue)));
34+
35+
return $resolved ?? self::DEFAULT;
36+
}
37+
38+
return self::DEFAULT;
39+
}
40+
}

0 commit comments

Comments
 (0)