|
6 | 6 | use PHPStan\Analyser\Error; |
7 | 7 | use PHPStan\Rules\Rule; |
8 | 8 | use PHPStan\Testing\RuleTestCase as OriginalRuleTestCase; |
| 9 | +use function array_filter; |
9 | 10 | use function array_values; |
10 | 11 | use function explode; |
11 | 12 | use function file_get_contents; |
|
15 | 16 | use function preg_match; |
16 | 17 | use function preg_match_all; |
17 | 18 | use function preg_replace; |
| 19 | +use function sort; |
18 | 20 | use function sprintf; |
19 | 21 | use function trim; |
20 | 22 | use function uniqid; |
|
26 | 28 | abstract class RuleTestCase extends OriginalRuleTestCase |
27 | 29 | { |
28 | 30 |
|
29 | | - protected function analyseFile(string $file, bool $autofix = false): void |
| 31 | + /** |
| 32 | + * @param list<string> $files |
| 33 | + */ |
| 34 | + protected function analyzeFiles(array $files, bool $autofix = false): void |
30 | 35 | { |
31 | | - $analyserErrors = $this->gatherAnalyserErrors([$file]); |
| 36 | + sort($files); |
| 37 | + |
| 38 | + $analyserErrors = $this->gatherAnalyserErrors($files); |
32 | 39 |
|
33 | 40 | if ($autofix) { |
34 | | - $this->autofix($file, $analyserErrors); |
35 | | - self::fail("File {$file} was autofixed. This setup should never remain in the codebase."); |
| 41 | + foreach ($files as $file) { |
| 42 | + $fileErrors = array_filter($analyserErrors, static fn(Error $error): bool => $error->getFile() === $file); |
| 43 | + $this->autofix($file, array_values($fileErrors)); |
| 44 | + } |
| 45 | + |
| 46 | + $filesStr = implode(', ', $files); |
| 47 | + self::fail("Files {$filesStr} were autofixed. This setup should never remain in the codebase."); |
36 | 48 | } |
37 | 49 |
|
38 | | - $actualErrors = $this->processActualErrors($analyserErrors); |
39 | | - $expectedErrors = $this->parseExpectedErrors($file); |
| 50 | + foreach ($files as $file) { |
| 51 | + $fileErrors = array_filter($analyserErrors, static fn(Error $error): bool => $error->getFile() === $file); |
| 52 | + $actualErrors = $this->processActualErrors(array_values($fileErrors)); |
| 53 | + $expectedErrors = $this->parseExpectedErrors($file); |
40 | 54 |
|
41 | | - self::assertSame( |
42 | | - implode("\n", $expectedErrors) . "\n", |
43 | | - implode("\n", $actualErrors) . "\n", |
44 | | - ); |
| 55 | + self::assertSame( |
| 56 | + implode("\n", $expectedErrors) . "\n", |
| 57 | + implode("\n", $actualErrors) . "\n", |
| 58 | + "Errors in file {$file} do not match", |
| 59 | + ); |
| 60 | + } |
45 | 61 | } |
46 | 62 |
|
47 | 63 | /** |
|
0 commit comments