ListSyntaxFixerTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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\ListNotation;
  13. use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
  14. use PhpCsFixer\Tests\Test\TestCaseUtils;
  15. /**
  16. * @internal
  17. *
  18. * @covers \PhpCsFixer\Fixer\ListNotation\ListSyntaxFixer
  19. *
  20. * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ListNotation\ListSyntaxFixer>
  21. *
  22. * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ListNotation\ListSyntaxFixer
  23. */
  24. final class ListSyntaxFixerTest extends AbstractFixerTestCase
  25. {
  26. public function testFixWithDefaultConfiguration(): void
  27. {
  28. $this->fixer->configure([]);
  29. $this->doTest(
  30. '<?php $a = [$a, $b] = $a; [$b] = $a;',
  31. '<?php $a = list($a, $b) = $a; [$b] = $a;'
  32. );
  33. }
  34. /**
  35. * @dataProvider provideFixToLongSyntaxCases
  36. */
  37. public function testFixToLongSyntax(string $expected, ?string $input = null): void
  38. {
  39. $this->fixer->configure(['syntax' => 'long']);
  40. $this->doTest($expected, $input);
  41. }
  42. /**
  43. * @return iterable<int|string, array{0: string, 1?: string}>
  44. */
  45. public static function provideFixToLongSyntaxCases(): iterable
  46. {
  47. // reverse testing
  48. $shortCases = self::provideFixToShortSyntaxCases();
  49. foreach ($shortCases as $label => $shortCase) {
  50. if ('messy comments case' === $label) {
  51. continue;
  52. }
  53. yield $label => [$shortCase[1], $shortCase[0]];
  54. }
  55. // the reverse of this is different because of all the comments and white space,
  56. // therefore we override with a similar case here
  57. yield 'comment case' => [
  58. '<?php
  59. #
  60. list(#
  61. $a#
  62. )#
  63. =#
  64. $a#
  65. ;#',
  66. '<?php
  67. #
  68. [#
  69. $a#
  70. ]#
  71. =#
  72. $a#
  73. ;#',
  74. ];
  75. yield ['<?php
  76. class Test
  77. {
  78. public function updateAttributeKey($key, $value)
  79. {
  80. $this->{camel_case($attributes)}[$key] = $value;
  81. }
  82. }',
  83. ];
  84. yield ['<?php [$b[$a]] = $foo();'];
  85. yield [
  86. '<?php [$iHaveList => list($x, $y) = getList()];',
  87. '<?php [$iHaveList => [$x, $y] = getList()];',
  88. ];
  89. }
  90. /**
  91. * @dataProvider provideFixToShortSyntaxCases
  92. */
  93. public function testFixToShortSyntax(string $expected, ?string $input = null): void
  94. {
  95. $this->fixer->configure(['syntax' => 'short']);
  96. $this->doTest($expected, $input);
  97. }
  98. /**
  99. * @return iterable<int|string, array{string, string}>
  100. */
  101. public static function provideFixToShortSyntaxCases(): iterable
  102. {
  103. yield [
  104. '<?php [$x] = $a;',
  105. '<?php list($x) = $a;',
  106. ];
  107. yield [
  108. '<?php [$a, $b, $c] = $array;',
  109. '<?php list($a, $b, $c) = $array;',
  110. ];
  111. yield [
  112. '<?php ["a" => $a, "b" => $b, "c" => $c] = $array;',
  113. '<?php list("a" => $a, "b" => $b, "c" => $c) = $array;',
  114. ];
  115. yield [
  116. '<?php
  117. #
  118. [//
  119. $x] =/**/$a?>',
  120. '<?php
  121. #
  122. list(//
  123. $x) =/**/$a?>',
  124. ];
  125. yield 'messy comments case' => [
  126. '<?php
  127. #a
  128. #g
  129. [#h
  130. #f
  131. $a#
  132. #e
  133. ]#
  134. #
  135. =#c
  136. #
  137. $a;#
  138. #
  139. ',
  140. '<?php
  141. #a
  142. list#g
  143. (#h
  144. #f
  145. $a#
  146. #e
  147. )#
  148. #
  149. =#c
  150. #
  151. $a;#
  152. #
  153. ',
  154. ];
  155. yield [
  156. '<?php [$a, $b,, [$c, $d]] = $a;',
  157. '<?php list($a, $b,, list($c, $d)) = $a;',
  158. ];
  159. yield [
  160. '<?php [[$a, $b], [$c, $d]] = $a;',
  161. '<?php list(list($a, $b), list($c, $d)) = $a;',
  162. ];
  163. yield [
  164. '<?php [[$a, [$b]], [[$c, [$d]]]] = $a;',
  165. '<?php list(list($a, list($b)), list(list($c, list($d)))) = $a;',
  166. ];
  167. yield [
  168. '<?php [[$a]] = $foo();',
  169. '<?php list(list($a)) = $foo();',
  170. ];
  171. yield [
  172. '<?php foreach ($z as [$a, $b]) {}',
  173. '<?php foreach ($z as list($a, $b)) {}',
  174. ];
  175. }
  176. /**
  177. * @dataProvider provideFixToShortSyntaxPhp72Cases
  178. */
  179. public function testFixToShortSyntaxPhp72(string $expected, string $input): void
  180. {
  181. $this->fixer->configure(['syntax' => 'short']);
  182. $this->doTest($expected, $input);
  183. }
  184. /**
  185. * @dataProvider provideFixToLongSyntaxPhp72Cases
  186. */
  187. public function testFixToLongSyntaxPhp72(string $expected, string $input): void
  188. {
  189. $this->fixer->configure(['syntax' => 'long']);
  190. $this->doTest($expected, $input);
  191. }
  192. /**
  193. * @return iterable<array{string, string}>
  194. */
  195. public static function provideFixToShortSyntaxPhp72Cases(): iterable
  196. {
  197. yield [
  198. '<?php [$a, $b,, [$c, $d]] = $a;',
  199. '<?php list($a, $b,, list($c, $d)) = $a;',
  200. ];
  201. }
  202. /**
  203. * @return iterable<array{0: string, 1?: string}>
  204. */
  205. public static function provideFixToLongSyntaxPhp72Cases(): iterable
  206. {
  207. return TestCaseUtils::swapExpectedInputTestCases(self::provideFixToShortSyntaxPhp72Cases());
  208. }
  209. /**
  210. * @dataProvider provideFixToShortSyntaxPhp73Cases
  211. */
  212. public function testFixToShortSyntaxPhp73(string $expected, string $input): void
  213. {
  214. $this->fixer->configure(['syntax' => 'short']);
  215. $this->doTest($expected, $input);
  216. }
  217. /**
  218. * @dataProvider provideFixToLongSyntaxPhp73Cases
  219. */
  220. public function testFixToLongSyntaxPhp73(string $expected, string $input): void
  221. {
  222. $this->fixer->configure(['syntax' => 'long']);
  223. $this->doTest($expected, $input);
  224. }
  225. /**
  226. * @return iterable<array{string, string}>
  227. */
  228. public static function provideFixToShortSyntaxPhp73Cases(): iterable
  229. {
  230. yield [
  231. '<?php [&$a, $b] = $a;',
  232. '<?php list(&$a, $b) = $a;',
  233. ];
  234. yield [
  235. '<?php [&$a,/* */&$b] = $a;',
  236. '<?php list(&$a,/* */&$b) = $a;',
  237. ];
  238. yield [
  239. '<?php [&$a, $b,, [&$c, $d]] = $a;',
  240. '<?php list(&$a, $b,, list(&$c, $d)) = $a;',
  241. ];
  242. }
  243. /**
  244. * @return iterable<array{0: string, 1?: string}>
  245. */
  246. public static function provideFixToLongSyntaxPhp73Cases(): iterable
  247. {
  248. return TestCaseUtils::swapExpectedInputTestCases(self::provideFixToShortSyntaxPhp73Cases());
  249. }
  250. /**
  251. * @dataProvider provideFix81Cases
  252. *
  253. * @requires PHP 8.1
  254. */
  255. public function testFix81(string $expected, ?string $input = null): void
  256. {
  257. $this->doTest($expected, $input);
  258. }
  259. /**
  260. * @return iterable<string, array{string}>
  261. */
  262. public static function provideFix81Cases(): iterable
  263. {
  264. yield 'simple 8.1' => [
  265. '<?php $a = _list(...);',
  266. ];
  267. }
  268. }