OrderedImportsFixerTest.php 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  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\Import;
  12. use PhpCsFixer\Fixer\Import\OrderedImportsFixer;
  13. use PhpCsFixer\Test\AbstractFixerTestCase;
  14. /**
  15. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\Import\OrderedImportsFixer
  18. */
  19. final class OrderedImportsFixerTest extends AbstractFixerTestCase
  20. {
  21. public function testFix()
  22. {
  23. $expected = <<<'EOF'
  24. The normal
  25. use of this fixer
  26. should not change this sentence nor those statements below
  27. use Zoo\Bar as ZooBar;
  28. use Foo\Bar;
  29. use Foo\Zar\Baz;
  30. <?php
  31. use Foo\Bar;
  32. use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar as FooBaz;
  33. use Foo\Bir as FBB;
  34. use Foo\Zar\Baz;
  35. use SomeClass;
  36. use Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity;
  37. use Zoo\Bar as ZooBar;
  38. $a = new Bar();
  39. $a = new FooBaz();
  40. $a = new someclass();
  41. use Zoo\Tar;
  42. class AnnotatedClass
  43. {
  44. /**
  45. * @Template(foobar=21)
  46. * @param Entity $foo
  47. */
  48. public function doSomething($foo)
  49. {
  50. $bar = $foo->toArray();
  51. /** @var ArrayInterface $bar */
  52. return function () use ($bar, $foo) {};
  53. }
  54. }
  55. EOF;
  56. $input = <<<'EOF'
  57. The normal
  58. use of this fixer
  59. should not change this sentence nor those statements below
  60. use Zoo\Bar as ZooBar;
  61. use Foo\Bar;
  62. use Foo\Zar\Baz;
  63. <?php
  64. use Foo\Bar\FooBar as FooBaz;
  65. use Zoo\Bar as ZooBar, Zoo\Tar;
  66. use Foo\Bar;
  67. use Foo\Zar\Baz;
  68. use Symfony\Annotation\Template;
  69. use Foo\Bar\Foo as Fooo, Foo\Bir as FBB;
  70. use SomeClass;
  71. $a = new Bar();
  72. $a = new FooBaz();
  73. $a = new someclass();
  74. use Symfony\Doctrine\Entities\Entity;
  75. class AnnotatedClass
  76. {
  77. /**
  78. * @Template(foobar=21)
  79. * @param Entity $foo
  80. */
  81. public function doSomething($foo)
  82. {
  83. $bar = $foo->toArray();
  84. /** @var ArrayInterface $bar */
  85. return function () use ($bar, $foo) {};
  86. }
  87. }
  88. EOF;
  89. $this->doTest($expected, $input);
  90. }
  91. public function testFixWithMultipleNamespace()
  92. {
  93. $expected = <<<'EOF'
  94. <?php
  95. namespace FooRoo {
  96. use Foo\Bar;
  97. use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar as FooBaz;
  98. use Foo\Bir as FBB;
  99. use Foo\Zar\Baz;
  100. use SomeClass;
  101. use Symfony\Annotation\Template, Zoo\Bar as ZooBar;
  102. use Zoo\Tar1;
  103. $a = new Bar();
  104. $a = new FooBaz();
  105. $a = new someclass();
  106. use Zoo\Tar2;
  107. class AnnotatedClass
  108. {
  109. /**
  110. * @Template(foobar=21)
  111. * @param Entity $foo
  112. */
  113. public function doSomething($foo)
  114. {
  115. $bar = $foo->toArray();
  116. /** @var ArrayInterface $bar */
  117. return function () use ($bar, $foo) {};
  118. }
  119. }
  120. }
  121. namespace BlaRoo {
  122. use Foo\Zar\Baz;
  123. use SomeClass;
  124. use Symfony\Annotation\Template;
  125. use Symfony\Doctrine\Entities\Entity, Zoo\Bar;
  126. class AnnotatedClass
  127. {
  128. /**
  129. * @Template(foobar=21)
  130. * @param Entity $foo
  131. */
  132. public function doSomething($foo)
  133. {
  134. $bar = $foo->toArray();
  135. /** @var ArrayInterface $bar */
  136. return function () use ($bar, $foo) {};
  137. }
  138. }
  139. }
  140. EOF;
  141. $input = <<<'EOF'
  142. <?php
  143. namespace FooRoo {
  144. use Foo\Bar\FooBar as FooBaz;
  145. use Zoo\Bar as ZooBar, Zoo\Tar1;
  146. use Foo\Bar;
  147. use Foo\Zar\Baz;
  148. use Symfony\Annotation\Template;
  149. use Foo\Bar\Foo as Fooo, Foo\Bir as FBB;
  150. use SomeClass;
  151. $a = new Bar();
  152. $a = new FooBaz();
  153. $a = new someclass();
  154. use Zoo\Tar2;
  155. class AnnotatedClass
  156. {
  157. /**
  158. * @Template(foobar=21)
  159. * @param Entity $foo
  160. */
  161. public function doSomething($foo)
  162. {
  163. $bar = $foo->toArray();
  164. /** @var ArrayInterface $bar */
  165. return function () use ($bar, $foo) {};
  166. }
  167. }
  168. }
  169. namespace BlaRoo {
  170. use Foo\Zar\Baz;
  171. use Zoo\Bar;
  172. use SomeClass;
  173. use Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity;
  174. class AnnotatedClass
  175. {
  176. /**
  177. * @Template(foobar=21)
  178. * @param Entity $foo
  179. */
  180. public function doSomething($foo)
  181. {
  182. $bar = $foo->toArray();
  183. /** @var ArrayInterface $bar */
  184. return function () use ($bar, $foo) {};
  185. }
  186. }
  187. }
  188. EOF;
  189. $this->doTest($expected, $input);
  190. }
  191. public function testFixWithComment()
  192. {
  193. $expected = <<<'EOF'
  194. The normal
  195. use of this fixer
  196. should not change this sentence nor those statements below
  197. use Zoo\Bar;
  198. use Foo\Bar;
  199. use Foo\Zar\Baz;
  200. <?php
  201. use Foo\Bar;
  202. use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar /* He there */ as FooBaz;
  203. use Foo\Bir as FBB;
  204. use Foo\Zar\Baz;
  205. use SomeClass;
  206. use /* FIXME */Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity;
  207. use Zoo\Bar as ZooBar;
  208. $a = new Bar();
  209. $a = new FooBaz();
  210. $a = new someclass();
  211. use Zoo\Tar;
  212. class AnnotatedClass
  213. {
  214. /**
  215. * @Template(foobar=21)
  216. * @param Entity $foo
  217. */
  218. public function doSomething($foo)
  219. {
  220. $bar = $foo->toArray();
  221. /** @var ArrayInterface $bar */
  222. return function () use ($bar, $foo) {};
  223. }
  224. }
  225. EOF;
  226. $input = <<<'EOF'
  227. The normal
  228. use of this fixer
  229. should not change this sentence nor those statements below
  230. use Zoo\Bar;
  231. use Foo\Bar;
  232. use Foo\Zar\Baz;
  233. <?php
  234. use Foo\Bar\FooBar /* He there */ as FooBaz;
  235. use Zoo\Bar as ZooBar, Zoo\Tar;
  236. use Foo\Bar;
  237. use Foo\Zar\Baz;
  238. use /* FIXME */Symfony\Annotation\Template;
  239. use Foo\Bar\Foo as Fooo, Foo\Bir as FBB;
  240. use SomeClass;
  241. $a = new Bar();
  242. $a = new FooBaz();
  243. $a = new someclass();
  244. use Symfony\Doctrine\Entities\Entity;
  245. class AnnotatedClass
  246. {
  247. /**
  248. * @Template(foobar=21)
  249. * @param Entity $foo
  250. */
  251. public function doSomething($foo)
  252. {
  253. $bar = $foo->toArray();
  254. /** @var ArrayInterface $bar */
  255. return function () use ($bar, $foo) {};
  256. }
  257. }
  258. EOF;
  259. $this->doTest($expected, $input);
  260. }
  261. /**
  262. * @requires PHP 5.4
  263. */
  264. public function test54()
  265. {
  266. $expected = <<<'EOF'
  267. <?php
  268. use Foo\Bar;
  269. use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar as FooBaz;
  270. use Foo\Bir as FBB;
  271. use Foo\Zar\Baz;
  272. use SomeClass;
  273. use Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity;
  274. use Zoo\Bar as ZooBar;
  275. use Zoo\Tar;
  276. trait Foo {}
  277. trait Zoo {}
  278. class AnnotatedClass
  279. {
  280. use Foo, Bar;
  281. /**
  282. * @Template(foobar=21)
  283. * @param Entity $foo
  284. */
  285. public function doSomething($foo)
  286. {
  287. $bar = $foo->toArray();
  288. /** @var ArrayInterface $bar */
  289. return function () use ($bar, $foo) {};
  290. }
  291. }
  292. EOF;
  293. $input = <<<'EOF'
  294. <?php
  295. use Foo\Bar\FooBar as FooBaz;
  296. use Zoo\Bar as ZooBar, Zoo\Tar;
  297. use Foo\Bar;
  298. use Foo\Zar\Baz;
  299. use Symfony\Annotation\Template;
  300. use Foo\Bar\Foo as Fooo, Foo\Bir as FBB;
  301. use SomeClass;
  302. use Symfony\Doctrine\Entities\Entity;
  303. trait Foo {}
  304. trait Zoo {}
  305. class AnnotatedClass
  306. {
  307. use Foo, Bar;
  308. /**
  309. * @Template(foobar=21)
  310. * @param Entity $foo
  311. */
  312. public function doSomething($foo)
  313. {
  314. $bar = $foo->toArray();
  315. /** @var ArrayInterface $bar */
  316. return function () use ($bar, $foo) {};
  317. }
  318. }
  319. EOF;
  320. $this->doTest($expected, $input);
  321. }
  322. /**
  323. * @requires PHP 5.4
  324. */
  325. public function testFixWithTraitImports()
  326. {
  327. $expected = <<<'EOF'
  328. The normal
  329. use of this fixer
  330. should not change this sentence nor those statements below
  331. use Zoo\Bar;
  332. use Foo\Bar;
  333. use Foo\Zar\Baz;
  334. <?php
  335. use Acme\MyReusableTrait;
  336. use Foo\Bar, Foo\Bar\Foo as Fooo;
  337. use Foo\Bar\FooBar as FooBaz;
  338. use Foo\Bir as FBB;
  339. use Foo\Zar\Baz;
  340. use SomeClass;
  341. use Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity;
  342. use Zoo\Bar as ZooBar;
  343. $a = new Bar();
  344. $a = new FooBaz();
  345. $a = new someclass();
  346. use Zoo\Tar;
  347. class AnnotatedClass
  348. {
  349. use MyReusableTrait;
  350. /**
  351. * @Template(foobar=21)
  352. * @param Entity $foo
  353. */
  354. public function doSomething($foo)
  355. {
  356. $bar = $foo->toArray();
  357. /** @var ArrayInterface $bar */
  358. return function () use ($bar, $baz) {};
  359. }
  360. }
  361. EOF;
  362. $input = <<<'EOF'
  363. The normal
  364. use of this fixer
  365. should not change this sentence nor those statements below
  366. use Zoo\Bar;
  367. use Foo\Bar;
  368. use Foo\Zar\Baz;
  369. <?php
  370. use Foo\Bar\FooBar as FooBaz;
  371. use Zoo\Bar as ZooBar, Zoo\Tar;
  372. use Foo\Bar;
  373. use Foo\Zar\Baz;
  374. use Acme\MyReusableTrait;
  375. use Symfony\Annotation\Template;
  376. use Foo\Bar\Foo as Fooo, Foo\Bir as FBB;
  377. use SomeClass;
  378. $a = new Bar();
  379. $a = new FooBaz();
  380. $a = new someclass();
  381. use Symfony\Doctrine\Entities\Entity;
  382. class AnnotatedClass
  383. {
  384. use MyReusableTrait;
  385. /**
  386. * @Template(foobar=21)
  387. * @param Entity $foo
  388. */
  389. public function doSomething($foo)
  390. {
  391. $bar = $foo->toArray();
  392. /** @var ArrayInterface $bar */
  393. return function () use ($bar, $baz) {};
  394. }
  395. }
  396. EOF;
  397. $this->doTest($expected, $input);
  398. }
  399. public function testFixWithDifferentCases()
  400. {
  401. $expected = <<<'EOF'
  402. The normal
  403. use of this fixer
  404. should not change this sentence nor those statements below
  405. use Zoo\Baz;
  406. use abc\Bar;
  407. <?php
  408. use abc\Bar;
  409. use Zoo\Baz;
  410. class Test
  411. {
  412. }
  413. EOF;
  414. $input = <<<'EOF'
  415. The normal
  416. use of this fixer
  417. should not change this sentence nor those statements below
  418. use Zoo\Baz;
  419. use abc\Bar;
  420. <?php
  421. use Zoo\Baz;
  422. use abc\Bar;
  423. class Test
  424. {
  425. }
  426. EOF;
  427. $this->doTest($expected, $input);
  428. }
  429. public function testWithoutUses()
  430. {
  431. $expected = <<<'EOF'
  432. <?php
  433. $c = 1;
  434. EOF
  435. ;
  436. $this->doTest($expected);
  437. }
  438. public function testOrderWithTrailingDigit()
  439. {
  440. $expected = <<<'EOF'
  441. <?php
  442. use abc\Bar;
  443. use abc2\Bar2;
  444. use xyz\abc\Bar6;
  445. use xyz\abc2\Bar7;
  446. use xyz\xyz\Bar4;
  447. use xyz\xyz\Bar5;
  448. class Test
  449. {
  450. }
  451. EOF;
  452. $input = <<<'EOF'
  453. <?php
  454. use abc2\Bar2;
  455. use abc\Bar;
  456. use xyz\abc2\Bar7;
  457. use xyz\abc\Bar6;
  458. use xyz\xyz\Bar4;
  459. use xyz\xyz\Bar5;
  460. class Test
  461. {
  462. }
  463. EOF;
  464. $this->doTest($expected, $input);
  465. }
  466. public function testCodeWithImportsOnly()
  467. {
  468. $expected = <<<'EOF'
  469. <?php
  470. use Aaa;
  471. use Bbb;
  472. EOF;
  473. $input = <<<'EOF'
  474. <?php
  475. use Bbb;
  476. use Aaa;
  477. EOF;
  478. $this->doTest($expected, $input);
  479. }
  480. public function testCodeWithCloseTag()
  481. {
  482. $this->doTest(
  483. '<?php
  484. use A\C1;
  485. use A\D?><?php use B\C2; use E\F ?>',
  486. '<?php
  487. use A\C1;
  488. use B\C2?><?php use A\D; use E\F ?>'
  489. );
  490. }
  491. /**
  492. * @param string $expected
  493. * @param null|string $input
  494. *
  495. * @dataProvider provideCommentCases
  496. */
  497. public function testCodeWithComments($expected, $input = null)
  498. {
  499. $this->doTest($expected, $input);
  500. }
  501. public function provideCommentCases()
  502. {
  503. return array(
  504. array(
  505. '<?php
  506. use A\C1 /* A */;
  507. use /* B */ B\C2;',
  508. '<?php
  509. use /* B */ B\C2;
  510. use A\C1 /* A */;',
  511. ),
  512. array(
  513. '<?php
  514. use#
  515. A\C1;
  516. use B#
  517. \C2#
  518. #
  519. ;',
  520. '<?php
  521. use#
  522. B#
  523. \C2#
  524. #
  525. ;
  526. use A\C1;',
  527. ),
  528. );
  529. }
  530. /**
  531. * @param string $expected
  532. * @param null|string $input
  533. *
  534. * @dataProvider provide70Cases
  535. * @requires PHP 7.0
  536. */
  537. public function test70($expected, $input = null)
  538. {
  539. $this->doTest($expected, $input);
  540. }
  541. public function provide70Cases()
  542. {
  543. return array(
  544. array(
  545. '<?php
  546. use A\B;
  547. use some\a\{ClassA, ClassB, ClassC as C};
  548. use some\b\{
  549. ClassF,
  550. ClassG
  551. };
  552. use const some\a\{ConstA, ConstB, ConstC};
  553. use const some\b\{
  554. ConstA,
  555. ConstB,
  556. ConstC
  557. };
  558. use function some\a\{fn_a, fn_b, fn_c};
  559. use function some\b\{
  560. fn_a,
  561. fn_b,
  562. fn_c
  563. };
  564. ',
  565. '<?php
  566. use some\a\{ClassA, ClassB, ClassC as C};
  567. use function some\b\{
  568. fn_b,
  569. fn_c,
  570. fn_a
  571. };
  572. use function some\a\{fn_a, fn_b, fn_c};
  573. use A\B;
  574. use const some\b\{
  575. ConstC,
  576. ConstA,
  577. ConstB
  578. };
  579. use const some\a\{ConstA, ConstB, ConstC};
  580. use some\b\{
  581. ClassG,
  582. ClassF
  583. };
  584. ',
  585. ),
  586. array(
  587. '<?php
  588. use A\B;
  589. use some\a\{ClassA as A /*z*/, ClassB, ClassC};
  590. use const some\a\{
  591. ConstA,
  592. ConstB,
  593. ConstC
  594. };
  595. use function some\a\{fn_a, fn_b, fn_c};
  596. ',
  597. '<?php
  598. use some\a\{ ClassB,ClassC, /*z*/ ClassA as A};
  599. use function some\a\{fn_c, fn_a,fn_b };
  600. use A\B;
  601. use const some\a\{
  602. ConstA,
  603. ConstB,
  604. ConstC
  605. };
  606. ',
  607. ),
  608. array(
  609. '<?php
  610. use Foo\Bar\Baz;use Foo\Bar\{ClassA, ClassB, ClassC};
  611. use Foo\Bir;
  612. ',
  613. '<?php
  614. use Foo\Bar\Baz, Foo\Bir;
  615. use Foo\Bar\{ClassC, ClassB, ClassA};
  616. ',
  617. ),
  618. array(
  619. '<?php
  620. use A\A;use Foo3\Bar\{ClassA};use G\G;use H\H;use Ioo2\Bar\{ClassB};use J\J;use K\K;use Loo1\Bar\{ClassC};use M\M;
  621. ',
  622. '<?php
  623. use A\A,G\G;use Foo3\Bar\{ClassA};use H\H,J\J;use Ioo2\Bar\{ClassB};use K\K,M\M;use Loo1\Bar\{ClassC};
  624. ',
  625. ),
  626. array(
  627. '<?php
  628. use Foo\Bar\Baz;use Foo\Bar\{ClassA, ClassB, ClassC};
  629. use Foo\Bir;
  630. ',
  631. '<?php
  632. use Foo\Bar\Baz, Foo\Bir;
  633. use Foo\Bar\{ClassC, ClassB, ClassA};
  634. ',
  635. ),
  636. array(
  637. '<?php
  638. use Foo\Bar\{ClassA, ClassB, ClassC};
  639. use Foo\Bir\{
  640. ClassD,
  641. ClassE,
  642. ClassF
  643. };
  644. use Foo\Bor\{
  645. ClassG,
  646. ClassH,
  647. ClassI,
  648. ClassJ
  649. };
  650. ',
  651. '<?php
  652. use Foo\Bar\{ClassC, ClassB, ClassA};
  653. use Foo\Bir\{ClassE, ClassF,
  654. ClassD};
  655. use Foo\Bor\{
  656. ClassJ,
  657. ClassI,
  658. ClassH,
  659. ClassG
  660. };
  661. ',
  662. ),
  663. );
  664. }
  665. /*
  666. |--------------------------------------------------------------------------
  667. | Test sorting by length
  668. |--------------------------------------------------------------------------
  669. */
  670. public function testInvalidOrderTypesSize()
  671. {
  672. $this->setExpectedException(
  673. 'PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException',
  674. '[ordered_imports] Invalid configuration: Missing sort type "function".'
  675. );
  676. $this->fixer->configure(array(
  677. 'sortAlgorithm' => OrderedImportsFixer::SORT_ALPHA,
  678. 'importsOrder' => array('class', 'const'),
  679. ));
  680. }
  681. public function testInvalidOrderType()
  682. {
  683. $this->setExpectedException(
  684. 'PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException',
  685. '[ordered_imports] Invalid configuration: Missing sort type "class".'
  686. );
  687. $this->fixer->configure(array(
  688. 'sortAlgorithm' => OrderedImportsFixer::SORT_ALPHA,
  689. 'importsOrder' => array('const', 'function', 'bar'),
  690. ));
  691. }
  692. /**
  693. * @dataProvider provideInvalidSortAlgorithmConfiguration
  694. *
  695. * @param array $configuration
  696. * @param string $expectedValue
  697. */
  698. public function testInvalidSortAlgorithm($configuration, $expectedValue)
  699. {
  700. $this->setExpectedException(
  701. 'PhpCsFixer\ConfigurationException\InvalidFixerConfigurationException',
  702. sprintf(
  703. '[ordered_imports] Invalid configuration: The option "sortAlgorithm" with value %s is invalid. Accepted values are: "alpha", "length".',
  704. $expectedValue
  705. )
  706. );
  707. $this->fixer->configure($configuration);
  708. }
  709. public function provideInvalidSortAlgorithmConfiguration()
  710. {
  711. return array(
  712. array(
  713. array(
  714. 'sortAlgorithm' => 'dope',
  715. 'importsOrder' => null,
  716. ),
  717. '"dope"',
  718. ),
  719. array(
  720. array(
  721. 'sortAlgorithm' => array(OrderedImportsFixer::SORT_ALPHA, OrderedImportsFixer::SORT_LENGTH),
  722. 'importsOrder' => null,
  723. ),
  724. 'array',
  725. ),
  726. array(
  727. array(
  728. 'sortAlgorithm' => new \stdClass(),
  729. 'importsOrder' => null,
  730. ),
  731. 'stdClass',
  732. ),
  733. );
  734. }
  735. public function testFixByLength()
  736. {
  737. $this->fixer->configure(array(
  738. 'sortAlgorithm' => OrderedImportsFixer::SORT_LENGTH,
  739. 'importsOrder' => null,
  740. ));
  741. $expected = <<<'EOF'
  742. The normal
  743. use of this fixer
  744. should not change this sentence nor those statements below
  745. use Zoo\Bar as ZooBar;
  746. use Foo\Bar;
  747. use Foo\Zar\Baz;
  748. <?php
  749. use Foo\Bar;
  750. use Zoo\Tar, SomeClass;
  751. use Foo\Zar\Baz;
  752. use Foo\Bir as FBB;
  753. use Zoo\Bar as ZooBar;
  754. use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar as FooBaz;
  755. use Symfony\Annotation\Template;
  756. $a = new Bar();
  757. $a = new FooBaz();
  758. $a = new someclass();
  759. use Symfony\Doctrine\Entities\Entity;
  760. class AnnotatedClass
  761. {
  762. /**
  763. * @Template(foobar=21)
  764. * @param Entity $foo
  765. */
  766. public function doSomething($foo)
  767. {
  768. $bar = $foo->toArray();
  769. /** @var ArrayInterface $bar */
  770. return function () use ($bar, $foo) {};
  771. }
  772. }
  773. EOF;
  774. $input = <<<'EOF'
  775. The normal
  776. use of this fixer
  777. should not change this sentence nor those statements below
  778. use Zoo\Bar as ZooBar;
  779. use Foo\Bar;
  780. use Foo\Zar\Baz;
  781. <?php
  782. use Foo\Bar\FooBar as FooBaz;
  783. use Zoo\Bar as ZooBar, Zoo\Tar;
  784. use Foo\Bar;
  785. use Foo\Zar\Baz;
  786. use Symfony\Annotation\Template;
  787. use Foo\Bar\Foo as Fooo, Foo\Bir as FBB;
  788. use SomeClass;
  789. $a = new Bar();
  790. $a = new FooBaz();
  791. $a = new someclass();
  792. use Symfony\Doctrine\Entities\Entity;
  793. class AnnotatedClass
  794. {
  795. /**
  796. * @Template(foobar=21)
  797. * @param Entity $foo
  798. */
  799. public function doSomething($foo)
  800. {
  801. $bar = $foo->toArray();
  802. /** @var ArrayInterface $bar */
  803. return function () use ($bar, $foo) {};
  804. }
  805. }
  806. EOF;
  807. $this->doTest($expected, $input);
  808. }
  809. public function testByLengthFixWithSameLength()
  810. {
  811. $this->fixer->configure(array(
  812. 'sortAlgorithm' => OrderedImportsFixer::SORT_LENGTH,
  813. 'importsOrder' => null,
  814. ));
  815. $expected = <<<'EOF'
  816. <?php
  817. use Acme;
  818. use Bar1;
  819. use Barr;
  820. use Fooo;
  821. class AnnotatedClass
  822. {
  823. /**
  824. * @Template(foobar=21)
  825. * @param Entity $foo
  826. */
  827. public function doSomething($foo)
  828. {
  829. $bar = $foo->toArray();
  830. /** @var ArrayInterface $bar */
  831. return function () use ($bar, $foo) {};
  832. }
  833. }
  834. EOF;
  835. $input = <<<'EOF'
  836. <?php
  837. use Acme;
  838. use Fooo;
  839. use Barr;
  840. use Bar1;
  841. class AnnotatedClass
  842. {
  843. /**
  844. * @Template(foobar=21)
  845. * @param Entity $foo
  846. */
  847. public function doSomething($foo)
  848. {
  849. $bar = $foo->toArray();
  850. /** @var ArrayInterface $bar */
  851. return function () use ($bar, $foo) {};
  852. }
  853. }
  854. EOF;
  855. $this->doTest($expected, $input);
  856. }
  857. public function testByLengthFixWithMultipleNamespace()
  858. {
  859. $this->fixer->configure(array(
  860. 'sortAlgorithm' => OrderedImportsFixer::SORT_LENGTH,
  861. 'importsOrder' => null,
  862. ));
  863. $expected = <<<'EOF'
  864. <?php
  865. namespace FooRoo {
  866. use Foo\Bar;
  867. use Zoo\Tar1, Zoo\Tar2;
  868. use SomeClass;
  869. use Foo\Zar\Baz;
  870. use Foo\Bir as FBB;
  871. use Zoo\Bar as ZooBar, Foo\Bar\Foo as Fooo;
  872. use Foo\Bar\FooBar as FooBaz;
  873. $a = new Bar();
  874. $a = new FooBaz();
  875. $a = new someclass();
  876. use Symfony\Annotation\Template;
  877. class AnnotatedClass
  878. {
  879. /**
  880. * @Template(foobar=21)
  881. * @param Entity $foo
  882. */
  883. public function doSomething($foo)
  884. {
  885. $bar = $foo->toArray();
  886. /** @var ArrayInterface $bar */
  887. return function () use ($bar, $foo) {};
  888. }
  889. }
  890. }
  891. namespace BlaRoo {
  892. use Zoo\Bar;
  893. use SomeClass;
  894. use Foo\Zar\Baz;
  895. use Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity;
  896. class AnnotatedClass
  897. {
  898. /**
  899. * @Template(foobar=21)
  900. * @param Entity $foo
  901. */
  902. public function doSomething($foo)
  903. {
  904. $bar = $foo->toArray();
  905. /** @var ArrayInterface $bar */
  906. return function () use ($bar, $foo) {};
  907. }
  908. }
  909. }
  910. EOF;
  911. $input = <<<'EOF'
  912. <?php
  913. namespace FooRoo {
  914. use Foo\Bar\FooBar as FooBaz;
  915. use Zoo\Bar as ZooBar, Zoo\Tar1;
  916. use Foo\Bar;
  917. use Foo\Zar\Baz;
  918. use Symfony\Annotation\Template;
  919. use Foo\Bar\Foo as Fooo, Foo\Bir as FBB;
  920. use SomeClass;
  921. $a = new Bar();
  922. $a = new FooBaz();
  923. $a = new someclass();
  924. use Zoo\Tar2;
  925. class AnnotatedClass
  926. {
  927. /**
  928. * @Template(foobar=21)
  929. * @param Entity $foo
  930. */
  931. public function doSomething($foo)
  932. {
  933. $bar = $foo->toArray();
  934. /** @var ArrayInterface $bar */
  935. return function () use ($bar, $foo) {};
  936. }
  937. }
  938. }
  939. namespace BlaRoo {
  940. use Foo\Zar\Baz;
  941. use Zoo\Bar;
  942. use SomeClass;
  943. use Symfony\Annotation\Template, Symfony\Doctrine\Entities\Entity;
  944. class AnnotatedClass
  945. {
  946. /**
  947. * @Template(foobar=21)
  948. * @param Entity $foo
  949. */
  950. public function doSomething($foo)
  951. {
  952. $bar = $foo->toArray();
  953. /** @var ArrayInterface $bar */
  954. return function () use ($bar, $foo) {};
  955. }
  956. }
  957. }
  958. EOF;
  959. $this->doTest($expected, $input);
  960. }
  961. public function testByLengthFixWithComment()
  962. {
  963. $this->fixer->configure(array(
  964. 'sortAlgorithm' => OrderedImportsFixer::SORT_LENGTH,
  965. 'importsOrder' => null,
  966. ));
  967. $expected = <<<'EOF'
  968. The normal
  969. use of this fixer
  970. should not change this sentence nor those statements below
  971. use Zoo\Bar;
  972. use Foo\Bar;
  973. use Foo\Zar\Baz;
  974. <?php
  975. use Foo\Bar;
  976. use Zoo\Tar, SomeClass;
  977. use Foo\Zar\Baz;
  978. use Foo\Bir as FBB;
  979. use Zoo\Bar as ZooBar;
  980. use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar /* He there */ as FooBaz;
  981. use /* FIXME */Symfony\Annotation\Template;
  982. $a = new Bar();
  983. $a = new FooBaz();
  984. $a = new someclass();
  985. use Symfony\Doctrine\Entities\Entity;
  986. class AnnotatedClass
  987. {
  988. /**
  989. * @Template(foobar=21)
  990. * @param Entity $foo
  991. */
  992. public function doSomething($foo)
  993. {
  994. $bar = $foo->toArray();
  995. /** @var ArrayInterface $bar */
  996. return function () use ($bar, $foo) {};
  997. }
  998. }
  999. EOF;
  1000. $input = <<<'EOF'
  1001. The normal
  1002. use of this fixer
  1003. should not change this sentence nor those statements below
  1004. use Zoo\Bar;
  1005. use Foo\Bar;
  1006. use Foo\Zar\Baz;
  1007. <?php
  1008. use Foo\Bar\FooBar /* He there */ as FooBaz;
  1009. use Zoo\Bar as ZooBar, Zoo\Tar;
  1010. use Foo\Bar;
  1011. use Foo\Zar\Baz;
  1012. use /* FIXME */Symfony\Annotation\Template;
  1013. use Foo\Bar\Foo as Fooo, Foo\Bir as FBB;
  1014. use SomeClass;
  1015. $a = new Bar();
  1016. $a = new FooBaz();
  1017. $a = new someclass();
  1018. use Symfony\Doctrine\Entities\Entity;
  1019. class AnnotatedClass
  1020. {
  1021. /**
  1022. * @Template(foobar=21)
  1023. * @param Entity $foo
  1024. */
  1025. public function doSomething($foo)
  1026. {
  1027. $bar = $foo->toArray();
  1028. /** @var ArrayInterface $bar */
  1029. return function () use ($bar, $foo) {};
  1030. }
  1031. }
  1032. EOF;
  1033. $this->doTest($expected, $input);
  1034. }
  1035. /**
  1036. * @requires PHP 5.4
  1037. */
  1038. public function testByLength54()
  1039. {
  1040. $this->fixer->configure(array(
  1041. 'sortAlgorithm' => OrderedImportsFixer::SORT_LENGTH,
  1042. 'importsOrder' => null,
  1043. ));
  1044. $expected = <<<'EOF'
  1045. <?php
  1046. use Foo\Bar;
  1047. use Zoo\Tar, SomeClass;
  1048. use Foo\Zar\Baz;
  1049. use Foo\Bir as FBB;
  1050. use Zoo\Bar as ZooBar;
  1051. use Foo\Bar\Foo as Fooo, Foo\Bar\FooBar as FooBaz;
  1052. use Symfony\Annotation\Template;
  1053. use Symfony\Doctrine\Entities\Entity;
  1054. trait Foo {}
  1055. trait Zoo {}
  1056. class AnnotatedClass
  1057. {
  1058. use Foo, Bar;
  1059. /**
  1060. * @Template(foobar=21)
  1061. * @param Entity $foo
  1062. */
  1063. public function doSomething($foo)
  1064. {
  1065. $bar = $foo->toArray();
  1066. /** @var ArrayInterface $bar */
  1067. return function () use ($bar, $foo) {};
  1068. }
  1069. }
  1070. EOF;
  1071. $input = <<<'EOF'
  1072. <?php
  1073. use Foo\Bar\FooBar as FooBaz;
  1074. use Zoo\Bar as ZooBar, Zoo\Tar;
  1075. use Foo\Bar;
  1076. use Foo\Zar\Baz;
  1077. use Symfony\Annotation\Template;
  1078. use Foo\Bar\Foo as Fooo, Foo\Bir as FBB;
  1079. use SomeClass;
  1080. use Symfony\Doctrine\Entities\Entity;
  1081. trait Foo {}
  1082. trait Zoo {}
  1083. class AnnotatedClass
  1084. {
  1085. use Foo, Bar;
  1086. /**
  1087. * @Template(foobar=21)
  1088. * @param Entity $foo
  1089. */
  1090. public function doSomething($foo)
  1091. {
  1092. $bar = $foo->toArray();
  1093. /** @var ArrayInterface $bar */
  1094. return function () use ($bar, $foo) {};
  1095. }
  1096. }
  1097. EOF;
  1098. $this->doTest($expected, $input);
  1099. }
  1100. /**
  1101. * @requires PHP 5.4
  1102. */
  1103. public function testByLengthFixWithTraitImports()
  1104. {
  1105. $this->fixer->configure(array(
  1106. 'sortAlgorithm' => OrderedImportsFixer::SORT_LENGTH,
  1107. 'importsOrder' => null,
  1108. ));
  1109. $expected = <<<'EOF'
  1110. The normal
  1111. use of this fixer
  1112. should not change this sentence nor those statements below
  1113. use Zoo\Bar;
  1114. use Foo\Bar;
  1115. use Foo\Zar\Baz;
  1116. <?php
  1117. use Foo\Bar;
  1118. use Zoo\Tar, SomeClass;
  1119. use Foo\Zar\Baz;
  1120. use Foo\Bir as FBB;
  1121. use Zoo\Bar as ZooBar;
  1122. use Foo\Bar\Foo as Fooo;
  1123. use Acme\MyReusableTrait, Foo\Bar\FooBar as FooBaz;
  1124. use Symfony\Annotation\Template;
  1125. $a = new Bar();
  1126. $a = new FooBaz();
  1127. $a = new someclass();
  1128. use Symfony\Doctrine\Entities\Entity;
  1129. class AnnotatedClass
  1130. {
  1131. use MyReusableTrait;
  1132. /**
  1133. * @Template(foobar=21)
  1134. * @param Entity $foo
  1135. */
  1136. public function doSomething($foo)
  1137. {
  1138. $bar = $foo->toArray();
  1139. /** @var ArrayInterface $bar */
  1140. return function () use ($bar, $baz) {};
  1141. }
  1142. }
  1143. EOF;
  1144. $input = <<<'EOF'
  1145. The normal
  1146. use of this fixer
  1147. should not change this sentence nor those statements below
  1148. use Zoo\Bar;
  1149. use Foo\Bar;
  1150. use Foo\Zar\Baz;
  1151. <?php
  1152. use Foo\Bar\FooBar as FooBaz;
  1153. use Zoo\Bar as ZooBar, Zoo\Tar;
  1154. use Foo\Bar;
  1155. use Foo\Zar\Baz;
  1156. use Acme\MyReusableTrait;
  1157. use Symfony\Annotation\Template;
  1158. use Foo\Bar\Foo as Fooo, Foo\Bir as FBB;
  1159. use SomeClass;
  1160. $a = new Bar();
  1161. $a = new FooBaz();
  1162. $a = new someclass();
  1163. use Symfony\Doctrine\Entities\Entity;
  1164. class AnnotatedClass
  1165. {
  1166. use MyReusableTrait;
  1167. /**
  1168. * @Template(foobar=21)
  1169. * @param Entity $foo
  1170. */
  1171. public function doSomething($foo)
  1172. {
  1173. $bar = $foo->toArray();
  1174. /** @var ArrayInterface $bar */
  1175. return function () use ($bar, $baz) {};
  1176. }
  1177. }
  1178. EOF;
  1179. $this->doTest($expected, $input);
  1180. }
  1181. public function testByLengthFixWithDifferentCases()
  1182. {
  1183. $this->fixer->configure(array(
  1184. 'sortAlgorithm' => OrderedImportsFixer::SORT_LENGTH,
  1185. 'importsOrder' => null,
  1186. ));
  1187. $expected = <<<'EOF'
  1188. The normal
  1189. use of this fixer
  1190. should not change this sentence nor those statements below
  1191. use Zoo\Baz;
  1192. use abc\Bar;
  1193. <?php
  1194. use abc\Bar;
  1195. use Zoo\Baz;
  1196. class Test
  1197. {
  1198. }
  1199. EOF;
  1200. $input = <<<'EOF'
  1201. The normal
  1202. use of this fixer
  1203. should not change this sentence nor those statements below
  1204. use Zoo\Baz;
  1205. use abc\Bar;
  1206. <?php
  1207. use Zoo\Baz;
  1208. use abc\Bar;
  1209. class Test
  1210. {
  1211. }
  1212. EOF;
  1213. $this->doTest($expected, $input);
  1214. }
  1215. public function testByLengthOrderWithTrailingDigit()
  1216. {
  1217. $this->fixer->configure(array(
  1218. 'sortAlgorithm' => OrderedImportsFixer::SORT_LENGTH,
  1219. 'importsOrder' => null,
  1220. ));
  1221. $expected = <<<'EOF'
  1222. <?php
  1223. use abc\Bar;
  1224. use abc2\Bar2;
  1225. use xyz\abc\Bar6;
  1226. use xyz\xyz\Bar4;
  1227. use xyz\xyz\Bar5;
  1228. use xyz\abc2\Bar7;
  1229. class Test
  1230. {
  1231. }
  1232. EOF;
  1233. $input = <<<'EOF'
  1234. <?php
  1235. use abc2\Bar2;
  1236. use abc\Bar;
  1237. use xyz\abc2\Bar7;
  1238. use xyz\abc\Bar6;
  1239. use xyz\xyz\Bar4;
  1240. use xyz\xyz\Bar5;
  1241. class Test
  1242. {
  1243. }
  1244. EOF;
  1245. $this->doTest($expected, $input);
  1246. }
  1247. public function testByLengthCodeWithImportsOnly()
  1248. {
  1249. $this->fixer->configure(array(
  1250. 'sortAlgorithm' => OrderedImportsFixer::SORT_LENGTH,
  1251. 'importsOrder' => null,
  1252. ));
  1253. $expected = <<<'EOF'
  1254. <?php
  1255. use Aaa;
  1256. use Bbb;
  1257. EOF;
  1258. $input = <<<'EOF'
  1259. <?php
  1260. use Bbb;
  1261. use Aaa;
  1262. EOF;
  1263. $this->doTest($expected, $input);
  1264. }
  1265. public function testByLengthWithoutUses()
  1266. {
  1267. $this->fixer->configure(array(
  1268. 'sortAlgorithm' => OrderedImportsFixer::SORT_LENGTH,
  1269. 'importsOrder' => null,
  1270. ));
  1271. $expected = <<<'EOF'
  1272. <?php
  1273. $c = 1;
  1274. EOF
  1275. ;
  1276. $this->doTest($expected);
  1277. }
  1278. /**
  1279. * @param string $expected
  1280. * @param null|string $input
  1281. *
  1282. * @dataProvider provide70CasesByLength
  1283. * @requires PHP 7.0
  1284. */
  1285. public function test70ByLength($expected, $input = null)
  1286. {
  1287. $this->fixer->configure(array(
  1288. 'sortAlgorithm' => OrderedImportsFixer::SORT_LENGTH,
  1289. 'importsOrder' => null,
  1290. ));
  1291. $this->doTest($expected, $input);
  1292. }
  1293. public function provide70CasesByLength()
  1294. {
  1295. return array(
  1296. array(
  1297. '<?php
  1298. use A\B;
  1299. use Foo\Bar\Biz;
  1300. use some\b\{
  1301. ClassF,
  1302. ClassG
  1303. };
  1304. use function some\a\{fn_a, fn_b, fn_c};
  1305. use some\b\{ClassA, ClassB, ClassC as C};
  1306. use const some\a\{ConstA, ConstB, ConstC};
  1307. use some\a\{ClassA as A /*z*/, ClassB, ClassC};
  1308. use Some\Biz\Barz\Boozz\Foz\Which\Is\Really\Long;
  1309. use const some\b\{ConstG, ConstX, ConstY, ConstZ};
  1310. use some\c\{ClassR, ClassT, ClassV as V, NiceClassName};
  1311. ',
  1312. '<?php
  1313. use function some\a\{fn_a, fn_b, fn_c};
  1314. use Foo\Bar\Biz;
  1315. use some\c\{ClassR, ClassT, ClassV as V, NiceClassName};
  1316. use A\B;
  1317. use Some\Biz\Barz\Boozz\Foz\Which\Is\Really\Long;
  1318. use some\b\{
  1319. ClassF,
  1320. ClassG
  1321. };
  1322. use const some\a\{ConstB, ConstA, ConstC};
  1323. use const some\b\{ConstX, ConstY, ConstZ, ConstG};
  1324. use some\b\{ClassA, ClassB, ClassC as C};
  1325. use some\a\{ ClassB,ClassC, /*z*/ ClassA as A};
  1326. ',
  1327. ),
  1328. );
  1329. }
  1330. /**
  1331. * @dataProvider provide70TypesOrderAndLength
  1332. * @requires PHP 7.0
  1333. *
  1334. * @param string $expected
  1335. * @param null|string $input
  1336. */
  1337. public function test70TypesOrderAndLength($expected, $input = null)
  1338. {
  1339. $this->fixer->configure(array(
  1340. 'sortAlgorithm' => OrderedImportsFixer::SORT_LENGTH,
  1341. 'importsOrder' => array(OrderedImportsFixer::IMPORT_TYPE_CLASS, OrderedImportsFixer::IMPORT_TYPE_CONST, OrderedImportsFixer::IMPORT_TYPE_FUNCTION),
  1342. ));
  1343. $this->doTest($expected, $input);
  1344. }
  1345. public function provide70TypesOrderAndLength()
  1346. {
  1347. return array(
  1348. array(
  1349. '<?php
  1350. use A\B;
  1351. use Some\Bar;
  1352. use Foo\Zar\Baz;
  1353. use some\b\{
  1354. ClassF,
  1355. ClassG
  1356. };
  1357. use some\a\{ClassA, ClassB, ClassC as C};
  1358. use some\b\{ClassK, ClassL, ClassM as M};
  1359. use some\a\{ClassA as A /*z*/, ClassB, ClassC};
  1360. use const some\a\{ConstA, ConstB, ConstC};
  1361. use const some\b\{ConstD, ConstE, ConstF};
  1362. use function some\a\{fn_a, fn_b};
  1363. use function some\f\{fn_c, fn_d, fn_e};
  1364. use function some\b\{fn_k, fn_l, func_m};
  1365. ',
  1366. '<?php
  1367. use const some\a\{ConstA, ConstB, ConstC};
  1368. use some\a\{ClassA, ClassB, ClassC as C};
  1369. use Foo\Zar\Baz;
  1370. use some\b\{ClassK, ClassL, ClassM as M};
  1371. use some\a\{ClassA as A /*z*/, ClassB, ClassC};
  1372. use A\B;
  1373. use some\b\{
  1374. ClassF,
  1375. ClassG
  1376. };
  1377. use function some\b\{fn_k, fn_l, func_m};
  1378. use Some\Bar;
  1379. use function some\a\{fn_a, fn_b};
  1380. use const some\b\{ConstD, ConstE, ConstF};
  1381. use function some\f\{fn_c, fn_d, fn_e};
  1382. ',
  1383. ),
  1384. );
  1385. }
  1386. /**
  1387. * @dataProvider provide70TypesOrderAndAlphabet
  1388. * @requires PHP 7.0
  1389. *
  1390. * @param string $expected
  1391. * @param null|string $input
  1392. * @param string[] $importOrder
  1393. */
  1394. public function test70TypesOrderAndAlphabet($expected, $input = null, array $importOrder = null)
  1395. {
  1396. $this->fixer->configure(array(
  1397. 'sortAlgorithm' => OrderedImportsFixer::SORT_ALPHA,
  1398. 'importsOrder' => $importOrder,
  1399. ));
  1400. $this->doTest($expected, $input);
  1401. }
  1402. public function provide70TypesOrderAndAlphabet()
  1403. {
  1404. return array(
  1405. array(
  1406. '<?php
  1407. use Aaa\Bbb;
  1408. use Aaa\Ccc;
  1409. use Bar\Biz\Boooz\Bum;
  1410. use Foo\Zar\Baz;
  1411. use some\a\{ClassA};
  1412. use some\b\{
  1413. ClassF,
  1414. ClassG
  1415. };
  1416. use some\b\{ClassB, ClassC as C, ClassD};
  1417. use Some\Cloz;
  1418. use const some\a\{ConstA};
  1419. use const some\a\{ConstB, ConstC as CC};
  1420. use const some\a\{ConstD};
  1421. use const some\b\{ConstE};
  1422. use function some\a\{fn_a, fn_b};
  1423. use function some\a\{fn};
  1424. use function some\b\{fn_c, fn_d, fn_e};
  1425. use function some\c\{fn_f};
  1426. use function some\f\{fn_g, fn_h, fn_i};
  1427. ',
  1428. '<?php
  1429. use Aaa\Ccc;
  1430. use Foo\Zar\Baz;
  1431. use function some\f\{fn_g, fn_h, fn_i};
  1432. use some\a\{ClassA};
  1433. use some\b\{ClassD, ClassB, ClassC as C};
  1434. use Bar\Biz\Boooz\Bum;
  1435. use function some\c\{fn_f};
  1436. use some\b\{
  1437. ClassF,
  1438. ClassG
  1439. };
  1440. use const some\a\{ConstD};
  1441. use Some\Cloz;
  1442. use function some\a\{fn};
  1443. use const some\a\{ConstA};
  1444. use function some\b\{fn_c, fn_d, fn_e};
  1445. use const some\a\{ConstB, ConstC as CC};
  1446. use Aaa\Bbb;
  1447. use const some\b\{ConstE};
  1448. use function some\a\{fn_a, fn_b};
  1449. ',
  1450. array(OrderedImportsFixer::IMPORT_TYPE_CLASS, OrderedImportsFixer::IMPORT_TYPE_CONST, OrderedImportsFixer::IMPORT_TYPE_FUNCTION),
  1451. ),
  1452. );
  1453. }
  1454. }