* 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\ControlStructure; use PhpCsFixer\ConfigurationException\InvalidForEnvFixerConfigurationException; use PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @author Sebastiaan Stok * @author Kuba Werłos * * @internal * * @covers \PhpCsFixer\Fixer\ControlStructure\TrailingCommaInMultilineFixer */ final class TrailingCommaInMultilineFixerTest extends AbstractFixerTestCase { /** * @requires PHP <8.0 * * @dataProvider provideInvalidConfigurationCases * * @param mixed $exceptionMessega * @param mixed $configuration */ public function testInvalidConfiguration($exceptionMessega, $configuration): void { $this->expectException(InvalidForEnvFixerConfigurationException::class); $this->expectExceptionMessage($exceptionMessega); $this->fixer->configure($configuration); } public static function provideInvalidConfigurationCases(): iterable { yield [ '[trailing_comma_in_multiline] Invalid configuration for env: "parameters" option can only be enabled with PHP 8.0+.', ['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_PARAMETERS]], ]; } /** * @param array $config * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $config = []): void { $this->fixer->configure($config); $this->doTest($expected, $input); } public static function provideFixCases(): iterable { // long syntax tests yield [' array( 2 => 3, ), );', ' array( 2 => 3 ) );', ]; yield [ " function ($b) { return "bar".$b; });', ]; yield [ ' 1, "b" => 2, );', ' 1, "b" => 2 );', ]; yield [ ' function ($b) { return "bar".$b; }];', ]; yield [ ' 1, "b" => 2, ];', ' 1, "b" => 2 ];', ]; yield [ ' 1, "b" => 2, ); }', ' 1, "b" => 2 ); }', ]; yield [ ' 1, "b" => 2, ]; }', ' 1, "b" => 2 ]; }', ]; yield [' [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; yield [ " [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; yield [ ' [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; yield [ " [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; yield [ " [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; yield [ " [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; yield [ " [TrailingCommaInMultilineFixer::ELEMENTS_ARRAYS]], ]; yield [ " [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; yield [ " [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; yield [ " [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; yield [ " [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; yield [ " [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; yield [ " [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; yield [ 'method( 1, 2, ); ', 'method( 1, 2 ); ', ['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; yield [ ' [TrailingCommaInMultilineFixer::ELEMENTS_ARRAYS, TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; yield [ ' [TrailingCommaInMultilineFixer::ELEMENTS_ARRAYS, TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; yield [ <<<'EXPECTED' true], ]; yield [ ' [TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; } /** * @param array $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 [ ' [TrailingCommaInMultilineFixer::ELEMENTS_PARAMETERS]], ]; yield [ ' [TrailingCommaInMultilineFixer::ELEMENTS_ARRAYS, TrailingCommaInMultilineFixer::ELEMENTS_ARGUMENTS]], ]; yield [ ' [TrailingCommaInMultilineFixer::ELEMENTS_PARAMETERS]], ]; yield [ ' [TrailingCommaInMultilineFixer::ELEMENTS_PARAMETERS]], ]; yield [ ' $x + $y;', ' $x + $y;', ['elements' => [TrailingCommaInMultilineFixer::ELEMENTS_PARAMETERS]], ]; yield 'match' => [ ' null, 400 => 1, 500 => function() {return 2;}, 600 => static function() {return 4;}, default => 3, }; $z = match ($a) { 1 => 0, 2 => 1, }; $b = match($c) {19 => 28, default => 333}; ', ' null, 400 => 1, 500 => function() {return 2;}, 600 => static function() {return 4;}, default => 3 }; $z = match ($a) { 1 => 0, 2 => 1 }; $b = match($c) {19 => 28, default => 333}; ', ['elements' => ['match']], ]; yield 'match with last comma in the same line as closing brace' => [ ' 0, 2 => 1 }; ', null, ['elements' => ['match']], ]; } }