* 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 */ 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); } public function provideFixCases(): array { return [ [ 'foobarfoobarfoobarfoobarfoobarfoobar fixer->configure([ 'comment_text' => 'fall-through case!', ]); $this->doTest($expected, $input); } public function provideTestFixWithDifferentCommentTextCases(): array { $cases = $this->provideFixCases(); $replaceCommentText = static function (string $php): string { return 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]); } } return array_merge($cases, [ [ 'fixer->setWhitespacesConfig(new WhitespacesFixerConfig(' ', "\r\n")); $this->doTest($expected, $input); } public function provideTestFixWithDifferentLineEndingCases(): \Generator { foreach ($this->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, ]); } public function provideFixWithCommentTextContainingNewLinesCases(): array { return [ ["No\nbreak"], ["No\r\nbreak"], ["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); } public function provideFix80Cases(): \Generator { 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" }; ', ]; } /** * @dataProvider provideFix81Cases * @requires PHP 8.1 */ public function testFix81(string $expected, ?string $input = null): void { $this->doTest($expected, $input); } public function provideFix81Cases(): \Generator { yield 'enums' => [ '