SingleLineCommentStyleFixerTest.php 5.9 KB

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