JsonReporterTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. use PhpCsFixer\Report\ReportSummary;
  14. /**
  15. * @author Boris Gorbylev <ekho@ekho.name>
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. *
  18. * @internal
  19. */
  20. final class JsonReporterTest extends \PHPUnit_Framework_TestCase
  21. {
  22. /** @var JsonReporter */
  23. private $reporter;
  24. protected function setUp()
  25. {
  26. $this->reporter = new JsonReporter();
  27. }
  28. /**
  29. * @covers \PhpCsFixer\Report\JsonReporter::getFormat
  30. */
  31. public function testGetFormat()
  32. {
  33. $this->assertSame('json', $this->reporter->getFormat());
  34. }
  35. public function testGenerateNoErrors()
  36. {
  37. $expectedReport = <<<'JSON'
  38. {
  39. "files": [
  40. ],
  41. "time": {
  42. "total": 0
  43. },
  44. "memory": 0
  45. }
  46. JSON;
  47. $actualReport = $this->reporter->generate(
  48. new ReportSummary(
  49. array(),
  50. 0,
  51. 0,
  52. false,
  53. false,
  54. false
  55. )
  56. );
  57. $this->assertJsonSchema($actualReport);
  58. $this->assertJsonStringEqualsJsonString($expectedReport, $actualReport);
  59. }
  60. public function testGenerateSimple()
  61. {
  62. $expectedReport = <<<'JSON'
  63. {
  64. "files": [
  65. {
  66. "name": "someFile.php"
  67. }
  68. ],
  69. "time": {
  70. "total": 5
  71. },
  72. "memory": 2
  73. }
  74. JSON;
  75. $actualReport = $this->reporter->generate(
  76. new ReportSummary(
  77. array(
  78. 'someFile.php' => array(
  79. 'appliedFixers' => array('some_fixer_name_here'),
  80. ),
  81. ),
  82. 5 * 1000,
  83. 2 * 1024 * 1024,
  84. false,
  85. false,
  86. false
  87. )
  88. );
  89. $this->assertJsonSchema($actualReport);
  90. $this->assertJsonStringEqualsJsonString($expectedReport, $actualReport);
  91. }
  92. public function testGenerateWithDiff()
  93. {
  94. $expectedReport = <<<'JSON'
  95. {
  96. "files": [
  97. {
  98. "name": "someFile.php",
  99. "diff": "this text is a diff ;)"
  100. }
  101. ],
  102. "time": {
  103. "total": 5
  104. },
  105. "memory": 2
  106. }
  107. JSON;
  108. $actualReport = $this->reporter->generate(
  109. new ReportSummary(
  110. array(
  111. 'someFile.php' => array(
  112. 'appliedFixers' => array('some_fixer_name_here'),
  113. 'diff' => 'this text is a diff ;)',
  114. ),
  115. ),
  116. 5 * 1000,
  117. 2 * 1024 * 1024,
  118. false,
  119. false,
  120. false
  121. )
  122. );
  123. $this->assertJsonSchema($actualReport);
  124. $this->assertJsonStringEqualsJsonString($expectedReport, $actualReport);
  125. }
  126. public function testGenerateWithAppliedFixers()
  127. {
  128. $expectedReport = <<<'JSON'
  129. {
  130. "files": [
  131. {
  132. "name": "someFile.php",
  133. "appliedFixers":["some_fixer_name_here"]
  134. }
  135. ],
  136. "time": {
  137. "total": 5
  138. },
  139. "memory": 2
  140. }
  141. JSON;
  142. $actualReport = $this->reporter->generate(
  143. new ReportSummary(
  144. array(
  145. 'someFile.php' => array(
  146. 'appliedFixers' => array('some_fixer_name_here'),
  147. ),
  148. ),
  149. 5 * 1000,
  150. 2 * 1024 * 1024,
  151. true,
  152. false,
  153. false
  154. )
  155. );
  156. $this->assertJsonSchema($actualReport);
  157. $this->assertJsonStringEqualsJsonString($expectedReport, $actualReport);
  158. }
  159. public function testGenerateWithTimeAndMemory()
  160. {
  161. $expectedReport = <<<'JSON'
  162. {
  163. "files": [
  164. {
  165. "name": "someFile.php"
  166. }
  167. ],
  168. "memory": 2.5,
  169. "time": {
  170. "total": 1.234
  171. }
  172. }
  173. JSON;
  174. $actualReport = $this->reporter->generate(
  175. new ReportSummary(
  176. array(
  177. 'someFile.php' => array(
  178. 'appliedFixers' => array('some_fixer_name_here'),
  179. ),
  180. ),
  181. 1234,
  182. 2.5 * 1024 * 1024,
  183. false,
  184. false,
  185. false
  186. )
  187. );
  188. $this->assertJsonSchema($actualReport);
  189. $this->assertJsonStringEqualsJsonString($expectedReport, $actualReport);
  190. }
  191. public function testGenerateComplex()
  192. {
  193. $expectedReport = <<<'JSON'
  194. {
  195. "files": [
  196. {
  197. "name": "someFile.php",
  198. "appliedFixers":["some_fixer_name_here"],
  199. "diff": "this text is a diff ;)"
  200. },
  201. {
  202. "name": "anotherFile.php",
  203. "appliedFixers":["another_fixer_name_here"],
  204. "diff": "another diff here ;)"
  205. }
  206. ],
  207. "memory": 2.5,
  208. "time": {
  209. "total": 1.234
  210. }
  211. }
  212. JSON;
  213. $actualReport = $this->reporter->generate(
  214. new ReportSummary(
  215. array(
  216. 'someFile.php' => array(
  217. 'appliedFixers' => array('some_fixer_name_here'),
  218. 'diff' => 'this text is a diff ;)',
  219. ),
  220. 'anotherFile.php' => array(
  221. 'appliedFixers' => array('another_fixer_name_here'),
  222. 'diff' => 'another diff here ;)',
  223. ),
  224. ),
  225. 1234,
  226. 2.5 * 1024 * 1024,
  227. true,
  228. true,
  229. true
  230. )
  231. );
  232. $this->assertJsonSchema($actualReport);
  233. $this->assertJsonStringEqualsJsonString($expectedReport, $actualReport);
  234. }
  235. /**
  236. * @param string $json
  237. */
  238. private function assertJsonSchema($json)
  239. {
  240. $jsonPath = __DIR__.'/../../doc/schema.json';
  241. $data = json_decode($json);
  242. $validator = new \JsonSchema\Validator();
  243. $validator->validate(
  244. $data,
  245. (object) array('$ref' => 'file://'.realpath($jsonPath))
  246. );
  247. $this->assertTrue(
  248. $validator->isValid(),
  249. implode(
  250. "\n",
  251. array_map(
  252. function (array $item) { return sprintf('Property `%s`: %s.', $item['property'], $item['message']); },
  253. $validator->getErrors()
  254. )
  255. )
  256. );
  257. }
  258. }