JunitReporterTest.php 7.5 KB

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