SingleLineCommentStyleFixerTest.php 6.3 KB

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