Skip to content

Commit 9cf91e1

Browse files
VasekPurchartondrejmirtes
authored andcommitted
Test baseline end of file newlines
1 parent c900ee2 commit 9cf91e1

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

tests/PHPStan/Command/ErrorFormatter/BaselineNeonErrorFormatterTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,22 @@
66
use Nette\Neon\Neon;
77
use PHPStan\Analyser\Error;
88
use PHPStan\Command\AnalysisResult;
9+
use PHPStan\Command\ErrorsConsoleStyle;
10+
use PHPStan\Command\Symfony\SymfonyOutput;
11+
use PHPStan\Command\Symfony\SymfonyStyle;
912
use PHPStan\File\SimpleRelativePathHelper;
13+
use PHPStan\ShouldNotHappenException;
1014
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;
1119
use function mt_srand;
20+
use function rewind;
1221
use function shuffle;
1322
use function sprintf;
23+
use function stream_get_contents;
24+
use function substr;
1425
use function trim;
1526

1627
class BaselineNeonErrorFormatterTest extends ErrorFormatterTestCase
@@ -284,4 +295,63 @@ public function testOutputOrdering(array $errors): void
284295
);
285296
}
286297

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+
287357
}

0 commit comments

Comments
 (0)