SingleLineCommentStyleFixerTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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\Fixer\Comment;
  12. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Filippo Tessarotto <zoeslam@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Comment\SingleLineCommentStyleFixer
  20. */
  21. final class SingleLineCommentStyleFixerTest extends AbstractFixerTestCase
  22. {
  23. public function testInvalidConfig()
  24. {
  25. $this->expectException(InvalidFixerConfigurationException::class);
  26. $this->fixer->configure(['abc']);
  27. }
  28. /**
  29. * @param string $expected
  30. * @param null|string $input
  31. *
  32. * @dataProvider provideAsteriskCases
  33. */
  34. public function testAsterisk($expected, $input = null)
  35. {
  36. $this->fixer->configure(['comment_types' => ['asterisk']]);
  37. $this->doTest($expected, $input);
  38. }
  39. public function provideAsteriskCases()
  40. {
  41. return [
  42. [
  43. '<?php
  44. // lonely line
  45. ',
  46. '<?php
  47. /* lonely line */
  48. ',
  49. ],
  50. [
  51. '<?php
  52. // indented line
  53. ',
  54. '<?php
  55. /* indented line */
  56. ',
  57. ],
  58. [
  59. '<?php
  60. // weird-spaced line
  61. ',
  62. '<?php
  63. /* weird-spaced line*/
  64. ',
  65. ],
  66. [
  67. '<?php // start-end',
  68. '<?php /* start-end */',
  69. ],
  70. [
  71. "<?php\n \t \n \t // weird indent\n",
  72. "<?php\n \t \n \t /* weird indent */\n",
  73. ],
  74. [
  75. "<?php\n// with spaces after\n \t ",
  76. "<?php\n/* with spaces after */ \t \n \t ",
  77. ],
  78. [
  79. '<?php
  80. $a = 1; // after code
  81. ',
  82. '<?php
  83. $a = 1; /* after code */
  84. ',
  85. ],
  86. [
  87. '<?php
  88. /* first */ // second
  89. ',
  90. '<?php
  91. /* first */ /* second */
  92. ',
  93. ],
  94. [
  95. '<?php
  96. /* first */// second',
  97. '<?php
  98. /* first *//*
  99. second
  100. */',
  101. ],
  102. [
  103. '<?php
  104. // one line',
  105. '<?php
  106. /*one line
  107. */',
  108. ],
  109. [
  110. '<?php
  111. // one line',
  112. '<?php
  113. /*
  114. one line*/',
  115. ],
  116. [
  117. '<?php
  118. // one line',
  119. "<?php
  120. /* \t "."
  121. \t * one line ".'
  122. *
  123. */',
  124. ],
  125. [
  126. '<?php
  127. //',
  128. '<?php
  129. /***
  130. *
  131. */',
  132. ],
  133. [
  134. '<?php
  135. // s',
  136. '<?php
  137. /***
  138. s *
  139. */',
  140. ],
  141. // Untouched cases
  142. [
  143. '<?php
  144. $a = 1; /* in code */ $b = 2;
  145. ',
  146. ],
  147. [
  148. '<?php
  149. /*
  150. * in code 2
  151. */ $a = 1;
  152. ',
  153. ],
  154. [
  155. '<?php
  156. /***
  157. *
  158. */ $a = 1;',
  159. ],
  160. [
  161. '<?php
  162. /***
  163. s *
  164. */ $a = 1;',
  165. ],
  166. [
  167. '<?php
  168. /*
  169. * first line
  170. * second line
  171. */',
  172. ],
  173. [
  174. '<?php
  175. /*
  176. * first line
  177. *
  178. * second line
  179. */',
  180. ],
  181. [
  182. '<?php
  183. /*first line
  184. second line*/',
  185. ],
  186. [
  187. '<?php /** inline doc comment */',
  188. ],
  189. [
  190. '<?php
  191. /**
  192. * Doc comment
  193. */',
  194. ],
  195. [
  196. '<?php # test',
  197. ],
  198. ];
  199. }
  200. /**
  201. * @param string $expected
  202. * @param null|string $input
  203. *
  204. * @dataProvider provideHashCases
  205. */
  206. public function testHashCases($expected, $input = null)
  207. {
  208. $this->fixer->configure(['comment_types' => ['hash']]);
  209. $this->doTest($expected, $input);
  210. }
  211. public function provideHashCases()
  212. {
  213. return [
  214. [
  215. '<h1>This is an <?php //echo 123;?> example</h1>',
  216. '<h1>This is an <?php #echo 123;?> example</h1>',
  217. ],
  218. [
  219. '<?php
  220. // test
  221. ',
  222. '<?php
  223. # test
  224. ',
  225. ],
  226. [
  227. '<?php
  228. // test1
  229. //test2
  230. // test3
  231. // test 4
  232. ',
  233. '<?php
  234. # test1
  235. #test2
  236. # test3
  237. # test 4
  238. ',
  239. ],
  240. // Untouched cases
  241. [
  242. '<?php
  243. //#test
  244. ',
  245. ],
  246. [
  247. '<?php
  248. /*
  249. #test
  250. */
  251. ',
  252. ],
  253. [
  254. '<?php // a',
  255. '<?php # a',
  256. ],
  257. [
  258. '<?php /* start-end */',
  259. ],
  260. ];
  261. }
  262. /**
  263. * @param string $expected
  264. * @param null|string $input
  265. *
  266. * @dataProvider provideAllCases
  267. */
  268. public function testAllCases($expected, $input = null)
  269. {
  270. $this->doTest($expected, $input);
  271. }
  272. public function provideAllCases()
  273. {
  274. return [
  275. [
  276. '<?php
  277. // 1
  278. // 2
  279. /*
  280. * 3.a
  281. * 3.b
  282. */
  283. /**
  284. * 4
  285. */
  286. // 5
  287. ',
  288. '<?php
  289. /* 1 */
  290. /*
  291. * 2
  292. */
  293. /*
  294. * 3.a
  295. * 3.b
  296. */
  297. /**
  298. * 4
  299. */
  300. # 5
  301. ',
  302. ],
  303. [
  304. '<?php
  305. function foo() {
  306. /* ?> */
  307. return "bar";
  308. }',
  309. ],
  310. ];
  311. }
  312. }