Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Feb 19, 2024
1 parent 96128ad commit 9e816d3
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 74 deletions.
8 changes: 4 additions & 4 deletions src/Report/Reporter/TextReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public function display(
$formattedText[] = sprintf(
self::ERROR_LINE_FORMAT,
$no,
wordwrap($code, self::ERROR_LINE_WIDTH)
wordwrap($code, self::ERROR_LINE_WIDTH, \PHP_EOL)
);

if ($no === $violation->getLine()) {
Expand All @@ -75,7 +75,7 @@ public function display(
$messageLevel = Violation::getLevelAsString($violation->getLevel());
$rows[] = [
new TableCell(sprintf('<comment>%s</comment>', $messageLevel)),
implode("\n", $formattedText),
implode(\PHP_EOL, $formattedText),
];
}

Expand Down Expand Up @@ -106,7 +106,7 @@ public function display(
*/
private function getContext(string $template, int $line): array
{
$lines = explode("\n", $template);
$lines = explode(\PHP_EOL, $template);
$position = max(0, $line - 2);
$max = min(\count($lines), $line + 1);

Expand All @@ -128,7 +128,7 @@ private function formatErrorMessage(Violation $message, bool $debug): string
return sprintf(
sprintf('<fg=red>%s</fg=red>', self::ERROR_LINE_FORMAT),
self::ERROR_CURSOR_CHAR,
wordwrap($message->getDebugMessage($debug), self::ERROR_LINE_WIDTH)
wordwrap($message->getDebugMessage($debug), self::ERROR_LINE_WIDTH, \PHP_EOL)
);
}
}
35 changes: 21 additions & 14 deletions tests/Report/Reporter/CheckstyleReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public function testDisplayErrors(string $expected, ?string $level, bool $debug)
{
$textFormatter = new CheckstyleReporter();

$file = __DIR__.'/Fixtures/file.twig';
$file2 = __DIR__.'/Fixtures/file2.twig';
$file3 = __DIR__.'/Fixtures/file3.twig';
$slash = \DIRECTORY_SEPARATOR;
$file = __DIR__.$slash.'Fixtures'.$slash.'file.twig';
$file2 = __DIR__.$slash.'Fixtures'.$slash.'file2.twig';
$file3 = __DIR__.$slash.'Fixtures'.$slash.'file3.twig';
$report = new Report([new \SplFileInfo($file), new \SplFileInfo($file2), new \SplFileInfo($file3)]);

$violation0 = new Violation(
Expand Down Expand Up @@ -81,34 +82,38 @@ public function testDisplayErrors(string $expected, ?string $level, bool $debug)
$textFormatter->display($output, $report, $level, $debug);

$text = $output->fetch();
static::assertStringContainsString($expected, $text);
static::assertSame($expected, rtrim($text));
}

/**
* @return iterable<array-key, array{string, string|null, bool}>
*/
public static function displayDataProvider(): iterable
{
$slash = \DIRECTORY_SEPARATOR;

yield [
sprintf(
<<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<file name="%1\$s/Fixtures/file.twig">
<file name="%1\$s">
<error line="1" severity="notice" message="Notice" source="NoticeRule"/>
<error line="2" column="22" severity="warning" message="Warning" source="WarningRule"/>
<error line="3" column="33" severity="error" message="Error" source="ErrorRule"/>
<error severity="fatal" message="Fatal"/>
</file>
<file name="%1\$s/Fixtures/file2.twig">
<file name="%2\$s">
<error line="1" severity="notice" message="Notice2" source="Notice2Rule"/>
</file>
<file name="%1\$s/Fixtures/file3.twig">
<file name="%3\$s">
<error severity="fatal" message="&apos;&quot;&lt;&amp;&gt;&quot;&apos;"/>
</file>
</checkstyle>
EOD,
__DIR__
__DIR__.$slash.'Fixtures'.$slash.'file.twig',
__DIR__.$slash.'Fixtures'.$slash.'file2.twig',
__DIR__.$slash.'Fixtures'.$slash.'file3.twig',
),
null,
false,
Expand All @@ -119,16 +124,17 @@ public static function displayDataProvider(): iterable
<<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<file name="%1\$s/Fixtures/file.twig">
<file name="%1\$s">
<error line="3" column="33" severity="error" message="Error" source="ErrorRule"/>
<error severity="fatal" message="Fatal"/>
</file>
<file name="%1\$s/Fixtures/file3.twig">
<file name="%2\$s">
<error severity="fatal" message="&apos;&quot;&lt;&amp;&gt;&quot;&apos;"/>
</file>
</checkstyle>
EOD,
__DIR__
__DIR__.$slash.'Fixtures'.$slash.'file.twig',
__DIR__.$slash.'Fixtures'.$slash.'file3.twig',
),
Report::MESSAGE_TYPE_ERROR,
false,
Expand All @@ -139,16 +145,17 @@ public static function displayDataProvider(): iterable
<<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<file name="%1\$s/Fixtures/file.twig">
<file name="%1\$s">
<error line="3" column="33" severity="error" message="ErrorId:3:33" source="ErrorRule"/>
<error severity="fatal" message="FatalId"/>
</file>
<file name="%1\$s/Fixtures/file3.twig">
<file name="%2\$s">
<error severity="fatal" message="FatalId"/>
</file>
</checkstyle>
EOD,
__DIR__
__DIR__.$slash.'Fixtures'.$slash.'file.twig',
__DIR__.$slash.'Fixtures'.$slash.'file3.twig',
),
Report::MESSAGE_TYPE_ERROR,
true,
Expand Down
27 changes: 15 additions & 12 deletions tests/Report/Reporter/GithubReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ public function testDisplayErrors(string $expected, ?string $level, bool $debug)
{
$textFormatter = new GithubReporter();

$file = __DIR__.'/Fixtures/file.twig';
$slash = \DIRECTORY_SEPARATOR;
$file = __DIR__.$slash.'Fixtures'.$slash.'file.twig';
$report = new Report([new \SplFileInfo($file)]);

$violation0 = new Violation(
Expand Down Expand Up @@ -61,36 +62,38 @@ public function testDisplayErrors(string $expected, ?string $level, bool $debug)
$textFormatter->display($output, $report, $level, $debug);

$text = $output->fetch();
static::assertStringContainsString($expected, $text);
static::assertSame($expected, rtrim($text));
}

/**
* @return iterable<array-key, array{string, string|null, bool}>
*/
public static function displayDataProvider(): iterable
{
$slash = \DIRECTORY_SEPARATOR;

yield [
sprintf(
<<<EOD
::notice file=%1\$s/Fixtures/file.twig,line=1::Notice
::warning file=%1\$s/Fixtures/file.twig,line=2,col=22::Warning
::error file=%1\$s/Fixtures/file.twig,line=3,col=33::Error
::error file=%1\$s/Fixtures/file.twig::Fatal%%0Awith new line
::notice file=%1\$s,line=1::Notice
::warning file=%1\$s,line=2,col=22::Warning
::error file=%1\$s,line=3,col=33::Error
::error file=%1\$s::Fatal%%0Awith new line
EOD,
__DIR__
__DIR__.$slash.'Fixtures'.$slash.'file.twig',
),
null,
false,
];
yield [
sprintf(
<<<EOD
::notice file=%1\$s/Fixtures/file.twig,line=1::NoticeId:1
::warning file=%1\$s/Fixtures/file.twig,line=2,col=22::WarningId:2:22
::error file=%1\$s/Fixtures/file.twig,line=3,col=33::ErrorId:3:33
::error file=%1\$s/Fixtures/file.twig::FatalId
::notice file=%1\$s,line=1::NoticeId:1
::warning file=%1\$s,line=2,col=22::WarningId:2:22
::error file=%1\$s,line=3,col=33::ErrorId:3:33
::error file=%1\$s::FatalId
EOD,
__DIR__
__DIR__.$slash.'Fixtures'.$slash.'file.twig',
),
null,
true,
Expand Down
52 changes: 30 additions & 22 deletions tests/Report/Reporter/JUnitReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ public function testDisplayErrors(string $expected, ?string $level, bool $debug)
{
$textFormatter = new JUnitReporter();

$file = __DIR__.'/Fixtures/file.twig';
$file2 = __DIR__.'/Fixtures/file2.twig';
$file3 = __DIR__.'/Fixtures/file3.twig';
$slash = \DIRECTORY_SEPARATOR;
$file = __DIR__.$slash.'Fixtures'.$slash.'file.twig';
$file2 = __DIR__.$slash.'Fixtures'.$slash.'file2.twig';
$file3 = __DIR__.$slash.'Fixtures'.$slash.'file3.twig';
$report = new Report([new \SplFileInfo($file), new \SplFileInfo($file2), new \SplFileInfo($file3)]);

$violation0 = new Violation(
Expand Down Expand Up @@ -81,42 +82,46 @@ public function testDisplayErrors(string $expected, ?string $level, bool $debug)
$textFormatter->display($output, $report, $level, $debug);

$text = $output->fetch();
static::assertStringContainsString($expected, $text);
static::assertSame($expected, rtrim($text));
}

/**
* @return iterable<array-key, array{string, string|null, bool}>
*/
public static function displayDataProvider(): iterable
{
$slash = \DIRECTORY_SEPARATOR;

yield [
sprintf(
<<<EOD
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="Twig CS Fixer" tests="6" failures="6">
<testcase name="%1\$s/Fixtures/file.twig:1">
<testcase name="%1\$s:1">
<failure type="notice" message="Notice" />
</testcase>
<testcase name="%1\$s/Fixtures/file.twig:2">
<testcase name="%1\$s:2">
<failure type="warning" message="Warning" />
</testcase>
<testcase name="%1\$s/Fixtures/file.twig:3">
<testcase name="%1\$s:3">
<failure type="error" message="Error" />
</testcase>
<testcase name="%1\$s/Fixtures/file.twig:0">
<testcase name="%1\$s:0">
<failure type="fatal" message="Fatal" />
</testcase>
<testcase name="%1\$s/Fixtures/file2.twig:1">
<testcase name="%2\$s:1">
<failure type="notice" message="Notice2" />
</testcase>
<testcase name="%1\$s/Fixtures/file3.twig:0">
<testcase name="%3\$s:0">
<failure type="fatal" message="&apos;&quot;&lt;&amp;&gt;&quot;&apos;" />
</testcase>
</testsuite>
</testsuites>
EOD,
__DIR__
__DIR__.$slash.'Fixtures'.$slash.'file.twig',
__DIR__.$slash.'Fixtures'.$slash.'file2.twig',
__DIR__.$slash.'Fixtures'.$slash.'file3.twig',
),
null,
false,
Expand All @@ -127,28 +132,30 @@ public static function displayDataProvider(): iterable
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="Twig CS Fixer" tests="6" failures="6">
<testcase name="%1\$s/Fixtures/file.twig:1">
<testcase name="%1\$s:1">
<failure type="notice" message="NoticeId:1" />
</testcase>
<testcase name="%1\$s/Fixtures/file.twig:2">
<testcase name="%1\$s:2">
<failure type="warning" message="WarningId:2:22" />
</testcase>
<testcase name="%1\$s/Fixtures/file.twig:3">
<testcase name="%1\$s:3">
<failure type="error" message="ErrorId:3:33" />
</testcase>
<testcase name="%1\$s/Fixtures/file.twig:0">
<testcase name="%1\$s:0">
<failure type="fatal" message="FatalId" />
</testcase>
<testcase name="%1\$s/Fixtures/file2.twig:1">
<testcase name="%2\$s:1">
<failure type="notice" message="NoticeId:1" />
</testcase>
<testcase name="%1\$s/Fixtures/file3.twig:0">
<testcase name="%3\$s:0">
<failure type="fatal" message="FatalId" />
</testcase>
</testsuite>
</testsuites>
EOD,
__DIR__
__DIR__.$slash.'Fixtures'.$slash.'file.twig',
__DIR__.$slash.'Fixtures'.$slash.'file2.twig',
__DIR__.$slash.'Fixtures'.$slash.'file3.twig',
),
null,
true,
Expand All @@ -159,9 +166,10 @@ public function testDisplaySuccess(): void
{
$textFormatter = new JUnitReporter();

$file = __DIR__.'/Fixtures/file.twig';
$file2 = __DIR__.'/Fixtures/file2.twig';
$file3 = __DIR__.'/Fixtures/file3.twig';
$slash = \DIRECTORY_SEPARATOR;
$file = __DIR__.$slash.'Fixtures'.$slash.'file.twig';
$file2 = __DIR__.$slash.'Fixtures'.$slash.'file2.twig';
$file3 = __DIR__.$slash.'Fixtures'.$slash.'file3.twig';
$report = new Report([new \SplFileInfo($file), new \SplFileInfo($file2), new \SplFileInfo($file3)]);

$output = new BufferedOutput(OutputInterface::VERBOSITY_NORMAL, true);
Expand All @@ -178,6 +186,6 @@ public function testDisplaySuccess(): void
EOD;

$text = $output->fetch();
static::assertStringContainsString($expected, $text);
static::assertSame($expected, rtrim($text));
}
}
3 changes: 2 additions & 1 deletion tests/Report/Reporter/NullReporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public function testDisplayErrors(?string $level): void
{
$textFormatter = new NullReporter();

$file = __DIR__.'/Fixtures/file.twig';
$slash = \DIRECTORY_SEPARATOR;
$file = __DIR__.$slash.'Fixtures'.$slash.'file.twig';
$report = new Report([new \SplFileInfo($file)]);

$violation0 = new Violation(Violation::LEVEL_NOTICE, 'Notice', $file);
Expand Down
Loading

0 comments on commit 9e816d3

Please sign in to comment.