From e349fe39af2ce971cb002ada59377d72de5e9aa3 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 12 Mar 2023 20:46:29 +0100 Subject: [PATCH] PHPUnit 10 | Remove support for expecting PHP notices/exceptions Remove the `ExpectPHPException` polyfill and all references to it as support for expecting PHP native and user added deprecations, notices, warnings and errors has been dropped in PHPUnit 10.0. PHP native Exceptions can still be tested using the `expectException()` method with the name of the PHP native Exception. Refs: * https://phpunit.de/announcements/phpunit-10.html * https://github.com/sebastianbergmann/phpunit/blob/main/ChangeLog-10.0.md#1000---2023-02-03 * https://github.com/sebastianbergmann/phpunit/issues/3775 * https://github.com/sebastianbergmann/phpunit/issues/5062 * https://github.com/sebastianbergmann/phpunit/commit/a2c784c39bf5741caf436f92fc8c5bfb91bac025 --- README.md | 26 ---- phpunitpolyfills-autoload.php | 55 -------- src/Polyfills/ExpectPHPException.php | 148 --------------------- src/Polyfills/ExpectPHPException_Empty.php | 8 -- src/TestCases/TestCasePHPUnitGte8.php | 2 - src/TestCases/TestCasePHPUnitLte7.php | 2 - src/TestCases/XTestCase.php | 2 - tests/Polyfills/ExpectPHPExceptionTest.php | 68 ---------- tests/TestCases/TestCaseTestTrait.php | 11 -- 9 files changed, 322 deletions(-) delete mode 100644 src/Polyfills/ExpectPHPException.php delete mode 100644 src/Polyfills/ExpectPHPException_Empty.php delete mode 100644 tests/Polyfills/ExpectPHPExceptionTest.php diff --git a/README.md b/README.md index bdbe4d1..96f006a 100644 --- a/README.md +++ b/README.md @@ -233,32 +233,6 @@ These methods were introduced in PHPUnit 7.5.0 as alternatives to using `Assert: [`Assert::assertEqualsWithDelta()`]: https://docs.phpunit.de/en/9.6/assertions.html#assertequalswithdelta [`Assert::assertNotEqualsWithDelta()`]: https://docs.phpunit.de/en/9.6/assertions.html#assertequalswithdelta -#### PHPUnit < 8.4.0: `Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException` - -Polyfills the following methods: -| | | | -|-------------------------------------|--------------------------------|---------------------------------------| -| `TestCase::`[`expectError()`] | [`expectErrorMessage()`] | [`expectErrorMessageMatches()`] | -| `TestCase::`[`expectWarning()`] | [`expectWarningMessage()`] | [`expectWarningMessageMatches()`] | -| `TestCase::`[`expectNotice()`] | [`expectNoticeMessage()`] | [`expectNoticeMessageMatches()`] | -| `TestCase::`[`expectDeprecation()`] | [`expectDeprecationMessage()`] | [`expectDeprecationMessageMatches()`] | - -These methods were introduced in PHPUnit 8.4.0 as alternatives to using `TestCase::expectException()` et al for expecting PHP native errors, warnings and notices. -Using `TestCase::expectException*()` for testing PHP native notices was soft deprecated in PHPUnit 8.4.0, hard deprecated (warning) in PHPUnit 9.0.0 and (will be) removed in PHPUnit 10.0.0. - -[`expectError()`]: https://docs.phpunit.de/en/9.6/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices -[`expectErrorMessage()`]: https://docs.phpunit.de/en/9.6/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices -[`expectErrorMessageMatches()`]: https://docs.phpunit.de/en/9.6/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices -[`expectWarning()`]: https://docs.phpunit.de/en/9.6/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices -[`expectWarningMessage()`]: https://docs.phpunit.de/en/9.6/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices -[`expectWarningMessageMatches()`]: https://docs.phpunit.de/en/9.6/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices -[`expectNotice()`]: https://docs.phpunit.de/en/9.6/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices -[`expectNoticeMessage()`]: https://docs.phpunit.de/en/9.6/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices -[`expectNoticeMessageMatches()`]: https://docs.phpunit.de/en/9.6/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices -[`expectDeprecation()`]: https://docs.phpunit.de/en/9.6/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices -[`expectDeprecationMessage()`]: https://docs.phpunit.de/en/9.6/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices -[`expectDeprecationMessageMatches()`]: https://docs.phpunit.de/en/9.6/writing-tests-for-phpunit.html#testing-php-errors-warnings-and-notices - #### PHPUnit < 8.4.0: `Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches` Polyfills the [`TestCase::expectExceptionMessageMatches()`] method. diff --git a/phpunitpolyfills-autoload.php b/phpunitpolyfills-autoload.php index 75f29b5..c8de739 100644 --- a/phpunitpolyfills-autoload.php +++ b/phpunitpolyfills-autoload.php @@ -67,10 +67,6 @@ public static function load( $className ) { self::loadAssertEqualsSpecializations(); return true; - case 'Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException': - self::loadExpectPHPException(); - return true; - case 'Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches': self::loadExpectExceptionMessageMatches(); return true; @@ -191,57 +187,6 @@ public static function loadAssertEqualsSpecializations() { require_once __DIR__ . '/src/Polyfills/AssertEqualsSpecializations_Empty.php'; } - /** - * Load the ExpectPHPException polyfill or an empty trait with the same name - * if a PHPUnit version is used which already contains this functionality. - * - * Includes aliasing any PHPUnit native classes needed for this functionality - * which aren't available under their namespaced name in PHPUnit 5.x. - * - * @return void - */ - public static function loadExpectPHPException() { - /* - * Alias the PHPUnit 5.x Error classes to their PHPUnit >= 6 name. - * - * {@internal The `class_exists` wrappers are needed to play nice with - * PHPUnit bootstrap files of test suites implementing this library - * which may be creating cross-version compatibility in a similar manner.}} - */ - if ( \class_exists( 'PHPUnit_Framework_Error' ) === true - && \class_exists( 'PHPUnit\Framework\Error\Error' ) === false - ) { - \class_alias( 'PHPUnit_Framework_Error', 'PHPUnit\Framework\Error\Error' ); - } - - if ( \class_exists( 'PHPUnit_Framework_Error_Warning' ) === true - && \class_exists( 'PHPUnit\Framework\Error\Warning' ) === false - ) { - \class_alias( 'PHPUnit_Framework_Error_Warning', 'PHPUnit\Framework\Error\Warning' ); - } - - if ( \class_exists( 'PHPUnit_Framework_Error_Notice' ) === true - && \class_exists( 'PHPUnit\Framework\Error\Notice' ) === false - ) { - \class_alias( 'PHPUnit_Framework_Error_Notice', 'PHPUnit\Framework\Error\Notice' ); - } - - if ( \class_exists( 'PHPUnit_Framework_Error_Deprecated' ) === true - && \class_exists( 'PHPUnit\Framework\Error\Deprecated' ) === false - ) { - \class_alias( 'PHPUnit_Framework_Error_Deprecated', 'PHPUnit\Framework\Error\Deprecated' ); - } - - if ( \method_exists( TestCase::class, 'expectErrorMessage' ) === false ) { - // PHPUnit < 8.4.0. - require_once __DIR__ . '/src/Polyfills/ExpectPHPException.php'; - return; - } - - // PHPUnit >= 8.4.0. - require_once __DIR__ . '/src/Polyfills/ExpectPHPException_Empty.php'; - } - /** * Load the ExpectExceptionMessageMatches polyfill or an empty trait with the same name * if a PHPUnit version is used which already contains this functionality. diff --git a/src/Polyfills/ExpectPHPException.php b/src/Polyfills/ExpectPHPException.php deleted file mode 100644 index a08e21c..0000000 --- a/src/Polyfills/ExpectPHPException.php +++ /dev/null @@ -1,148 +0,0 @@ -expectException( Deprecated::class ); - } - - /** - * Set expectation for the message when receiving a PHP native deprecation notice. - * - * @param string $message The message to expect. - * - * @return void - */ - final public function expectDeprecationMessage( $message ) { - $this->expectExceptionMessage( $message ); - } - - /** - * Set expectation for the message when receiving a PHP native deprecation notice (regex based). - * - * @param string $regularExpression A regular expression which must match the message. - * - * @return void - */ - final public function expectDeprecationMessageMatches( $regularExpression ) { - $this->expectExceptionMessageRegExp( $regularExpression ); - } - - /** - * Set expectation for receiving a PHP native notice. - * - * @return void - */ - final public function expectNotice() { - $this->expectException( Notice::class ); - } - - /** - * Set expectation for the message when receiving a PHP native notice. - * - * @param string $message The message to expect. - * - * @return void - */ - final public function expectNoticeMessage( $message ) { - $this->expectExceptionMessage( $message ); - } - - /** - * Set expectation for the message when receiving a PHP native notice (regex based). - * - * @param string $regularExpression A regular expression which must match the message. - * - * @return void - */ - final public function expectNoticeMessageMatches( $regularExpression ) { - $this->expectExceptionMessageRegExp( $regularExpression ); - } - - /** - * Set expectation for receiving a PHP native warning. - * - * @return void - */ - final public function expectWarning() { - $this->expectException( Warning::class ); - } - - /** - * Set expectation for the message when receiving a PHP native warning. - * - * @param string $message The message to expect. - * - * @return void - */ - final public function expectWarningMessage( $message ) { - $this->expectExceptionMessage( $message ); - } - - /** - * Set expectation for the message when receiving a PHP native warning (regex based). - * - * @param string $regularExpression A regular expression which must match the message. - * - * @return void - */ - final public function expectWarningMessageMatches( $regularExpression ) { - $this->expectExceptionMessageRegExp( $regularExpression ); - } - - /** - * Set expectation for receiving a PHP native error. - * - * @return void - */ - final public function expectError() { - $this->expectException( Error::class ); - } - - /** - * Set expectation for the message when receiving a PHP native error. - * - * @param string $message The message to expect. - * - * @return void - */ - final public function expectErrorMessage( $message ) { - $this->expectExceptionMessage( $message ); - } - - /** - * Set expectation for the message when receiving a PHP native error (regex based). - * - * @param string $regularExpression A regular expression which must match the message. - * - * @return void - */ - final public function expectErrorMessageMatches( $regularExpression ) { - $this->expectExceptionMessageRegExp( $regularExpression ); - } -} diff --git a/src/Polyfills/ExpectPHPException_Empty.php b/src/Polyfills/ExpectPHPException_Empty.php deleted file mode 100644 index 4d787b2..0000000 --- a/src/Polyfills/ExpectPHPException_Empty.php +++ /dev/null @@ -1,8 +0,0 @@ -= 8.4.0 in which this polyfill is not needed. - */ -trait ExpectPHPException {} diff --git a/src/TestCases/TestCasePHPUnitGte8.php b/src/TestCases/TestCasePHPUnitGte8.php index cfe397c..e635bec 100644 --- a/src/TestCases/TestCasePHPUnitGte8.php +++ b/src/TestCases/TestCasePHPUnitGte8.php @@ -10,7 +10,6 @@ use Yoast\PHPUnitPolyfills\Polyfills\AssertObjectEquals; use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations; use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches; -use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException; /** * Basic test case for use with PHPUnit >= 8. @@ -30,7 +29,6 @@ abstract class TestCase extends PHPUnit_TestCase { use AssertObjectEquals; use EqualToSpecializations; use ExpectExceptionMessageMatches; - use ExpectPHPException; /** * This method is called before the first test of this test class is run. diff --git a/src/TestCases/TestCasePHPUnitLte7.php b/src/TestCases/TestCasePHPUnitLte7.php index 37506e8..2943ce0 100644 --- a/src/TestCases/TestCasePHPUnitLte7.php +++ b/src/TestCases/TestCasePHPUnitLte7.php @@ -14,7 +14,6 @@ use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations; use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches; use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject; -use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException; /** * Basic test case for use with PHPUnit <= 7. @@ -38,7 +37,6 @@ abstract class TestCase extends PHPUnit_TestCase { use EqualToSpecializations; use ExpectExceptionMessageMatches; use ExpectExceptionObject; - use ExpectPHPException; /** * This method is called before the first test of this test class is run. diff --git a/src/TestCases/XTestCase.php b/src/TestCases/XTestCase.php index 699c017..f1339f4 100644 --- a/src/TestCases/XTestCase.php +++ b/src/TestCases/XTestCase.php @@ -14,7 +14,6 @@ use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations; use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches; use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject; -use Yoast\PHPUnitPolyfills\Polyfills\ExpectPHPException; /** * Basic test case for use with PHPUnit cross-version. @@ -40,7 +39,6 @@ abstract class XTestCase extends PHPUnit_TestCase { use EqualToSpecializations; use ExpectExceptionMessageMatches; use ExpectExceptionObject; - use ExpectPHPException; /** * This method is called before the first test of this test class is run. diff --git a/tests/Polyfills/ExpectPHPExceptionTest.php b/tests/Polyfills/ExpectPHPExceptionTest.php deleted file mode 100644 index 37cb299..0000000 --- a/tests/Polyfills/ExpectPHPExceptionTest.php +++ /dev/null @@ -1,68 +0,0 @@ -expectDeprecation(); - $this->expectDeprecationMessage( 'foo' ); - $this->expectDeprecationMessageMatches( '/foo/' ); - - \trigger_error( 'foo', \E_USER_DEPRECATED ); - } - - /** - * Verify availability of the expectNotice*() methods. - * - * @return void - */ - public function testNoticeCanBeExpected() { - $this->expectNotice(); - $this->expectNoticeMessage( 'foo' ); - $this->expectNoticeMessageMatches( '/foo/' ); - - \trigger_error( 'foo', \E_USER_NOTICE ); - } - - /** - * Verify availability of the expectWarning*() methods. - * - * @return void - */ - public function testWarningCanBeExpected() { - $this->expectWarning(); - $this->expectWarningMessage( 'foo' ); - $this->expectWarningMessageMatches( '/foo/' ); - - \trigger_error( 'foo', \E_USER_WARNING ); - } - - /** - * Verify availability of the expectError*() methods. - * - * @return void - */ - public function testErrorCanBeExpected() { - $this->expectError(); - $this->expectErrorMessage( 'foo' ); - $this->expectErrorMessageMatches( '/foo/' ); - - \trigger_error( 'foo', \E_USER_ERROR ); - } -} diff --git a/tests/TestCases/TestCaseTestTrait.php b/tests/TestCases/TestCaseTestTrait.php index 4f620f9..956ee69 100644 --- a/tests/TestCases/TestCaseTestTrait.php +++ b/tests/TestCases/TestCaseTestTrait.php @@ -65,17 +65,6 @@ final public function testAvailabilityAssertEqualsSpecializationsTrait() { static::assertEqualsIgnoringCase( 'a', 'A' ); } - /** - * Test availability of trait polyfilled PHPUnit methods [5]. - * - * @return void - */ - final public function testAvailabilityExpectPHPExceptionTrait() { - $this->expectDeprecation(); - - \trigger_error( 'foo', \E_USER_DEPRECATED ); - } - /** * Test availability of trait polyfilled PHPUnit methods [6]. *