Skip to content

Commit a434523

Browse files
authored
RuleTestCase: improve main assert error message (#11)
* RuleTestCase: improve main assert error message * Add simple test * squash tests * Win fix
1 parent d5b110c commit a434523

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/RuleTestCase.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use function file_get_contents;
1515
use function file_put_contents;
1616
use function implode;
17+
use function in_array;
1718
use function ksort;
1819
use function preg_match;
1920
use function preg_match_all;
@@ -62,10 +63,15 @@ protected function analyzeFiles(
6263
$actualErrors = $this->processActualErrors(array_values($fileErrors));
6364
$expectedErrors = $this->parseExpectedErrors($file);
6465

66+
$extraErrors = array_filter($actualErrors, static fn (string $error): bool => !in_array($error, $expectedErrors, true));
67+
$missingErrors = array_filter($expectedErrors, static fn (string $error): bool => !in_array($error, $actualErrors, true));
68+
6569
self::assertSame(
6670
implode("\n", $expectedErrors) . "\n",
6771
implode("\n", $actualErrors) . "\n",
68-
"Errors in file {$file} do not match",
72+
"Errors in file {$file} do not match:\n\n" .
73+
($extraErrors === [] ? '' : "New errors reported:\n" . implode("\n", $extraErrors) . "\n\n") .
74+
($missingErrors === [] ? '' : "Errors not reported:\n" . implode("\n", $missingErrors) . "\n\n"),
6975
);
7076
}
7177
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace DisallowDivisionByLiteralZeroRule;
4+
5+
function testErrorMessage(): void
6+
{
7+
$a = 10;
8+
$invalidDivision = $a / 0; // error: This error should not be reported
9+
$validDivision = $a / 2; // error: Division by literal zero is not allowed
10+
}

tests/RuleTestCaseTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,28 @@ public function testAutofix(): void
5353
self::assertFileEquals($expectedFile, $tmpFile);
5454
}
5555

56+
public function testErrorMessageFormat(): void
57+
{
58+
$testFile = str_replace('/', DIRECTORY_SEPARATOR, __DIR__ . '/Rule/Data/DisallowDivisionByLiteralZeroRule/error-message.php');
59+
60+
try {
61+
$this->analyzeFiles([$testFile]);
62+
self::fail('Expected assertion to fail');
63+
} catch (AssertionFailedError $e) { // @phpstan-ignore catch.internalClass
64+
$expectedMessage = <<<MSG
65+
Errors in file {$testFile} do not match:
66+
67+
New errors reported:
68+
08: Division by literal zero is not allowed
69+
70+
Errors not reported:
71+
08: This error should not be reported
72+
09: Division by literal zero is not allowed
73+
74+
75+
MSG;
76+
self::assertStringContainsString($expectedMessage, $e->getMessage());
77+
}
78+
}
79+
5680
}

0 commit comments

Comments
 (0)