|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Command\ErrorFormatter; |
| 4 | + |
| 5 | +use PHPStan\File\FuzzyRelativePathHelper; |
| 6 | +use PHPStan\File\NullRelativePathHelper; |
| 7 | +use PHPStan\Testing\ErrorFormatterTestCase; |
| 8 | +use function sprintf; |
| 9 | + |
| 10 | +class GroupedErrorFormatterTest extends ErrorFormatterTestCase |
| 11 | +{ |
| 12 | + |
| 13 | + public function dataFormatterOutputProvider(): iterable |
| 14 | + { |
| 15 | + yield [ |
| 16 | + 'message' => 'No errors', |
| 17 | + 'exitCode' => 0, |
| 18 | + 'numFileErrors' => 0, |
| 19 | + 'numGenericErrors' => 0, |
| 20 | + 'expected' => ' |
| 21 | + [OK] No errors |
| 22 | +
|
| 23 | +', |
| 24 | + ]; |
| 25 | + |
| 26 | + yield [ |
| 27 | + 'message' => 'One file error', |
| 28 | + 'exitCode' => 1, |
| 29 | + 'numFileErrors' => 1, |
| 30 | + 'numGenericErrors' => 0, |
| 31 | + 'expected' => '[without identifier] (1x): |
| 32 | + - /data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with "spaces" and unicode 😃.php:4: Foo |
| 33 | +
|
| 34 | +
|
| 35 | + [ERROR] Found 1 error |
| 36 | +
|
| 37 | +', |
| 38 | + ]; |
| 39 | + |
| 40 | + yield [ |
| 41 | + 'message' => 'One generic error', |
| 42 | + 'exitCode' => 1, |
| 43 | + 'numFileErrors' => 0, |
| 44 | + 'numGenericErrors' => 1, |
| 45 | + 'expected' => '?:?:first generic error |
| 46 | +
|
| 47 | + [ERROR] Found 1 error |
| 48 | +
|
| 49 | +', |
| 50 | + ]; |
| 51 | + |
| 52 | + yield [ |
| 53 | + 'message' => 'Multiple file errors', |
| 54 | + 'exitCode' => 1, |
| 55 | + 'numFileErrors' => 4, |
| 56 | + 'numGenericErrors' => 0, |
| 57 | + 'expected' => '[without identifier] (4x): |
| 58 | + - /data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with "spaces" and unicode 😃.php:2: Bar |
| 59 | +Bar2 |
| 60 | + - /data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with "spaces" and unicode 😃.php:4: Foo |
| 61 | + - /data/folder/with space/and unicode 😃/project/foo.php:1: Foo<Bar> |
| 62 | + - /data/folder/with space/and unicode 😃/project/foo.php:5: Bar |
| 63 | +Bar2 |
| 64 | +
|
| 65 | +
|
| 66 | + [ERROR] Found 4 errors |
| 67 | +
|
| 68 | +', |
| 69 | + ]; |
| 70 | + |
| 71 | + yield [ |
| 72 | + 'message' => 'Multiple generic errors', |
| 73 | + 'exitCode' => 1, |
| 74 | + 'numFileErrors' => 0, |
| 75 | + 'numGenericErrors' => 2, |
| 76 | + 'expected' => '?:?:first generic error |
| 77 | +?:?:second generic<error> |
| 78 | +
|
| 79 | + [ERROR] Found 2 errors |
| 80 | +
|
| 81 | +', |
| 82 | + ]; |
| 83 | + |
| 84 | + yield [ |
| 85 | + 'message' => 'Multiple file, multiple generic errors', |
| 86 | + 'exitCode' => 1, |
| 87 | + 'numFileErrors' => 4, |
| 88 | + 'numGenericErrors' => 2, |
| 89 | + 'expected' => '[without identifier] (4x): |
| 90 | + - /data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with "spaces" and unicode 😃.php:2: Bar |
| 91 | +Bar2 |
| 92 | + - /data/folder/with space/and unicode 😃/project/folder with unicode 😃/file name with "spaces" and unicode 😃.php:4: Foo |
| 93 | + - /data/folder/with space/and unicode 😃/project/foo.php:1: Foo<Bar> |
| 94 | + - /data/folder/with space/and unicode 😃/project/foo.php:5: Bar |
| 95 | +Bar2 |
| 96 | +
|
| 97 | +?:?:first generic error |
| 98 | +?:?:second generic<error> |
| 99 | +
|
| 100 | + [ERROR] Found 6 errors |
| 101 | +
|
| 102 | +', |
| 103 | + ]; |
| 104 | + |
| 105 | + yield [ |
| 106 | + 'message' => 'One file error with identifier', |
| 107 | + 'exitCode' => 1, |
| 108 | + 'numFileErrors' => [5, 6], |
| 109 | + 'numGenericErrors' => 0, |
| 110 | + 'expected' => '[foobar.buz] (1x): |
| 111 | + - /data/folder/with space/and unicode 😃/project/foo.php:5: Foobar\Buz |
| 112 | +
|
| 113 | +
|
| 114 | + [ERROR] Found 1 error |
| 115 | +
|
| 116 | +', |
| 117 | + ]; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @dataProvider dataFormatterOutputProvider |
| 122 | + * @param array{int, int}|int $numFileErrors |
| 123 | + */ |
| 124 | + public function testFormatErrors( |
| 125 | + string $message, |
| 126 | + int $exitCode, |
| 127 | + array|int $numFileErrors, |
| 128 | + int $numGenericErrors, |
| 129 | + string $expected, |
| 130 | + ): void |
| 131 | + { |
| 132 | + $formatter = $this->createErrorFormatter(null); |
| 133 | + |
| 134 | + $this->assertSame($exitCode, $formatter->formatErrors( |
| 135 | + $this->getAnalysisResult($numFileErrors, $numGenericErrors), |
| 136 | + $this->getOutput(), |
| 137 | + ), sprintf('%s: response code do not match', $message)); |
| 138 | + |
| 139 | + $this->assertEquals($expected, $this->getOutputContent(), sprintf('%s: output do not match', $message)); |
| 140 | + } |
| 141 | + |
| 142 | + private function createErrorFormatter(?string $editorUrl, ?string $editorUrlTitle = null): GroupedErrorFormatter |
| 143 | + { |
| 144 | + $relativePathHelper = new FuzzyRelativePathHelper(new NullRelativePathHelper(), self::DIRECTORY_PATH, [], '/'); |
| 145 | + |
| 146 | + return new GroupedErrorFormatter( |
| 147 | + $relativePathHelper, |
| 148 | + $editorUrl, |
| 149 | + $editorUrlTitle, |
| 150 | + ); |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments