SingleLineCommentStyleFixerTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. 'empty comment' => [
  142. '<?php
  143. //
  144. ',
  145. '<?php
  146. /**/
  147. ',
  148. ],
  149. // Untouched cases
  150. [
  151. '<?php
  152. $a = 1; /* in code */ $b = 2;
  153. ',
  154. ],
  155. [
  156. '<?php
  157. /*
  158. * in code 2
  159. */ $a = 1;
  160. ',
  161. ],
  162. [
  163. '<?php
  164. /***
  165. *
  166. */ $a = 1;',
  167. ],
  168. [
  169. '<?php
  170. /***
  171. s *
  172. */ $a = 1;',
  173. ],
  174. [
  175. '<?php
  176. /*
  177. * first line
  178. * second line
  179. */',
  180. ],
  181. [
  182. '<?php
  183. /*
  184. * first line
  185. *
  186. * second line
  187. */',
  188. ],
  189. [
  190. '<?php
  191. /*first line
  192. second line*/',
  193. ],
  194. [
  195. '<?php /** inline doc comment */',
  196. ],
  197. [
  198. '<?php
  199. /**
  200. * Doc comment
  201. */',
  202. ],
  203. [
  204. '<?php # test',
  205. ],
  206. ];
  207. }
  208. /**
  209. * @param string $expected
  210. * @param null|string $input
  211. *
  212. * @dataProvider provideHashCases
  213. */
  214. public function testHashCases($expected, $input = null)
  215. {
  216. $this->fixer->configure(['comment_types' => ['hash']]);
  217. $this->doTest($expected, $input);
  218. }
  219. public function provideHashCases()
  220. {
  221. return [
  222. [
  223. '<h1>This is an <?php //echo 123;?> example</h1>',
  224. '<h1>This is an <?php #echo 123;?> example</h1>',
  225. ],
  226. [
  227. '<?php
  228. // test
  229. ',
  230. '<?php
  231. # test
  232. ',
  233. ],
  234. [
  235. '<?php
  236. // test1
  237. //test2
  238. // test3
  239. // test 4
  240. ',
  241. '<?php
  242. # test1
  243. #test2
  244. # test3
  245. # test 4
  246. ',
  247. ],
  248. [
  249. '<?php //',
  250. '<?php #',
  251. ],
  252. // Untouched cases
  253. [
  254. '<?php
  255. //#test
  256. ',
  257. ],
  258. [
  259. '<?php
  260. /*
  261. #test
  262. */
  263. ',
  264. ],
  265. [
  266. '<?php // a',
  267. '<?php # a',
  268. ],
  269. [
  270. '<?php /* start-end */',
  271. ],
  272. [
  273. '<?php function foo(
  274. #[MyAttr([1, 2])] Type $myParam,
  275. ) {} // foo',
  276. ],
  277. ];
  278. }
  279. /**
  280. * @param string $expected
  281. * @param null|string $input
  282. *
  283. * @dataProvider provideAllCases
  284. */
  285. public function testAllCases($expected, $input = null)
  286. {
  287. $this->doTest($expected, $input);
  288. }
  289. public function provideAllCases()
  290. {
  291. return [
  292. [
  293. '<?php
  294. // 1
  295. // 2
  296. /*
  297. * 3.a
  298. * 3.b
  299. */
  300. /**
  301. * 4
  302. */
  303. // 5
  304. ',
  305. '<?php
  306. /* 1 */
  307. /*
  308. * 2
  309. */
  310. /*
  311. * 3.a
  312. * 3.b
  313. */
  314. /**
  315. * 4
  316. */
  317. # 5
  318. ',
  319. ],
  320. [
  321. '<?php
  322. function foo() {
  323. /* ?> */
  324. return "bar";
  325. }',
  326. ],
  327. ];
  328. }
  329. }