SingleTraitInsertPerStatementFixerTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. * @internal
  16. *
  17. * @covers \PhpCsFixer\Fixer\ClassNotation\SingleTraitInsertPerStatementFixer
  18. *
  19. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\SingleTraitInsertPerStatementFixer>
  20. */
  21. final class SingleTraitInsertPerStatementFixerTest extends AbstractFixerTestCase
  22. {
  23. /**
  24. * @dataProvider provideFixCases
  25. */
  26. public function testFix(string $expected, ?string $input = null): void
  27. {
  28. $this->doTest($expected, $input);
  29. }
  30. /**
  31. * @return iterable<string, array{0: string, 1?: string}>
  32. */
  33. public static function provideFixCases(): iterable
  34. {
  35. yield 'simple' => [
  36. '<?php
  37. final class Example
  38. {
  39. use Foo;use Bar;
  40. }
  41. ',
  42. '<?php
  43. final class Example
  44. {
  45. use Foo, Bar;
  46. }
  47. ',
  48. ];
  49. yield 'simple I' => [
  50. '<?php
  51. final class Example
  52. {
  53. use Foo;use Bar;
  54. }
  55. ',
  56. '<?php
  57. final class Example
  58. {
  59. use Foo,Bar;
  60. }
  61. ',
  62. ];
  63. yield 'simple II' => [
  64. '<?php
  65. use Foo\Bar, Foo\Bar2; // do not touch
  66. final class Example
  67. {
  68. use Foo;use Bar ;
  69. }
  70. ',
  71. '<?php
  72. use Foo\Bar, Foo\Bar2; // do not touch
  73. final class Example
  74. {
  75. use Foo, Bar ;
  76. }
  77. ',
  78. ];
  79. yield 'simple III' => [
  80. '<?php
  81. class Example
  82. {
  83. use Foo;use Bar;
  84. public function baz() {}
  85. }
  86. ',
  87. '<?php
  88. class Example
  89. {
  90. use Foo, Bar;
  91. public function baz() {}
  92. }
  93. ',
  94. ];
  95. yield 'multiple' => [
  96. '<?php
  97. final class Example
  98. {
  99. use Foo;
  100. use Foo00;use Bar01;
  101. use Foo10;use Bar11;use Bar110;
  102. use Foo20;use Bar20;use Bar200;use Bar201;
  103. }
  104. ',
  105. '<?php
  106. final class Example
  107. {
  108. use Foo;
  109. use Foo00, Bar01;
  110. use Foo10, Bar11, Bar110;
  111. use Foo20, Bar20, Bar200, Bar201;
  112. }
  113. ',
  114. ];
  115. yield 'multiple_multiline' => [
  116. '<?php
  117. final class Example
  118. {
  119. use Foo;
  120. use Bar;
  121. use Baz;
  122. }
  123. ',
  124. '<?php
  125. final class Example
  126. {
  127. use Foo,
  128. Bar,
  129. Baz;
  130. }
  131. ',
  132. ];
  133. yield 'multiple_multiline_with_comment' => [
  134. '<?php
  135. final class Example
  136. {
  137. use Foo;
  138. use Bar;
  139. // Bazz,
  140. use Baz;
  141. }
  142. ',
  143. '<?php
  144. final class Example
  145. {
  146. use Foo,
  147. Bar,
  148. // Bazz,
  149. Baz;
  150. }
  151. ',
  152. ];
  153. yield 'namespaces' => [
  154. '<?php
  155. class Z
  156. {
  157. use X\Y\Z0;use X\Y\Z0;use M;
  158. use X\Y\Z1;use X\Y\Z1;
  159. }
  160. ',
  161. '<?php
  162. class Z
  163. {
  164. use X\Y\Z0, X\Y\Z0, M;
  165. use X\Y\Z1, X\Y\Z1;
  166. }
  167. ',
  168. ];
  169. yield 'comments' => [
  170. '<?php
  171. class ZZ
  172. {#1
  173. use#2
  174. Z/* 2 */ #3
  175. #4
  176. ;#5
  177. #6
  178. use T#7
  179. #8
  180. ;#9
  181. #10
  182. }
  183. ',
  184. '<?php
  185. class ZZ
  186. {#1
  187. use#2
  188. Z/* 2 */ #3
  189. #4
  190. ,#5
  191. #6
  192. T#7
  193. #8
  194. ;#9
  195. #10
  196. }
  197. ',
  198. ];
  199. yield 'two classes. same file' => [
  200. '<?php
  201. namespace Foo;
  202. class Test1
  203. {
  204. use A;use B; /** use A2, B2; */
  205. }
  206. ?>
  207. <?php
  208. class Test2
  209. {
  210. use A1;use B1; # use A2, B2;
  211. }
  212. ',
  213. '<?php
  214. namespace Foo;
  215. class Test1
  216. {
  217. use A, B; /** use A2, B2; */
  218. }
  219. ?>
  220. <?php
  221. class Test2
  222. {
  223. use A1, B1; # use A2, B2;
  224. }
  225. ',
  226. ];
  227. yield 'do not fix group' => [
  228. '<?php
  229. class Talker {
  230. use A, B {
  231. B::smallTalk insteadof A;
  232. A::bigTalk insteadof B;
  233. }
  234. }',
  235. ];
  236. yield 'anonymous class' => [
  237. '<?php new class { use A;use B;}?>',
  238. '<?php new class { use A, B;}?>',
  239. ];
  240. }
  241. }