Skip to content

Commit 3e1cca8

Browse files
committed
refactor(phpunit): delegate console output to ConsoleCoverageRenderer
Integrate ConsoleOutput enum and ConsoleCoverageRenderer into OpenApiCoverageExtension, replacing inline printReport() and percentage() methods. Parse console_output parameter and pass ConsoleOutput to the subscriber. Refs: #27
1 parent c765b5f commit 3e1cca8

1 file changed

Lines changed: 7 additions & 42 deletions

File tree

src/PHPUnit/OpenApiCoverageExtension.php

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
use function fwrite;
2424
use function getcwd;
2525
use function getenv;
26-
use function round;
27-
use function str_repeat;
2826
use function str_starts_with;
2927

3028
final class OpenApiCoverageExtension implements Extension
@@ -59,15 +57,20 @@ public function bootstrap(Configuration $configuration, Facade $facade, Paramete
5957
}
6058
}
6159

60+
$consoleOutput = ConsoleOutput::resolve(
61+
$parameters->has('console_output') ? $parameters->get('console_output') : null,
62+
);
63+
6264
$githubSummaryPath = getenv('GITHUB_STEP_SUMMARY') ?: null;
6365

64-
$facade->registerSubscriber(new class ($specs, $outputFile, $githubSummaryPath) implements ExecutionFinishedSubscriber {
66+
$facade->registerSubscriber(new class ($specs, $outputFile, $consoleOutput, $githubSummaryPath) implements ExecutionFinishedSubscriber {
6567
/**
6668
* @param string[] $specs
6769
*/
6870
public function __construct(
6971
private readonly array $specs,
7072
private readonly ?string $outputFile,
73+
private readonly ConsoleOutput $consoleOutput,
7174
private readonly ?string $githubSummaryPath,
7275
) {}
7376

@@ -80,7 +83,7 @@ public function notify(ExecutionFinished $event): void
8083
return;
8184
}
8285

83-
$this->printReport($results);
86+
echo ConsoleCoverageRenderer::render($results, $this->consoleOutput);
8487

8588
if ($this->outputFile !== null || $this->githubSummaryPath !== null) {
8689
$this->writeMarkdownReport($results);
@@ -122,39 +125,6 @@ private function computeAllResults(): array
122125
return $results;
123126
}
124127

125-
/**
126-
* @param array<string, array{covered: string[], uncovered: string[], total: int, coveredCount: int}> $results
127-
*/
128-
private function printReport(array $results): void
129-
{
130-
echo "\n\n";
131-
echo "OpenAPI Contract Test Coverage\n";
132-
echo str_repeat('=', 50) . "\n";
133-
134-
foreach ($results as $spec => $result) {
135-
$percentage = self::percentage($result['coveredCount'], $result['total']);
136-
137-
echo "\n[{$spec}] {$result['coveredCount']}/{$result['total']} endpoints ({$percentage}%)\n";
138-
echo str_repeat('-', 50) . "\n";
139-
140-
if ($result['covered'] !== []) {
141-
echo "Covered:\n";
142-
143-
foreach ($result['covered'] as $endpoint) {
144-
echo "{$endpoint}\n";
145-
}
146-
}
147-
148-
$uncoveredCount = $result['total'] - $result['coveredCount'];
149-
150-
if ($uncoveredCount > 0) {
151-
echo "Uncovered: {$uncoveredCount} endpoints\n";
152-
}
153-
}
154-
155-
echo "\n";
156-
}
157-
158128
/**
159129
* @param array<string, array{covered: string[], uncovered: string[], total: int, coveredCount: int}> $results
160130
*/
@@ -178,11 +148,6 @@ private function writeMarkdownReport(array $results): void
178148
}
179149
}
180150
}
181-
182-
private static function percentage(int $covered, int $total): float|int
183-
{
184-
return $total > 0 ? round($covered / $total * 100, 1) : 0;
185-
}
186151
});
187152
}
188153
}

0 commit comments

Comments
 (0)