OrderedInterfacesFixerTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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\Fixer\ClassNotation\OrderedInterfacesFixer;
  14. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  15. /**
  16. * @author Dave van der Brugge <dmvdbrugge@gmail.com>
  17. *
  18. * @internal
  19. *
  20. * @covers \PhpCsFixer\Fixer\ClassNotation\OrderedInterfacesFixer
  21. *
  22. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ClassNotation\OrderedInterfacesFixer>
  23. *
  24. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ClassNotation\OrderedInterfacesFixer
  25. */
  26. final class OrderedInterfacesFixerTest extends AbstractFixerTestCase
  27. {
  28. /**
  29. * @dataProvider provideFixCases
  30. *
  31. * @param _AutogeneratedInputConfiguration $configuration
  32. */
  33. public function testFix(string $expected, ?string $input = null, array $configuration = []): void
  34. {
  35. $this->fixer->configure($configuration);
  36. $this->doTest($expected, $input);
  37. }
  38. public static function provideFixCases(): iterable
  39. {
  40. yield 'single' => [
  41. '<?php class T implements A {}',
  42. ];
  43. yield 'multiple' => [
  44. '<?php class T implements A, B, C {}',
  45. '<?php class T implements C, A, B {}',
  46. ];
  47. yield 'newlines' => [
  48. "<?php class T implements\nB,\nC\n{}",
  49. "<?php class T implements\nC,\nB\n{}",
  50. ];
  51. yield 'newlines and comments' => [
  52. "<?php class T implements\n// Here's A\nA,\n// Here's B\nB\n{}",
  53. "<?php class T implements\n// Here's B\nB,\n// Here's A\nA\n{}",
  54. ];
  55. yield 'no whitespace' => [
  56. '<?php class T implements/*An*/AnInterface,/*Second*/SecondInterface/*end*/{}',
  57. '<?php class T implements/*Second*/SecondInterface,/*An*/AnInterface/*end*/{}',
  58. ];
  59. yield 'FQCN' => [
  60. '<?php class T implements \F\Q\C\N, \F\Q\I\N {}',
  61. '<?php class T implements \F\Q\I\N, \F\Q\C\N {}',
  62. ];
  63. yield 'mixed' => [
  64. '<?php class T implements \F\Q\C\N, Partially\Q\C\N, /* Who mixes these? */ UnNamespaced {}',
  65. '<?php class T implements /* Who mixes these? */ UnNamespaced, \F\Q\C\N, Partially\Q\C\N {}',
  66. ];
  67. yield 'multiple in file' => [
  68. '<?php
  69. class A1 implements A\B\C, Z\X\Y {}
  70. class B2 implements A\B, Z\X {}
  71. class C3 implements A, Z\X {}
  72. class D4 implements A\B, B\V, Z\X\V {}
  73. class E5 implements U\B, X\B, Y\V, Z\X\V {}
  74. ',
  75. '<?php
  76. class A1 implements Z\X\Y, A\B\C {}
  77. class B2 implements Z\X, A\B {}
  78. class C3 implements Z\X, A {}
  79. class D4 implements Z\X\V, B\V, A\B {}
  80. class E5 implements Z\X\V, Y\V, X\B, U\B {}
  81. ',
  82. ];
  83. yield 'interface extends' => [
  84. '<?php interface T extends A, B, C {}',
  85. '<?php interface T extends C, A, B {}',
  86. ];
  87. yield 'nested anonymous classes' => [
  88. '<?php
  89. class T implements A, B, C
  90. {
  91. public function getAnonymousClassObject()
  92. {
  93. return new class() implements C, D, E
  94. {
  95. public function getNestedAnonymousClassObject()
  96. {
  97. return new class() implements E, F, G {};
  98. }
  99. };
  100. }
  101. }
  102. ',
  103. '<?php
  104. class T implements C, A, B
  105. {
  106. public function getAnonymousClassObject()
  107. {
  108. return new class() implements E, C, D
  109. {
  110. public function getNestedAnonymousClassObject()
  111. {
  112. return new class() implements F, G, E {};
  113. }
  114. };
  115. }
  116. }
  117. ',
  118. ];
  119. yield 'single line after interfaces' => [
  120. '<?php
  121. class Foo implements B, C //, A
  122. {}
  123. ',
  124. '<?php
  125. class Foo implements C, B //, A
  126. {}
  127. ',
  128. ];
  129. yield 'descend single' => [
  130. '<?php class T implements A {}',
  131. null,
  132. [OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND],
  133. ];
  134. yield 'descend multiple' => [
  135. '<?php class T implements C, B, A {}',
  136. '<?php class T implements C, A, B {}',
  137. [OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND],
  138. ];
  139. yield 'descend mixed' => [
  140. '<?php class T implements /* Who mixes these? */ UnNamespaced, Partially\Q\C\N, \F\Q\C\N {}',
  141. '<?php class T implements /* Who mixes these? */ UnNamespaced, \F\Q\C\N, Partially\Q\C\N {}',
  142. [OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND],
  143. ];
  144. yield 'length single' => [
  145. '<?php class A implements A {}',
  146. null,
  147. [OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH],
  148. ];
  149. yield 'length multiple' => [
  150. '<?php class A implements Short, Longer, MuchLonger {}',
  151. '<?php class A implements MuchLonger, Short, Longer {}',
  152. [OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH],
  153. ];
  154. yield 'length mixed' => [
  155. '<?php class T implements \F\Q\C\N, /* Who mixes these? */ UnNamespaced, Partially\Q\C\N {}',
  156. '<?php class T implements /* Who mixes these? */ UnNamespaced, \F\Q\C\N, Partially\Q\C\N {}',
  157. [OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH],
  158. ];
  159. yield 'length normalized' => [
  160. '<?php
  161. class A implements
  162. ABCDE,
  163. A\B\C\D
  164. { /* */ }
  165. ',
  166. '<?php
  167. class A implements
  168. A\B\C\D,
  169. ABCDE
  170. { /* */ }
  171. ',
  172. [OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH],
  173. ];
  174. yield 'length, descend single' => [
  175. '<?php class A implements A {}',
  176. null,
  177. [
  178. OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH,
  179. OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND,
  180. ],
  181. ];
  182. yield 'length, descend multiple' => [
  183. '<?php class A implements MuchLonger, Longer, Short {}',
  184. '<?php class A implements MuchLonger, Short, Longer {}',
  185. [
  186. OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH,
  187. OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND,
  188. ],
  189. ];
  190. yield 'length, descend mixed' => [
  191. '<?php class T implements Partially\Q\C\N, /* Who mixes these? */ UnNamespaced, \F\Q\C\N {}',
  192. '<?php class T implements /* Who mixes these? */ UnNamespaced, \F\Q\C\N, Partially\Q\C\N {}',
  193. [
  194. OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH,
  195. OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND,
  196. ],
  197. ];
  198. yield 'length, descend normalized' => [
  199. '<?php
  200. class A implements
  201. A\B\C\D,
  202. ABCDE
  203. { /* */ }
  204. ',
  205. '<?php
  206. class A implements
  207. ABCDE,
  208. A\B\C\D
  209. { /* */ }
  210. ',
  211. [
  212. OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_LENGTH,
  213. OrderedInterfacesFixer::OPTION_DIRECTION => OrderedInterfacesFixer::DIRECTION_DESCEND,
  214. ],
  215. ];
  216. yield 'case sensitive single' => [
  217. '<?php class A implements A {}',
  218. null,
  219. [
  220. OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_ALPHA,
  221. 'case_sensitive' => true,
  222. ],
  223. ];
  224. yield 'alpha multiple' => [
  225. '<?php class A implements AA, Aaa, FF, Fff {}',
  226. '<?php class A implements Fff, Aaa, FF, AA {}',
  227. [
  228. OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_ALPHA,
  229. 'case_sensitive' => true,
  230. ],
  231. ];
  232. yield 'alpha mixed' => [
  233. '<?php class T implements \F\Q\C\N, Partially\Q\C\N, /* Who mixes these? */ UnNamespaced {}',
  234. '<?php class T implements /* Who mixes these? */ UnNamespaced, \F\Q\C\N, Partially\Q\C\N {}',
  235. [
  236. OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_ALPHA,
  237. 'case_sensitive' => true,
  238. ],
  239. ];
  240. yield 'alpha normalized' => [
  241. '<?php
  242. class A implements
  243. A\B\C\D,
  244. AAa\B\C\D,
  245. ABCDE,
  246. Aaa\B\C\D
  247. { /* */ }
  248. ',
  249. '<?php
  250. class A implements
  251. Aaa\B\C\D,
  252. AAa\B\C\D,
  253. ABCDE,
  254. A\B\C\D
  255. { /* */ }
  256. ',
  257. [
  258. OrderedInterfacesFixer::OPTION_ORDER => OrderedInterfacesFixer::ORDER_ALPHA,
  259. 'case_sensitive' => true,
  260. ],
  261. ];
  262. }
  263. }