Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/RuleTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use function file_get_contents;
use function file_put_contents;
use function implode;
use function in_array;
use function ksort;
use function preg_match;
use function preg_match_all;
Expand Down Expand Up @@ -62,10 +63,15 @@ protected function analyzeFiles(
$actualErrors = $this->processActualErrors(array_values($fileErrors));
$expectedErrors = $this->parseExpectedErrors($file);

$extraErrors = array_filter($actualErrors, static fn (string $error): bool => !in_array($error, $expectedErrors, true));
$missingErrors = array_filter($expectedErrors, static fn (string $error): bool => !in_array($error, $actualErrors, true));

self::assertSame(
implode("\n", $expectedErrors) . "\n",
implode("\n", $actualErrors) . "\n",
"Errors in file {$file} do not match",
"Errors in file {$file} do not match:\n\n" .
($extraErrors === [] ? '' : "New errors reported:\n" . implode("\n", $extraErrors) . "\n\n") .
($missingErrors === [] ? '' : "Errors not reported:\n" . implode("\n", $missingErrors) . "\n\n"),
);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php declare(strict_types = 1);

namespace DisallowDivisionByLiteralZeroRule;

function testErrorMessage(): void
{
$a = 10;
$invalidDivision = $a / 0; // error: This error should not be reported
$validDivision = $a / 2; // error: Division by literal zero is not allowed
}
24 changes: 24 additions & 0 deletions tests/RuleTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,28 @@ public function testAutofix(): void
self::assertFileEquals($expectedFile, $tmpFile);
}

public function testErrorMessageFormat(): void
{
$testFile = str_replace('/', DIRECTORY_SEPARATOR, __DIR__ . '/Rule/Data/DisallowDivisionByLiteralZeroRule/error-message.php');

try {
$this->analyzeFiles([$testFile]);
self::fail('Expected assertion to fail');
} catch (AssertionFailedError $e) { // @phpstan-ignore catch.internalClass
$expectedMessage = <<<MSG
Errors in file {$testFile} do not match:

New errors reported:
08: Division by literal zero is not allowed

Errors not reported:
08: This error should not be reported
09: Division by literal zero is not allowed


MSG;
self::assertStringContainsString($expectedMessage, $e->getMessage());
}
}

}