* 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\FunctionNotation; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @author Kuanhung Chen * * @internal * * @covers \PhpCsFixer\Fixer\FunctionNotation\MethodArgumentSpaceFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\MethodArgumentSpaceFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\FunctionNotation\MethodArgumentSpaceFixer */ final class MethodArgumentSpaceFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $indent = ' '; $lineEnding = "\n"; if (str_contains($expected, "\t")) { $indent = "\t"; } elseif (preg_match('/\n \S/', $expected)) { $indent = ' '; } if (str_contains($expected, "\r")) { $lineEnding = "\r\n"; } $this->fixer->configure($configuration); $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig( $indent, $lineEnding )); $this->doTest($expected, $input); } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFixWithDifferentLineEndings(string $expected, ?string $input = null, array $configuration = []): void { if (null !== $input) { $input = str_replace("\n", "\r\n", $input); } $this->testFix( str_replace("\n", "\r\n", $expected), $input, $configuration ); } public static function provideFixCases(): iterable { yield [ 'some_method( $var2 ); // space '.' $var2 = some_function( $var2 ); // space '.' $var2a = $z[1]( $var2a ); '.' $var3 = function( $a, $b ) { }; ', 'some_method( $var2); // space '.' $var2 = some_function( $var2); // space '.' $var2a = $z[1]( $var2a ); '.' $var3 = function( $a , $b ) { }; ', [ 'on_multiline' => 'ensure_fully_multiline', ], ]; yield 'default' => [ ' [ ' [ ' [ ' true], ]; yield 'test method call (I)' => [ ' [ 'foo(), $c=30);', 'foo() ,$c=30);', ]; yield 'test method call with multiple spaces (I)' => [ ' [ ' true], ]; yield 'test method call with tab' => [ ' [ " true], ]; yield 'test method call with multiple spaces (II)' => [ 'foo(), $c=30);', 'foo() ,$c=30);', ]; yield 'test method call with multiple spaces (II) (kmsac)' => [ 'foo(), $c=30);', 'foo() ,$c=30);', ['keep_multiple_spaces_after_comma' => true], ]; yield 'test named class constructor call' => [ 'foo(), $c=30);', 'foo() ,$c=30);', ]; yield 'test named class constructor call with multiple spaces' => [ ' [ ' true], ]; yield 'test anonymous class constructor call' => [ 'foo(), $c=30) {};', 'foo() ,$c=30) {};', ]; yield 'test anonymous class constructor call with multiple spaces' => [ ' [ ' true], ]; yield 'test receiving data in list context with omitted values' => [ ' [ ' [ ' true], ]; yield 'skip array' => [ '', ]; yield 'list call with trailing comma' => [ ' [ ' [ ' [ ' true], ]; yield 'multi line testing method call' => [ ' [ ' [ ' [ ' true], ]; yield 'ignore commas inside call argument' => [ ' [ ' [ '"apple", "b"=>"bed" ,"c"=>"car"]; $bar = ["a" ,"b" ,"c"]; ', ]; yield 'don\'t change HEREDOC and NOWDOC' => [ "foo( << [ ' 'ignore'], ]; yield 'with_random_comments on_multiline:ensure_single_line' => [ ' 'ensure_single_line'], ]; yield 'with_random_comments on_multiline:ensure_fully_multiline' => [ ' 'ensure_fully_multiline'], ]; yield 'test half-multiline function becomes fully-multiline' => [ <<<'EXPECTED' [ ' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' [ <<<'UNAFFECTED' with(' [ <<<'UNAFFECTED' [ <<<'UNAFFECTED' [ <<<'EXPECTED' [ <<<'UNAFFECTED' [ <<<'UNAFFECTED' [ <<<'EXPECTED' [ <<<'EXPECTED' 'ensure_single_line'], ]; yield 'ensure_single_line_with_random_comments' => [ <<<'EXPECTED' 'ensure_single_line'], ]; yield 'ensure_single_line_with_consecutive_newlines' => [ <<<'EXPECTED' 'ensure_single_line'], ]; yield 'ensure_single_line_methods' => [ <<<'EXPECTED' 'ensure_single_line'], ]; yield 'ensure_single_line_methods_in_anonymous_class' => [ <<<'EXPECTED' 'ensure_single_line'], ]; yield 'ensure_single_line_keep_spaces_after_comma' => [ <<<'EXPECTED' 'ensure_single_line', 'keep_multiple_spaces_after_comma' => true, ], ]; yield 'fix closing parenthesis (without trailing comma)' => [ ' 'ensure_fully_multiline', ], ]; yield 'test anonymous functions' => [ ' [ ' [ 'method( 42 )} some text TEXT; } PHP]; yield [<<<'PHP' method( 42 )} some text TEXT; } PHP]; yield [<<<'PHP' method( 42 )} some text TEXT; } PHP]; } /** * @param _AutogeneratedInputConfiguration $config * * @dataProvider provideFix2Cases */ public function testFix2(string $expected, ?string $input = null, array $config = []): void { $this->fixer->configure($config); $this->doTest($expected, $input); } public static function provideFix2Cases(): iterable { yield [ ' true], ]; yield [ <<<'EXPECTED' 'ensure_fully_multiline'], ]; yield [ ' 'ensure_fully_multiline', ], ]; yield [ ' null;', ' null;', [ 'on_multiline' => 'ensure_fully_multiline', ], ]; } /** * @param _AutogeneratedInputConfiguration $config * * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null, array $config = []): void { $this->fixer->configure($config); $this->doTest($expected, $input); } public static function provideFix80Cases(): iterable { yield 'multiple attributes' => [ ' [ ' 'ignore', ], ]; yield 'multiple attributes on the same line as argument' => [ ' 'same_line', ], ]; yield 'single attribute markup with comma separated list' => [ ' [ ' [ ' [ 'doTest($expected, $input); } /** * @return iterable */ public static function provideFix81Cases(): iterable { yield [ '', '', ]; } }