JunitReporterTest.php 4.3 KB

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