CheckstyleReporterTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. * @var null|string
  31. */
  32. private static $xsd;
  33. public static function setUpBeforeClass(): void
  34. {
  35. parent::setUpBeforeClass();
  36. $content = file_get_contents(__DIR__.'/../../../../doc/schemas/fix/checkstyle.xsd');
  37. if (false === $content) {
  38. throw new \RuntimeException('Cannot read file.');
  39. }
  40. self::$xsd = $content;
  41. }
  42. public static function tearDownAfterClass(): void
  43. {
  44. parent::tearDownAfterClass();
  45. self::$xsd = null;
  46. }
  47. protected static function createNoErrorReport(): string
  48. {
  49. $about = Application::getAbout();
  50. return <<<XML
  51. <?xml version="1.0" encoding="UTF-8"?>
  52. <checkstyle version="{$about}" />
  53. XML;
  54. }
  55. protected static function createSimpleReport(): string
  56. {
  57. $about = Application::getAbout();
  58. return <<<XML
  59. <?xml version="1.0" encoding="UTF-8"?>
  60. <checkstyle version="{$about}">
  61. <file name="someFile.php">
  62. <error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here" message="Found violation(s) of type: some_fixer_name_here" />
  63. </file>
  64. </checkstyle>
  65. XML;
  66. }
  67. protected static function createWithDiffReport(): string
  68. {
  69. $about = Application::getAbout();
  70. // NOTE: checkstyle format does NOT include diffs
  71. return <<<XML
  72. <?xml version="1.0" encoding="UTF-8"?>
  73. <checkstyle version="{$about}">
  74. <file name="someFile.php">
  75. <error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here" message="Found violation(s) of type: some_fixer_name_here" />
  76. </file>
  77. </checkstyle>
  78. XML;
  79. }
  80. protected static function createWithAppliedFixersReport(): string
  81. {
  82. $about = Application::getAbout();
  83. return <<<XML
  84. <?xml version="1.0" encoding="UTF-8"?>
  85. <checkstyle version="{$about}">
  86. <file name="someFile.php">
  87. <error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here_1" message="Found violation(s) of type: some_fixer_name_here_1" />
  88. <error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here_2" message="Found violation(s) of type: some_fixer_name_here_2" />
  89. </file>
  90. </checkstyle>
  91. XML;
  92. }
  93. protected static function createWithTimeAndMemoryReport(): string
  94. {
  95. $about = Application::getAbout();
  96. // NOTE: checkstyle format does NOT include time or memory
  97. return <<<XML
  98. <?xml version="1.0" encoding="UTF-8"?>
  99. <checkstyle version="{$about}">
  100. <file name="someFile.php">
  101. <error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here" message="Found violation(s) of type: some_fixer_name_here" />
  102. </file>
  103. </checkstyle>
  104. XML;
  105. }
  106. protected static function createComplexReport(): string
  107. {
  108. $about = Application::getAbout();
  109. return <<<XML
  110. <?xml version="1.0" encoding="UTF-8"?>
  111. <checkstyle version="{$about}">
  112. <file name="someFile.php">
  113. <error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here_1" message="Found violation(s) of type: some_fixer_name_here_1" />
  114. <error severity="warning" source="PHP-CS-Fixer.some_fixer_name_here_2" message="Found violation(s) of type: some_fixer_name_here_2" />
  115. </file>
  116. <file name="anotherFile.php">
  117. <error severity="warning" source="PHP-CS-Fixer.another_fixer_name_here" message="Found violation(s) of type: another_fixer_name_here" />
  118. </file>
  119. </checkstyle>
  120. XML;
  121. }
  122. protected function createReporter(): ReporterInterface
  123. {
  124. return new CheckstyleReporter();
  125. }
  126. protected function getFormat(): string
  127. {
  128. return 'checkstyle';
  129. }
  130. protected function assertFormat(string $expected, string $input): void
  131. {
  132. $formatter = new OutputFormatter();
  133. $input = $formatter->format($input);
  134. self::assertThat($input, new XmlMatchesXsd(self::$xsd));
  135. self::assertXmlStringEqualsXmlString($expected, $input);
  136. }
  137. }