Skip to content

Commit 9ae5215

Browse files
committed
fix(phpunit): add warnings for invalid console_output and simplify renderer logic
Add stderr warnings when invalid values are provided for the OPENAPI_CONSOLE_OUTPUT env var or console_output phpunit.xml parameter. Remove arithmetic-derived uncoveredCount in favor of count() on the actual uncovered array. Add edge-case tests for zero coverage, full coverage, and percentage rounding.
1 parent e7113c0 commit 9ae5215

3 files changed

Lines changed: 75 additions & 6 deletions

File tree

src/PHPUnit/ConsoleCoverageRenderer.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Studio\OpenApiContractTesting\PHPUnit;
66

7+
use function count;
78
use function round;
89
use function str_repeat;
910

@@ -27,13 +28,12 @@ public static function render(array $results, ConsoleOutput $consoleOutput = Con
2728

2829
foreach ($results as $spec => $result) {
2930
$percentage = self::percentage($result['coveredCount'], $result['total']);
30-
$uncoveredCount = $result['total'] - $result['coveredCount'];
3131

3232
$output .= "\n[{$spec}] {$result['coveredCount']}/{$result['total']} endpoints ({$percentage}%)\n";
3333
$output .= str_repeat('-', 50) . "\n";
3434

35-
$output .= self::renderCovered($result, $consoleOutput, $uncoveredCount);
36-
$output .= self::renderUncovered($result, $consoleOutput, $uncoveredCount);
35+
$output .= self::renderCovered($result, $consoleOutput);
36+
$output .= self::renderUncovered($result, $consoleOutput);
3737
}
3838

3939
$output .= "\n";
@@ -44,7 +44,7 @@ public static function render(array $results, ConsoleOutput $consoleOutput = Con
4444
/**
4545
* @param CoverageResult $result
4646
*/
47-
private static function renderCovered(array $result, ConsoleOutput $consoleOutput, int $uncoveredCount): string
47+
private static function renderCovered(array $result, ConsoleOutput $consoleOutput): string
4848
{
4949
if ($result['covered'] === []) {
5050
return '';
@@ -66,12 +66,14 @@ private static function renderCovered(array $result, ConsoleOutput $consoleOutpu
6666
/**
6767
* @param CoverageResult $result
6868
*/
69-
private static function renderUncovered(array $result, ConsoleOutput $consoleOutput, int $uncoveredCount): string
69+
private static function renderUncovered(array $result, ConsoleOutput $consoleOutput): string
7070
{
71-
if ($uncoveredCount <= 0) {
71+
if ($result['uncovered'] === []) {
7272
return '';
7373
}
7474

75+
$uncoveredCount = count($result['uncovered']);
76+
7577
if ($consoleOutput === ConsoleOutput::DEFAULT) {
7678
return "Uncovered: {$uncoveredCount} endpoints\n";
7779
}

src/PHPUnit/ConsoleOutput.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace Studio\OpenApiContractTesting\PHPUnit;
66

7+
use const STDERR;
8+
9+
use function fwrite;
710
use function getenv;
811
use function mb_strtolower;
912
use function trim;
@@ -26,12 +29,20 @@ public static function resolve(?string $parameterValue): self
2629
if ($envValue !== false && trim($envValue) !== '') {
2730
$resolved = self::tryFrom(mb_strtolower(trim($envValue)));
2831

32+
if ($resolved === null) {
33+
fwrite(STDERR, "[OpenAPI Coverage] WARNING: Invalid OPENAPI_CONSOLE_OUTPUT value '{$envValue}'. Valid values: default, all, uncovered_only. Falling back to 'default'.\n");
34+
}
35+
2936
return $resolved ?? self::DEFAULT;
3037
}
3138

3239
if ($parameterValue !== null && trim($parameterValue) !== '') {
3340
$resolved = self::tryFrom(mb_strtolower(trim($parameterValue)));
3441

42+
if ($resolved === null) {
43+
fwrite(STDERR, "[OpenAPI Coverage] WARNING: Invalid console_output parameter '{$parameterValue}'. Valid values: default, all, uncovered_only. Falling back to 'default'.\n");
44+
}
45+
3546
return $resolved ?? self::DEFAULT;
3647
}
3748

tests/Unit/ConsoleCoverageRendererTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,62 @@ public function render_header_and_separators_are_present(): void
222222
$this->assertStringContainsString('--------------------------------------------------', $output);
223223
}
224224

225+
#[Test]
226+
public function render_zero_coverage_all_mode_shows_uncovered_list_only(): void
227+
{
228+
$results = [
229+
'front' => [
230+
'covered' => [],
231+
'uncovered' => ['GET /v1/pets', 'POST /v1/pets'],
232+
'total' => 2,
233+
'coveredCount' => 0,
234+
],
235+
];
236+
237+
$output = ConsoleCoverageRenderer::render($results, ConsoleOutput::ALL);
238+
239+
$this->assertStringContainsString('[front] 0/2 endpoints (0%)', $output);
240+
$this->assertStringNotContainsString('Covered', $output);
241+
$this->assertStringContainsString('Uncovered:', $output);
242+
$this->assertStringContainsString(' ✗ GET /v1/pets', $output);
243+
}
244+
245+
#[Test]
246+
public function render_full_coverage_uncovered_only_mode_shows_covered_count_only(): void
247+
{
248+
$results = [
249+
'front' => [
250+
'covered' => ['GET /v1/pets', 'POST /v1/pets'],
251+
'uncovered' => [],
252+
'total' => 2,
253+
'coveredCount' => 2,
254+
],
255+
];
256+
257+
$output = ConsoleCoverageRenderer::render($results, ConsoleOutput::UNCOVERED_ONLY);
258+
259+
$this->assertStringContainsString('Covered: 2 endpoints', $output);
260+
$this->assertStringNotContainsString('', $output);
261+
$this->assertStringNotContainsString('Uncovered', $output);
262+
}
263+
264+
#[Test]
265+
public function render_percentage_rounds_to_one_decimal_place(): void
266+
{
267+
$results = [
268+
'front' => [
269+
'covered' => ['GET /v1/pets'],
270+
'uncovered' => ['POST /v1/pets', 'DELETE /v1/pets/{petId}'],
271+
'total' => 3,
272+
'coveredCount' => 1,
273+
],
274+
];
275+
276+
$output = ConsoleCoverageRenderer::render($results);
277+
278+
$this->assertStringContainsString('[front] 1/3 endpoints (33.3%)', $output);
279+
}
280+
225281
#[Test]
226282
public function render_spec_with_zero_endpoints(): void
227283
{

0 commit comments

Comments
 (0)