JunitReporterTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer\Tests\Report;
  12. use GeckoPackages\PHPUnit\Constraints\XML\XMLMatchesXSDConstraint;
  13. use PhpCsFixer\Report\JunitReporter;
  14. use PhpCsFixer\Report\ReportSummary;
  15. use PHPUnit\Framework\TestCase;
  16. /**
  17. * @author Boris Gorbylev <ekho@ekho.name>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Report\JunitReporter
  22. */
  23. final class JunitReporterTest extends TestCase
  24. {
  25. /**
  26. * @var JunitReporter
  27. */
  28. private $reporter;
  29. /**
  30. * JUnit XML schema from Jenkins.
  31. *
  32. * @var string
  33. *
  34. * @see https://github.com/jenkinsci/xunit-plugin/blob/master/src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd
  35. */
  36. private $xsd;
  37. protected function setUp()
  38. {
  39. $this->reporter = new JunitReporter();
  40. $this->xsd = file_get_contents(__DIR__.'/../../doc/junit-10.xsd');
  41. }
  42. /**
  43. * @covers \PhpCsFixer\Report\JunitReporter::getFormat
  44. */
  45. public function testGetFormat()
  46. {
  47. $this->assertSame('junit', $this->reporter->getFormat());
  48. }
  49. public function testGenerateNoErrors()
  50. {
  51. $expectedReport = <<<'XML'
  52. <?xml version="1.0" encoding="UTF-8"?>
  53. <testsuites>
  54. <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="0" errors="0">
  55. <testcase name="All OK" assertions="1"/>
  56. </testsuite>
  57. </testsuites>
  58. XML;
  59. $actualReport = $this->reporter->generate(
  60. new ReportSummary(
  61. [],
  62. 0,
  63. 0,
  64. false,
  65. false,
  66. false
  67. )
  68. );
  69. $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
  70. $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
  71. }
  72. public function testGenerateSimple()
  73. {
  74. $expectedReport = <<<'XML'
  75. <?xml version="1.0" encoding="UTF-8"?>
  76. <testsuites>
  77. <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="1" errors="0">
  78. <testcase name="someFile" file="someFile.php" assertions="1">
  79. <failure type="code_style">Wrong code style</failure>
  80. </testcase>
  81. </testsuite>
  82. </testsuites>
  83. XML;
  84. $actualReport = $this->reporter->generate(
  85. new ReportSummary(
  86. [
  87. 'someFile.php' => [
  88. 'appliedFixers' => ['some_fixer_name_here'],
  89. ],
  90. ],
  91. 0,
  92. 0,
  93. false,
  94. false,
  95. false
  96. )
  97. );
  98. $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
  99. $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
  100. }
  101. public function testGenerateWithDiff()
  102. {
  103. $expectedReport = <<<'XML'
  104. <?xml version="1.0" encoding="UTF-8"?>
  105. <testsuites>
  106. <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="1" errors="0">
  107. <testcase name="someFile" file="someFile.php" assertions="1">
  108. <failure type="code_style"><![CDATA[Wrong code style
  109. Diff:
  110. ---------------
  111. this text is a diff ;)]]></failure>
  112. </testcase>
  113. </testsuite>
  114. </testsuites>
  115. XML;
  116. $actualReport = $this->reporter->generate(
  117. new ReportSummary(
  118. [
  119. 'someFile.php' => [
  120. 'appliedFixers' => ['some_fixer_name_here'],
  121. 'diff' => 'this text is a diff ;)',
  122. ],
  123. ],
  124. 0,
  125. 0,
  126. false,
  127. false,
  128. false
  129. )
  130. );
  131. $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
  132. $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
  133. }
  134. public function testGenerateWithAppliedFixers()
  135. {
  136. $expectedReport = <<<'XML'
  137. <?xml version="1.0" encoding="UTF-8"?>
  138. <testsuites>
  139. <testsuite name="PHP CS Fixer" tests="1" assertions="2" failures="2" errors="0">
  140. <testcase name="someFile" file="someFile.php" assertions="2">
  141. <failure type="code_style">applied fixers:
  142. ---------------
  143. * some_fixer_name_here_1
  144. * some_fixer_name_here_2</failure>
  145. </testcase>
  146. </testsuite>
  147. </testsuites>
  148. XML;
  149. $actualReport = $this->reporter->generate(
  150. new ReportSummary(
  151. [
  152. 'someFile.php' => [
  153. 'appliedFixers' => ['some_fixer_name_here_1', 'some_fixer_name_here_2'],
  154. ],
  155. ],
  156. 0,
  157. 0,
  158. true,
  159. false,
  160. false
  161. )
  162. );
  163. $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
  164. $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
  165. }
  166. public function testGenerateWithTimeAndMemory()
  167. {
  168. $expectedReport = <<<'XML'
  169. <?xml version="1.0" encoding="UTF-8"?>
  170. <testsuites>
  171. <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="1" errors="0" time="1.234">
  172. <testcase name="someFile" file="someFile.php" assertions="1">
  173. <failure type="code_style">Wrong code style</failure>
  174. </testcase>
  175. </testsuite>
  176. </testsuites>
  177. XML;
  178. $actualReport = $this->reporter->generate(
  179. new ReportSummary(
  180. [
  181. 'someFile.php' => [
  182. 'appliedFixers' => ['some_fixer_name_here'],
  183. ],
  184. ],
  185. 1234,
  186. 0,
  187. false,
  188. false,
  189. false
  190. )
  191. );
  192. $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
  193. $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
  194. }
  195. public function testGenerateComplex()
  196. {
  197. $expectedReport = <<<'XML'
  198. <?xml version="1.0"?>
  199. <testsuites>
  200. <testsuite assertions="3" errors="0" failures="3" name="PHP CS Fixer" tests="2" time="1.234">
  201. <testcase assertions="2" file="someFile.php" name="someFile">
  202. <failure type="code_style">applied fixers:
  203. ---------------
  204. * some_fixer_name_here_1
  205. * some_fixer_name_here_2
  206. Diff:
  207. ---------------
  208. this text is a diff ;)</failure>
  209. </testcase>
  210. <testcase assertions="1" file="anotherFile.php" name="anotherFile">
  211. <failure type="code_style">applied fixers:
  212. ---------------
  213. * another_fixer_name_here
  214. Diff:
  215. ---------------
  216. another diff here ;)</failure>
  217. </testcase>
  218. </testsuite>
  219. </testsuites>
  220. XML;
  221. $actualReport = $this->reporter->generate(
  222. new ReportSummary(
  223. [
  224. 'someFile.php' => [
  225. 'appliedFixers' => ['some_fixer_name_here_1', 'some_fixer_name_here_2'],
  226. 'diff' => 'this text is a diff ;)',
  227. ],
  228. 'anotherFile.php' => [
  229. 'appliedFixers' => ['another_fixer_name_here'],
  230. 'diff' => 'another diff here ;)',
  231. ],
  232. ],
  233. 1234,
  234. 0,
  235. true,
  236. true,
  237. false
  238. )
  239. );
  240. $this->assertThat($actualReport, new XMLMatchesXSDConstraint($this->xsd));
  241. $this->assertXmlStringEqualsXmlString($expectedReport, $actualReport);
  242. }
  243. }