AlignMultilineCommentFixerTest.php 5.3 KB

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