AlignMultilineCommentFixerTest.php 5.0 KB

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