BlankLineBetweenImportGroupsFixerTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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\Whitespace;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\Whitespace\BlankLineBetweenImportGroupsFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\BlankLineBetweenImportGroupsFixer>
  20. */
  21. final class BlankLineBetweenImportGroupsFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. /**
  31. * @return iterable<int|string, array{string, string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield [
  36. '<?php
  37. use Aaa\Ccc;
  38. use Foo\Zar\Baz;
  39. use some\a\{ClassA};
  40. use some\b\{ClassD, ClassB, ClassC as C};
  41. use Bar\Biz\Boooz\Bum;
  42. use some\b\{
  43. ClassF,
  44. ClassG
  45. };
  46. use Some\Cloz;
  47. use Aaa\Bbb;
  48. use const some\a\{ConstD};
  49. use const some\a\{ConstA};
  50. use const some\a\{ConstB, ConstC as CC};
  51. use const some\b\{ConstE};
  52. use function some\f\{fn_g, fn_h, fn_i};
  53. use function some\c\{fn_f};
  54. use function some\a\{fn_x};
  55. use function some\b\{fn_c, fn_d, fn_e};
  56. use function some\a\{fn_a, fn_b};
  57. ',
  58. '<?php
  59. use Aaa\Ccc;
  60. use Foo\Zar\Baz;
  61. use some\a\{ClassA};
  62. use some\b\{ClassD, ClassB, ClassC as C};
  63. use Bar\Biz\Boooz\Bum;
  64. use some\b\{
  65. ClassF,
  66. ClassG
  67. };
  68. use Some\Cloz;
  69. use Aaa\Bbb;
  70. use const some\a\{ConstD};
  71. use const some\a\{ConstA};
  72. use const some\a\{ConstB, ConstC as CC};
  73. use const some\b\{ConstE};
  74. use function some\f\{fn_g, fn_h, fn_i};
  75. use function some\c\{fn_f};
  76. use function some\a\{fn_x};
  77. use function some\b\{fn_c, fn_d, fn_e};
  78. use function some\a\{fn_a, fn_b};
  79. ',
  80. ];
  81. yield [
  82. '<?php
  83. use Aaa\Ccc;
  84. use Foo\Zar\Baz;
  85. use function some\f\{fn_g, fn_h, fn_i};
  86. use some\a\{ClassA};
  87. use some\b\{ClassD, ClassB, ClassC as C};
  88. use Bar\Biz\Boooz\Bum;
  89. use function some\c\{fn_f};
  90. use some\b\{
  91. ClassF,
  92. ClassG
  93. };
  94. use const some\a\{ConstD};
  95. use Some\Cloz;
  96. use function some\a\{fn_x};
  97. use const some\a\{ConstA};
  98. use function some\b\{fn_c, fn_d, fn_e};
  99. use const some\a\{ConstB, ConstC as CC};
  100. use Aaa\Bbb;
  101. use const some\b\{ConstE};
  102. use function some\a\{fn_a, fn_b};
  103. ',
  104. '<?php
  105. use Aaa\Ccc;
  106. use Foo\Zar\Baz;
  107. use function some\f\{fn_g, fn_h, fn_i};
  108. use some\a\{ClassA};
  109. use some\b\{ClassD, ClassB, ClassC as C};
  110. use Bar\Biz\Boooz\Bum;
  111. use function some\c\{fn_f};
  112. use some\b\{
  113. ClassF,
  114. ClassG
  115. };
  116. use const some\a\{ConstD};
  117. use Some\Cloz;
  118. use function some\a\{fn_x};
  119. use const some\a\{ConstA};
  120. use function some\b\{fn_c, fn_d, fn_e};
  121. use const some\a\{ConstB, ConstC as CC};
  122. use Aaa\Bbb;
  123. use const some\b\{ConstE};
  124. use function some\a\{fn_a, fn_b};
  125. ',
  126. ];
  127. yield [
  128. '<?php
  129. use Aaa\Ccc;
  130. use Foo\Zar\Baz;
  131. #use function some\f\{fn_g, fn_h, fn_i};
  132. use some\a\{ClassA};
  133. use some\b\{ClassD, ClassB, ClassC as C};
  134. use Bar\Biz\Boooz\Bum;
  135. use function some\c\{fn_f};
  136. use some\b\{
  137. ClassF,
  138. ClassG
  139. };
  140. use const some\a\{ConstD};
  141. use Some\Cloz;
  142. use function some\a\{fn_x};
  143. /** Import ConstA to do some nice magic */
  144. use const some\a\{ConstA};
  145. use function some\b\{fn_c, fn_d, fn_e};
  146. use const some\a\{ConstB, ConstC as CC};
  147. use Aaa\Bbb;
  148. use const some\b\{ConstE};
  149. use function some\a\{fn_a, fn_b};
  150. ',
  151. '<?php
  152. use Aaa\Ccc;
  153. use Foo\Zar\Baz;
  154. #use function some\f\{fn_g, fn_h, fn_i};
  155. use some\a\{ClassA};
  156. use some\b\{ClassD, ClassB, ClassC as C};
  157. use Bar\Biz\Boooz\Bum;
  158. use function some\c\{fn_f};
  159. use some\b\{
  160. ClassF,
  161. ClassG
  162. };
  163. use const some\a\{ConstD};
  164. use Some\Cloz;
  165. use function some\a\{fn_x};
  166. /** Import ConstA to do some nice magic */
  167. use const some\a\{ConstA};
  168. use function some\b\{fn_c, fn_d, fn_e};
  169. use const some\a\{ConstB, ConstC as CC};
  170. use Aaa\Bbb;
  171. use const some\b\{ConstE};
  172. use function some\a\{fn_a, fn_b};
  173. ',
  174. ];
  175. yield [
  176. '<?php
  177. /**
  178. use Aaa\Ccc;
  179. use Foo\Zar\Baz;
  180. */
  181. use function some\f\{fn_g, fn_h, fn_i};
  182. use some\a\{ClassA};
  183. use some\b\{ClassD, ClassB, ClassC as C};
  184. use Bar\Biz\Boooz\Bum;
  185. use function some\c\{fn_f};
  186. use some\b\{
  187. ClassF,
  188. ClassG
  189. };
  190. use const some\a\{ConstD};
  191. use Some\Cloz;
  192. // Ignore the following content
  193. // use function some\a\{fn_x};
  194. // use const some\a\{ConstA};
  195. use function some\b\{fn_c, fn_d, fn_e};
  196. use const some\a\{ConstB, ConstC as CC};
  197. use Aaa\Bbb;
  198. use const some\b\{ConstE};
  199. use function some\a\{fn_a, fn_b};
  200. ',
  201. '<?php
  202. /**
  203. use Aaa\Ccc;
  204. use Foo\Zar\Baz;
  205. */
  206. use function some\f\{fn_g, fn_h, fn_i};
  207. use some\a\{ClassA};
  208. use some\b\{ClassD, ClassB, ClassC as C};
  209. use Bar\Biz\Boooz\Bum;
  210. use function some\c\{fn_f};
  211. use some\b\{
  212. ClassF,
  213. ClassG
  214. };
  215. use const some\a\{ConstD};
  216. use Some\Cloz;
  217. // Ignore the following content
  218. // use function some\a\{fn_x};
  219. // use const some\a\{ConstA};
  220. use function some\b\{fn_c, fn_d, fn_e};
  221. use const some\a\{ConstB, ConstC as CC};
  222. use Aaa\Bbb;
  223. use const some\b\{ConstE};
  224. use function some\a\{fn_a, fn_b};
  225. ',
  226. ];
  227. yield [
  228. '<?php
  229. use Aaa\Ccc;
  230. /*use Foo\Zar\Baz;
  231. use function some\f\{fn_g, fn_h, fn_i};
  232. use some\a\{ClassA};
  233. use some\b\{ClassD, ClassB, ClassC as C};
  234. use Bar\Biz\Boooz\Bum;
  235. use function some\c\{fn_f};
  236. use some\b\{
  237. ClassF,
  238. ClassG
  239. };
  240. use const some\a\{ConstD};
  241. use Some\Cloz;
  242. use function some\a\{fn_x};
  243. use const some\a\{ConstA};
  244. use function some\b\{fn_c, fn_d, fn_e};
  245. use const some\a\{ConstB, ConstC as CC};
  246. use Aaa\Bbb;
  247. use const some\b\{ConstE};
  248. */
  249. use function some\a\{fn_a, fn_b};
  250. ',
  251. '<?php
  252. use Aaa\Ccc;
  253. /*use Foo\Zar\Baz;
  254. use function some\f\{fn_g, fn_h, fn_i};
  255. use some\a\{ClassA};
  256. use some\b\{ClassD, ClassB, ClassC as C};
  257. use Bar\Biz\Boooz\Bum;
  258. use function some\c\{fn_f};
  259. use some\b\{
  260. ClassF,
  261. ClassG
  262. };
  263. use const some\a\{ConstD};
  264. use Some\Cloz;
  265. use function some\a\{fn_x};
  266. use const some\a\{ConstA};
  267. use function some\b\{fn_c, fn_d, fn_e};
  268. use const some\a\{ConstB, ConstC as CC};
  269. use Aaa\Bbb;
  270. use const some\b\{ConstE};
  271. */
  272. use function some\a\{fn_a, fn_b};
  273. ',
  274. ];
  275. yield [
  276. '<?php
  277. use Aaa\Ccc;
  278. use function some\a\{fn_a, fn_b};
  279. use function some\b\{
  280. fn_c,
  281. fn_d
  282. };
  283. ',
  284. '<?php
  285. use Aaa\Ccc;
  286. use function some\a\{fn_a, fn_b};
  287. use function some\b\{
  288. fn_c,
  289. fn_d
  290. };
  291. ',
  292. ];
  293. yield [
  294. '<?php
  295. use Aaa\Ccc;
  296. use function some\a\{fn_a, fn_b}; // Do this because of reasons
  297. use function some\b\{
  298. fn_c,
  299. fn_d
  300. };
  301. ',
  302. '<?php
  303. use Aaa\Ccc;
  304. use function some\a\{fn_a, fn_b}; // Do this because of reasons
  305. use function some\b\{
  306. fn_c,
  307. fn_d
  308. };
  309. ',
  310. ];
  311. yield [
  312. '<?php
  313. use Aaa\Ccc;
  314. /** Beginning of line comment as well */use function some\a\{fn_a, fn_b};
  315. use function some\b\{
  316. fn_c,
  317. fn_d
  318. };
  319. ',
  320. '<?php
  321. use Aaa\Ccc;
  322. /** Beginning of line comment as well */use function some\a\{fn_a, fn_b};
  323. use function some\b\{
  324. fn_c,
  325. fn_d
  326. };
  327. ',
  328. ];
  329. yield [
  330. '<?php
  331. use /* x */ function /* x */ Psl\Str\trim;
  332. use /* x */ Psl\Str /* x */;
  333. use /* x */ const /* x */ Psl\Str\OTHER_ALPHABET;
  334. use /* x */ const /* x */ Psl\Str\ALPHABET;
  335. use /* x */ function /* x */ Psl\Str\ /* x */ {
  336. /* x */ trim_left /* x */,
  337. /* x */ trim_right /* x */,
  338. };
  339. ',
  340. '<?php
  341. use /* x */ function /* x */ Psl\Str\trim;
  342. use /* x */ Psl\Str /* x */;
  343. use /* x */ const /* x */ Psl\Str\OTHER_ALPHABET;
  344. use /* x */ const /* x */ Psl\Str\ALPHABET;
  345. use /* x */ function /* x */ Psl\Str\ /* x */ {
  346. /* x */ trim_left /* x */,
  347. /* x */ trim_right /* x */,
  348. };
  349. ',
  350. ];
  351. yield 'lots of inserts in same namespace' => [
  352. '<?php
  353. namespace A\B6 {
  354. use C1\B1;
  355. use const C6\Z1;
  356. use C2\B2;
  357. use const C7\Z2;
  358. use C3\B3;
  359. use const C8\Z3;
  360. use C4\B4;
  361. use const C9\Z4;
  362. use C5\B5;
  363. use const C0\Z5;
  364. }
  365. ',
  366. '<?php
  367. namespace A\B6 {
  368. use C1\B1;use const C6\Z1;
  369. use C2\B2;use const C7\Z2;
  370. use C3\B3;use const C8\Z3;
  371. use C4\B4;use const C9\Z4;
  372. use C5\B5;use const C0\Z5;
  373. }
  374. ',
  375. ];
  376. yield 'lots of inserts in multiple namespaces' => [
  377. '<?php
  378. namespace A\B1 {
  379. use C\B;
  380. use const C\Z;
  381. }
  382. namespace A\B2 {
  383. use C\B;
  384. use const C\Z;
  385. }
  386. namespace A\B3 {
  387. use C\B;
  388. use const C\Z;
  389. }
  390. namespace A\B4 {
  391. use C\B;
  392. use const C\Z;
  393. }
  394. namespace A\B5 {
  395. use C\B;
  396. use const C\Z;
  397. }
  398. ',
  399. '<?php
  400. namespace A\B1 {
  401. use C\B;use const C\Z;
  402. }
  403. namespace A\B2 {
  404. use C\B;use const C\Z;
  405. }
  406. namespace A\B3 {
  407. use C\B;use const C\Z;
  408. }
  409. namespace A\B4 {
  410. use C\B;use const C\Z;
  411. }
  412. namespace A\B5 {
  413. use C\B;use const C\Z;
  414. }
  415. ',
  416. ];
  417. yield [
  418. '<?php use A\B; /*1*/
  419. use const C\D;',
  420. '<?php use A\B; /*1*/ use const C\D;',
  421. ];
  422. yield [
  423. '<?php
  424. namespace Foo;
  425. use A\B; /* foo */ /* A */ /* B */ # X
  426. use const C\D; // bar
  427. ',
  428. '<?php
  429. namespace Foo;
  430. use A\B; /* foo */ /* A */ /* B */ # X
  431. use const C\D; // bar
  432. ',
  433. ];
  434. }
  435. /**
  436. * @dataProvider provideFix80Cases
  437. *
  438. * @requires PHP <8.0
  439. */
  440. public function testFix80(string $expected, ?string $input = null): void
  441. {
  442. $this->doTest($expected, $input);
  443. }
  444. /**
  445. * @return iterable<array{string, string}>
  446. */
  447. public static function provideFix80Cases(): iterable
  448. {
  449. yield [
  450. '<?php
  451. use Some\/**Namespace*/Weird\Be/**ha*/;
  452. use function Other\/**Namespace*/Weird\/**Comments*/have;
  453. ',
  454. '<?php
  455. use Some\/**Namespace*/Weird\Be/**ha*/;
  456. use function Other\/**Namespace*/Weird\/**Comments*/have;
  457. ',
  458. ];
  459. }
  460. }