|
6 | 6 | use Nette\Neon\Neon;
|
7 | 7 | use PHPStan\Analyser\Error;
|
8 | 8 | use PHPStan\Command\AnalysisResult;
|
| 9 | +use PHPStan\Command\ErrorsConsoleStyle; |
| 10 | +use PHPStan\Command\Symfony\SymfonyOutput; |
| 11 | +use PHPStan\Command\Symfony\SymfonyStyle; |
9 | 12 | use PHPStan\File\SimpleRelativePathHelper;
|
| 13 | +use PHPStan\ShouldNotHappenException; |
10 | 14 | use PHPStan\Testing\ErrorFormatterTestCase;
|
| 15 | +use PHPUnit\Framework\Assert; |
| 16 | +use Symfony\Component\Console\Input\StringInput; |
| 17 | +use Symfony\Component\Console\Output\StreamOutput; |
| 18 | +use function fopen; |
11 | 19 | use function mt_srand;
|
| 20 | +use function rewind; |
12 | 21 | use function shuffle;
|
13 | 22 | use function sprintf;
|
| 23 | +use function stream_get_contents; |
| 24 | +use function substr; |
14 | 25 | use function trim;
|
15 | 26 |
|
16 | 27 | class BaselineNeonErrorFormatterTest extends ErrorFormatterTestCase
|
@@ -284,4 +295,63 @@ public function testOutputOrdering(array $errors): void
|
284 | 295 | );
|
285 | 296 | }
|
286 | 297 |
|
| 298 | + /** |
| 299 | + * @return Generator<string, array{errors: list<Error>}> |
| 300 | + */ |
| 301 | + public function endOfFileNewlinesProvider(): Generator |
| 302 | + { |
| 303 | + yield 'one error' => [ |
| 304 | + 'errors' => [ |
| 305 | + new Error('Error #1', 'TestfileA', 1), |
| 306 | + ], |
| 307 | + ]; |
| 308 | + |
| 309 | + yield 'no errors' => [ |
| 310 | + 'errors' => [], |
| 311 | + ]; |
| 312 | + } |
| 313 | + |
| 314 | + /** |
| 315 | + * @dataProvider endOfFileNewlinesProvider |
| 316 | + * |
| 317 | + * @param list<Error> $errors |
| 318 | + */ |
| 319 | + public function testEndOfFileNewlines(array $errors): void |
| 320 | + { |
| 321 | + $formatter = new BaselineNeonErrorFormatter(new SimpleRelativePathHelper(self::DIRECTORY_PATH)); |
| 322 | + $result = new AnalysisResult( |
| 323 | + $errors, |
| 324 | + [], |
| 325 | + [], |
| 326 | + [], |
| 327 | + false, |
| 328 | + null, |
| 329 | + true, |
| 330 | + ); |
| 331 | + |
| 332 | + $resource = fopen('php://memory', 'w', false); |
| 333 | + if ($resource === false) { |
| 334 | + throw new ShouldNotHappenException(); |
| 335 | + } |
| 336 | + $outputStream = new StreamOutput($resource, StreamOutput::VERBOSITY_NORMAL, false); |
| 337 | + |
| 338 | + $errorConsoleStyle = new ErrorsConsoleStyle(new StringInput(''), $outputStream); |
| 339 | + $output = new SymfonyOutput($outputStream, new SymfonyStyle($errorConsoleStyle)); |
| 340 | + |
| 341 | + $formatter->formatErrors( |
| 342 | + $result, |
| 343 | + $output, |
| 344 | + ); |
| 345 | + |
| 346 | + rewind($outputStream->getStream()); |
| 347 | + |
| 348 | + $content = stream_get_contents($outputStream->getStream()); |
| 349 | + if ($content === false) { |
| 350 | + throw new ShouldNotHappenException(); |
| 351 | + } |
| 352 | + |
| 353 | + Assert::assertSame("\n\n", substr($content, -2)); |
| 354 | + Assert::assertNotSame("\n", substr($content, -3, 1)); |
| 355 | + } |
| 356 | + |
287 | 357 | }
|
0 commit comments