JsonReporterTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 PhpCsFixer\Report\JsonReporter;
  13. /**
  14. * @author Boris Gorbylev <ekho@ekho.name>
  15. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Report\JsonReporter
  20. */
  21. final class JsonReporterTest extends AbstractReporterTestCase
  22. {
  23. public function createSimpleReport()
  24. {
  25. return <<<'JSON'
  26. {
  27. "files": [
  28. {
  29. "name": "someFile.php"
  30. }
  31. ],
  32. "time": {
  33. "total": 0
  34. },
  35. "memory": 0
  36. }
  37. JSON;
  38. }
  39. public function createWithDiffReport()
  40. {
  41. return <<<'JSON'
  42. {
  43. "files": [
  44. {
  45. "name": "someFile.php",
  46. "diff": "this text is a diff ;)"
  47. }
  48. ],
  49. "time": {
  50. "total": 0
  51. },
  52. "memory": 0
  53. }
  54. JSON;
  55. }
  56. public function createWithAppliedFixersReport()
  57. {
  58. return <<<'JSON'
  59. {
  60. "files": [
  61. {
  62. "name": "someFile.php",
  63. "appliedFixers":["some_fixer_name_here_1", "some_fixer_name_here_2"]
  64. }
  65. ],
  66. "time": {
  67. "total": 0
  68. },
  69. "memory": 0
  70. }
  71. JSON;
  72. }
  73. public function createWithTimeAndMemoryReport()
  74. {
  75. return <<<'JSON'
  76. {
  77. "files": [
  78. {
  79. "name": "someFile.php"
  80. }
  81. ],
  82. "memory": 2.5,
  83. "time": {
  84. "total": 1.234
  85. }
  86. }
  87. JSON;
  88. }
  89. public function createComplexReport()
  90. {
  91. return <<<'JSON'
  92. {
  93. "files": [
  94. {
  95. "name": "someFile.php",
  96. "appliedFixers":["some_fixer_name_here_1", "some_fixer_name_here_2"],
  97. "diff": "this text is a diff ;)"
  98. },
  99. {
  100. "name": "anotherFile.php",
  101. "appliedFixers":["another_fixer_name_here"],
  102. "diff": "another diff here ;)"
  103. }
  104. ],
  105. "memory": 2.5,
  106. "time": {
  107. "total": 1.234
  108. }
  109. }
  110. JSON;
  111. }
  112. protected function createReporter()
  113. {
  114. return new JsonReporter();
  115. }
  116. protected function getFormat()
  117. {
  118. return 'json';
  119. }
  120. protected function createNoErrorReport()
  121. {
  122. return <<<'JSON'
  123. {
  124. "files": [
  125. ],
  126. "time": {
  127. "total": 0
  128. },
  129. "memory": 0
  130. }
  131. JSON;
  132. }
  133. protected function assertFormat($expected, $input)
  134. {
  135. $this->assertJsonSchema($input);
  136. $this->assertJsonStringEqualsJsonString($expected, $input);
  137. }
  138. /**
  139. * @param string $json
  140. */
  141. private function assertJsonSchema($json)
  142. {
  143. $jsonPath = __DIR__.'/../../doc/schema.json';
  144. $data = json_decode($json);
  145. $validator = new \JsonSchema\Validator();
  146. $validator->validate(
  147. $data,
  148. (object) array('$ref' => 'file://'.realpath($jsonPath))
  149. );
  150. $this->assertTrue(
  151. $validator->isValid(),
  152. implode(
  153. "\n",
  154. array_map(
  155. function (array $item) { return sprintf('Property `%s`: %s.', $item['property'], $item['message']); },
  156. $validator->getErrors()
  157. )
  158. )
  159. );
  160. }
  161. }