CheckstyleReporterTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer\Tests\Console\Report\FixReport;
  13. use PhpCsFixer\Console\Application;
  14. use PhpCsFixer\Console\Report\FixReport\CheckstyleReporter;
  15. use PhpCsFixer\Console\Report\FixReport\ReporterInterface;
  16. use PhpCsFixer\PhpunitConstraintXmlMatchesXsd\Constraint\XmlMatchesXsd;
  17. use Symfony\Component\Console\Formatter\OutputFormatter;
  18. /**
  19. * @author Kévin Gomez <contact@kevingomez.fr>
  20. *
  21. * @internal
  22. *
  23. * @covers \PhpCsFixer\Console\Report\FixReport\CheckstyleReporter
  24. */
  25. final class CheckstyleReporterTest extends AbstractReporterTestCase
  26. {
  27. /**
  28. * "checkstyle" XML schema.
  29. */
  30. private static ?string $xsd = null;
  31. public static function setUpBeforeClass(): void
  32. {
  33. parent::setUpBeforeClass();
  34. $content = file_get_contents(__DIR__.'/../../../../doc/schemas/fix/checkstyle.xsd');
  35. if (false === $content) {
  36. throw new \RuntimeException('Cannot read file.');
  37. }
  38. self::$xsd = $content;
  39. }
  40. public static function tearDownAfterClass(): void
  41. {
  42. parent::tearDownAfterClass();
  43. self::$xsd = null;
  44. }
  45. protected static function createNoErrorReport(): string
  46. {
  47. $about = Application::getAbout();
  48. return <<<XML
  49. <?xml version="1.0" encoding="UTF-8"?>
  50. <checkstyle version="{$about}" />
  51. XML;
  52. }
  53. protected static function createSimpleReport(): string
  54. {
  55. $about = Application::getAbout();
  56. return <<<XML
  57. <?xml version="1.0" encoding="UTF-8"?>
  58. <checkstyle version="{$about}">
  59. <file name="someFile.php">
  60. <error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here" message="Found violation(s) of type: some_fixer_name_here" />
  61. </file>
  62. </checkstyle>
  63. XML;
  64. }
  65. protected static function createWithDiffReport(): string
  66. {
  67. $about = Application::getAbout();
  68. // NOTE: checkstyle format does NOT include diffs
  69. return <<<XML
  70. <?xml version="1.0" encoding="UTF-8"?>
  71. <checkstyle version="{$about}">
  72. <file name="someFile.php">
  73. <error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here" message="Found violation(s) of type: some_fixer_name_here" />
  74. </file>
  75. </checkstyle>
  76. XML;
  77. }
  78. protected static function createWithAppliedFixersReport(): string
  79. {
  80. $about = Application::getAbout();
  81. return <<<XML
  82. <?xml version="1.0" encoding="UTF-8"?>
  83. <checkstyle version="{$about}">
  84. <file name="someFile.php">
  85. <error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here_1" message="Found violation(s) of type: some_fixer_name_here_1" />
  86. <error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here_2" message="Found violation(s) of type: some_fixer_name_here_2" />
  87. </file>
  88. </checkstyle>
  89. XML;
  90. }
  91. protected static function createWithTimeAndMemoryReport(): string
  92. {
  93. $about = Application::getAbout();
  94. // NOTE: checkstyle format does NOT include time or memory
  95. return <<<XML
  96. <?xml version="1.0" encoding="UTF-8"?>
  97. <checkstyle version="{$about}">
  98. <file name="someFile.php">
  99. <error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here" message="Found violation(s) of type: some_fixer_name_here" />
  100. </file>
  101. </checkstyle>
  102. XML;
  103. }
  104. protected static function createComplexReport(): string
  105. {
  106. $about = Application::getAbout();
  107. return <<<XML
  108. <?xml version="1.0" encoding="UTF-8"?>
  109. <checkstyle version="{$about}">
  110. <file name="someFile.php">
  111. <error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here_1" message="Found violation(s) of type: some_fixer_name_here_1" />
  112. <error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here_2" message="Found violation(s) of type: some_fixer_name_here_2" />
  113. </file>
  114. <file name="anotherFile.php">
  115. <error severity="warning" source="PHP-CS-Fixer.another_fixer_name_here" message="Found violation(s) of type: another_fixer_name_here" />
  116. </file>
  117. </checkstyle>
  118. XML;
  119. }
  120. protected function createReporter(): ReporterInterface
  121. {
  122. return new CheckstyleReporter();
  123. }
  124. protected function getFormat(): string
  125. {
  126. return 'checkstyle';
  127. }
  128. protected function assertFormat(string $expected, string $input): void
  129. {
  130. $formatter = new OutputFormatter();
  131. $input = $formatter->format($input);
  132. self::assertThat($input, new XmlMatchesXsd(self::$xsd));
  133. self::assertXmlStringEqualsXmlString($expected, $input);
  134. }
  135. }