JsonReporterTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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\JsonReporter;
  15. use PhpCsFixer\Console\Report\FixReport\ReporterInterface;
  16. use PhpCsFixer\Tests\Test\Assert\AssertJsonSchemaTrait;
  17. /**
  18. * @author Boris Gorbylev <ekho@ekho.name>
  19. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  20. *
  21. * @internal
  22. *
  23. * @covers \PhpCsFixer\Console\Report\FixReport\JsonReporter
  24. */
  25. final class JsonReporterTest extends AbstractReporterTestCase
  26. {
  27. use AssertJsonSchemaTrait;
  28. protected static function createSimpleReport(): string
  29. {
  30. $about = Application::getAbout();
  31. $diff = <<<'DIFF'
  32. --- Original\n+++ New\n@@ -2,7 +2,7 @@\n\n class Foo\n {\n- public function bar($foo = 1, $bar)\n+ public function bar($foo, $bar)\n {\n }\n }
  33. DIFF;
  34. return <<<JSON
  35. {
  36. "about": "{$about}",
  37. "files": [
  38. {
  39. "diff": "{$diff}",
  40. "name": "someFile.php"
  41. }
  42. ],
  43. "time": {
  44. "total": 0
  45. },
  46. "memory": 0
  47. }
  48. JSON;
  49. }
  50. protected static function createWithDiffReport(): string
  51. {
  52. $about = Application::getAbout();
  53. $diff = <<<'DIFF'
  54. --- Original\n+++ New\n@@ -2,7 +2,7 @@\n\n class Foo\n {\n- public function bar($foo = 1, $bar)\n+ public function bar($foo, $bar)\n {\n }\n }
  55. DIFF;
  56. return <<<JSON
  57. {
  58. "about": "{$about}",
  59. "files": [
  60. {
  61. "name": "someFile.php",
  62. "diff": "{$diff}"
  63. }
  64. ],
  65. "time": {
  66. "total": 0
  67. },
  68. "memory": 0
  69. }
  70. JSON;
  71. }
  72. protected static function createWithAppliedFixersReport(): string
  73. {
  74. $about = Application::getAbout();
  75. return <<<JSON
  76. {
  77. "about": "{$about}",
  78. "files": [
  79. {
  80. "name": "someFile.php",
  81. "appliedFixers":["some_fixer_name_here_1", "some_fixer_name_here_2"]
  82. }
  83. ],
  84. "time": {
  85. "total": 0
  86. },
  87. "memory": 0
  88. }
  89. JSON;
  90. }
  91. protected static function createWithTimeAndMemoryReport(): string
  92. {
  93. $about = Application::getAbout();
  94. $diff = <<<'DIFF'
  95. --- Original\n+++ New\n@@ -2,7 +2,7 @@\n\n class Foo\n {\n- public function bar($foo = 1, $bar)\n+ public function bar($foo, $bar)\n {\n }\n }
  96. DIFF;
  97. return <<<JSON
  98. {
  99. "about": "{$about}",
  100. "files": [
  101. {
  102. "diff": "{$diff}",
  103. "name": "someFile.php"
  104. }
  105. ],
  106. "memory": 2.5,
  107. "time": {
  108. "total": 1.234
  109. }
  110. }
  111. JSON;
  112. }
  113. protected static function createComplexReport(): string
  114. {
  115. $about = Application::getAbout();
  116. return <<<JSON
  117. {
  118. "about": "{$about}",
  119. "files": [
  120. {
  121. "name": "someFile.php",
  122. "appliedFixers":["some_fixer_name_here_1", "some_fixer_name_here_2"],
  123. "diff": "this text is a diff ;)"
  124. },
  125. {
  126. "name": "anotherFile.php",
  127. "appliedFixers":["another_fixer_name_here"],
  128. "diff": "another diff here ;)"
  129. }
  130. ],
  131. "memory": 2.5,
  132. "time": {
  133. "total": 1.234
  134. }
  135. }
  136. JSON;
  137. }
  138. protected function createReporter(): ReporterInterface
  139. {
  140. return new JsonReporter();
  141. }
  142. protected function getFormat(): string
  143. {
  144. return 'json';
  145. }
  146. protected static function createNoErrorReport(): string
  147. {
  148. $about = Application::getAbout();
  149. return <<<JSON
  150. {
  151. "about": "{$about}",
  152. "files": [
  153. ],
  154. "time": {
  155. "total": 0
  156. },
  157. "memory": 0
  158. }
  159. JSON;
  160. }
  161. protected function assertFormat(string $expected, string $input): void
  162. {
  163. self::assertJsonSchema(__DIR__.'/../../../../doc/schemas/fix/schema.json', $input);
  164. self::assertJsonStringEqualsJsonString($expected, $input);
  165. }
  166. }