-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c5555d
commit cdfdccf
Showing
8 changed files
with
230 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TwigCsFixer\Report\Reporter; | ||
|
||
use Symfony\Component\Console\Output\OutputInterface; | ||
use TwigCsFixer\Report\Report; | ||
use TwigCsFixer\Report\SniffViolation; | ||
|
||
final class JUnitReporter implements ReporterInterface | ||
{ | ||
public const NAME = 'junit'; | ||
|
||
public function display(OutputInterface $output, Report $report, ?string $level = null): void | ||
{ | ||
$messages = $report->getAllMessages($level); | ||
$totalErrors = \count($messages); | ||
|
||
$text = '<?xml version="1.0" encoding="UTF-8"?>'."\n"; | ||
$text .= '<testsuites>'."\n"; | ||
$text .= ' '.sprintf( | ||
'<testsuite name="Twig CS Fixer" tests="%d" failures="%d">', | ||
max($totalErrors, 1), | ||
$totalErrors | ||
)."\n"; | ||
|
||
if ($totalErrors > 0) { | ||
foreach ($messages as $message) { | ||
$text .= $this->createTestCase( | ||
sprintf('%s:%s', $message->getFilename(), $message->getLine() ?? 0), | ||
strtolower(SniffViolation::getLevelAsString($message->getLevel())), | ||
$message->getMessage() | ||
); | ||
} | ||
} else { | ||
$text .= $this->createTestCase('All OK'); | ||
} | ||
|
||
$text .= ' </testsuite>'."\n"; | ||
$text .= '</testsuites>'; | ||
|
||
$output->writeln($text); | ||
} | ||
|
||
private function createTestCase(string $name, string $type = '', ?string $message = null): string | ||
{ | ||
$result = ' '.sprintf('<testcase name="%s">', $this->xmlEncode($name))."\n"; | ||
|
||
if (null !== $message) { | ||
$result .= ' ' | ||
.sprintf('<failure type="%s" message="%s" />', $this->xmlEncode($type), $this->xmlEncode($message)) | ||
."\n"; | ||
} | ||
|
||
$result .= ' </testcase>'."\n"; | ||
|
||
return $result; | ||
} | ||
|
||
private function xmlEncode(string $data): string | ||
{ | ||
return htmlspecialchars($data, \ENT_XML1 | \ENT_QUOTES); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace TwigCsFixer\Tests\Report\Formatter; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use SplFileInfo; | ||
use Symfony\Component\Console\Output\BufferedOutput; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use TwigCsFixer\Report\Report; | ||
use TwigCsFixer\Report\Reporter\JUnitReporter; | ||
use TwigCsFixer\Report\SniffViolation; | ||
|
||
final class JUnitReporterTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider displayDataProvider | ||
*/ | ||
public function testDisplayErrors(string $expected, ?string $level): void | ||
{ | ||
$textFormatter = new JUnitReporter(); | ||
|
||
$file = __DIR__.'/Fixtures/file.twig'; | ||
$file2 = __DIR__.'/Fixtures/file2.twig'; | ||
$file3 = __DIR__.'/Fixtures/file3.twig'; | ||
$report = new Report([new SplFileInfo($file), new SplFileInfo($file2), new SplFileInfo($file3)]); | ||
|
||
$violation0 = new SniffViolation(SniffViolation::LEVEL_NOTICE, 'Notice', $file, 1, 11, 'NoticeSniff'); | ||
$report->addMessage($violation0); | ||
$violation1 = new SniffViolation(SniffViolation::LEVEL_WARNING, 'Warning', $file, 2, 22, 'WarningSniff'); | ||
$report->addMessage($violation1); | ||
$violation2 = new SniffViolation(SniffViolation::LEVEL_ERROR, 'Error', $file, 3, 33, 'ErrorSniff'); | ||
$report->addMessage($violation2); | ||
$violation3 = new SniffViolation(SniffViolation::LEVEL_FATAL, 'Fatal', $file); | ||
$report->addMessage($violation3); | ||
|
||
$violation4 = new SniffViolation(SniffViolation::LEVEL_NOTICE, 'Notice2', $file2, 1, 11, 'Notice2Sniff'); | ||
$report->addMessage($violation4); | ||
|
||
$violation5 = new SniffViolation(SniffViolation::LEVEL_FATAL, '\'"<&>"\'', $file3); | ||
$report->addMessage($violation5); | ||
|
||
$output = new BufferedOutput(OutputInterface::VERBOSITY_NORMAL, true); | ||
$textFormatter->display($output, $report, $level); | ||
|
||
$text = $output->fetch(); | ||
static::assertStringContainsString($expected, $text); | ||
} | ||
|
||
/** | ||
* @return iterable<array-key, array{string, string|null}> | ||
*/ | ||
public static function displayDataProvider(): iterable | ||
{ | ||
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"> | ||
<failure type="notice" message="Notice" /> | ||
</testcase> | ||
<testcase name="%1\$s/Fixtures/file.twig:2"> | ||
<failure type="warning" message="Warning" /> | ||
</testcase> | ||
<testcase name="%1\$s/Fixtures/file.twig:3"> | ||
<failure type="error" message="Error" /> | ||
</testcase> | ||
<testcase name="%1\$s/Fixtures/file.twig:0"> | ||
<failure type="fatal" message="Fatal" /> | ||
</testcase> | ||
<testcase name="%1\$s/Fixtures/file2.twig:1"> | ||
<failure type="notice" message="Notice2" /> | ||
</testcase> | ||
<testcase name="%1\$s/Fixtures/file3.twig:0"> | ||
<failure type="fatal" message="'"<&>"'" /> | ||
</testcase> | ||
</testsuite> | ||
</testsuites> | ||
EOD, | ||
__DIR__ | ||
), | ||
null, | ||
]; | ||
} | ||
|
||
public function testDisplaySuccess(): void | ||
{ | ||
$textFormatter = new JUnitReporter(); | ||
|
||
$file = __DIR__.'/Fixtures/file.twig'; | ||
$file2 = __DIR__.'/Fixtures/file2.twig'; | ||
$file3 = __DIR__.'/Fixtures/file3.twig'; | ||
$report = new Report([new SplFileInfo($file), new SplFileInfo($file2), new SplFileInfo($file3)]); | ||
|
||
$output = new BufferedOutput(OutputInterface::VERBOSITY_NORMAL, true); | ||
$textFormatter->display($output, $report); | ||
|
||
$expected = <<<EOD | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<testsuites> | ||
<testsuite name="Twig CS Fixer" tests="1" failures="0"> | ||
<testcase name="All OK"> | ||
</testcase> | ||
</testsuite> | ||
</testsuites> | ||
EOD; | ||
|
||
$text = $output->fetch(); | ||
static::assertStringContainsString($expected, $text); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters