* 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 */ 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); } public static function provideCommentsCases(): iterable { return [ [ 'bar[1]} {$foo->bar[1]}."; EOF; $this->doTest($expected); } public function testLeaveFunctions(): void { $expected = <<<'EOF' doTest($expected); } public static function provideFixSpaceOutsideOffsetCases(): iterable { yield [ ' $configuration * * @dataProvider provideFixWithConfigurationCases */ public function testFixWithConfiguration(array $configuration, string $expected, string $input): void { $this->fixer->configure(['positions' => $configuration]); $this->doTest($expected, $input); } public static function provideFixWithConfigurationCases(): iterable { $tests = [ [ ['inside', 'outside'], <<<'EOT' $test) { if (\PHP_VERSION_ID >= 8_00_00) { $test[1] = str_replace('{', '[', $test[1]); $test[1] = str_replace('}', ']', $test[1]); $test[2] = str_replace('{', '[', $test[2]); $test[2] = str_replace('}', ']', $test[2]); } yield $index => $test; } yield 'Config "default".' => [ ['inside', 'outside'], '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]; ', ]; } public function testWrongConfig(): void { $this->expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches('/^\[no_spaces_around_offset\] Invalid configuration: The option "positions" .*\.$/'); $this->fixer->configure(['positions' => ['foo']]); } }