OrderedTraitsFixerTest.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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\ClassNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @covers \PhpCsFixer\Fixer\ClassNotation\OrderedTraitsFixer
  16. *
  17. * @internal
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\OrderedTraitsFixer>
  20. *
  21. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\OrderedTraitsFixer
  22. */
  23. final class OrderedTraitsFixerTest extends AbstractFixerTestCase
  24. {
  25. /**
  26. * @dataProvider provideFixCases
  27. *
  28. * @param _AutogeneratedInputConfiguration $configuration
  29. */
  30. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  31. {
  32. $this->fixer->configure($configuration);
  33. $this->doTest($expected, $input);
  34. }
  35. /**
  36. * @return iterable<string, array{string, 1?: ?string}>
  37. */
  38. public static function provideFixCases(): iterable
  39. {
  40. yield 'simple' => [
  41. '<?php
  42. class Foo {
  43. use A;
  44. use B;
  45. }',
  46. '<?php
  47. class Foo {
  48. use B;
  49. use A;
  50. }',
  51. ];
  52. yield 'in multiple classes' => [
  53. '<?php
  54. class Foo {
  55. use A;
  56. use C;
  57. }
  58. class Bar {
  59. use B;
  60. use D;
  61. }',
  62. '<?php
  63. class Foo {
  64. use C;
  65. use A;
  66. }
  67. class Bar {
  68. use D;
  69. use B;
  70. }',
  71. ];
  72. yield 'separated by a property' => [
  73. '<?php
  74. class Foo {
  75. use A;
  76. use C;
  77. private $foo;
  78. use B;
  79. use D;
  80. }',
  81. '<?php
  82. class Foo {
  83. use C;
  84. use A;
  85. private $foo;
  86. use D;
  87. use B;
  88. }',
  89. ];
  90. yield 'separated by a method' => [
  91. '<?php
  92. class Foo {
  93. use A;
  94. use C;
  95. public function foo() { }
  96. use B;
  97. use D;
  98. }',
  99. '<?php
  100. class Foo {
  101. use C;
  102. use A;
  103. public function foo() { }
  104. use D;
  105. use B;
  106. }',
  107. ];
  108. yield 'grouped statements' => [
  109. '<?php
  110. class Foo {
  111. use A, C;
  112. use B;
  113. }',
  114. '<?php
  115. class Foo {
  116. use B;
  117. use A, C;
  118. }',
  119. ];
  120. yield 'with aliases and conflicts' => [
  121. '<?php
  122. class Foo {
  123. use A {
  124. A::foo insteadof B;
  125. A::bar as bazA;
  126. A::baz as protected;
  127. }
  128. use B {
  129. B::bar as bazB;
  130. }
  131. }',
  132. '<?php
  133. class Foo {
  134. use B {
  135. B::bar as bazB;
  136. }
  137. use A {
  138. A::foo insteadof B;
  139. A::bar as bazA;
  140. A::baz as protected;
  141. }
  142. }',
  143. ];
  144. yield 'symbol imports' => [
  145. '<?php
  146. use C;
  147. use B;
  148. use A;',
  149. ];
  150. yield 'anonymous function with inherited variables' => [
  151. '<?php
  152. $foo = function () use ($b, $a) { };
  153. $bar = function () use ($a, $b) { };',
  154. ];
  155. yield 'multiple traits in a single statement' => [
  156. '<?php
  157. class Foo {
  158. use A, B, C, D;
  159. }',
  160. '<?php
  161. class Foo {
  162. use C, B, D, A;
  163. }',
  164. ];
  165. yield 'multiple traits per statement' => [
  166. '<?php
  167. class Foo {
  168. use A, D;
  169. use B, C;
  170. }',
  171. '<?php
  172. class Foo {
  173. use C, B;
  174. use D, A;
  175. }',
  176. ];
  177. $uses = [];
  178. for ($i = 0; $i < 25; ++$i) {
  179. $uses[] = \sprintf(' use A%02d;', $i);
  180. }
  181. yield 'simple, multiple I' => [
  182. \sprintf("<?php\nclass Foo {\n%s\n}", implode("\n", $uses)),
  183. \sprintf("<?php\nclass Foo {\n%s\n}", implode("\n", array_reverse($uses))),
  184. ];
  185. yield 'simple, length diff. I' => [
  186. '<?php
  187. class Foo {
  188. use A;
  189. use B\B;
  190. use C\C\C;
  191. use D\D\D\D;
  192. }',
  193. '<?php
  194. class Foo {
  195. use D\D\D\D;
  196. use C\C\C;
  197. use B\B;
  198. use A;
  199. }',
  200. ];
  201. yield 'comments handling' => [
  202. '<?php
  203. class Foo {
  204. /* A */use A\A\A\A/* A */;
  205. /* B */use B\B\B/* B */;
  206. /* C */use C\C/* C */;
  207. /* D */use D/* D */;
  208. }',
  209. '<?php
  210. class Foo {
  211. /* D */use D/* D */;
  212. /* C */use C\C/* C */;
  213. /* B */use B\B\B/* B */;
  214. /* A */use A\A\A\A/* A */;
  215. }',
  216. ];
  217. yield 'grouped statements II' => [
  218. '<?php
  219. class Foo {
  220. use A\Z, C\Y;
  221. use B\E;
  222. }',
  223. '<?php
  224. class Foo {
  225. use B\E;
  226. use A\Z, C\Y;
  227. }',
  228. ];
  229. yield 'simple, leading \\' => [
  230. '<?php
  231. class Foo {
  232. use \A;
  233. use \B;
  234. }',
  235. '<?php
  236. class Foo {
  237. use \B;
  238. use \A;
  239. }',
  240. ];
  241. yield 'simple, leading \ before character order' => [
  242. '<?php
  243. class Foo {
  244. use A;
  245. use \B;
  246. use C;
  247. }',
  248. '<?php
  249. class Foo {
  250. use C;
  251. use \B;
  252. use A;
  253. }',
  254. ];
  255. yield 'with phpdoc' => [
  256. '<?php
  257. class Foo {
  258. // foo 1
  259. /** @phpstan-use A<Foo> */
  260. use A;
  261. /** @phpstan-use B<Foo> */
  262. use B;
  263. /** @phpstan-use C<Foo> */
  264. use C;
  265. }',
  266. '<?php
  267. class Foo {
  268. /** @phpstan-use C<Foo> */
  269. use C;
  270. /** @phpstan-use B<Foo> */
  271. use B;
  272. // foo 1
  273. /** @phpstan-use A<Foo> */
  274. use A;
  275. }',
  276. ];
  277. yield 'simple and with namespace' => [
  278. '<?php
  279. class User
  280. {
  281. use Test\B, TestA;
  282. }',
  283. '<?php
  284. class User
  285. {
  286. use TestA, Test\B;
  287. }',
  288. ];
  289. yield 'with case sensitive order' => [
  290. '<?php
  291. class Foo {
  292. use AA;
  293. use Aaa;
  294. }',
  295. '<?php
  296. class Foo {
  297. use Aaa;
  298. use AA;
  299. }',
  300. [
  301. 'case_sensitive' => true,
  302. ],
  303. ];
  304. }
  305. }