* 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; /** * @author Javier Spagnoletti * * @internal * * @covers \PhpCsFixer\Fixer\Whitespace\NoSpacesAroundOffsetFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\Whitespace\NoSpacesAroundOffsetFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\Whitespace\NoSpacesAroundOffsetFixer */ final class NoSpacesAroundOffsetFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixSpaceInsideOffsetCases */ public function testFixSpaceInsideOffset(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @dataProvider provideFixSpaceOutsideOffsetCases */ public function testFixSpaceOutsideOffset(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } public function testLeaveNewLinesAlone(): void { $expected = <<<'EOF' doTest($expected); } /** * @dataProvider provideCommentsCases */ public function testComments(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable */ public static function provideCommentsCases(): iterable { yield [ 'bar[1]} {$foo->bar[1]}."; EOF; $this->doTest($expected); } public function testLeaveFunctions(): void { $expected = <<<'EOF' doTest($expected); } /** * @return iterable */ public static function provideFixSpaceOutsideOffsetCases(): iterable { yield [ ' */ public static function provideFixSpaceInsideOffsetCases(): iterable { yield [ 'fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithConfigurationCases(): iterable { yield 'Config "default".' => [ 'attributes->get(1)) { return false; } [ $class , $method ] = $this->splitControllerClassAndMethod($controllerName); $a = $b[0]; ', 'attributes->get(1)) { return false; } [ $class , $method ] = $this->splitControllerClassAndMethod($controllerName); $a = $b [0]; ', ['positions' => ['inside', 'outside']], ]; } /** * @param _AutogeneratedInputConfiguration $configuration * * @dataProvider provideFixPre80Cases * * @requires PHP <8.0 */ public function testFixPre80(string $expected, ?string $input = null, array $configuration = []): void { $this->fixer->configure($configuration); $this->doTest($expected, $input); } public static function provideFixPre80Cases(): iterable { yield [ 'fixer->configure($configuration); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFix80Cases(): iterable { foreach (self::provideMultiDimensionalArrayCases() as $index => $test) { $test[0] = str_replace('{', '[', $test[0]); $test[0] = str_replace('}', ']', $test[0]); $test[1] = str_replace('{', '[', $test[1]); $test[1] = str_replace('}', ']', $test[1]); yield $index => $test; } } public function testWrongConfig(): void { $this->expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches('/^\[no_spaces_around_offset\] Invalid configuration: The option "positions" .*\.$/'); $this->fixer->configure(['positions' => ['foo']]); // @phpstan-ignore-line } /** * @return iterable */ private static function provideMultiDimensionalArrayCases(): iterable { yield [ <<<'EOT' ['inside']], ]; yield [ <<<'EOT' ['outside']], ]; } }