NoLeadingImportSlashFixerTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. /**
  15. * @author Carlos Cirello <carlos.cirello.nl@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\Import\NoLeadingImportSlashFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Import\NoLeadingImportSlashFixer>
  22. */
  23. final class NoLeadingImportSlashFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. */
  28. public function testFix(string $expected, ?string $input = null): void
  29. {
  30. $this->doTest($expected, $input);
  31. }
  32. /**
  33. * @return iterable<array{0: string, 1?: string}>
  34. */
  35. public static function provideFixCases(): iterable
  36. {
  37. yield [
  38. '<?php
  39. use A\B;
  40. ',
  41. '<?php
  42. use \A\B;
  43. ',
  44. ];
  45. yield [
  46. '<?php
  47. use/*1*/A\C;
  48. ',
  49. '<?php
  50. use/*1*/\A\C;
  51. ',
  52. ];
  53. yield [
  54. '<?php
  55. $a = function(\B\C $a) use ($b){
  56. };
  57. ',
  58. ];
  59. yield [
  60. '<?php
  61. namespace NS;
  62. use A\B;
  63. ',
  64. '<?php
  65. namespace NS;
  66. use \A\B;
  67. ',
  68. ];
  69. yield [
  70. '<?php
  71. namespace NS{
  72. use A\B;
  73. }
  74. namespace NS2{
  75. use C\D;
  76. }
  77. ',
  78. '<?php
  79. namespace NS{
  80. use \A\B;
  81. }
  82. namespace NS2{
  83. use \C\D;
  84. }
  85. ',
  86. ];
  87. yield [
  88. '<?php
  89. namespace Foo {
  90. use A;
  91. use A\X;
  92. new X();
  93. }
  94. namespace Bar {
  95. use B;
  96. use B\X;
  97. new X();
  98. }
  99. ',
  100. '<?php
  101. namespace Foo {
  102. use \A;
  103. use \A\X;
  104. new X();
  105. }
  106. namespace Bar {
  107. use \B;
  108. use \B\X;
  109. new X();
  110. }
  111. ',
  112. ];
  113. yield [
  114. '<?php
  115. namespace Foo\Bar;
  116. use Baz;
  117. class Foo implements Baz {}
  118. ',
  119. '<?php
  120. namespace Foo\Bar;
  121. use \Baz;
  122. class Foo implements Baz {}
  123. ',
  124. ];
  125. yield [
  126. '<?php
  127. trait SomeTrait {
  128. use \A;
  129. }
  130. ',
  131. ];
  132. yield [
  133. '<?php
  134. namespace NS{
  135. use A\B;
  136. trait Tr8A{
  137. use \B, \C;
  138. }
  139. }
  140. namespace NS2{
  141. use C\D;
  142. }
  143. ',
  144. '<?php
  145. namespace NS{
  146. use \A\B;
  147. trait Tr8A{
  148. use \B, \C;
  149. }
  150. }
  151. namespace NS2{
  152. use \C\D;
  153. }
  154. ',
  155. ];
  156. yield [
  157. '<?php
  158. trait Foo {}
  159. class Bar {
  160. use \Foo;
  161. }
  162. ',
  163. ];
  164. yield [
  165. '<?php
  166. use function a\b;
  167. use const d\e;
  168. ',
  169. '<?php
  170. use function \a\b;
  171. use const \d\e;
  172. ',
  173. ];
  174. yield [
  175. '<?php
  176. namespace AAA;
  177. use some\a\{ClassA, ClassB, ClassC as C,};
  178. use function some\a\{fn_a, fn_b, fn_c,};
  179. use const some\a\{ConstA,ConstB,ConstC
  180. ,
  181. };
  182. use const some\Z\{ConstX,ConstY,ConstZ,};
  183. ',
  184. '<?php
  185. namespace AAA;
  186. use \some\a\{ClassA, ClassB, ClassC as C,};
  187. use function \some\a\{fn_a, fn_b, fn_c,};
  188. use const \some\a\{ConstA,ConstB,ConstC
  189. ,
  190. };
  191. use const \some\Z\{ConstX,ConstY,ConstZ,};
  192. ',
  193. ];
  194. }
  195. /**
  196. * @dataProvider provideFixPre80Cases
  197. *
  198. * @requires PHP <8.0
  199. */
  200. public function testFixPre80(string $expected, ?string $input = null): void
  201. {
  202. $this->doTest($expected, $input);
  203. }
  204. /**
  205. * @return iterable<int|string, array{string, string}>
  206. */
  207. public static function provideFixPre80Cases(): iterable
  208. {
  209. yield [
  210. '<?php use /*1*/A\D;',
  211. '<?php use\/*1*/A\D;',
  212. ];
  213. yield 'no space case' => [
  214. '<?php
  215. use Events\Payment\Base as PaymentEvent;
  216. use const d\e;
  217. ',
  218. '<?php
  219. use\Events\Payment\Base as PaymentEvent;
  220. use const\d\e;
  221. ',
  222. ];
  223. yield [
  224. '<?php
  225. use C;
  226. use C\X;
  227. namespace Foo {
  228. use A;
  229. use A\X;
  230. new X();
  231. }
  232. namespace Bar {
  233. use B;
  234. use B\X;
  235. new X();
  236. }
  237. ',
  238. '<?php
  239. use \C;
  240. use \C\X;
  241. namespace Foo {
  242. use \A;
  243. use \A\X;
  244. new X();
  245. }
  246. namespace Bar {
  247. use \B;
  248. use \B\X;
  249. new X();
  250. }
  251. ',
  252. ];
  253. }
  254. }