* 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; /** * @author Vladimir Boliev * * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\MethodChainingIndentationFixer> */ final class MethodChainingIndentationFixerTest 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 [ 'setEmail(\'voff.web@gmail.com\') ->setPassword(\'233434\') ->setEmailConfirmed(false) ->setEmailConfirmationCode(\'123456\') ->setHashsalt(\'1234\') ->setTncAccepted(true); ', 'setEmail(\'voff.web@gmail.com\') ->setPassword(\'233434\') ->setEmailConfirmed(false) ->setEmailConfirmationCode(\'123456\') ->setHashsalt(\'1234\') ->setTncAccepted(true); ', ]; yield [ 'bar1() // comment ->bar2() /* comment */ ->bar3() // comment ->bar4() ->bar5() /** buahaha */ ->bar6() /** buahaha */ ->bar7();', 'bar1() // comment ->bar2() /* comment */ ->bar3() // comment ->bar4() ->bar5() /** buahaha */ ->bar6() /** buahaha */->bar7();', ]; yield [ 'bar1() ->bar2();', 'bar1() ->bar2();', ]; yield [ 'bar();', 'bar();', ]; yield [ 'bar()->baz() ->qux();', 'bar()->baz() ->qux();', ]; yield [ 'bar1() ->bar2();', ]; yield [ 'files() ;', ]; yield [ 'files() ;', ]; yield [ 'setAllowedTypes([\'array\']) ->setNormalizer(function (Options $options, $value) use ($toTypes, $default) { return $normalizedValue; }) ->setDefault($default) ->setWhitespacesConfig( new WhitespacesFixerConfig($config[\'indent\'], $config[\'lineEnding\']) ) ;', ]; yield [ 'bar ( new foo() ) ->bar(); ', ]; yield [ 'Foo([ (new Bar())->foo(), ]) ]); ', ]; yield [ 'foo() ->bar() ;', 'foo() ->bar() ;', ]; yield [ '
method() ->method(); ?>
method() ->method(); ?>', '
method() ->method(); ?>
method() ->method(); ?>', ]; yield [ 'setFoo(1) ->setBar([ 1 => 1, ]) ->setBaz(true) ->setX(array( 2 => 2, )) ->setY(); ', 'setFoo(1) ->setBar([ 1 => 1, ]) ->setBaz(true) ->setX(array( 2 => 2, )) ->setY(); ', ]; yield [ 'setEmail("voff.web@gmail.com", ) ->setPassword("233434" ,) ->setEmailConfirmed(false , ) ->setEmailConfirmationCode("123456", ); ', 'setEmail("voff.web@gmail.com", ) ->setPassword("233434" ,) ->setEmailConfirmed(false , ) ->setEmailConfirmationCode("123456", ); ', ]; yield [ 'setBar((new Bar) ->baz()); ', 'setBar((new Bar) ->baz()); ', ]; yield [ 'foo("bar", function ($baz) { return $baz ->on("table1", "table2"); }) ->where("a", "b"); ', 'foo("bar", function ($baz) { return $baz ->on("table1", "table2"); }) ->where("a", "b"); ', ]; yield [ 'foo("baz", fn ($bar) => $bar ->baz("foobar")) ->baz(); ', 'foo("baz", fn ($bar) => $bar ->baz("foobar")) ->baz(); ', ]; yield [ 'foo("baz", fn (string $bar) => otherFunc($bar) ->baz("foobar")) ->baz(); ', 'foo("baz", fn (string $bar) => otherFunc($bar) ->baz("foobar")) ->baz(); ', ]; yield [ 'foo("baz", fn (SomeClass $bar) => $bar ->baz("foobar")) ->baz(); ', 'foo("baz", fn (SomeClass $bar) => $bar ->baz("foobar")) ->baz(); ', ]; yield [ 'foo("baz", fn (?AnotherClass $bar) => $bar ->baz("foobar")) ->baz(); ', 'foo("baz", fn (?AnotherClass $bar) => $bar ->baz("foobar")) ->baz(); ', ]; yield [ 'foo("baz", fn ($bar) => $bar ->baz/*buahaha*/("foobar")) ->/**buahaha*/baz(); ', 'foo("baz", fn ($bar) => $bar ->baz/*buahaha*/("foobar")) ->/**buahaha*/baz(); ', ]; yield [ ' foo("baz", fn ($bar) => $bar ->baz ("foobar")) -> baz (); ', ' foo("baz", fn ($bar) => $bar ->baz ("foobar")) -> baz (); ', ]; yield [ 'bar;', ]; yield [ 'bar; if (foo()) { echo 123; } ', ]; yield [ 'bar?> bar, 1, 2, abc(), ]; ', ]; yield [ 'foo() ->bar; ', 'foo() ->bar; ', ]; yield [ 'foo() ->bar ->baz(); ', 'foo() ->bar ->baz(); ', ]; yield [ 'bar() ->baz; $obj ->foo(\'123\', 456) ->bar(\'789\') ->baz; ', 'bar() ->baz; $obj ->foo(\'123\', 456) ->bar(\'789\') ->baz; ', ]; } /** * @dataProvider provideWithWhitespacesConfigCases */ public function testWithWhitespacesConfig(string $expected, ?string $input = null): void { $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideWithWhitespacesConfigCases(): iterable { yield [ "setEmail('voff.web@gmail.com')\r\n\t->setPassword('233434')\r\n\t->setEmailConfirmed(false)\r\n\t->setEmailConfirmationCode('123456')\r\n\t->setHashsalt('1234')\r\n\t->setTncAccepted(true);", "setEmail('voff.web@gmail.com')\r\n\r\n ->setPassword('233434')\r\n\t\t\t->setEmailConfirmed(false)\r\n\t\t ->setEmailConfirmationCode('123456')\r\n->setHashsalt('1234')\r\n\t\t->setTncAccepted(true);", ]; } /** * @requires PHP 8.0 */ public function testFix80(): void { $this->doTest( 'setEmail("voff.web@gmail.com") ?->setPassword("233434") ?->setEmailConfirmed(false) ?->setEmailConfirmationCode("123456"); ', 'setEmail("voff.web@gmail.com") ?->setPassword("233434") ?->setEmailConfirmed(false) ?->setEmailConfirmationCode("123456"); ' ); } }