* 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\Fixer\ControlStructure\NoUnneededControlParenthesesFixer; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; /** * @author Sullivan Senechal * @author Gregor Harlan * * @internal * * @covers \PhpCsFixer\Fixer\ControlStructure\NoUnneededControlParenthesesFixer */ final class NoUnneededControlParenthesesFixerTest extends AbstractFixerTestCase { private static $defaultStatements; public static function setUpBeforeClass(): void { parent::setUpBeforeClass(); $fixer = new NoUnneededControlParenthesesFixer(); foreach ($fixer->getConfigurationDefinition()->getOptions() as $option) { if ('statements' === $option->getName()) { self::$defaultStatements = $option->getDefault(); break; } } } /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, ?string $fixStatement = null): void { $this->fixerTest($expected, $input, $fixStatement); } public function provideFixCases(): array { return [ [ 'getSubject() : $obj2); ', ], [ '', ], [ '', '', ], [ '', '', ], [ 'getOutput(1); ', 'getOutput(1); ', ], [ ' ', ' ', ], [ 'getSubject() ?? $obj2); ', ], ]; } /** * @dataProvider provideFixYieldFromCases */ public function testFixYieldFrom(string $expected, ?string $input = null): void { $this->fixer->configure(['statements' => ['yield_from']]); $this->doTest($expected, $input); } public function provideFixYieldFromCases(): array { return [ [ 'fixer->configure(['statements' => ['switch_case']]); $this->doTest($expected, $input); } public function provideFix81Cases(): \Generator { yield 'enums' => [ 'doTest($expected, $input); $this->fixer->configure($legacy ? self::$defaultStatements : ['statements' => self::$defaultStatements]); $this->doTest($expected, $input); // Empty array config. Should not fix anything. $this->fixer->configure([]); $this->doTest($expected, null); // Test with only one statement foreach (self::$defaultStatements as $statement) { $withInput = false; if (null !== $input && (null === $fixStatement || $fixStatement === $statement)) { foreach (explode('_', $statement) as $singleStatement) { if (str_contains($input, $singleStatement)) { $withInput = true; break; } } } $this->fixer->configure($legacy ? [$statement] : ['statements' => [$statement]]); $this->doTest( $expected, $withInput ? $input : null ); } } }