* 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\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @author Dariusz Rumiński * @author Andreas Möller * * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\BlankLineBeforeStatementFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\BlankLineBeforeStatementFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Whitespace\BlankLineBeforeStatementFixer */ final class BlankLineBeforeStatementFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideConfigureRejectsInvalidControlStatementCases * * @param mixed $controlStatement */ public function testConfigureRejectsInvalidControlStatement($controlStatement): void { $this->expectException(InvalidFixerConfigurationException::class); $this->fixer->configure([ 'statements' => [$controlStatement], ]); } public static function provideConfigureRejectsInvalidControlStatementCases(): iterable { yield 'null' => [null]; yield 'false' => [false]; yield 'true' => [true]; yield 'int' => [0]; yield 'float' => [3.14]; yield 'array' => [[]]; yield 'object' => [new \stdClass()]; yield 'unknown' => ['foo']; } /** * @dataProvider provideFixWithReturnCases */ public function testFixWithDefaultConfiguration(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @dataProvider provideFixWithBreakCases */ public function testFixWithBreak(string $expected, ?string $input = null): void { $this->fixer->configure([ 'statements' => ['break'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithBreakCases(): iterable { yield [ 'fixer->configure([ 'statements' => ['case'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithCaseCases(): iterable { yield [ 'fixer->configure([ 'statements' => ['continue'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithContinueCases(): iterable { yield [ 'fixer->configure([ 'statements' => ['declare'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithDeclareCases(): iterable { yield [ 'fixer->configure([ 'statements' => ['default'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithDefaultCases(): iterable { yield [ 'fixer->configure([ 'statements' => ['do'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithDoCases(): iterable { yield [ 'fixer->configure([ 'statements' => ['exit'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithExitCases(): iterable { yield [ 'fixer->configure([ 'statements' => ['for'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithForCases(): iterable { yield [ 'fixer->configure([ 'statements' => ['goto'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithGotoCases(): iterable { yield [ 'fixer->configure([ 'statements' => ['if'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithIfCases(): iterable { yield [ 'fixer->configure([ 'statements' => ['foreach'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithForEachCases(): iterable { yield [ 'fixer->configure([ 'statements' => ['include'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithIncludeCases(): iterable { yield [ 'fixer->configure([ 'statements' => ['include_once'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithIncludeOnceCases(): iterable { yield [ 'fixer->configure([ 'statements' => ['require'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithRequireCases(): iterable { yield [ 'fixer->configure([ 'statements' => ['require_once'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithRequireOnceCases(): iterable { yield [ 'fixer->configure([ 'statements' => ['return'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithReturnCases(): iterable { yield [ ' [ ' [ 'fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithReturnAndMessyWhitespacesCases(): iterable { yield [ "fixer->configure([ 'statements' => ['switch'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithSwitchCases(): iterable { yield [ 'fixer->configure([ 'statements' => ['throw'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithThrowCases(): iterable { yield [ 'error("No"); throw new \Exception("Something unexpected happened."); }', 'error("No"); throw new \Exception("Something unexpected happened."); }', ]; } /** * @dataProvider provideFixWithTryCases */ public function testFixWithTry(string $expected, ?string $input = null): void { $this->fixer->configure([ 'statements' => ['try'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithTryCases(): iterable { yield [ 'commit(); } catch (\Exception $exception) { $transaction->rollback(); }', ]; yield [ 'commit(); } catch (\Exception $exception) { $transaction->rollback(); }', 'commit(); } catch (\Exception $exception) { $transaction->rollback(); }', ]; } /** * @dataProvider provideFixWithWhileCases */ public function testFixWithWhile(string $expected, ?string $input = null): void { $this->fixer->configure([ 'statements' => ['while'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithWhileCases(): iterable { yield [ 'work(); }', ]; yield [ 'work(); }', 'work(); }', ]; yield [ 'work(); } while (true);', 'work(); } while (true);', ]; } /** * @dataProvider provideFixWithYieldCases */ public function testFixWithYield(string $expected, ?string $input = null): void { $this->fixer->configure([ 'statements' => ['yield'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithYieldCases(): iterable { yield [ ' $a; yield "a" => $b; }', ' $a; yield "a" => $b; }', ]; yield [ 'fixer->configure([ 'statements' => ['yield_from'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithYieldFromCases(): iterable { yield [ 'fixer->configure(['statements' => $statements]); $this->doTest($expected, $input); } public static function provideFixWithMultipleConfigStatementsCases(): iterable { $statementsWithoutCaseOrDefault = [ 'break', 'continue', 'declare', 'do', 'for', 'foreach', 'if', 'include', 'include_once', 'require', 'require_once', 'return', 'switch', 'throw', 'try', 'while', ]; $allStatements = [...$statementsWithoutCaseOrDefault, 'case', 'default']; yield [ $statementsWithoutCaseOrDefault, 'fixer->configure([ 'statements' => ['default'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFix80Cases(): iterable { yield 'match' => [ ' "a", default => "b" }; match ($foo) { 1 => "a1", default => "b2" }; ', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->fixer->configure([ 'statements' => ['case'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFix81Cases(): iterable { yield 'enum' => [ 'fixer->configure([ 'statements' => ['phpdoc'], ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithDocCommentCases(): iterable { yield [ '