* 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\ConfigurationException\InvalidFixerConfigurationException; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @internal * * @covers \PhpCsFixer\Fixer\ControlStructure\NoBreakCommentFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\ControlStructure\NoBreakCommentFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\ControlStructure\NoBreakCommentFixer */ final class NoBreakCommentFixerTest extends AbstractFixerTestCase { /** * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @dataProvider provideFixCases */ public function testFixWithExplicitDefaultConfiguration(string $expected, ?string $input = null): void { $this->fixer->configure([ 'comment_text' => 'no break', ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixCases(): iterable { yield [ 'foobarfoobarfoobarfoobarfoobarfoobar fixer->configure([ 'comment_text' => 'fall-through case!', ]); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithDifferentCommentTextCases(): iterable { $cases = self::provideFixCases(); $replaceCommentText = static fn (string $php): string => strtr($php, [ 'No break' => 'Fall-through case!', 'no break' => 'fall-through case!', ]); foreach ($cases as $case) { $case[0] = $replaceCommentText($case[0]); if (isset($case[1])) { $case[1] = $replaceCommentText($case[1]); } yield $case; } yield [ 'fixer->setWhitespacesConfig(new WhitespacesFixerConfig(' ', "\r\n")); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFixWithDifferentLineEndingCases(): iterable { foreach (self::provideFixCases() as $case) { $case[0] = str_replace("\n", "\r\n", $case[0]); if (isset($case[1])) { $case[1] = str_replace("\n", "\r\n", $case[1]); } yield $case; } } public function testFixWithCommentTextWithSpecialRegexpCharacters(): void { $this->fixer->configure([ 'comment_text' => '~***(//[No break here.]\\\)***~', ]); $this->doTest( 'fixer->configure([ 'comment_text' => 'no break ', ]); $this->doTest( 'expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches('/^\[no_break_comment\] Invalid configuration: The comment text must not contain new lines\.$/'); $this->fixer->configure([ 'comment_text' => $text, ]); } /** * @return iterable */ public static function provideFixWithCommentTextContainingNewLinesCases(): iterable { yield ["No\nbreak"]; yield ["No\r\nbreak"]; yield ["No\rbreak"]; } public function testConfigureWithInvalidOptions(): void { $this->expectException(InvalidFixerConfigurationException::class); $this->expectExceptionMessageMatches('/^\[no_break_comment\] Invalid configuration: The option "foo" does not exist\. Defined options are: "comment_text"\.$/'); $this->fixer->configure(['foo' => true]); } /** * @dataProvider provideFix80Cases * * @requires PHP 8.0 */ public function testFix80(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFix80Cases(): iterable { yield [ ' throw new Exception(); // no break case 4: $value = $falsableValue ?: throw new InvalidArgumentException(); // no break default: echo "PHP8"; } ', ' throw new Exception(); case 4: $value = $falsableValue ?: throw new InvalidArgumentException(); default: echo "PHP8"; } ', ]; yield [ ' "a", default => "b" }; match ($bar) { 2 => "c", default => "d" }; match ($baz) { 3 => "e", default => "f" }; ', ]; yield 'switch with break and nested match' => [ ' 2 }; break; default: $x = 3; }', ]; yield 'switch without break and nested match' => [ ' 2 }; // no break default: $x = 3; }', ' 2 }; default: $x = 3; }', ]; } /** * @dataProvider provideFix81Cases * * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } /** * @return iterable */ public static function provideFix81Cases(): iterable { yield 'enums' => [ '