AlignMultilineCommentFixerTest.php 5.2 KB

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