* 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 */ final class BlankLineBeforeStatementFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideInvalidControlStatementCases * * @param mixed $controlStatement */ public function testConfigureRejectsInvalidControlStatement($controlStatement): void { $this->expectException(InvalidFixerConfigurationException::class); $this->fixer->configure([ 'statements' => [$controlStatement], ]); } public function provideInvalidControlStatementCases(): array { return [ 'null' => [null], 'false' => [false], 'true' => [true], 'int' => [0], 'float' => [3.14], 'array' => [[]], 'object' => [new \stdClass()], '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); } public function provideFixWithBreakCases(): array { return [ [ 'fixer->configure([ 'statements' => ['case'], ]); $this->doTest($expected, $input); } public function provideFixWithCaseCases(): array { return [ [ 'fixer->configure([ 'statements' => ['continue'], ]); $this->doTest($expected, $input); } public function provideFixWithContinueCases(): array { return [ [ 'fixer->configure([ 'statements' => ['declare'], ]); $this->doTest($expected, $input); } public function provideFixWithDeclareCases(): array { return [ [ 'fixer->configure([ 'statements' => ['default'], ]); $this->doTest($expected, $input); } public function provideFixWithDefaultCases(): array { return [ [ 'fixer->configure([ 'statements' => ['do'], ]); $this->doTest($expected, $input); } public function provideFixWithDoCases(): array { return [ [ 'fixer->configure([ 'statements' => ['exit'], ]); $this->doTest($expected, $input); } public function provideFixWithExitCases(): array { return [ [ 'fixer->configure([ 'statements' => ['for'], ]); $this->doTest($expected, $input); } public function provideFixWithForCases(): array { return [ [ 'fixer->configure([ 'statements' => ['goto'], ]); $this->doTest($expected, $input); } public function provideFixWithGotoCases(): array { return [ [ 'fixer->configure([ 'statements' => ['if'], ]); $this->doTest($expected, $input); } public function provideFixWithIfCases(): array { return [ [ 'fixer->configure([ 'statements' => ['foreach'], ]); $this->doTest($expected, $input); } public function provideFixWithForEachCases(): array { return [ [ 'fixer->configure([ 'statements' => ['include'], ]); $this->doTest($expected, $input); } public function provideFixWithIncludeCases(): array { return [ [ 'fixer->configure([ 'statements' => ['include_once'], ]); $this->doTest($expected, $input); } public function provideFixWithIncludeOnceCases(): array { return [ [ 'fixer->configure([ 'statements' => ['require'], ]); $this->doTest($expected, $input); } public function provideFixWithRequireCases(): array { return [ [ 'fixer->configure([ 'statements' => ['require_once'], ]); $this->doTest($expected, $input); } public function provideFixWithRequireOnceCases(): array { return [ [ 'fixer->configure([ 'statements' => ['return'], ]); $this->doTest($expected, $input); } public function provideFixWithReturnCases(): array { return [ [ 'fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->doTest($expected, $input); } public function provideFixWithReturnAndMessyWhitespacesCases(): array { return [ [ "fixer->configure([ 'statements' => ['switch'], ]); $this->doTest($expected, $input); } public function provideFixWithSwitchCases(): array { return [ [ 'fixer->configure([ 'statements' => ['throw'], ]); $this->doTest($expected, $input); } public function provideFixWithThrowCases(): array { return [ [ '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); } public function provideFixWithTryCases(): array { return [ [ 'commit(); } catch (\Exception $exception) { $transaction->rollback(); }', ], [ '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); } public function provideFixWithWhileCases(): array { return [ [ 'work(); }', ], [ 'work(); }', 'work(); }', ], [ '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); } /** * @yield array */ public function provideFixWithYieldCases(): array { return [ [ ' $a; yield "a" => $b; }', ' $a; yield "a" => $b; }', ], [ 'fixer->configure([ 'statements' => ['yield_from'], ]); $this->doTest($expected, $input); } /** * @yield array */ public function provideFixWithYieldFromCases(): array { return [ [ 'fixer->configure(['statements' => $statements]); $this->doTest($expected, $input); } public function provideFixWithMultipleConfigStatementsCases(): array { $statementsWithoutCaseOrDefault = [ 'break', 'continue', 'declare', 'do', 'for', 'foreach', 'if', 'include', 'include_once', 'require', 'require_once', 'return', 'switch', 'throw', 'try', 'while', ]; $allStatements = array_merge($statementsWithoutCaseOrDefault, ['case', 'default']); return [ [ $statementsWithoutCaseOrDefault, 'fixer->configure([ 'statements' => ['default'], ]); $this->doTest($expected, $input); } public function provideFix80Cases(): \Generator { 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); } public function provideFix81Cases(): \Generator { yield 'enum' => [ '