JunitReporterTest.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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\JunitReporter;
  15. use PhpCsFixer\Console\Report\FixReport\ReporterInterface;
  16. use PhpCsFixer\PhpunitConstraintXmlMatchesXsd\Constraint\XmlMatchesXsd;
  17. use Symfony\Component\Console\Formatter\OutputFormatter;
  18. /**
  19. * @author Boris Gorbylev <ekho@ekho.name>
  20. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  21. *
  22. * @internal
  23. *
  24. * @covers \PhpCsFixer\Console\Report\FixReport\JunitReporter
  25. */
  26. final class JunitReporterTest extends AbstractReporterTestCase
  27. {
  28. /**
  29. * JUnit XML schema from Jenkins.
  30. *
  31. * @see https://github.com/jenkinsci/xunit-plugin/blob/master/src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd
  32. */
  33. private static ?string $xsd = null;
  34. public static function setUpBeforeClass(): void
  35. {
  36. parent::setUpBeforeClass();
  37. $content = file_get_contents(__DIR__.'/../../../../doc/schemas/fix/junit-10.xsd');
  38. if (false === $content) {
  39. throw new \RuntimeException('Cannot read file.');
  40. }
  41. self::$xsd = $content;
  42. }
  43. public static function tearDownAfterClass(): void
  44. {
  45. parent::tearDownAfterClass();
  46. self::$xsd = null;
  47. }
  48. protected function getFormat(): string
  49. {
  50. return 'junit';
  51. }
  52. protected static function createNoErrorReport(): string
  53. {
  54. $about = Application::getAbout();
  55. return <<<XML
  56. <?xml version="1.0" encoding="UTF-8"?>
  57. <testsuites>
  58. <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="0" errors="0">
  59. <properties>
  60. <property name="about" value="{$about}"/>
  61. </properties>
  62. <testcase name="All OK" assertions="1"/>
  63. </testsuite>
  64. </testsuites>
  65. XML;
  66. }
  67. protected static function createSimpleReport(): string
  68. {
  69. $about = Application::getAbout();
  70. return <<<XML
  71. <?xml version="1.0" encoding="UTF-8"?>
  72. <testsuites>
  73. <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="1" errors="0">
  74. <properties>
  75. <property name="about" value="{$about}"/>
  76. </properties>
  77. <testcase name="someFile" file="someFile.php" assertions="1">
  78. <failure type="code_style">Wrong code style
  79. Diff:
  80. ---------------
  81. --- Original
  82. +++ New
  83. @@ -2,7 +2,7 @@
  84. class Foo
  85. {
  86. - public function bar(\$foo = 1, \$bar)
  87. + public function bar(\$foo, \$bar)
  88. {
  89. }
  90. }</failure>
  91. </testcase>
  92. </testsuite>
  93. </testsuites>
  94. XML;
  95. }
  96. protected static function createWithDiffReport(): string
  97. {
  98. $about = Application::getAbout();
  99. return <<<XML
  100. <?xml version="1.0" encoding="UTF-8"?>
  101. <testsuites>
  102. <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="1" errors="0">
  103. <properties>
  104. <property name="about" value="{$about}"/>
  105. </properties>
  106. <testcase name="someFile" file="someFile.php" assertions="1">
  107. <failure type="code_style"><![CDATA[Wrong code style
  108. Diff:
  109. ---------------
  110. --- Original
  111. +++ New
  112. @@ -2,7 +2,7 @@
  113. class Foo
  114. {
  115. - public function bar(\$foo = 1, \$bar)
  116. + public function bar(\$foo, \$bar)
  117. {
  118. }
  119. }]]></failure>
  120. </testcase>
  121. </testsuite>
  122. </testsuites>
  123. XML;
  124. }
  125. protected static function createWithAppliedFixersReport(): string
  126. {
  127. $about = Application::getAbout();
  128. return <<<XML
  129. <?xml version="1.0" encoding="UTF-8"?>
  130. <testsuites>
  131. <testsuite name="PHP CS Fixer" tests="1" assertions="2" failures="2" errors="0">
  132. <properties>
  133. <property name="about" value="{$about}"/>
  134. </properties>
  135. <testcase name="someFile" file="someFile.php" assertions="2">
  136. <failure type="code_style">applied fixers:
  137. ---------------
  138. * some_fixer_name_here_1
  139. * some_fixer_name_here_2</failure>
  140. </testcase>
  141. </testsuite>
  142. </testsuites>
  143. XML;
  144. }
  145. protected static function createWithTimeAndMemoryReport(): string
  146. {
  147. $about = Application::getAbout();
  148. return <<<XML
  149. <?xml version="1.0" encoding="UTF-8"?>
  150. <testsuites>
  151. <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="1" errors="0" time="1.234">
  152. <properties>
  153. <property name="about" value="{$about}"/>
  154. </properties>
  155. <testcase name="someFile" file="someFile.php" assertions="1">
  156. <failure type="code_style">Wrong code style
  157. Diff:
  158. ---------------
  159. --- Original
  160. +++ New
  161. @@ -2,7 +2,7 @@
  162. class Foo
  163. {
  164. - public function bar(\$foo = 1, \$bar)
  165. + public function bar(\$foo, \$bar)
  166. {
  167. }
  168. }</failure>
  169. </testcase>
  170. </testsuite>
  171. </testsuites>
  172. XML;
  173. }
  174. protected static function createComplexReport(): string
  175. {
  176. $about = Application::getAbout();
  177. return <<<XML
  178. <?xml version="1.0"?>
  179. <testsuites>
  180. <testsuite assertions="3" errors="0" failures="3" name="PHP CS Fixer" tests="2" time="1.234">
  181. <properties>
  182. <property name="about" value="{$about}"/>
  183. </properties>
  184. <testcase assertions="2" file="someFile.php" name="someFile">
  185. <failure type="code_style">applied fixers:
  186. ---------------
  187. * some_fixer_name_here_1
  188. * some_fixer_name_here_2
  189. Diff:
  190. ---------------
  191. this text is a diff ;)</failure>
  192. </testcase>
  193. <testcase assertions="1" file="anotherFile.php" name="anotherFile">
  194. <failure type="code_style">applied fixers:
  195. ---------------
  196. * another_fixer_name_here
  197. Diff:
  198. ---------------
  199. another diff here ;)</failure>
  200. </testcase>
  201. </testsuite>
  202. </testsuites>
  203. XML;
  204. }
  205. protected function assertFormat(string $expected, string $input): void
  206. {
  207. $formatter = new OutputFormatter();
  208. $input = $formatter->format($input);
  209. self::assertThat($input, new XmlMatchesXsd(self::$xsd));
  210. self::assertXmlStringEqualsXmlString($expected, $input);
  211. }
  212. protected function createReporter(): ReporterInterface
  213. {
  214. return new JunitReporter();
  215. }
  216. }