Skip to content

Commit a8e9a5e

Browse files
Luc45claude
andcommitted
Reports/Ctrf: PHPUnit 8 compatibility, iconv test, comment
- Fix Coverage: 7.2 (Linux) failure: assertMatchesRegularExpression() was added in PHPUnit 9.1, but assertRegExp() was removed in PHPUnit 10. PHPCS supports both. Use the same runtime feature-detection pattern the codebase already uses elsewhere (StatusWriterTestHelper.php, Generators/HTMLTest.php) via a small `assertRegex()` helper. - Add testNonUtf8EncodingIsTranscoded covering the iconv fallback path for messages from non-UTF-8 source files. - Add a comment in generate() explaining why `passed` is counted from the cached partials while `failed`/`other` come from the parameters (the asymmetry is deliberate: prefer authoritative sources where PHPCS provides them; fall back to scanning only when it doesn't). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent 1f9748d commit a8e9a5e

2 files changed

Lines changed: 72 additions & 2 deletions

File tree

src/Reports/Ctrf.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,12 @@ public function generate(
184184
$stop = $now;
185185
$duration = ($stop - $start);
186186

187+
// `failed` and `other` come from the authoritative parameters PHPCS
188+
// passes us. `passed` is not provided as a parameter (PHPCS doesn't
189+
// separately track clean files), so we count it from the cached
190+
// partials, where each clean file emitted exactly one entry with
191+
// `"status":"passed"`. The asymmetry is intentional: prefer the
192+
// authoritative source where one exists.
187193
$passed = substr_count($cachedData, '"status":"passed"');
188194
$failed = $totalErrors;
189195
$other = $totalWarnings;

tests/Core/Reports/CtrfTest.php

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ public function testEnvelopeShape()
7878

7979
$this->assertSame('CTRF', $report['reportFormat']);
8080
$this->assertSame('1.0.0', $report['specVersion']);
81-
$this->assertMatchesRegularExpression(
81+
$this->assertRegex(
8282
'/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/',
8383
$report['reportId'],
8484
'reportId should be a v4 UUID.'
8585
);
86-
$this->assertMatchesRegularExpression(
86+
$this->assertRegex(
8787
'/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$/',
8888
$report['timestamp']
8989
);
@@ -93,6 +93,28 @@ public function testEnvelopeShape()
9393
}
9494

9595

96+
/**
97+
* Cross-version regex assertion: assertMatchesRegularExpression() was added
98+
* in PHPUnit 9.1.0 and assertRegExp() was removed in PHPUnit 10. PHPCS
99+
* supports both, so dispatch by feature detection.
100+
*
101+
* @param string $regex The regular expression pattern.
102+
* @param string $value The string to match against.
103+
* @param string $message Optional failure message.
104+
*
105+
* @return void
106+
*/
107+
private function assertRegex($regex, $value, $message = '')
108+
{
109+
if (method_exists($this, 'assertMatchesRegularExpression') === true) {
110+
$this->assertMatchesRegularExpression($regex, $value, $message);
111+
} else {
112+
// PHPUnit < 9.1.0.
113+
$this->assertRegExp($regex, $value, $message);
114+
}
115+
}
116+
117+
96118
/**
97119
* With no inputs the summary should still be valid: all counts 0, tests array empty.
98120
*
@@ -292,6 +314,48 @@ public function testWarningViolationMapsToOther()
292314
}
293315

294316

317+
/**
318+
* Messages from a non-UTF-8 source file must be transcoded so the JSON output is UTF-8.
319+
*
320+
* @return void
321+
*/
322+
public function testNonUtf8EncodingIsTranscoded()
323+
{
324+
$reporter = new Ctrf();
325+
$file = $this->fileWithEncoding('iso-8859-1');
326+
// The byte 0xE9 is "é" in ISO-8859-1 but invalid as a standalone byte in UTF-8.
327+
$latinMessage = "Found char \xE9 (e-acute)";
328+
$report = [
329+
'filename' => '/tmp/iso.php',
330+
'errors' => 1,
331+
'warnings' => 0,
332+
'fixable' => 0,
333+
'messages' => [
334+
1 => [
335+
1 => [
336+
[
337+
'message' => $latinMessage,
338+
'source' => 'X.Y.Z',
339+
'severity' => 5,
340+
'fixable' => false,
341+
'type' => 'ERROR',
342+
],
343+
],
344+
],
345+
],
346+
];
347+
348+
ob_start();
349+
$reporter->generateFileReport($report, $file);
350+
$output = ob_get_clean();
351+
352+
$decoded = json_decode('[' . rtrim($output, ',') . ']', true);
353+
$this->assertCount(1, $decoded);
354+
// After transcoding, the message must contain the proper UTF-8 sequence for é.
355+
$this->assertStringContainsString("\xC3\xA9", $decoded[0]['message']);
356+
}
357+
358+
295359
/**
296360
* Invalid UTF-8 bytes in messages must not silently drop the test entry.
297361
*

0 commit comments

Comments
 (0)