From 17c09b33ac5d9cad1459ace0ae7b1f942d1e9afd Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Mon, 19 Nov 2018 13:06:20 +0100 Subject: [PATCH] Closes #3332 --- ChangeLog-8.0.md | 1 + src/Framework/TestCase.php | 2 + src/Util/Test.php | 3 + tests/end-to-end/regression/GitHub/244.phpt | 32 ------- .../regression/GitHub/244/Issue244Test.php | 65 -------------- .../Constraint/ExceptionMessageRegExpTest.php | 28 ++---- .../Constraint/ExceptionMessageTest.php | 28 +++--- tests/unit/Framework/TestCaseTest.php | 3 +- tests/unit/Framework/TestSuiteTest.php | 7 +- tests/unit/Util/JsonTest.php | 6 +- tests/unit/Util/TestTest.php | 90 ------------------- 11 files changed, 36 insertions(+), 229 deletions(-) delete mode 100644 tests/end-to-end/regression/GitHub/244.phpt delete mode 100644 tests/end-to-end/regression/GitHub/244/Issue244Test.php diff --git a/ChangeLog-8.0.md b/ChangeLog-8.0.md index 4c8d6338ecc..390485d1f18 100644 --- a/ChangeLog-8.0.md +++ b/ChangeLog-8.0.md @@ -7,6 +7,7 @@ All notable changes of the PHPUnit 8.0 release series are documented in this fil ### Changed * Implemented [#3288](https://github.com/sebastianbergmann/phpunit/issues/3288): The `void_return` fixer of php-cs-fixer is now in effect +* Implemented [#3332](https://github.com/sebastianbergmann/phpunit/issues/3332): Deprecate annotation(s) for expecting exceptions * Implemented [#3338](https://github.com/sebastianbergmann/phpunit/issues/3338): Deprecate assertions (and helper methods) that operate on (non-public) attributes * Implemented [#3341](https://github.com/sebastianbergmann/phpunit/issues/3341): Deprecate optional parameters of `assertEquals()` and `assertNotEquals()` * Implemented [#3369](https://github.com/sebastianbergmann/phpunit/issues/3369): Deprecate `assertInternalType()` and `assertNotInternalType()` diff --git a/src/Framework/TestCase.php b/src/Framework/TestCase.php index 58b9a2f6a7b..f70f658a354 100644 --- a/src/Framework/TestCase.php +++ b/src/Framework/TestCase.php @@ -1599,6 +1599,8 @@ private function setExpectedExceptionFromAnnotation(): void ); if ($expectedException !== false) { + $this->addWarning('The @expectedException, @expectedExceptionCode, @expectedExceptionMessage, and @expectedExceptionMessageRegExp annotations are deprecated. They will be removed in PHPUnit 9. Refactor your test to use expectException(), expectExceptionCode(), expectExceptionMessage(), or expectExceptionMessageRegExp() instead.'); + $this->expectException($expectedException['class']); if ($expectedException['code'] !== null) { diff --git a/src/Util/Test.php b/src/Util/Test.php index ee0561de186..639cf0eee39 100644 --- a/src/Util/Test.php +++ b/src/Util/Test.php @@ -344,6 +344,9 @@ public static function getMissingRequirements(string $className, string $methodN * Returns the expected exception for a test. * * @return array|false + * + * @deprecated + * @codeCoverageIgnore */ public static function getExpectedException(string $className, ?string $methodName) { diff --git a/tests/end-to-end/regression/GitHub/244.phpt b/tests/end-to-end/regression/GitHub/244.phpt deleted file mode 100644 index 2b5f2698eee..00000000000 --- a/tests/end-to-end/regression/GitHub/244.phpt +++ /dev/null @@ -1,32 +0,0 @@ ---TEST-- -GH-244: Expected Exception should support string codes ---FILE-- - ---EXPECTF-- -PHPUnit %s by Sebastian Bergmann and contributors. - -.FFF 4 / 4 (100%) - -Time: %s, Memory: %s - -There were 3 failures: - -1) Issue244Test::testFails -Failed asserting that '123StringCode' is equal to expected exception code 'OtherString'. - -2) Issue244Test::testFailsTooIfExpectationIsANumber -Failed asserting that '123StringCode' is equal to expected exception code 123. - -3) Issue244Test::testFailsTooIfExceptionCodeIsANumber -Failed asserting that 123 is equal to expected exception code '123String'. - -FAILURES! -Tests: 4, Assertions: 8, Failures: 3. diff --git a/tests/end-to-end/regression/GitHub/244/Issue244Test.php b/tests/end-to-end/regression/GitHub/244/Issue244Test.php deleted file mode 100644 index dc544b2cf72..00000000000 --- a/tests/end-to-end/regression/GitHub/244/Issue244Test.php +++ /dev/null @@ -1,65 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ -use PHPUnit\Framework\TestCase; - -class Issue244Test extends TestCase -{ - /** - * @expectedException Issue244Exception - * @expectedExceptionCode 123StringCode - */ - public function testWorks(): void - { - throw new Issue244Exception; - } - - /** - * @expectedException Issue244Exception - * @expectedExceptionCode OtherString - */ - public function testFails(): void - { - throw new Issue244Exception; - } - - /** - * @expectedException Issue244Exception - * @expectedExceptionCode 123 - */ - public function testFailsTooIfExpectationIsANumber(): void - { - throw new Issue244Exception; - } - - /** - * @expectedException Issue244ExceptionIntCode - * @expectedExceptionCode 123String - */ - public function testFailsTooIfExceptionCodeIsANumber(): void - { - throw new Issue244ExceptionIntCode; - } -} - -class Issue244Exception extends Exception -{ - public function __construct() - { - $this->code = '123StringCode'; - } -} - -class Issue244ExceptionIntCode extends Exception -{ - public function __construct() - { - $this->code = 123; - } -} diff --git a/tests/unit/Framework/Constraint/ExceptionMessageRegExpTest.php b/tests/unit/Framework/Constraint/ExceptionMessageRegExpTest.php index 4052f8cf4a4..0546616353b 100644 --- a/tests/unit/Framework/Constraint/ExceptionMessageRegExpTest.php +++ b/tests/unit/Framework/Constraint/ExceptionMessageRegExpTest.php @@ -13,43 +13,33 @@ class ExceptionMessageRegExpTest extends TestCase { - /** - * @expectedException \Exception - * @expectedExceptionMessageRegExp /^A polymorphic \w+ message/ - */ public function testRegexMessage(): void { + $this->expectException(\Exception::class); + $this->expectExceptionMessageRegExp('/^A polymorphic \w+ message/'); + throw new \Exception('A polymorphic exception message'); } - /** - * @expectedException \Exception - * @expectedExceptionMessageRegExp /^a poly[a-z]+ [a-zA-Z0-9_]+ me(s){2}age$/i - */ public function testRegexMessageExtreme(): void { + $this->expectException(\Exception::class); + $this->expectExceptionMessageRegExp('/^a poly[a-z]+ [a-zA-Z0-9_]+ me(s){2}age$/i'); + throw new \Exception('A polymorphic exception message'); } /** * @runInSeparateProcess * @requires extension xdebug - * @expectedException \Exception - * @expectedExceptionMessageRegExp #Screaming preg_match# */ public function testMessageXdebugScreamCompatibility(): void { \ini_set('xdebug.scream', '1'); - throw new \Exception('Screaming preg_match'); - } + $this->expectException(\Exception::class); + $this->expectExceptionMessageRegExp('#Screaming preg_match#'); - /** - * @expectedException \Exception variadic - * @expectedExceptionMessageRegExp /^A variadic \w+ message/ - */ - public function testSimultaneousLiteralAndRegExpExceptionMessage(): void - { - throw new \Exception('A variadic exception message'); + throw new \Exception('Screaming preg_match'); } } diff --git a/tests/unit/Framework/Constraint/ExceptionMessageTest.php b/tests/unit/Framework/Constraint/ExceptionMessageTest.php index fafb2f57685..e238c0a0fe0 100644 --- a/tests/unit/Framework/Constraint/ExceptionMessageTest.php +++ b/tests/unit/Framework/Constraint/ExceptionMessageTest.php @@ -13,39 +13,35 @@ class ExceptionMessageTest extends TestCase { - /** - * @expectedException \Exception - * @expectedExceptionMessage A literal exception message - */ public function testLiteralMessage(): void { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('A literal exception message'); + throw new \Exception('A literal exception message'); } - /** - * @expectedException \Exception - * @expectedExceptionMessage A partial - */ public function testPartialMessageBegin(): void { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('A partial'); + throw new \Exception('A partial exception message'); } - /** - * @expectedException \Exception - * @expectedExceptionMessage partial exception - */ public function testPartialMessageMiddle(): void { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('partial exception'); + throw new \Exception('A partial exception message'); } - /** - * @expectedException \Exception - * @expectedExceptionMessage exception message - */ public function testPartialMessageEnd(): void { + $this->expectException(\Exception::class); + $this->expectExceptionMessage('exception message'); + throw new \Exception('A partial exception message'); } } diff --git a/tests/unit/Framework/TestCaseTest.php b/tests/unit/Framework/TestCaseTest.php index 71076eac966..282104e1597 100644 --- a/tests/unit/Framework/TestCaseTest.php +++ b/tests/unit/Framework/TestCaseTest.php @@ -644,10 +644,11 @@ public function testCurrentWorkingDirectoryIsRestored(): void /** * @requires PHP 7 - * @expectedException \TypeError */ public function testTypeErrorCanBeExpected(): void { + $this->expectException(\TypeError::class); + $o = new \ClassWithScalarTypeDeclarations; $o->foo(null, null); } diff --git a/tests/unit/Framework/TestSuiteTest.php b/tests/unit/Framework/TestSuiteTest.php index 1d84f7ed6b9..5ad82945093 100644 --- a/tests/unit/Framework/TestSuiteTest.php +++ b/tests/unit/Framework/TestSuiteTest.php @@ -201,12 +201,11 @@ public function testDoNotSkipInheritedClass(): void $this->assertCount(2, $result); } - /** - * @expectedException PHPUnit\Framework\Exception - * @expectedExceptionMessage No valid test provided. - */ public function testCreateTestForConstructorlessTestClass(): void { + $this->expectException(Exception::class); + $this->expectExceptionMessage('No valid test provided.'); + $reflection = $this->getMockBuilder(\ReflectionClass::class) ->setConstructorArgs([$this]) ->getMock(); diff --git a/tests/unit/Util/JsonTest.php b/tests/unit/Util/JsonTest.php index 2dd282255c1..3a437e6395a 100644 --- a/tests/unit/Util/JsonTest.php +++ b/tests/unit/Util/JsonTest.php @@ -9,6 +9,7 @@ */ namespace PHPUnit\Util; +use PHPUnit\Framework\Exception; use PHPUnit\Framework\TestCase; class JsonTest extends TestCase @@ -60,11 +61,12 @@ public function prettifyProvider(): array /** * @dataProvider prettifyExceptionProvider - * @expectedException \PHPUnit\Framework\Exception - * @expectedExceptionMessage Cannot prettify invalid json */ public function testPrettifyException($json): void { + $this->expectException(Exception::class); + $this->expectExceptionMessage('Cannot prettify invalid json'); + Json::prettify($json); } diff --git a/tests/unit/Util/TestTest.php b/tests/unit/Util/TestTest.php index effb388dbb1..6a1cf49122b 100644 --- a/tests/unit/Util/TestTest.php +++ b/tests/unit/Util/TestTest.php @@ -17,96 +17,6 @@ class TestTest extends TestCase { - /** - * @todo Split up in separate tests - */ - public function testGetExpectedException(): void - { - $this->assertArraySubset( - ['class' => 'FooBarBaz', 'code' => null, 'message' => ''], - Test::getExpectedException(\ExceptionTest::class, 'testOne') - ); - - $this->assertArraySubset( - ['class' => 'Foo_Bar_Baz', 'code' => null, 'message' => ''], - Test::getExpectedException(\ExceptionTest::class, 'testTwo') - ); - - $this->assertArraySubset( - ['class' => 'Foo\Bar\Baz', 'code' => null, 'message' => ''], - Test::getExpectedException(\ExceptionTest::class, 'testThree') - ); - - $this->assertArraySubset( - ['class' => 'ほげ', 'code' => null, 'message' => ''], - Test::getExpectedException(\ExceptionTest::class, 'testFour') - ); - - $this->assertArraySubset( - ['class' => 'Class', 'code' => 1234, 'message' => 'Message'], - Test::getExpectedException(\ExceptionTest::class, 'testFive') - ); - - $this->assertArraySubset( - ['class' => 'Class', 'code' => 1234, 'message' => 'Message'], - Test::getExpectedException(\ExceptionTest::class, 'testSix') - ); - - $this->assertArraySubset( - ['class' => 'Class', 'code' => 'ExceptionCode', 'message' => 'Message'], - Test::getExpectedException(\ExceptionTest::class, 'testSeven') - ); - - $this->assertArraySubset( - ['class' => 'Class', 'code' => 0, 'message' => 'Message'], - Test::getExpectedException(\ExceptionTest::class, 'testEight') - ); - - $this->assertArraySubset( - ['class' => 'Class', 'code' => \ExceptionTest::ERROR_CODE, 'message' => \ExceptionTest::ERROR_MESSAGE], - Test::getExpectedException(\ExceptionTest::class, 'testNine') - ); - - $this->assertArraySubset( - ['class' => 'Class', 'code' => null, 'message' => ''], - Test::getExpectedException(\ExceptionTest::class, 'testSingleLine') - ); - - $this->assertArraySubset( - ['class' => 'Class', 'code' => \My\Space\ExceptionNamespaceTest::ERROR_CODE, 'message' => \My\Space\ExceptionNamespaceTest::ERROR_MESSAGE], - Test::getExpectedException(\My\Space\ExceptionNamespaceTest::class, 'testConstants') - ); - - // Ensure the Class::CONST expression is only evaluated when the constant really exists - $this->assertArraySubset( - ['class' => 'Class', 'code' => 'ExceptionTest::UNKNOWN_CODE_CONSTANT', 'message' => 'ExceptionTest::UNKNOWN_MESSAGE_CONSTANT'], - Test::getExpectedException(\ExceptionTest::class, 'testUnknownConstants') - ); - - $this->assertArraySubset( - ['class' => 'Class', 'code' => 'My\Space\ExceptionNamespaceTest::UNKNOWN_CODE_CONSTANT', 'message' => 'My\Space\ExceptionNamespaceTest::UNKNOWN_MESSAGE_CONSTANT'], - Test::getExpectedException(\My\Space\ExceptionNamespaceTest::class, 'testUnknownConstants') - ); - } - - public function testGetExpectedRegExp(): void - { - $this->assertArraySubset( - ['message_regex' => '#regex#'], - Test::getExpectedException(\ExceptionTest::class, 'testWithRegexMessage') - ); - - $this->assertArraySubset( - ['message_regex' => '#regex#'], - Test::getExpectedException(\ExceptionTest::class, 'testWithRegexMessageFromClassConstant') - ); - - $this->assertArraySubset( - ['message_regex' => 'ExceptionTest::UNKNOWN_MESSAGE_REGEX_CONSTANT'], - Test::getExpectedException(\ExceptionTest::class, 'testWithUnknowRegexMessageFromClassConstant') - ); - } - /** * @dataProvider requirementsProvider *