AlignMultilineCommentFixerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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\Phpdoc;
  13. use PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. use PhpCsFixer\WhitespacesFixerConfig;
  16. /**
  17. * @author Filippo Tessarotto <zoeslam@gmail.com>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\Phpdoc\AlignMultilineCommentFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Phpdoc\AlignMultilineCommentFixer>
  24. *
  25. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Phpdoc\AlignMultilineCommentFixer
  26. */
  27. final class AlignMultilineCommentFixerTest extends AbstractFixerTestCase
  28. {
  29. public function testInvalidConfiguration(): void
  30. {
  31. $this->expectException(InvalidFixerConfigurationException::class);
  32. $this->fixer->configure(['a' => 1]);
  33. }
  34. /**
  35. * @dataProvider provideDefaultCases
  36. */
  37. public function testDefaults(string $expected, ?string $input = null): void
  38. {
  39. $this->doTest($expected, $input);
  40. }
  41. /**
  42. * @return iterable<int|string, array{0: string, 1?: string}>
  43. */
  44. public static function provideDefaultCases(): iterable
  45. {
  46. yield [
  47. '<?php
  48. $a = 1;
  49. /**
  50. * Doc comment
  51. *
  52. *
  53. *
  54. * first without an asterisk
  55. * second without an asterisk or space
  56. */',
  57. '<?php
  58. $a = 1;
  59. /**
  60. * Doc comment
  61. *
  62. *
  63. first without an asterisk
  64. second without an asterisk or space
  65. */',
  66. ];
  67. yield [
  68. '<?php
  69. /**
  70. * Document start
  71. */',
  72. '<?php
  73. /**
  74. * Document start
  75. */',
  76. ];
  77. yield [
  78. "<?php\n \n /**\n * Two new lines\n */",
  79. "<?php\n \n /**\n* Two new lines\n*/",
  80. ];
  81. yield [
  82. "<?php
  83. \t/**
  84. \t * Tabs as indentation
  85. \t */",
  86. "<?php
  87. \t/**
  88. * Tabs as indentation
  89. */",
  90. ];
  91. yield [
  92. '<?php
  93. $a = 1;
  94. /**
  95. * Doc command without prior indentation
  96. */',
  97. '<?php
  98. $a = 1;
  99. /**
  100. * Doc command without prior indentation
  101. */',
  102. ];
  103. yield [
  104. '<?php
  105. /**
  106. * Doc command without prior indentation
  107. * Document start
  108. */',
  109. '<?php
  110. /**
  111. * Doc command without prior indentation
  112. * Document start
  113. */',
  114. ];
  115. // Untouched cases
  116. yield [
  117. '<?php
  118. /*
  119. * Multiline comment
  120. *
  121. *
  122. */',
  123. ];
  124. yield [
  125. '<?php
  126. /** inline doc comment */',
  127. ];
  128. yield [
  129. '<?php
  130. $a=1; /**
  131. *
  132. doc comment that doesn\'t start in a new line
  133. */',
  134. ];
  135. yield [
  136. '<?php
  137. # Hash single line comments are untouched
  138. #
  139. #
  140. #',
  141. ];
  142. yield [
  143. '<?php
  144. // Slash single line comments are untouched
  145. //
  146. //
  147. //',
  148. ];
  149. yield 'uni code test' => [
  150. '<?php
  151. class A
  152. {
  153. /**
  154. * @SWG\Get(
  155. * path="/api/v0/cards",
  156. * operationId="listCards",
  157. * tags={"Банковские карты"},
  158. * summary="Возвращает список банковских карт."
  159. * )
  160. */
  161. public function indexAction()
  162. {
  163. }
  164. }',
  165. ];
  166. }
  167. /**
  168. * @dataProvider provideDocLikeMultilineCommentsCases
  169. */
  170. public function testDocLikeMultilineComments(string $expected, ?string $input = null): void
  171. {
  172. $this->fixer->configure(['comment_type' => 'phpdocs_like']);
  173. $this->doTest($expected, $input);
  174. }
  175. /**
  176. * @return iterable<array{0: string, 1?: string}>
  177. */
  178. public static function provideDocLikeMultilineCommentsCases(): iterable
  179. {
  180. yield [
  181. '<?php
  182. /*
  183. * Doc-like Multiline comment
  184. *
  185. *
  186. */',
  187. '<?php
  188. /*
  189. * Doc-like Multiline comment
  190. *
  191. *
  192. */',
  193. ];
  194. yield [
  195. '<?php
  196. /*
  197. * Multiline comment with mixed content
  198. *
  199. Line without an asterisk
  200. *
  201. */',
  202. ];
  203. yield [
  204. '<?php
  205. /*
  206. * Two empty lines
  207. *
  208. *
  209. */',
  210. ];
  211. }
  212. /**
  213. * @dataProvider provideMixedContentMultilineCommentsCases
  214. */
  215. public function testMixedContentMultilineComments(string $expected, ?string $input = null): void
  216. {
  217. $this->fixer->configure(['comment_type' => 'all_multiline']);
  218. $this->doTest($expected, $input);
  219. }
  220. /**
  221. * @return iterable<array{string, string}>
  222. */
  223. public static function provideMixedContentMultilineCommentsCases(): iterable
  224. {
  225. yield [
  226. '<?php
  227. /*
  228. * Multiline comment with mixed content
  229. *
  230. Line without an asterisk
  231. *
  232. */',
  233. '<?php
  234. /*
  235. * Multiline comment with mixed content
  236. *
  237. Line without an asterisk
  238. *
  239. */',
  240. ];
  241. }
  242. /**
  243. * @dataProvider provideDefaultCases
  244. */
  245. public function testWhitespaceAwareness(string $expected, ?string $input = null): void
  246. {
  247. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  248. $expected = str_replace("\n", "\r\n", $expected);
  249. if (null !== $input) {
  250. $input = str_replace("\n", "\r\n", $input);
  251. }
  252. $this->doTest($expected, $input);
  253. }
  254. }