* Dariusz RumiƄski * * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace PhpCsFixer\Tests\Fixer\Whitespace; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\ArrayIndentationFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\ArrayIndentationFixer> */ final class ArrayIndentationFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixCases(): iterable { yield from self::withLongArraySyntaxCases([ [ <<<'EXPECTED' 'baz', ]; EXPECTED, <<<'INPUT' 'baz', ]; INPUT, ], [ <<<'EXPECTED' 'baz', ]; EXPECTED, <<<'INPUT' 'baz', ]; INPUT, ], [ <<<'EXPECTED' foo( 1, 2 ), 'bar' => bar( 1, 2 ), 'baz' => baz( 1, 2 ), ]; EXPECTED, <<<'INPUT' foo( 1, 2 ), 'bar' => bar( 1, 2 ), 'baz' => baz( 1, 2 ), ]; INPUT, ], [ <<<'EXPECTED' ['bar' => [ 'baz', ]], 'qux', ]; EXPECTED, <<<'INPUT' ['bar' => [ 'baz', ]], 'qux', ]; INPUT, ], [ <<<'EXPECTED' [ (new Foo()) ->foo(), ], ]; EXPECTED, <<<'INPUT' [ (new Foo()) ->foo(), ], ]; INPUT, ], [ <<<'EXPECTED' foo([ 'foo', 'foo', ]) ->bar(['bar', 'bar']) ->baz([ 'baz', 'baz', ]) , ]); EXPECTED, <<<'INPUT' foo([ 'foo', 'foo', ]) ->bar(['bar', 'bar']) ->baz([ 'baz', 'baz', ]) , ]); INPUT, ], [ <<<'EXPECTED' qux([function ($a) { foreach ($a as $b) { if ($b) { throw new Exception(sprintf( 'Oops: %s', $b )); } } }]), ]); } } EXPECTED, <<<'INPUT' qux([function ($a) { foreach ($a as $b) { if ($b) { throw new Exception(sprintf( 'Oops: %s', $b )); } } }]), ]); } } INPUT, ], [ <<<'EXPECTED' EXPECTED, <<<'INPUT' INPUT, ], [ <<<'EXPECTED'
Link text
EXPECTED, <<<'INPUT'
Link text
INPUT, ], [ <<<'EXPECTED' 'b', // 'c' => 'd', ]; EXPECTED, <<<'INPUT' 'b', // 'c' => 'd', ]; INPUT, ], [ ' 'Some' .' long' .' string', 'bar' => 'Another' .' long' .' string' ]; EXPECTED, <<<'INPUT' 'Some' .' long' .' string', 'bar' => 'Another' .' long' .' string' ]; INPUT, ], [ <<<'EXPECTED' [ 'bar' => [ 'baz', ], ], // <- this one ]; EXPECTED, <<<'INPUT' [ 'bar' => [ 'baz', ], ], // <- this one ]; INPUT, ], [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t")); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideWithWhitespacesConfigCases(): iterable { yield from self::withLongArraySyntaxCases([ [ << 'baz', ]; EXPECTED, <<<'INPUT' 'baz', ]; INPUT, ], [ << 'baz', ]; EXPECTED, << 'baz', ]; INPUT, ], ]); yield 'array destructuring' => [ << [ <<doTest($expected, $input); } /** * @return iterable */ public static function provideFix80Cases(): iterable { yield 'attribute' => [ ' $cases * * @return list */ private static function withLongArraySyntaxCases(array $cases): array { $longSyntaxCases = []; foreach ($cases as $case) { $case[0] = self::toLongArraySyntax($case[0]); if (isset($case[1])) { $case[1] = self::toLongArraySyntax($case[1]); } $longSyntaxCases[] = $case; } return [...$cases, ...$longSyntaxCases]; } private static function toLongArraySyntax(string $php): string { return strtr($php, [ '[' => 'array(', ']' => ')', ]); } }