SingleLineAfterImportsFixerTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  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\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\WhitespacesFixerConfig;
  15. /**
  16. * @author Ceeram <ceeram@cakephp.org>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\Import\SingleLineAfterImportsFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Import\SingleLineAfterImportsFixer>
  23. */
  24. final class SingleLineAfterImportsFixerTest extends AbstractFixerTestCase
  25. {
  26. /**
  27. * @dataProvider provideFixCases
  28. */
  29. public function testFix(string $expected, ?string $input = null): void
  30. {
  31. $this->doTest($expected, $input);
  32. }
  33. /**
  34. * @return iterable<int|string, array{0: string, 1?: string}>
  35. */
  36. public static function provideFixCases(): iterable
  37. {
  38. yield [
  39. '<?php
  40. use D;
  41. use E;
  42. use DP; /**/
  43. use EZ; //
  44. use DAZ;
  45. use EGGGG; /**/
  46. use A\B;
  47. use C\DE;
  48. use E\F;
  49. use G\H;
  50. ',
  51. '<?php
  52. use D; use E;
  53. use DP; /**/ use EZ; //
  54. use DAZ; use EGGGG; /**/
  55. use A\B;
  56. use C\DE;
  57. use E\F;
  58. use G\H;
  59. ',
  60. ];
  61. yield [
  62. '<?php use \Exception;
  63. ?>
  64. <?php
  65. $a = new Exception();
  66. ',
  67. '<?php use \Exception?>
  68. <?php
  69. $a = new Exception();
  70. ',
  71. ];
  72. yield [
  73. '<?php use \stdClass;
  74. use \DateTime;
  75. ?>
  76. <?php
  77. $a = new DateTime();
  78. ',
  79. '<?php use \stdClass; use \DateTime?>
  80. <?php
  81. $a = new DateTime();
  82. ',
  83. ];
  84. yield [
  85. '<?php namespace Foo;
  86. '.'
  87. use Bar\Baz;
  88. /**
  89. * Foo.
  90. */',
  91. '<?php namespace Foo;
  92. '.'
  93. use Bar\Baz;
  94. /**
  95. * Foo.
  96. */',
  97. ];
  98. yield [
  99. '<?php
  100. namespace A\B;
  101. use D;
  102. class C {}
  103. ',
  104. '<?php
  105. namespace A\B;
  106. use D;
  107. class C {}
  108. ',
  109. ];
  110. yield [
  111. '<?php
  112. namespace A\B;
  113. use D;
  114. class C {}
  115. ',
  116. '<?php
  117. namespace A\B;
  118. use D;
  119. class C {}
  120. ',
  121. ];
  122. yield [
  123. '<?php
  124. namespace A\B;
  125. use D;
  126. use E;
  127. class C {}
  128. ',
  129. '<?php
  130. namespace A\B;
  131. use D;
  132. use E;
  133. class C {}
  134. ',
  135. ];
  136. yield [
  137. '<?php
  138. namespace A\B;
  139. use D;
  140. class C {}
  141. ',
  142. '<?php
  143. namespace A\B;
  144. use D; class C {}
  145. ',
  146. ];
  147. yield [
  148. '<?php
  149. namespace A\B;
  150. use D;
  151. use E;
  152. {
  153. class C {}
  154. }',
  155. '<?php
  156. namespace A\B;
  157. use D; use E; {
  158. class C {}
  159. }',
  160. ];
  161. yield [
  162. '<?php
  163. namespace A\B;
  164. use D;
  165. use E;
  166. {
  167. class C {}
  168. }',
  169. '<?php
  170. namespace A\B;
  171. use D;
  172. use E; {
  173. class C {}
  174. }',
  175. ];
  176. yield [
  177. '<?php
  178. namespace A\B {
  179. use D;
  180. use E;
  181. class C {}
  182. }',
  183. '<?php
  184. namespace A\B {
  185. use D; use E; class C {}
  186. }',
  187. ];
  188. yield [
  189. '<?php
  190. namespace A\B;
  191. class C {
  192. use SomeTrait;
  193. }',
  194. ];
  195. yield [
  196. '<?php
  197. $lambda = function () use (
  198. $arg
  199. ){
  200. return true;
  201. };',
  202. ];
  203. yield [
  204. '<?php
  205. namespace A\B;
  206. use D, E;
  207. class C {
  208. }',
  209. '<?php
  210. namespace A\B;
  211. use D, E;
  212. class C {
  213. }',
  214. ];
  215. yield [
  216. '<?php
  217. namespace A1;
  218. use B1; // need to import this !
  219. use B2;
  220. class C1 {}
  221. ',
  222. ];
  223. yield [
  224. '<?php
  225. namespace A2;
  226. use B2;// need to import this !
  227. use B3;
  228. class C4 {}
  229. ',
  230. ];
  231. yield [
  232. '<?php
  233. namespace A1;
  234. use B1; // need to import this !
  235. use B2;
  236. class C1 {}
  237. ',
  238. ];
  239. yield [
  240. '<?php
  241. namespace A1;
  242. use B1;// need to import this !
  243. use B2;
  244. class C1 {}
  245. ',
  246. ];
  247. yield [
  248. '<?php
  249. namespace A1;
  250. use B1; /** need to import this !*/
  251. use B2;
  252. class C1 {}
  253. ',
  254. ];
  255. yield [
  256. '<?php
  257. namespace A1;
  258. use B1;# need to import this !
  259. use B2;
  260. class C1 {}
  261. ',
  262. ];
  263. yield [
  264. '<?php
  265. namespace Foo;
  266. use Bar;
  267. use Baz;
  268. class Hello {}
  269. ',
  270. '<?php
  271. namespace Foo;
  272. use Bar;
  273. use Baz;
  274. class Hello {}
  275. ',
  276. ];
  277. yield [
  278. '<?php
  279. class HelloTrait {
  280. use SomeTrait;
  281. use Another;// ensure use statements for traits are not touched
  282. }
  283. ',
  284. ];
  285. yield [
  286. '<?php
  287. namespace Foo {}
  288. namespace Bar {
  289. class Baz
  290. {
  291. use Aaa;
  292. }
  293. }
  294. ',
  295. ];
  296. yield [
  297. '<?php use A\B;
  298. ?>',
  299. '<?php use A\B?>',
  300. ];
  301. yield [
  302. '<?php use A\B;
  303. ',
  304. '<?php use A\B;',
  305. ];
  306. yield [
  307. str_replace("\n", "\r\n", '<?php
  308. use Foo;
  309. use Bar;
  310. class Baz {}
  311. '),
  312. ];
  313. yield [
  314. '<?php
  315. use some\test\{ClassA, ClassB, ClassC as C};
  316. ?>
  317. test 123
  318. ',
  319. '<?php
  320. use some\test\{ClassA, ClassB, ClassC as C} ?>
  321. test 123
  322. ',
  323. ];
  324. yield [
  325. '<?php
  326. use some\test\{CA, Cl, ClassC as C};
  327. class Test {}
  328. ',
  329. '<?php
  330. use some\test\{CA, Cl, ClassC as C};
  331. class Test {}
  332. ',
  333. ];
  334. yield [
  335. '<?php
  336. use function some\test\{fn_g, fn_f, fn_e};
  337. fn_a();',
  338. '<?php
  339. use function some\test\{fn_g, fn_f, fn_e};
  340. fn_a();',
  341. ];
  342. yield [
  343. '<?php
  344. use const some\test\{ConstA, ConstB, ConstD};
  345. ',
  346. '<?php
  347. use const some\test\{ConstA, ConstB, ConstD};
  348. ',
  349. ];
  350. yield [
  351. '<?php
  352. namespace Z\B;
  353. use const some\test\{ConstA, ConstB, ConstC};
  354. use A\B\C;
  355. ',
  356. '<?php
  357. namespace Z\B;
  358. use const some\test\{ConstA, ConstB, ConstC};
  359. use A\B\C;
  360. ',
  361. ];
  362. yield [
  363. ' <?php
  364. use some\a\ClassA;
  365. use function some\a\fn_a;
  366. use const some\c;
  367. ',
  368. ' <?php
  369. use some\a\ClassA; use function some\a\fn_a; use const some\c;
  370. ',
  371. ];
  372. yield [
  373. "<?php use some\\a\\{ClassA,};\n\n",
  374. '<?php use some\a\{ClassA,};',
  375. ];
  376. yield [
  377. "<?php use some\\a\\{ClassA};\nuse some\\b\\{ClassB};\n\n",
  378. '<?php use some\a\{ClassA};use some\b\{ClassB};',
  379. ];
  380. yield [
  381. "<?php use some\\a\\{ClassA};\nuse const some\\b\\{ClassB};\n\n",
  382. '<?php use some\a\{ClassA};use const some\b\{ClassB};',
  383. ];
  384. yield [
  385. "<?php use some\\a\\{ClassA, ClassZ};\nuse const some\\b\\{ClassB, ClassX};\nuse function some\\d;\n\n",
  386. '<?php use some\a\{ClassA, ClassZ};use const some\b\{ClassB, ClassX};use function some\d;',
  387. ];
  388. $imports = [
  389. 'some\a\{ClassA, ClassB, ClassC as C,};',
  390. 'function some\a\{fn_a, fn_b, fn_c,};',
  391. 'const some\a\{ConstA,ConstB,ConstC,};',
  392. 'const some\Z\{ConstX,ConstY,ConstZ,};',
  393. ];
  394. yield 'group types with trailing comma' => [
  395. "<?php\nuse ".implode("\nuse ", $imports)."\n\necho 1;",
  396. "<?php\nuse ".implode('use ', $imports).' echo 1;',
  397. ];
  398. foreach ($imports as $import) {
  399. $case = [
  400. "<?php\nuse ".$import."\n\necho 1;",
  401. "<?php\nuse ".$import.' echo 1;',
  402. ];
  403. yield [
  404. str_replace('some', '\some', $case[0]),
  405. str_replace('some', '\some', $case[1]),
  406. ];
  407. yield $case;
  408. }
  409. }
  410. /**
  411. * @dataProvider provideWithWhitespacesConfigCases
  412. */
  413. public function testWithWhitespacesConfig(string $expected, ?string $input = null): void
  414. {
  415. $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n"));
  416. $this->doTest($expected, $input);
  417. }
  418. /**
  419. * @return iterable<array{string, string}>
  420. */
  421. public static function provideWithWhitespacesConfigCases(): iterable
  422. {
  423. yield [
  424. "<?php namespace A\\B;\r\n use D;\r\n\r\n class C {}",
  425. "<?php namespace A\\B;\r\n use D;\r\n\r\n\r\n class C {}",
  426. ];
  427. yield [
  428. "<?php namespace A\\B;\r\n use D;\r\n\r\n class C {}",
  429. "<?php namespace A\\B;\r\n use D;\r\n class C {}",
  430. ];
  431. }
  432. }