GroupImportFixerTest.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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\Import;
  13. use PhpCsFixer\Fixer\Import\GroupImportFixer;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Volodymyr Kupriienko <vldmr.kuprienko@gmail.com>
  17. * @author Greg Korba <greg@codito.dev>
  18. *
  19. * @internal
  20. *
  21. * @covers \PhpCsFixer\Fixer\Import\GroupImportFixer
  22. *
  23. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Import\GroupImportFixer>
  24. *
  25. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Import\GroupImportFixer
  26. */
  27. final class GroupImportFixerTest extends AbstractFixerTestCase
  28. {
  29. /**
  30. * @dataProvider provideFixCases
  31. *
  32. * @phpstan-param _AutogeneratedInputConfiguration $config
  33. */
  34. public function testFix(string $expected, ?string $input = null, ?array $config = []): void
  35. {
  36. $this->fixer->configure($config);
  37. $this->doTest($expected, $input);
  38. }
  39. /**
  40. * @return iterable<array{0: string, 1?: string, 2?: _AutogeneratedInputConfiguration}>
  41. */
  42. public static function provideFixCases(): iterable
  43. {
  44. yield [
  45. '<?php
  46. namespace X1\Y1;
  47. use A\{B as X, C as Y};
  48. use B\C;
  49. ',
  50. '<?php
  51. namespace X1\Y1;
  52. use A\B as X;
  53. use A\C as Y;
  54. use B\C;
  55. ',
  56. ];
  57. yield [
  58. '<?php
  59. namespace Foo\Bar;
  60. use A\{B as X, C as Y};
  61. ',
  62. '<?php
  63. namespace Foo\Bar;
  64. use A\B as X;
  65. use A\C as Y;
  66. ',
  67. ];
  68. yield [
  69. '<?php
  70. namespace Test;
  71. use Foo\{Bar, Baz, Test};
  72. ',
  73. '<?php
  74. namespace Test;
  75. use Foo\Bar;
  76. use Foo\Baz;
  77. use Foo\Test;
  78. ',
  79. ];
  80. yield [
  81. '<?php
  82. use App\First;
  83. use Foo\{Bar, Baz};
  84. use Test\Second;
  85. ',
  86. '<?php
  87. use App\First;
  88. use Foo\Bar;
  89. use Test\Second;
  90. use Foo\Baz;
  91. ',
  92. ];
  93. yield [
  94. '<?php
  95. use App\{First, Second};
  96. use Foo\{Bar, Baz};
  97. ',
  98. '<?php
  99. use Foo\Bar;
  100. use Foo\Baz;
  101. use App\First;
  102. use App\Second;
  103. ',
  104. ];
  105. yield [
  106. '<?php
  107. use App\{First, Second};
  108. use Foo\{Bar, Baz};
  109. ',
  110. '<?php
  111. use Foo\Bar;
  112. use App\First;
  113. use Foo\Baz;
  114. use App\Second;
  115. ',
  116. ];
  117. yield [
  118. '<?php
  119. use Foo\{Bar as Test, Baz};
  120. use App;
  121. ',
  122. '<?php
  123. use Foo\Bar as Test;
  124. use Foo\Baz;
  125. use App;
  126. ',
  127. ];
  128. yield [
  129. '<?php
  130. use App\Repository\{Customer as Client, Profile, User};
  131. ',
  132. '<?php
  133. use App\Repository\User;
  134. use App\Repository\Profile;
  135. use App\Repository\Customer as Client;
  136. ',
  137. ];
  138. yield [
  139. '<?php
  140. use function Foo\{Bar, Baz, Test as Alias};
  141. ',
  142. '<?php
  143. use function Foo\Bar;
  144. use function Foo\Baz;
  145. use function Foo\Test as Alias;
  146. ',
  147. ];
  148. yield [
  149. '<?php
  150. use const Some\Place\{A, B, C as D};
  151. ',
  152. '<?php
  153. use const Some\Place\A;
  154. use const Some\Place\B;
  155. use const Some\Place\C as D;
  156. ',
  157. ];
  158. yield [
  159. '<?php
  160. use Foo\Bar;
  161. use Foo\Baz\Lorem\Ipsum\Lets\Write\Some\More\Strings\{One, Two};
  162. ',
  163. '<?php
  164. use Foo\Bar;
  165. use Foo\Baz\Lorem\Ipsum\Lets\Write\Some\More\Strings\One;
  166. use Foo\Baz\Lorem\Ipsum\Lets\Write\Some\More\Strings\Two;
  167. ',
  168. ];
  169. yield [
  170. '<?php
  171. use Foo\{Bar, Baz};
  172. use Foo\Baz\John\{Doe, Smith};
  173. use Foo\Baz\Johnny\{DoeSecond, SmithSecond};
  174. use Foo\Baz\John\Smith\Junior;
  175. ',
  176. '<?php
  177. use Foo\Bar;
  178. use Foo\Baz;
  179. use Foo\Baz\John\Doe;
  180. use Foo\Baz\John\Smith;
  181. use Foo\Baz\John\Smith\Junior;
  182. use Foo\Baz\Johnny\DoeSecond;
  183. use Foo\Baz\Johnny\SmithSecond;
  184. ',
  185. ];
  186. yield [
  187. '<?php
  188. use PhpCsFixer\Tokenizer\{AbstractTransformer, CT, Token, Tokens};
  189. ',
  190. '<?php
  191. use PhpCsFixer\Tokenizer\AbstractTransformer;
  192. use PhpCsFixer\Tokenizer\CT;
  193. use PhpCsFixer\Tokenizer\Token;
  194. use PhpCsFixer\Tokenizer\Tokens;
  195. ',
  196. ];
  197. yield [
  198. '<?php
  199. use Foo\{Bar, Baz};
  200. ',
  201. '<?php
  202. use Foo\Bar;use Foo\Baz;
  203. ',
  204. ];
  205. yield [
  206. '<?php
  207. use Foo\{Bar, Baz};
  208. \DontTouch::me();
  209. ',
  210. '<?php
  211. use Foo\Bar;use Foo\Baz;\DontTouch::me();
  212. ',
  213. ];
  214. yield [
  215. '<?php
  216. use Foo\{Bar, Baz};
  217. use ReflectionClass;
  218. use ReflectionMethod;
  219. ',
  220. '<?php
  221. use Foo\Bar;
  222. use Foo\Baz;
  223. use ReflectionClass;
  224. use ReflectionMethod;
  225. ',
  226. ];
  227. yield [
  228. '<?php
  229. use Foo\{Bar, Baz};
  230. use \ReflectionClass;
  231. use \ReflectionMethod;
  232. ',
  233. '<?php
  234. use Foo\Bar;
  235. use Foo\Baz;
  236. use \ReflectionClass;
  237. use \ReflectionMethod;
  238. ',
  239. ];
  240. yield [
  241. '<?php
  242. use Framework\Database\Query\JoinClause;
  243. use Framework\Support\{Arr, Collection};
  244. use Framework\Database\ORM\{Model, SoftDeletes};
  245. use Framework\Notifications\{Notifiable, Notification};
  246. use Framework\Support\Facades\{DB, Log};
  247. use Framework\Database\ORM\Relations\{BelongsTo, HasOne};
  248. ',
  249. '<?php
  250. use Framework\Database\ORM\Model;
  251. use Framework\Database\ORM\Relations\BelongsTo;
  252. use Framework\Database\ORM\Relations\HasOne;
  253. use Framework\Database\ORM\SoftDeletes;
  254. use Framework\Database\Query\JoinClause;
  255. use Framework\Notifications\Notifiable;
  256. use Framework\Notifications\Notification;
  257. use Framework\Support\Arr;
  258. use Framework\Support\Collection;
  259. use Framework\Support\Facades\DB;
  260. use Framework\Support\Facades\Log;
  261. ',
  262. ];
  263. yield [
  264. '<?php
  265. use Framework\Bar\{Class3, Class4, Class5};
  266. use Framework\Foo\{Class1, Class2, Class7};
  267. use Framework\Baz\Class6;
  268. ',
  269. '<?php
  270. use Framework\Foo\Class1;
  271. use Framework\Foo\Class2;
  272. use Framework\Bar\Class3;
  273. use Framework\Bar\Class4;
  274. use Framework\Bar\Class5;
  275. use Framework\Baz\Class6;
  276. use Framework\Foo\Class7;
  277. ',
  278. ];
  279. yield [
  280. '<?php
  281. use Foo\{Bar, Baz};
  282. use function Foo\baz;
  283. ',
  284. '<?php
  285. use Foo\Bar;
  286. use function Foo\baz;
  287. use Foo\Baz;
  288. ',
  289. ];
  290. yield [
  291. '<?php
  292. use Foo\Bar\C;
  293. use Foo\{D, E};
  294. use Foo\Bar\Baz\{A, B};
  295. use function Foo\{a, b};
  296. ',
  297. '<?php
  298. use Foo\Bar\Baz\A;
  299. use Foo\Bar\Baz\B;
  300. use Foo\Bar\C;
  301. use Foo\D;
  302. use Foo\E;
  303. use function Foo\a;
  304. use function Foo\b;
  305. ',
  306. ];
  307. yield 'only class-like' => [
  308. '<?php
  309. use Foo\Bar\C;
  310. use Foo\{D, E};
  311. use Foo\Bar\Baz\{A, B};
  312. use function Foo\a;
  313. use function Foo\b;
  314. use const Foo\X;
  315. use const Foo\Y;
  316. ',
  317. '<?php
  318. use Foo\Bar\Baz\A;
  319. use Foo\Bar\Baz\B;
  320. use Foo\Bar\C;
  321. use Foo\D;
  322. use Foo\E;
  323. use function Foo\a;
  324. use function Foo\b;
  325. use const Foo\X;
  326. use const Foo\Y;
  327. ',
  328. ['group_types' => [GroupImportFixer::GROUP_CLASSY]],
  329. ];
  330. yield 'only functions' => [
  331. '<?php
  332. use Foo\Bar\Baz\A;
  333. use Foo\Bar\Baz\B;
  334. use function Foo\{a, b};
  335. use Foo\Bar\C;
  336. use Foo\D;
  337. use Foo\E;
  338. use const Foo\X;
  339. use const Foo\Y;
  340. ',
  341. '<?php
  342. use Foo\Bar\Baz\A;
  343. use Foo\Bar\Baz\B;
  344. use function Foo\a;
  345. use function Foo\b;
  346. use Foo\Bar\C;
  347. use Foo\D;
  348. use Foo\E;
  349. use const Foo\X;
  350. use const Foo\Y;
  351. ',
  352. ['group_types' => [GroupImportFixer::GROUP_FUNCTIONS]],
  353. ];
  354. yield 'only constants' => [
  355. '<?php
  356. use Foo\Bar\Baz\A;
  357. use Foo\Bar\Baz\B;
  358. use function Foo\a;
  359. use function Foo\b;
  360. use Foo\Bar\C;
  361. use Foo\D;
  362. use Foo\E;
  363. use const Foo\{X, Y};
  364. ',
  365. '<?php
  366. use Foo\Bar\Baz\A;
  367. use Foo\Bar\Baz\B;
  368. use function Foo\a;
  369. use function Foo\b;
  370. use Foo\Bar\C;
  371. use Foo\D;
  372. use Foo\E;
  373. use const Foo\X;
  374. use const Foo\Y;
  375. ',
  376. ['group_types' => [GroupImportFixer::GROUP_CONSTANTS]],
  377. ];
  378. yield 'class-likes and functions' => [
  379. '<?php
  380. use function Foo\{a, b};
  381. use Foo\Bar\C;
  382. use Foo\{D, E};
  383. use Foo\Bar\Baz\{A, B};
  384. use const Foo\X;
  385. use const Foo\Y;
  386. ',
  387. '<?php
  388. use Foo\Bar\Baz\A;
  389. use Foo\Bar\Baz\B;
  390. use function Foo\a;
  391. use function Foo\b;
  392. use Foo\Bar\C;
  393. use Foo\D;
  394. use Foo\E;
  395. use const Foo\X;
  396. use const Foo\Y;
  397. ',
  398. ['group_types' => [GroupImportFixer::GROUP_CLASSY, GroupImportFixer::GROUP_FUNCTIONS]],
  399. ];
  400. yield 'class-likes and constants' => [
  401. '<?php
  402. use function Foo\a;
  403. use function Foo\b;
  404. use Foo\Bar\C;
  405. use Foo\{D, E};
  406. use Foo\Bar\Baz\{A, B};
  407. use const Foo\{X, Y};
  408. ',
  409. '<?php
  410. use Foo\Bar\Baz\A;
  411. use Foo\Bar\Baz\B;
  412. use function Foo\a;
  413. use function Foo\b;
  414. use Foo\Bar\C;
  415. use Foo\D;
  416. use Foo\E;
  417. use const Foo\X;
  418. use const Foo\Y;
  419. ',
  420. ['group_types' => [GroupImportFixer::GROUP_CLASSY, GroupImportFixer::GROUP_CONSTANTS]],
  421. ];
  422. yield 'functions and constants' => [
  423. '<?php
  424. use Foo\Bar\Baz\A;
  425. use Foo\Bar\Baz\B;
  426. use function Foo\{a, b};
  427. use Foo\Bar\C;
  428. use Foo\D;
  429. use Foo\E;
  430. use const Foo\{X, Y};
  431. ',
  432. '<?php
  433. use Foo\Bar\Baz\A;
  434. use Foo\Bar\Baz\B;
  435. use function Foo\a;
  436. use function Foo\b;
  437. use Foo\Bar\C;
  438. use Foo\D;
  439. use Foo\E;
  440. use const Foo\X;
  441. use const Foo\Y;
  442. ',
  443. ['group_types' => [GroupImportFixer::GROUP_FUNCTIONS, GroupImportFixer::GROUP_CONSTANTS]],
  444. ];
  445. }
  446. /**
  447. * @dataProvider provideFixPre80Cases
  448. *
  449. * @requires PHP <8.0
  450. */
  451. public function testFixPre80(string $expected, ?string $input = null): void
  452. {
  453. $this->doTest($expected, $input);
  454. }
  455. /**
  456. * @return iterable<array{string, string}>
  457. */
  458. public static function provideFixPre80Cases(): iterable
  459. {
  460. yield [
  461. '<?php
  462. /*1*/use A\{B, C, D};
  463. /*2*//*3*//*4*//*5*//*6*/
  464. /*7*//*8*//*9*//*10*//*11*//*12*/
  465. /*13*//*14*//*15*//*16*//*17*//*18*/
  466. ',
  467. '<?php
  468. /*1*/use/*2*/A/*3*/\/*4*/B/*5*/;/*6*/
  469. /*7*/use/*8*/A/*9*/\/*10*/C/*11*/;/*12*/
  470. /*13*/use/*14*/A/*15*/\/*16*/D/*17*/;/*18*/
  471. ',
  472. ];
  473. }
  474. }