* 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\PhpUnitExpectationFixer * * @extends AbstractFixerTestCase<\PhpCsFixer\Fixer\PhpUnit\PhpUnitExpectationFixer> * * @phpstan-import-type _AutogeneratedInputConfiguration from \PhpCsFixer\Fixer\PhpUnit\PhpUnitExpectationFixer */ final class PhpUnitExpectationFixerTest 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 [ 'expectException(\'RuntimeException\'); $this->expectExceptionMessage(\'msg\'/*B*/); $this->expectExceptionCode(/*C*/123); zzz(); } }', 'setExpectedException(\'RuntimeException\', \'msg\'/*B*/, /*C*/123); zzz(); } }', ]; yield [ 'expectException(\'RuntimeException\'/*B*/ /*B2*/); $this->expectExceptionCode(/*C*/123); zzz(); } function testFnc2() { aaa(); $this->expectException(\'RuntimeException\'); $this->expectExceptionMessage(/*B*/ null /*B2*/ + 1); $this->expectExceptionCode(/*C*/123); zzz(); } }', 'setExpectedException(\'RuntimeException\',/*B*/ null /*B2*/,/*C*/123); zzz(); } function testFnc2() { aaa(); $this->setExpectedException(\'RuntimeException\',/*B*/ null /*B2*/ + 1,/*C*/123); zzz(); } }', ]; yield [ 'expectException( \Exception::class ); } }', 'setExpectedException( \Exception::class ); } }', ]; yield [ 'expectException( \Exception::class ); $this->expectExceptionMessage( "foo" ); $this->expectExceptionCode( 123 ); } }', 'setExpectedException( \Exception::class, "foo", 123 ); } }', ]; yield [ 'expectException("RuntimeException"); $this->expectExceptionMessage("Msg"); $this->expectExceptionCode(123); foo(); } public function testBar() { $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123); bar(); } }', 'setExpectedException("RuntimeException", "Msg", 123); foo(); } public function testBar() { $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123); bar(); } }', ['target' => PhpUnitTargetVersion::VERSION_5_2], ]; yield [ 'expectException("RuntimeException"); $this->expectExceptionMessage("Msg"); $this->expectExceptionCode(123); foo(); } public function testBar() { $this->expectException("RuntimeException"); $this->expectExceptionMessageRegExp("/Msg.*/"); $this->expectExceptionCode(123); bar(); } }', 'setExpectedException("RuntimeException", "Msg", 123); foo(); } public function testBar() { $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123); bar(); } }', ['target' => PhpUnitTargetVersion::VERSION_5_6], ]; yield [ 'expectException("RuntimeException"); $this->expectExceptionMessage("Msg"); $this->expectExceptionCode(123); foo(); } public function testBar() { $this->expectException("RuntimeException"); $this->expectExceptionMessageMatches("/Msg.*/"); $this->expectExceptionCode(123); bar(); } }', 'setExpectedException("RuntimeException", "Msg", 123); foo(); } public function testBar() { $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123); bar(); } }', ['target' => PhpUnitTargetVersion::VERSION_8_4], ]; yield [ 'expectExceptionMessageMatches("/Msg.*/"); foo(); } }', 'expectExceptionMessageRegExp("/Msg.*/"); foo(); } }', ['target' => PhpUnitTargetVersion::VERSION_8_4], ]; yield [ 'expectExceptionMessageMatches("/Msg.*/"); $this->expectExceptionMessageMatches("fail-case"); foo(); } }', 'expectExceptionMessageRegExp("/Msg.*/", "fail-case"); foo(); } }', ['target' => PhpUnitTargetVersion::VERSION_8_4], ]; yield [ 'expectException("RuntimeException"); $this->expectExceptionMessage("Msg"); $this->expectExceptionCode(123); foo(); } public function testBar() { $this->expectException("RuntimeException"); $this->expectExceptionMessageMatches("/Msg.*/"); $this->expectExceptionCode(123); bar(); } }', 'setExpectedException("RuntimeException", "Msg", 123); foo(); } public function testBar() { $this->setExpectedExceptionRegExp("RuntimeException", "/Msg.*/", 123); bar(); } }', ['target' => PhpUnitTargetVersion::VERSION_NEWEST], ]; yield [ 'expectException("RuntimeException"); $this->expectExceptionMessage("msg"/*B*/); $this->expectExceptionCode(/*C*/123); zzz(); } }', 'setExpectedException("RuntimeException", "msg"/*B*/, /*C*/123, ); zzz(); } }', ]; yield [ 'expectException("RuntimeException"); $this->expectExceptionMessage("msg"/*B*/); $this->expectExceptionCode(/*C*/123/*D*/); zzz(); } }', 'setExpectedException("RuntimeException", "msg"/*B*/, /*C*/123, /*D*/); zzz(); } }', ]; } /** * @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 { $expectedTemplate = ' function testFnc%d() { aaa(); $this->expectException(\'RuntimeException\'); $this->expectExceptionMessage(\'msg\'/*B*/); $this->expectExceptionCode(/*C*/123); zzz(); } '; $inputTemplate = ' function testFnc%d() { aaa(); $this->setExpectedException(\'RuntimeException\', \'msg\'/*B*/, /*C*/123); zzz(); } '; $input = $expected = 'doTest( 'expectException("RuntimeException"); $this->expectExceptionMessage("message"); $this->expectExceptionCode(123); zzz(); } }', 'setExpectedException("RuntimeException", "message", 123); zzz(); } }' ); } }