* 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\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @author Denis Sokolov * @author Dariusz Rumiński * * @internal * * @covers \PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\FunctionNotation\FunctionDeclarationFixer */ final class FunctionDeclarationFixerTest extends AbstractFixerTestCase { /** * @var array */ private static $configurationClosureSpacingNone = ['closure_function_spacing' => FunctionDeclarationFixer::SPACING_NONE]; /** * @var array */ private static $configurationArrowSpacingNone = ['closure_fn_spacing' => FunctionDeclarationFixer::SPACING_NONE]; public function testInvalidConfigurationClosureFunctionSpacing(): void { $this->expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches( '#^\[function_declaration\] Invalid configuration: The option "closure_function_spacing" with value "neither" is invalid\. Accepted values are: "none", "one"\.$#' ); $this->fixer->configure(['closure_function_spacing' => 'neither']); // @phpstan-ignore-line } public function testInvalidConfigurationClosureFnSpacing(): void { $this->expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches( '#^\[function_declaration\] Invalid configuration: The option "closure_fn_spacing" with value "neither" is invalid\. Accepted values are: "none", "one"\.$#' ); $this->fixer->configure(['closure_fn_spacing' => 'neither']); // @phpstan-ignore-line } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } public static function provideFixCases(): iterable { yield [ // non-PHP test 'function foo () {}', ]; yield [ '']; yield ['', null, self::$configurationClosureSpacingNone]; yield [ ' null;', ' null;', ]; yield [ ' null;', ' null;', ]; yield [ ' null;', ' null;', ]; yield [ ' null;', ' null;', ]; yield [ ' null;', null, self::$configurationArrowSpacingNone, ]; yield [ ' null;', ' null;', self::$configurationArrowSpacingNone, ]; yield [ ' null;', ' null;', self::$configurationArrowSpacingNone, ]; yield [ ' null;', ' null;', self::$configurationArrowSpacingNone, ]; yield [ ' null;', ' null;', self::$configurationArrowSpacingNone, ]; yield [ ' $a;', ' $a;', ]; yield [ ' $a;', ' $a;', self::$configurationArrowSpacingNone, ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixPhp80Cases * * @requires PHP 8.0 */ public function testFixPhp80(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } public static function provideFixPhp80Cases(): iterable { yield [ ' null;', ' null;', self::$configurationArrowSpacingNone, ]; yield [ ' true], ]; } }