XmlReporterTest.php 6.0 KB

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