* 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\PhpUnit; use PhpCsFixer\Fixer\PhpUnit\PhpUnitTargetVersion; use PhpCsFixer\Tests\Test\AbstractFixerTestCase; use PhpCsFixer\WhitespacesFixerConfig; /** * @author Dariusz Rumiński * * @internal * * @covers \PhpCsFixer\Fixer\PhpUnit\PhpUnitNoExpectationAnnotationFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitNoExpectationAnnotationFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitNoExpectationAnnotationFixer */ final class PhpUnitNoExpectationAnnotationFixerTest extends AbstractFixerTestCase { /** * @param _AutogeneratedInputConfiguration $config * * @dataProvider provideFixCases */ public function testFix(string $expected, ?string $input = null, array $config = []): void { $this->fixer->configure($config); $this->doTest($expected, $input); } public static function provideFixCases(): iterable { yield 'empty exception message' => [ 'setExpectedException(\FooException::class, \'\'); aaa(); } }', ' [ 'setExpectedException(\FooException::class); aaa(); } }', ' [ 'setExpectedException(\FooException::class); aaa(); } }', ' [ 'setExpectedException(\FooException::class, \'foo@bar\'); aaa(); } }', ' [ 'setExpectedException(\FooException::class, null, 123); aaa(); } }', ' [ 'setExpectedException(\FooException::class, \'foo\', 123); aaa(); } }', ' [ ' PhpUnitTargetVersion::VERSION_3_2], ]; yield 'expecting exception with msg regex' => [ 'setExpectedExceptionRegExp(\FooException::class, \'/foo.*$/\'); aaa(); } }', ' PhpUnitTargetVersion::VERSION_4_3], ]; yield 'use_class_const=false' => [ 'setExpectedException(\'FooException\'); aaa(); } }', ' false], ]; yield 'keep rest of docblock' => [ 'setExpectedException(\FooException::class); aaa(); } }', ' [ 'setExpectedException(\FooException::class); aaa(); } }', ' [ 'setExpectedException(\FooException::class); aaa(); } }', ' [ 'setExpectedException(\FooException::class); aaa(); } }', ' [ ' [ 'setExpectedException(\FooException::class, \'Foo \\\' bar " baz\'); aaa(); } }', ' [ <<<'EOT' setExpectedException(\Cake\View\Exception\MissingElementException::class, 'A backslash at the end \\'); $this->View->element('non_existent_element'); } /** */ public function testElementNonExistentTwo() { $this->setExpectedExceptionRegExp(\Cake\View\Exception\MissingElementException::class, '#^Element file "Element[\\\\/]non_existent_element\\.ctp" is missing\\.$#'); $this->View->element('non_existent_element'); } } EOT, <<<'EOT' View->element('non_existent_element'); } /** * @expectedException \Cake\View\Exception\MissingElementException * @expectedExceptionMessageRegExp #^Element file "Element[\\/]non_existent_element\.ctp" is missing\.$# */ public function testElementNonExistentTwo() { $this->View->element('non_existent_element'); } } EOT, ]; yield 'message on newline' => [ <<<'EOT' setExpectedException(\RuntimeException::class, 'Message on multilines AAA €'); aaa(); } /** * @foo */ public function testMessageOnMultilinesWithAnotherTag() { $this->setExpectedException(\RuntimeException::class, 'Message on multilines BBB è'); bbb(); } /** * * @foo */ public function testMessageOnMultilinesWithAnotherSpaceAndTag() { $this->setExpectedException(\RuntimeException::class, 'Message on multilines CCC ✔'); ccc(); } } EOT, <<<'EOT' [ 'setExpectedException(\FooException::class); aaa(); } }', ' [ ' [ 'setExpectedException(\FooException::class); aaa(); } }', ' [ 'setExpectedException(\Foo\Bar::class); $this->initialize(); } }', 'initialize(); } }', ]; } /** * @dataProvider provideWithWhitespacesConfigCases */ public function testWithWhitespacesConfig(string $expected, ?string $input = null): void { $expected = str_replace([' ', "\n"], ["\t", "\r\n"], $expected); if (null !== $input) { $input = str_replace([' ', "\n"], ["\t", "\r\n"], $input); } $this->fixer->setWhitespacesConfig(new WhitespacesFixerConfig("\t", "\r\n")); $this->doTest($expected, $input); } /** * @return iterable */ public static function provideWithWhitespacesConfigCases(): iterable { yield [ 'setExpectedException(\FooException::class, \'foo\', 123); aaa(); } }', 'setExpectedException(\FooException::class, \'foo\', 123); aaa(); } }', '