XmlReporterTest.php 6.0 KB

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