123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305 |
- <?php
- declare(strict_types=1);
- /*
- * This file is part of PHP CS Fixer.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- * Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace PhpCsFixer\Tests\Fixer\ListNotation;
- use PhpCsFixer\Tests\Test\AbstractFixerTestCase;
- use PhpCsFixer\Tests\Test\TestCaseUtils;
- /**
- * @internal
- *
- * @covers \PhpCsFixer\Fixer\ListNotation\ListSyntaxFixer
- *
- * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ListNotation\ListSyntaxFixer>
- *
- * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ListNotation\ListSyntaxFixer
- */
- final class ListSyntaxFixerTest extends AbstractFixerTestCase
- {
- public function testFixWithDefaultConfiguration(): void
- {
- $this->fixer->configure([]);
- $this->doTest(
- '<?php $a = [$a, $b] = $a; [$b] = $a;',
- '<?php $a = list($a, $b) = $a; [$b] = $a;'
- );
- }
- /**
- * @dataProvider provideFixToLongSyntaxCases
- */
- public function testFixToLongSyntax(string $expected, ?string $input = null): void
- {
- $this->fixer->configure(['syntax' => 'long']);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{0: string, 1?: string}>
- */
- public static function provideFixToLongSyntaxCases(): iterable
- {
- // reverse testing
- $shortCases = self::provideFixToShortSyntaxCases();
- foreach ($shortCases as $label => $shortCase) {
- if ('messy comments case' === $label) {
- continue;
- }
- yield $label => [$shortCase[1], $shortCase[0]];
- }
- // the reverse of this is different because of all the comments and white space,
- // therefore we override with a similar case here
- yield 'comment case' => [
- '<?php
- #
- list(#
- $a#
- )#
- =#
- $a#
- ;#',
- '<?php
- #
- [#
- $a#
- ]#
- =#
- $a#
- ;#',
- ];
- yield ['<?php
- class Test
- {
- public function updateAttributeKey($key, $value)
- {
- $this->{camel_case($attributes)}[$key] = $value;
- }
- }',
- ];
- yield ['<?php [$b[$a]] = $foo();'];
- yield [
- '<?php [$iHaveList => list($x, $y) = getList()];',
- '<?php [$iHaveList => [$x, $y] = getList()];',
- ];
- }
- /**
- * @dataProvider provideFixToShortSyntaxCases
- */
- public function testFixToShortSyntax(string $expected, ?string $input = null): void
- {
- $this->fixer->configure(['syntax' => 'short']);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<int|string, array{string, string}>
- */
- public static function provideFixToShortSyntaxCases(): iterable
- {
- yield [
- '<?php [$x] = $a;',
- '<?php list($x) = $a;',
- ];
- yield [
- '<?php [$a, $b, $c] = $array;',
- '<?php list($a, $b, $c) = $array;',
- ];
- yield [
- '<?php ["a" => $a, "b" => $b, "c" => $c] = $array;',
- '<?php list("a" => $a, "b" => $b, "c" => $c) = $array;',
- ];
- yield [
- '<?php
- #
- [//
- $x] =/**/$a?>',
- '<?php
- #
- list(//
- $x) =/**/$a?>',
- ];
- yield 'messy comments case' => [
- '<?php
- #a
- #g
- [#h
- #f
- $a#
- #e
- ]#
- #
- =#c
- #
- $a;#
- #
- ',
- '<?php
- #a
- list#g
- (#h
- #f
- $a#
- #e
- )#
- #
- =#c
- #
- $a;#
- #
- ',
- ];
- yield [
- '<?php [$a, $b,, [$c, $d]] = $a;',
- '<?php list($a, $b,, list($c, $d)) = $a;',
- ];
- yield [
- '<?php [[$a, $b], [$c, $d]] = $a;',
- '<?php list(list($a, $b), list($c, $d)) = $a;',
- ];
- yield [
- '<?php [[$a, [$b]], [[$c, [$d]]]] = $a;',
- '<?php list(list($a, list($b)), list(list($c, list($d)))) = $a;',
- ];
- yield [
- '<?php [[$a]] = $foo();',
- '<?php list(list($a)) = $foo();',
- ];
- yield [
- '<?php foreach ($z as [$a, $b]) {}',
- '<?php foreach ($z as list($a, $b)) {}',
- ];
- }
- /**
- * @dataProvider provideFixToShortSyntaxPhp72Cases
- */
- public function testFixToShortSyntaxPhp72(string $expected, string $input): void
- {
- $this->fixer->configure(['syntax' => 'short']);
- $this->doTest($expected, $input);
- }
- /**
- * @dataProvider provideFixToLongSyntaxPhp72Cases
- */
- public function testFixToLongSyntaxPhp72(string $expected, string $input): void
- {
- $this->fixer->configure(['syntax' => 'long']);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideFixToShortSyntaxPhp72Cases(): iterable
- {
- yield [
- '<?php [$a, $b,, [$c, $d]] = $a;',
- '<?php list($a, $b,, list($c, $d)) = $a;',
- ];
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideFixToLongSyntaxPhp72Cases(): iterable
- {
- return TestCaseUtils::swapExpectedInputTestCases(self::provideFixToShortSyntaxPhp72Cases());
- }
- /**
- * @dataProvider provideFixToShortSyntaxPhp73Cases
- */
- public function testFixToShortSyntaxPhp73(string $expected, string $input): void
- {
- $this->fixer->configure(['syntax' => 'short']);
- $this->doTest($expected, $input);
- }
- /**
- * @dataProvider provideFixToLongSyntaxPhp73Cases
- */
- public function testFixToLongSyntaxPhp73(string $expected, string $input): void
- {
- $this->fixer->configure(['syntax' => 'long']);
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<array{string, string}>
- */
- public static function provideFixToShortSyntaxPhp73Cases(): iterable
- {
- yield [
- '<?php [&$a, $b] = $a;',
- '<?php list(&$a, $b) = $a;',
- ];
- yield [
- '<?php [&$a,/* */&$b] = $a;',
- '<?php list(&$a,/* */&$b) = $a;',
- ];
- yield [
- '<?php [&$a, $b,, [&$c, $d]] = $a;',
- '<?php list(&$a, $b,, list(&$c, $d)) = $a;',
- ];
- }
- /**
- * @return iterable<array{0: string, 1?: string}>
- */
- public static function provideFixToLongSyntaxPhp73Cases(): iterable
- {
- return TestCaseUtils::swapExpectedInputTestCases(self::provideFixToShortSyntaxPhp73Cases());
- }
- /**
- * @dataProvider provideFix81Cases
- *
- * @requires PHP 8.1
- */
- public function testFix81(string $expected, ?string $input = null): void
- {
- $this->doTest($expected, $input);
- }
- /**
- * @return iterable<string, array{string}>
- */
- public static function provideFix81Cases(): iterable
- {
- yield 'simple 8.1' => [
- '<?php $a = _list(...);',
- ];
- }
- }
|