JsonReporterTest.php 5.9 KB

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