XmlReporterTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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\ReporterInterface;
  15. use PhpCsFixer\Console\Report\FixReport\XmlReporter;
  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\XmlReporter
  25. */
  26. final class XmlReporterTest extends AbstractReporterTestCase
  27. {
  28. /**
  29. * @var null|string
  30. */
  31. private static $xsd;
  32. public static function setUpBeforeClass(): void
  33. {
  34. parent::setUpBeforeClass();
  35. self::$xsd = file_get_contents(__DIR__.'/../../../../doc/schemas/fix/xml.xsd');
  36. }
  37. public static function tearDownAfterClass(): void
  38. {
  39. parent::tearDownAfterClass();
  40. self::$xsd = null;
  41. }
  42. protected static function createNoErrorReport(): string
  43. {
  44. $about = Application::getAbout();
  45. return <<<XML
  46. <?xml version="1.0" encoding="UTF-8"?>
  47. <report>
  48. <about value="{$about}"/>
  49. <files />
  50. </report>
  51. XML;
  52. }
  53. protected static function createSimpleReport(): string
  54. {
  55. $about = Application::getAbout();
  56. return <<<XML
  57. <?xml version="1.0" encoding="UTF-8"?>
  58. <report>
  59. <about value="{$about}"/>
  60. <files>
  61. <file id="1" name="someFile.php">
  62. <diff>--- Original
  63. +++ New
  64. @@ -2,7 +2,7 @@
  65. class Foo
  66. {
  67. - public function bar(\$foo = 1, \$bar)
  68. + public function bar(\$foo, \$bar)
  69. {
  70. }
  71. }</diff>
  72. </file>
  73. </files>
  74. </report>
  75. XML;
  76. }
  77. protected static function createWithDiffReport(): string
  78. {
  79. $about = Application::getAbout();
  80. return <<<XML
  81. <?xml version="1.0" encoding="UTF-8"?>
  82. <report>
  83. <about value="{$about}"/>
  84. <files>
  85. <file id="1" name="someFile.php">
  86. <diff>--- Original
  87. +++ New
  88. @@ -2,7 +2,7 @@
  89. class Foo
  90. {
  91. - public function bar(\$foo = 1, \$bar)
  92. + public function bar(\$foo, \$bar)
  93. {
  94. }
  95. }</diff>
  96. </file>
  97. </files>
  98. </report>
  99. XML;
  100. }
  101. protected static function createWithAppliedFixersReport(): string
  102. {
  103. $about = Application::getAbout();
  104. return <<<XML
  105. <?xml version="1.0" encoding="UTF-8"?>
  106. <report>
  107. <about value="{$about}"/>
  108. <files>
  109. <file id="1" name="someFile.php">
  110. <applied_fixers>
  111. <applied_fixer name="some_fixer_name_here_1"/>
  112. <applied_fixer name="some_fixer_name_here_2"/>
  113. </applied_fixers>
  114. </file>
  115. </files>
  116. </report>
  117. XML;
  118. }
  119. protected static function createWithTimeAndMemoryReport(): string
  120. {
  121. $about = Application::getAbout();
  122. return <<<XML
  123. <?xml version="1.0" encoding="UTF-8"?>
  124. <report>
  125. <about value="{$about}"/>
  126. <files>
  127. <file id="1" name="someFile.php">
  128. <diff>--- Original
  129. +++ New
  130. @@ -2,7 +2,7 @@
  131. class Foo
  132. {
  133. - public function bar(\$foo = 1, \$bar)
  134. + public function bar(\$foo, \$bar)
  135. {
  136. }
  137. }</diff>
  138. </file>
  139. </files>
  140. <time unit="s">
  141. <total value="1.234"/>
  142. </time>
  143. <memory value="2.5" unit="MB"/>
  144. </report>
  145. XML;
  146. }
  147. protected static function createComplexReport(): string
  148. {
  149. $about = Application::getAbout();
  150. return <<<XML
  151. <?xml version="1.0" encoding="UTF-8"?>
  152. <report>
  153. <about value="{$about}"/>
  154. <files>
  155. <file id="1" name="someFile.php">
  156. <applied_fixers>
  157. <applied_fixer name="some_fixer_name_here_1"/>
  158. <applied_fixer name="some_fixer_name_here_2"/>
  159. </applied_fixers>
  160. <diff>this text is a diff ;)</diff>
  161. </file>
  162. <file id="2" name="anotherFile.php">
  163. <applied_fixers>
  164. <applied_fixer name="another_fixer_name_here"/>
  165. </applied_fixers>
  166. <diff>another diff here ;)</diff>
  167. </file>
  168. </files>
  169. <time unit="s">
  170. <total value="1.234"/>
  171. </time>
  172. <memory value="2.5" unit="MB"/>
  173. </report>
  174. XML;
  175. }
  176. protected function createReporter(): ReporterInterface
  177. {
  178. return new XmlReporter();
  179. }
  180. protected function getFormat(): string
  181. {
  182. return 'xml';
  183. }
  184. protected function assertFormat(string $expected, string $input): void
  185. {
  186. $formatter = new OutputFormatter();
  187. $input = $formatter->format($input);
  188. self::assertThat($input, new XmlMatchesXsd(self::$xsd));
  189. self::assertXmlStringEqualsXmlString($expected, $input);
  190. }
  191. }