SwitchCaseSpaceFixerTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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\ControlStructure;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. /**
  15. * @author Sullivan Senechal <soullivaneuh@gmail.com>
  16. *
  17. * @internal
  18. *
  19. * @covers \PhpCsFixer\Fixer\ControlStructure\SwitchCaseSpaceFixer
  20. *
  21. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\SwitchCaseSpaceFixer>
  22. */
  23. final class SwitchCaseSpaceFixerTest 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. public static function provideFixCases(): iterable
  33. {
  34. yield [
  35. '<?php
  36. switch (1) {
  37. case (1 #
  38. )#
  39. :
  40. echo 1;
  41. }
  42. ?>
  43. ',
  44. ];
  45. yield [
  46. '<?php
  47. switch (1) {
  48. case 1 #
  49. : echo 1;
  50. }
  51. ?>
  52. ',
  53. ];
  54. yield [
  55. '<?php
  56. switch ($a) {
  57. case 42:
  58. break;
  59. }
  60. ',
  61. ];
  62. yield [
  63. '<?php
  64. switch ($a) {
  65. case false:
  66. break;
  67. }
  68. ',
  69. ];
  70. yield [
  71. '<?php
  72. switch ($a) {
  73. case false:
  74. break;
  75. default:
  76. }
  77. ',
  78. ];
  79. yield [
  80. '<?php
  81. switch ($a) {
  82. case "prod":
  83. break;
  84. }
  85. ',
  86. '<?php
  87. switch ($a) {
  88. case "prod" :
  89. break;
  90. }
  91. ',
  92. ];
  93. yield [
  94. '<?php
  95. switch ($a) {
  96. case "prod":
  97. break;
  98. }
  99. ',
  100. '<?php
  101. switch ($a) {
  102. case "prod" :
  103. break;
  104. }
  105. ',
  106. ];
  107. yield [
  108. '<?php
  109. switch ($a) {
  110. case 42:
  111. break;
  112. }
  113. ',
  114. '<?php
  115. switch ($a) {
  116. case 42 :
  117. break;
  118. }
  119. ',
  120. ];
  121. yield [
  122. '<?php
  123. switch ($a) {
  124. case false:
  125. break;
  126. }
  127. ',
  128. '<?php
  129. switch ($a) {
  130. case false :
  131. break;
  132. }
  133. ',
  134. ];
  135. yield [
  136. '<?php
  137. switch ($a) {
  138. case false:
  139. break;
  140. default:
  141. }
  142. ',
  143. '<?php
  144. switch ($a) {
  145. case false :
  146. break;
  147. default :
  148. }
  149. ',
  150. ];
  151. yield [
  152. '<?php
  153. switch ($a) {
  154. case 42:
  155. break;
  156. }
  157. ',
  158. '<?php
  159. switch ($a) {
  160. case 42 :
  161. break;
  162. }
  163. ',
  164. ];
  165. yield [
  166. '<?php
  167. switch ($a) {
  168. case $b ? "c" : "d":
  169. break;
  170. }
  171. ',
  172. '<?php
  173. switch ($a) {
  174. case $b ? "c" : "d" :
  175. break;
  176. }
  177. ',
  178. ];
  179. yield [
  180. '<?php
  181. switch ($a) {
  182. case $b ? "c" : "d": break;
  183. }
  184. ',
  185. '<?php
  186. switch ($a) {
  187. case $b ? "c" : "d" : break;
  188. }
  189. ',
  190. ];
  191. yield [
  192. '<?php
  193. switch ($a) {
  194. case $b ?: $c:
  195. break;
  196. }
  197. ',
  198. '<?php
  199. switch ($a) {
  200. case $b ?: $c :
  201. break;
  202. }
  203. ',
  204. ];
  205. yield [
  206. '<?php
  207. $a = 5.1;
  208. $b = 1.0;
  209. switch($a) {
  210. case (int) $a < 1: {
  211. echo "leave alone";
  212. break;
  213. }
  214. case ($a < 2)/* test */ : {
  215. echo "fix 1";
  216. break;
  217. }
  218. case (3): {
  219. echo "fix 2";
  220. break;
  221. }
  222. case /**/(/**/ // test
  223. 4
  224. /**/)//
  225. /**/ : {
  226. echo "fix 3";
  227. break;
  228. }
  229. case (((int)$b) + 4.1): {
  230. echo "fix 4";
  231. break;
  232. }
  233. case ($b + 1) * 2: {
  234. echo "leave alone";
  235. break;
  236. }
  237. }
  238. ',
  239. '<?php
  240. $a = 5.1;
  241. $b = 1.0;
  242. switch($a) {
  243. case (int) $a < 1 : {
  244. echo "leave alone";
  245. break;
  246. }
  247. case ($a < 2)/* test */ : {
  248. echo "fix 1";
  249. break;
  250. }
  251. case (3) : {
  252. echo "fix 2";
  253. break;
  254. }
  255. case /**/(/**/ // test
  256. 4
  257. /**/)//
  258. /**/ : {
  259. echo "fix 3";
  260. break;
  261. }
  262. case (((int)$b) + 4.1) : {
  263. echo "fix 4";
  264. break;
  265. }
  266. case ($b + 1) * 2 : {
  267. echo "leave alone";
  268. break;
  269. }
  270. }
  271. ',
  272. [
  273. '<?php
  274. switch ($a) {
  275. case 42:
  276. break;
  277. case 1:
  278. switch ($a) {
  279. case 42:
  280. break;
  281. default:
  282. echo 1 ;
  283. }
  284. }
  285. ',
  286. '<?php
  287. switch ($a) {
  288. case 42 :
  289. break;
  290. case 1 :
  291. switch ($a) {
  292. case 42 :
  293. break;
  294. default :
  295. echo 1 ;
  296. }
  297. }
  298. ',
  299. ],
  300. ];
  301. yield [
  302. '<?php
  303. switch($foo) {
  304. case 4: ; ;
  305. case 31 + test(";"); ; ; ;;
  306. case 1 + test(";"); // ;
  307. case (1+2/*;*/);
  308. case 1;
  309. case 2;
  310. return 1;
  311. default;
  312. return 2;
  313. }',
  314. '<?php
  315. switch($foo) {
  316. case 4 : ; ;
  317. case 31 + test(";") ; ; ; ;;
  318. case 1 + test(";") ; // ;
  319. case (1+2/*;*/) ;
  320. case 1 ;
  321. case 2 ;
  322. return 1;
  323. default ;
  324. return 2;
  325. }',
  326. ];
  327. }
  328. /**
  329. * @dataProvider provideFixPre80Cases
  330. *
  331. * @requires PHP <8.0
  332. */
  333. public function testFixPre80(string $expected, ?string $input = null): void
  334. {
  335. $this->doTest($expected, $input);
  336. }
  337. /**
  338. * @return iterable<array{string, string}>
  339. */
  340. public static function provideFixPre80Cases(): iterable
  341. {
  342. yield [
  343. '<?php
  344. switch ($a) {
  345. case $b ? "c" : "this" ? "is" : "ugly":
  346. break;
  347. }
  348. ',
  349. '<?php
  350. switch ($a) {
  351. case $b ? "c" : "this" ? "is" : "ugly" :
  352. break;
  353. }
  354. ',
  355. ];
  356. }
  357. /**
  358. * @dataProvider provideFix80Cases
  359. *
  360. * @requires PHP 8.0
  361. */
  362. public function testFix80(string $expected, ?string $input = null): void
  363. {
  364. $this->doTest($expected, $input);
  365. }
  366. /**
  367. * @return iterable<array{string}>
  368. */
  369. public static function provideFix80Cases(): iterable
  370. {
  371. yield [
  372. '<?php
  373. match ($foo) {
  374. 1 => "a",
  375. default => "b"
  376. };
  377. match ($bar) {
  378. 2 => "c",
  379. default=> "d"
  380. };
  381. match ($baz) {
  382. 3 => "e",
  383. default => "f"
  384. };
  385. ',
  386. ];
  387. yield [
  388. '<?php
  389. $a = function (): ?string {
  390. return $rank ? match (true) {
  391. $rank <= 1000 => \'bronze\',
  392. default => null,
  393. } : null;
  394. };',
  395. ];
  396. }
  397. /**
  398. * @dataProvider provideFix81Cases
  399. *
  400. * @requires PHP 8.1
  401. */
  402. public function testFix81(string $expected, ?string $input = null): void
  403. {
  404. $this->doTest($expected, $input);
  405. }
  406. /**
  407. * @return iterable<string, array{string}>
  408. */
  409. public static function provideFix81Cases(): iterable
  410. {
  411. yield 'enums' => [
  412. '<?php
  413. enum Suit {
  414. case Hearts;
  415. case Diamonds ;
  416. case Clubs ;
  417. case Spades ;
  418. }
  419. enum UserStatus: string {
  420. case Pending = \'P\';
  421. case Active = \'A\';
  422. case Suspended = \'S\';
  423. case CanceledByUser = \'C\' ;
  424. }
  425. switch ($a) {
  426. default:
  427. echo 1;
  428. }
  429. ',
  430. ];
  431. }
  432. }