From 1dcba37a54f0f1706f545bc32149c58441dc1b48 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Sun, 12 Jan 2025 06:52:03 +0100 Subject: [PATCH] Drop support for PHPUnit < 7.5 [6] Even though the TestListener implementation is not (yet) compatible with PHPUnit 10 (nor 11 or 12), we should still drop support for PHPUnit < 7.5 from the existing implementation. This includes: * Dropping support for the PHPUnit 6 specific implementation of the TestListener. * Removing the semi-duplicate test fixture classes for PHPUnit < 7, as well as removing the test helper method containing the toggle for which fixture to use. * Updating the documentation in the README. --- README.md | 12 +- phpstan.neon.dist | 2 +- phpunitpolyfills-autoload.php | 7 - ...tListenerDefaultImplementationPHPUnit6.php | 150 ------------------ tests/TestListeners/Fixtures/Failure.php | 2 +- .../Fixtures/FailurePHPUnitGte7.php | 24 --- tests/TestListeners/Fixtures/Incomplete.php | 2 +- .../Fixtures/IncompletePHPUnitGte7.php | 24 --- tests/TestListeners/Fixtures/Risky.php | 2 +- .../Fixtures/RiskyPHPUnitGte7.php | 24 --- tests/TestListeners/Fixtures/Skipped.php | 2 +- .../Fixtures/SkippedPHPUnitGte7.php | 24 --- tests/TestListeners/Fixtures/Success.php | 2 +- .../Fixtures/SuccessPHPUnitGte7.php | 24 --- tests/TestListeners/Fixtures/TestError.php | 6 +- .../Fixtures/TestErrorPHPUnitGte7.php | 31 ---- tests/TestListeners/Fixtures/Warning.php | 2 +- .../Fixtures/WarningPHPUnitGte7.php | 27 ---- tests/TestListeners/TestListenerTest.php | 46 ++---- 19 files changed, 29 insertions(+), 384 deletions(-) delete mode 100644 src/TestListeners/TestListenerDefaultImplementationPHPUnit6.php delete mode 100644 tests/TestListeners/Fixtures/FailurePHPUnitGte7.php delete mode 100644 tests/TestListeners/Fixtures/IncompletePHPUnitGte7.php delete mode 100644 tests/TestListeners/Fixtures/RiskyPHPUnitGte7.php delete mode 100644 tests/TestListeners/Fixtures/SkippedPHPUnitGte7.php delete mode 100644 tests/TestListeners/Fixtures/SuccessPHPUnitGte7.php delete mode 100644 tests/TestListeners/Fixtures/TestErrorPHPUnitGte7.php delete mode 100644 tests/TestListeners/Fixtures/WarningPHPUnitGte7.php diff --git a/README.md b/README.md index 69c6965..29c8b36 100644 --- a/README.md +++ b/README.md @@ -554,11 +554,11 @@ class MyTest extends XTestCase { > :warning: **Important** :warning: > -> The TestListener polyfill in PHPUnit Polyfills 2.0/3.0 is [not (yet) compatible with PHPUnit 10.x/11.x][polyfill-ticket]. +> The TestListener polyfill in PHPUnit Polyfills 2.0/3.0/4.0 is [not (yet) compatible with PHPUnit 10.x/11.x/12.x][polyfill-ticket]. > > If you need the TestListener polyfill, it is recommended to stay on the PHPUnit Polyfills 1.x series for the time being and to watch and upvote the [related ticket][polyfill-ticket]. > -> The below documentation is for the PHPUnit 6.x-9.x TestListener polyfill implementation. +> The below documentation is for the PHPUnit 7.x-9.x TestListener polyfill implementation. [polyfill-ticket]: https://github.com/Yoast/PHPUnit-Polyfills/issues/128 @@ -567,9 +567,7 @@ Additionally, the use of the TestListener principle has been deprecated in PHPUn > Note: while deprecated in PHPUnit 7, the TestListener interface has not yet been removed and is still supported in PHPUnit 9.x. -If your test suite does not need to support PHPUnit < 7, it is strongly recommended to use the TestRunner hook interfaces extensions instead. - -However, for test suites that still need to support PHPUnit 6 or lower, implementing the `TestListener` interface is the only viable option. +It is recommended to use the TestRunner hook interfaces extensions instead. #### `Yoast\PHPUnitPolyfills\TestListeners\TestListenerDefaultImplementation` @@ -592,10 +590,6 @@ Similar to the `TestCase` implementation, snake_case methods without type declar Implementations of the `TestListener` interface may be using any of the following patterns: ```php -// PHPUnit 6. -class MyTestListener extends \PHPUnit\Framework\BaseTestListener {} - -// PHPUnit 7+. class MyTestListener implements \PHPUnit\Framework\TestListener { use \PHPUnit\Framework\TestListenerDefaultImplementation; } diff --git a/phpstan.neon.dist b/phpstan.neon.dist index 3621d3d..b9796f2 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -46,7 +46,7 @@ parameters: # The traits are functionality, but not used by the package itself. Can't be helped. - identifier: trait.unused - path: src/TestListeners/TestListenerDefaultImplementationPHPUnit6.php + path: src/TestListeners/TestListenerDefaultImplementationPHPUnitGte7.php - identifier: trait.unused path: src/TestListeners/TestListenerSnakeCaseMethods.php diff --git a/phpunitpolyfills-autoload.php b/phpunitpolyfills-autoload.php index 5d925d3..0d645fb 100644 --- a/phpunitpolyfills-autoload.php +++ b/phpunitpolyfills-autoload.php @@ -361,13 +361,6 @@ public static function loadTestCase() { * @return void */ public static function loadTestListenerDefaultImplementation() { - if ( \version_compare( PHPUnit_Version::id(), '7.0.0', '<' ) ) { - // PHPUnit 6.4.4 < 7.0.0. - require_once __DIR__ . '/src/TestListeners/TestListenerDefaultImplementationPHPUnit6.php'; - return; - } - - // PHPUnit >= 7.0.0. require_once __DIR__ . '/src/TestListeners/TestListenerDefaultImplementationPHPUnitGte7.php'; } diff --git a/src/TestListeners/TestListenerDefaultImplementationPHPUnit6.php b/src/TestListeners/TestListenerDefaultImplementationPHPUnit6.php deleted file mode 100644 index d6bca59..0000000 --- a/src/TestListeners/TestListenerDefaultImplementationPHPUnit6.php +++ /dev/null @@ -1,150 +0,0 @@ -add_error( $test, $e, $time ); - } - - /** - * A warning occurred. - * - * This method is only functional in PHPUnit 6.0 and above. - * - * @param Test $test Test object. - * @param Warning $e Instance of the warning encountered. - * @param float $time Execution time of this test. - * - * @return void - */ - public function addWarning( Test $test, Warning $e, $time ) { - $this->add_warning( $test, $e, $time ); - } - - /** - * A failure occurred. - * - * @param Test $test Test object. - * @param AssertionFailedError $e Instance of the assertion failure exception encountered. - * @param float $time Execution time of this test. - * - * @return void - */ - public function addFailure( Test $test, AssertionFailedError $e, $time ) { - $this->add_failure( $test, $e, $time ); - } - - /** - * Incomplete test. - * - * @param Test $test Test object. - * @param Exception $e Instance of the incomplete test exception. - * @param float $time Execution time of this test. - * - * @return void - */ - public function addIncompleteTest( Test $test, Exception $e, $time ) { - $this->add_incomplete_test( $test, $e, $time ); - } - - /** - * Risky test. - * - * @param Test $test Test object. - * @param Exception $e Instance of the risky test exception. - * @param float $time Execution time of this test. - * - * @return void - */ - public function addRiskyTest( Test $test, Exception $e, $time ) { - $this->add_risky_test( $test, $e, $time ); - } - - /** - * Skipped test. - * - * @param Test $test Test object. - * @param Exception $e Instance of the skipped test exception. - * @param float $time Execution time of this test. - * - * @return void - */ - public function addSkippedTest( Test $test, Exception $e, $time ) { - $this->add_skipped_test( $test, $e, $time ); - } - - /** - * A test suite started. - * - * @param TestSuite $suite Test suite object. - * - * @return void - */ - public function startTestSuite( TestSuite $suite ) { - $this->start_test_suite( $suite ); - } - - /** - * A test suite ended. - * - * @param TestSuite $suite Test suite object. - * - * @return void - */ - public function endTestSuite( TestSuite $suite ) { - $this->end_test_suite( $suite ); - } - - /** - * A test started. - * - * @param Test $test Test object. - * - * @return void - */ - public function startTest( Test $test ) { - $this->start_test( $test ); - } - - /** - * A test ended. - * - * @param Test $test Test object. - * @param float $time Execution time of this test. - * - * @return void - */ - public function endTest( Test $test, $time ) { - $this->end_test( $test, $time ); - } -} diff --git a/tests/TestListeners/Fixtures/Failure.php b/tests/TestListeners/Fixtures/Failure.php index 449270a..c2b0e9b 100644 --- a/tests/TestListeners/Fixtures/Failure.php +++ b/tests/TestListeners/Fixtures/Failure.php @@ -14,7 +14,7 @@ class Failure extends TestCase { * * @return void */ - protected function runTest() { + protected function testForListener() { $this->fail(); } } diff --git a/tests/TestListeners/Fixtures/FailurePHPUnitGte7.php b/tests/TestListeners/Fixtures/FailurePHPUnitGte7.php deleted file mode 100644 index 5fa2dff..0000000 --- a/tests/TestListeners/Fixtures/FailurePHPUnitGte7.php +++ /dev/null @@ -1,24 +0,0 @@ -fail(); - } -} diff --git a/tests/TestListeners/Fixtures/Incomplete.php b/tests/TestListeners/Fixtures/Incomplete.php index 4c79b3c..f57947e 100644 --- a/tests/TestListeners/Fixtures/Incomplete.php +++ b/tests/TestListeners/Fixtures/Incomplete.php @@ -14,7 +14,7 @@ class Incomplete extends TestCase { * * @return void */ - protected function runTest() { + protected function testForListener() { $this->markTestIncomplete( 'Test incomplete' ); } } diff --git a/tests/TestListeners/Fixtures/IncompletePHPUnitGte7.php b/tests/TestListeners/Fixtures/IncompletePHPUnitGte7.php deleted file mode 100644 index cf45762..0000000 --- a/tests/TestListeners/Fixtures/IncompletePHPUnitGte7.php +++ /dev/null @@ -1,24 +0,0 @@ -markTestIncomplete( 'Test incomplete' ); - } -} diff --git a/tests/TestListeners/Fixtures/Risky.php b/tests/TestListeners/Fixtures/Risky.php index 7de1296..f9a9f18 100644 --- a/tests/TestListeners/Fixtures/Risky.php +++ b/tests/TestListeners/Fixtures/Risky.php @@ -14,7 +14,7 @@ class Risky extends TestCase { * * @return void */ - protected function runTest() { + protected function testForListener() { $this->markAsRisky(); } } diff --git a/tests/TestListeners/Fixtures/RiskyPHPUnitGte7.php b/tests/TestListeners/Fixtures/RiskyPHPUnitGte7.php deleted file mode 100644 index cc33c0f..0000000 --- a/tests/TestListeners/Fixtures/RiskyPHPUnitGte7.php +++ /dev/null @@ -1,24 +0,0 @@ -markAsRisky(); - } -} diff --git a/tests/TestListeners/Fixtures/Skipped.php b/tests/TestListeners/Fixtures/Skipped.php index 4b4a2be..adb56d7 100644 --- a/tests/TestListeners/Fixtures/Skipped.php +++ b/tests/TestListeners/Fixtures/Skipped.php @@ -14,7 +14,7 @@ class Skipped extends TestCase { * * @return void */ - protected function runTest() { + protected function testForListener() { $this->markTestSkipped( 'Skipped test' ); } } diff --git a/tests/TestListeners/Fixtures/SkippedPHPUnitGte7.php b/tests/TestListeners/Fixtures/SkippedPHPUnitGte7.php deleted file mode 100644 index ba982cb..0000000 --- a/tests/TestListeners/Fixtures/SkippedPHPUnitGte7.php +++ /dev/null @@ -1,24 +0,0 @@ -markTestSkipped( 'Skipped test' ); - } -} diff --git a/tests/TestListeners/Fixtures/Success.php b/tests/TestListeners/Fixtures/Success.php index 66c8646..fba2fe3 100644 --- a/tests/TestListeners/Fixtures/Success.php +++ b/tests/TestListeners/Fixtures/Success.php @@ -14,7 +14,7 @@ class Success extends TestCase { * * @return void */ - protected function runTest() { + protected function testForListener() { $this->assertTrue( true ); } } diff --git a/tests/TestListeners/Fixtures/SuccessPHPUnitGte7.php b/tests/TestListeners/Fixtures/SuccessPHPUnitGte7.php deleted file mode 100644 index b9b3fcb..0000000 --- a/tests/TestListeners/Fixtures/SuccessPHPUnitGte7.php +++ /dev/null @@ -1,24 +0,0 @@ -assertTrue( true ); - } -} diff --git a/tests/TestListeners/Fixtures/TestError.php b/tests/TestListeners/Fixtures/TestError.php index 6b2ff79..0eefc53 100644 --- a/tests/TestListeners/Fixtures/TestError.php +++ b/tests/TestListeners/Fixtures/TestError.php @@ -3,11 +3,15 @@ namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures; use Exception; +use PHPUnit\Framework\Attributes\CoversNothing; use PHPUnit\Framework\TestCase; /** * Fixture to generate a test error to pass to the test listener. + * + * @coversNothing */ +#[CoversNothing] class TestError extends TestCase { /** @@ -17,7 +21,7 @@ class TestError extends TestCase { * * @throws Exception For test purposes. */ - protected function runTest() { + protected function testForListener() { throw new Exception(); } } diff --git a/tests/TestListeners/Fixtures/TestErrorPHPUnitGte7.php b/tests/TestListeners/Fixtures/TestErrorPHPUnitGte7.php deleted file mode 100644 index 1f7778c..0000000 --- a/tests/TestListeners/Fixtures/TestErrorPHPUnitGte7.php +++ /dev/null @@ -1,31 +0,0 @@ -getTestObject( 'TestError' ); + $test = new TestError( 'testForListener' ); $test->run( $this->result ); $this->assertSame( 1, $this->listener->startTestCount, 'test start count failed' ); @@ -71,7 +76,7 @@ public function testError() { * @return void */ public function testWarning() { - $test = $this->getTestObject( 'Warning' ); + $test = new Warning( 'testForListener' ); $test->run( $this->result ); $this->assertSame( 1, $this->listener->startTestCount, 'test start count failed' ); @@ -85,7 +90,7 @@ public function testWarning() { * @return void */ public function testFailure() { - $test = $this->getTestObject( 'Failure' ); + $test = new Failure( 'testForListener' ); $test->run( $this->result ); $this->assertSame( 1, $this->listener->startTestCount, 'test start count failed' ); @@ -99,7 +104,7 @@ public function testFailure() { * @return void */ public function testIncomplete() { - $test = $this->getTestObject( 'Incomplete' ); + $test = new Incomplete( 'testForListener' ); $test->run( $this->result ); $this->assertSame( 1, $this->listener->startTestCount, 'test start count failed' ); @@ -113,7 +118,7 @@ public function testIncomplete() { * @return void */ public function testRisky() { - $test = $this->getTestObject( 'Risky' ); + $test = new Risky( 'testForListener' ); $test->run( $this->result ); $this->assertSame( 1, $this->listener->startTestCount, 'test start count failed' ); @@ -127,7 +132,7 @@ public function testRisky() { * @return void */ public function testSkipped() { - $test = $this->getTestObject( 'Skipped' ); + $test = new Skipped( 'testForListener' ); $test->run( $this->result ); $this->assertSame( 1, $this->listener->startTestCount, 'test start count failed' ); @@ -141,33 +146,10 @@ public function testSkipped() { * @return void */ public function testStartStop() { - $test = $this->getTestObject( 'Success' ); + $test = new Success( 'testForListener' ); $test->run( $this->result ); $this->assertSame( 1, $this->listener->startTestCount, 'test start count failed' ); $this->assertSame( 1, $this->listener->endTestCount, 'test end count failed' ); } - - /** - * Helper method to get the right Test class object. - * - * Toggles between different versions of the same Test class object: - * - Base version using `runTest()` method, compatible with PHPUnit < 10.0. - * - Version using `testForListener()` method, compatible with PHPUnit > 7.0. - * - * @param string $className Base class name of the test class to instantiate. - * - * @return PHPUnitTestCase - */ - private function getTestObject( $className ) { - $className = '\Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures\\' . $className; - $testName = 'runTest'; - - if ( \version_compare( Autoload::getPHPUnitVersion(), '7.0.0', '>=' ) ) { - $className .= 'PHPUnitGte7'; - $testName = 'testForListener'; - } - - return new $className( $testName ); - } }