* 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 * @author SpacePossum * * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\BlankLineBeforeStatementFixer */ final class BlankLineBeforeStatementFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideInvalidControlStatementCases * * @param mixed $controlStatement */ public function testConfigureRejectsInvalidControlStatement($controlStatement) { $this->expectException(InvalidFixerConfigurationException::class); $this->fixer->configure([ 'statements' => [$controlStatement], ]); } /** * @return array */ public function provideInvalidControlStatementCases() { return [ 'null' => [null], 'false' => [false], 'true' => [true], 'int' => [0], 'float' => [3.14], 'array' => [[]], 'object' => [new \stdClass()], 'unknown' => ['foo'], ]; } /** * @dataProvider provideFixWithReturnCases * * @param string $expected * @param null|string $input */ public function testFixWithDefaultConfiguration($expected, $input = null) { $this->doTest($expected, $input); } /** * @dataProvider provideFixWithBreakCases * * @param string $expected * @param null|string $input */ public function testFixWithBreak($expected, $input = null) { $this->fixer->configure([ 'statements' => ['break'], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithBreakCases() { return [ [ 'fixer->configure([ 'statements' => ['continue'], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithContinueCases() { return [ [ 'fixer->configure([ 'statements' => ['declare'], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithDeclareCases() { return [ [ 'fixer->configure([ 'statements' => ['die'], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithDieCases() { return [ [ 'fixer->configure([ 'statements' => ['do'], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithDoCases() { return [ [ 'fixer->configure([ 'statements' => ['exit'], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithExitCases() { return [ [ 'fixer->configure([ 'statements' => ['for'], ]); $this->doTest($expected, $input); } public function provideFixWithForCases() { return [ [ 'fixer->configure([ 'statements' => ['goto'], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithGotoCases() { return [ [ 'fixer->configure([ 'statements' => ['if'], ]); $this->doTest($expected, $input); } /** * @dataProvider provideFixWithForEachCases * * @param string $expected * @param null|string $input */ public function testFixWithForEach($expected, $input = null) { $this->fixer->configure([ 'statements' => ['foreach'], ]); $this->doTest($expected, $input); } public function provideFixWithForEachCases() { return [ [ 'fixer->configure([ 'statements' => ['include'], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithIncludeCases() { return [ [ 'fixer->configure([ 'statements' => ['include_once'], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithIncludeOnceCases() { return [ [ 'fixer->configure([ 'statements' => ['require'], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithRequireCases() { return [ [ 'fixer->configure([ 'statements' => ['require_once'], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithRequireOnceCases() { return [ [ 'fixer->configure([ 'statements' => ['return'], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithReturnCases() { return [ [ ' $a = $a; return $a;', ], [ 'fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithReturnAndMessyWhitespacesCases() { return [ [ "fixer->configure([ 'statements' => ['switch'], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithSwitchCases() { return [ [ 'fixer->configure([ 'statements' => ['throw'], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithThrowCases() { return [ [ 'error("No"); throw new \Exception("Something unexpected happened"); }', 'error("No"); throw new \Exception("Something unexpected happened"); }', ], ]; } /** * @dataProvider provideFixWithTryCases * * @param string $expected * @param null|string $input */ public function testFixWithTry($expected, $input = null) { $this->fixer->configure([ 'statements' => ['try'], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithTryCases() { return [ [ 'commit(); } catch (\Exception $exception) { $transaction->rollback(); }', ], [ 'commit(); } catch (\Exception $exception) { $transaction->rollback(); }', 'commit(); } catch (\Exception $exception) { $transaction->rollback(); }', ], ]; } /** * @dataProvider provideFixWithWhileCases * * @param string $expected * @param null|string $input */ public function testFixWithWhile($expected, $input = null) { $this->fixer->configure([ 'statements' => ['while'], ]); $this->doTest($expected, $input); } /** * @return array */ public function provideFixWithWhileCases() { return [ [ 'work(); }', ], [ 'work(); }', 'work(); }', ], [ 'work(); } while (true);', 'work(); } while (true);', ], ]; } /** * @dataProvider provideFixWithYieldCases * * @param string $expected * @param null|string $input */ public function testFixWithYield($expected, $input = null) { $this->fixer->configure([ 'statements' => ['yield'], ]); $this->doTest($expected, $input); } /** * @yield array */ public function provideFixWithYieldCases() { return [ [ 'fixer->configure(['statements' => $statements]); $this->doTest($expected, $input); } public function provideFixWithMultipleConfigStatementsCases() { $allStatements = [ 'break', 'continue', 'declare', 'do', 'for', 'foreach', 'if', 'include', 'include_once', 'require', 'require_once', 'return', 'switch', 'throw', 'try', 'while', ]; return [ [ $allStatements, '