JunitReporterTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 PhpCsFixer\PhpunitConstraintXmlMatchesXsd\Constraint\XmlMatchesXsd;
  13. use PhpCsFixer\Report\JunitReporter;
  14. use Symfony\Component\Console\Formatter\OutputFormatter;
  15. /**
  16. * @author Boris Gorbylev <ekho@ekho.name>
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Report\JunitReporter
  22. */
  23. final class JunitReporterTest extends AbstractReporterTestCase
  24. {
  25. /**
  26. * JUnit XML schema from Jenkins.
  27. *
  28. * @var null|string
  29. *
  30. * @see https://github.com/jenkinsci/xunit-plugin/blob/master/src/main/resources/org/jenkinsci/plugins/xunit/types/model/xsd/junit-10.xsd
  31. */
  32. private static $xsd;
  33. public static function setUpBeforeClass()
  34. {
  35. parent::setUpBeforeClass();
  36. self::$xsd = file_get_contents(__DIR__.'/../../doc/report-schema/junit-10.xsd');
  37. }
  38. public static function tearDownAfterClass()
  39. {
  40. parent::tearDownAfterClass();
  41. self::$xsd = null;
  42. }
  43. protected function getFormat()
  44. {
  45. return 'junit';
  46. }
  47. protected function createNoErrorReport()
  48. {
  49. return <<<'XML'
  50. <?xml version="1.0" encoding="UTF-8"?>
  51. <testsuites>
  52. <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="0" errors="0">
  53. <testcase name="All OK" assertions="1"/>
  54. </testsuite>
  55. </testsuites>
  56. XML;
  57. }
  58. protected function createSimpleReport()
  59. {
  60. return <<<'XML'
  61. <?xml version="1.0" encoding="UTF-8"?>
  62. <testsuites>
  63. <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="1" errors="0">
  64. <testcase name="someFile" file="someFile.php" assertions="1">
  65. <failure type="code_style">Wrong code style</failure>
  66. </testcase>
  67. </testsuite>
  68. </testsuites>
  69. XML;
  70. }
  71. protected function createWithDiffReport()
  72. {
  73. return <<<'XML'
  74. <?xml version="1.0" encoding="UTF-8"?>
  75. <testsuites>
  76. <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="1" errors="0">
  77. <testcase name="someFile" file="someFile.php" assertions="1">
  78. <failure type="code_style"><![CDATA[Wrong code style
  79. Diff:
  80. ---------------
  81. this text is a diff ;)]]></failure>
  82. </testcase>
  83. </testsuite>
  84. </testsuites>
  85. XML;
  86. }
  87. protected function createWithAppliedFixersReport()
  88. {
  89. return <<<'XML'
  90. <?xml version="1.0" encoding="UTF-8"?>
  91. <testsuites>
  92. <testsuite name="PHP CS Fixer" tests="1" assertions="2" failures="2" errors="0">
  93. <testcase name="someFile" file="someFile.php" assertions="2">
  94. <failure type="code_style">applied fixers:
  95. ---------------
  96. * some_fixer_name_here_1
  97. * some_fixer_name_here_2</failure>
  98. </testcase>
  99. </testsuite>
  100. </testsuites>
  101. XML;
  102. }
  103. protected function createWithTimeAndMemoryReport()
  104. {
  105. return <<<'XML'
  106. <?xml version="1.0" encoding="UTF-8"?>
  107. <testsuites>
  108. <testsuite name="PHP CS Fixer" tests="1" assertions="1" failures="1" errors="0" time="1.234">
  109. <testcase name="someFile" file="someFile.php" assertions="1">
  110. <failure type="code_style">Wrong code style</failure>
  111. </testcase>
  112. </testsuite>
  113. </testsuites>
  114. XML;
  115. }
  116. protected function createComplexReport()
  117. {
  118. return <<<'XML'
  119. <?xml version="1.0"?>
  120. <testsuites>
  121. <testsuite assertions="3" errors="0" failures="3" name="PHP CS Fixer" tests="2" time="1.234">
  122. <testcase assertions="2" file="someFile.php" name="someFile">
  123. <failure type="code_style">applied fixers:
  124. ---------------
  125. * some_fixer_name_here_1
  126. * some_fixer_name_here_2
  127. Diff:
  128. ---------------
  129. this text is a diff ;)</failure>
  130. </testcase>
  131. <testcase assertions="1" file="anotherFile.php" name="anotherFile">
  132. <failure type="code_style">applied fixers:
  133. ---------------
  134. * another_fixer_name_here
  135. Diff:
  136. ---------------
  137. another diff here ;)</failure>
  138. </testcase>
  139. </testsuite>
  140. </testsuites>
  141. XML;
  142. }
  143. protected function assertFormat($expected, $input)
  144. {
  145. $formatter = new OutputFormatter();
  146. $input = $formatter->format($input);
  147. static::assertThat($input, new XmlMatchesXsd(self::$xsd));
  148. static::assertXmlStringEqualsXmlString($expected, $input);
  149. }
  150. protected function createReporter()
  151. {
  152. return new JunitReporter();
  153. }
  154. }