AlignMultilineCommentFixerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Filippo Tessarotto <zoeslam@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\Phpdoc\AlignMultilineCommentFixer
  21. */
  22. final class AlignMultilineCommentFixerTest extends AbstractFixerTestCase
  23. {
  24. public function testInvalidConfiguration(): void
  25. {
  26. $this->expectException(\PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException::class);
  27. $this->fixer->configure(['a' => 1]);
  28. }
  29. /**
  30. * @dataProvider provideDefaultCases
  31. */
  32. public function testDefaults(string $expected, ?string $input = null): void
  33. {
  34. $this->doTest($expected, $input);
  35. }
  36. public function provideDefaultCases()
  37. {
  38. return [
  39. [
  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. [
  61. '<?php
  62. /**
  63. * Document start
  64. */',
  65. '<?php
  66. /**
  67. * Document start
  68. */',
  69. ],
  70. [
  71. "<?php\n \n /**\n * Two new lines\n */",
  72. "<?php\n \n /**\n* Two new lines\n*/",
  73. ],
  74. [
  75. "<?php
  76. \t/**
  77. \t * Tabs as indentation
  78. \t */",
  79. "<?php
  80. \t/**
  81. * Tabs as indentation
  82. */",
  83. ],
  84. [
  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. [
  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. [
  110. '<?php
  111. /*
  112. * Multiline comment
  113. *
  114. *
  115. */',
  116. ],
  117. [
  118. '<?php
  119. /** inline doc comment */',
  120. ],
  121. [
  122. '<?php
  123. $a=1; /**
  124. *
  125. doc comment that doesn\'t start in a new line
  126. */',
  127. ],
  128. [
  129. '<?php
  130. # Hash single line comments are untouched
  131. #
  132. #
  133. #',
  134. ],
  135. [
  136. '<?php
  137. // Slash single line comments are untouched
  138. //
  139. //
  140. //',
  141. ],
  142. '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. /**
  162. * @dataProvider provideDocLikeMultilineCommentsCases
  163. */
  164. public function testDocLikeMultilineComments(string $expected, ?string $input = null): void
  165. {
  166. $this->fixer->configure(['comment_type' => 'phpdocs_like']);
  167. $this->doTest($expected, $input);
  168. }
  169. public function provideDocLikeMultilineCommentsCases()
  170. {
  171. return [
  172. [
  173. '<?php
  174. /*
  175. * Doc-like Multiline comment
  176. *
  177. *
  178. */',
  179. '<?php
  180. /*
  181. * Doc-like Multiline comment
  182. *
  183. *
  184. */',
  185. ],
  186. [
  187. '<?php
  188. /*
  189. * Multiline comment with mixed content
  190. *
  191. Line without an asterisk
  192. *
  193. */',
  194. ],
  195. [
  196. '<?php
  197. /*
  198. * Two empty lines
  199. *
  200. *
  201. */',
  202. ],
  203. ];
  204. }
  205. /**
  206. * @dataProvider provideMixedContentMultilineCommentsCases
  207. */
  208. public function testMixedContentMultilineComments(string $expected, ?string $input = null): void
  209. {
  210. $this->fixer->configure(['comment_type' => 'all_multiline']);
  211. $this->doTest($expected, $input);
  212. }
  213. public function provideMixedContentMultilineCommentsCases()
  214. {
  215. return [
  216. [
  217. '<?php
  218. /*
  219. * Multiline comment with mixed content
  220. *
  221. Line without an asterisk
  222. *
  223. */',
  224. '<?php
  225. /*
  226. * Multiline comment with mixed content
  227. *
  228. Line without an asterisk
  229. *
  230. */',
  231. ],
  232. ];
  233. }
  234. /**
  235. * @dataProvider provideDefaultCases
  236. */
  237. public function testWhitespaceAwareness(string $expected, ?string $input = null): void
  238. {
  239. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  240. $expected = str_replace("\n", "\r\n", $expected);
  241. if (null !== $input) {
  242. $input = str_replace("\n", "\r\n", $input);
  243. }
  244. $this->doTest($expected, $input);
  245. }
  246. }