diff --git a/README.md b/README.md index 6d2a390..d9b173a 100644 --- a/README.md +++ b/README.md @@ -579,7 +579,7 @@ class MyTest extends XTestCase { > > 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 5.x-9.x TestListener polyfill implementation. [polyfill-ticket]: https://github.com/Yoast/PHPUnit-Polyfills/issues/128 @@ -598,21 +598,24 @@ This `TestListenerDefaultImplementation` trait overcomes the signature mismatche Similar to the `TestCase` implementation, snake_case methods without type declarations are used to get round the signature mismatches. The snake_case methods will automatically be called. -| PHPUnit native method name | Replacement | -| -------------------------- | --------------------------------------- | -| `addError()` | `add_error($test, $e, $time)` | -| `addWarning()` | `add_warning($test, $e, $time)` | -| `addFailure()` | `add_failure($test, $e, $time)` | -| `addIncompleteTest()` | `add_incomplete_test($test, $e, $time)` | -| `addRiskyTest()` | `add_risky_test($test, $e, $time)` | -| `addSkippedTest()` | `add_skipped_test($test, $e, $time)` | -| `startTestSuite()` | `start_test_suite($suite)` | -| `endTestSuite()` | `end_test_suite($suite)` | -| `startTest()` | `start_test($test)` | -| `endTest()` | `end_test($test, $time)` | +| PHPUnit native method name | Replacement | Notes | +| -------------------------- | --------------------------------------- | ----------------------------------------- | +| `addError()` | `add_error($test, $e, $time)` | | +| `addWarning()` | `add_warning($test, $e, $time)` | Introduced in PHPUnit 6. | +| `addFailure()` | `add_failure($test, $e, $time)` | | +| `addIncompleteTest()` | `add_incomplete_test($test, $e, $time)` | | +| `addRiskyTest()` | `add_risky_test($test, $e, $time)` | Support appears to be flaky on PHPUnit 5. | +| `addSkippedTest()` | `add_skipped_test($test, $e, $time)` | | +| `startTestSuite()` | `start_test_suite($suite)` | | +| `endTestSuite()` | `end_test_suite($suite)` | | +| `startTest()` | `start_test($test)` | | +| `endTest()` | `end_test($test, $time)` | | Implementations of the `TestListener` interface may be using any of the following patterns: ```php +// PHPUnit < 6. +class MyTestListener extends \PHPUnit_Framework_BaseTestListener {} + // PHPUnit 6. class MyTestListener extends \PHPUnit\Framework\BaseTestListener {} diff --git a/phpunitpolyfills-autoload.php b/phpunitpolyfills-autoload.php index 6ddf1b8..273a5d1 100644 --- a/phpunitpolyfills-autoload.php +++ b/phpunitpolyfills-autoload.php @@ -337,8 +337,29 @@ public static function loadTestCase() { * @return void */ public static function loadTestListenerDefaultImplementation() { + if ( \version_compare( self::getPHPUnitVersion(), '6.0.0', '<' ) ) { + /* + * Alias one particular PHPUnit 5.x class to its PHPUnit >= 6 name. + * + * All other classes needed are part of the forward-compatibility layer. + * + * {@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_Warning' ) === true + && \class_exists( 'PHPUnit\Framework\Warning' ) === false + ) { + \class_alias( 'PHPUnit_Framework_Warning', 'PHPUnit\Framework\Warning' ); + } + + // PHPUnit < 6.0.0. + require_once __DIR__ . '/src/TestListeners/TestListenerDefaultImplementationPHPUnitLte5.php'; + return; + } + if ( \version_compare( PHPUnit_Version::id(), '7.0.0', '<' ) ) { - // PHPUnit 6.4.0 < 7.0.0. + // PHPUnit 6.0.0 < 7.0.0. require_once __DIR__ . '/src/TestListeners/TestListenerDefaultImplementationPHPUnit6.php'; return; } diff --git a/src/TestListeners/TestListenerDefaultImplementationPHPUnitLte5.php b/src/TestListeners/TestListenerDefaultImplementationPHPUnitLte5.php new file mode 100644 index 0000000..a2da65a --- /dev/null +++ b/src/TestListeners/TestListenerDefaultImplementationPHPUnitLte5.php @@ -0,0 +1,155 @@ +add_error( $test, $e, $time ); + } + + /** + * A warning occurred. + * + * This method is only functional in PHPUnit 6.0 and above. + * + * @param PHPUnit_Framework_Test $test Test object. + * @param PHPUnit_Framework_Warning $e Instance of the warning encountered. + * @param float $time Execution time of this test. + * + * @return void + */ + public function addWarning( PHPUnit_Framework_Test $test, PHPUnit_Framework_Warning $e, $time ) { + $this->add_warning( $test, $e, $time ); + } + + /** + * A failure occurred. + * + * @param PHPUnit_Framework_Test $test Test object. + * @param PHPUnit_Framework_AssertionFailedError $e Instance of the assertion failure + * exception encountered. + * @param float $time Execution time of this test. + * + * @return void + */ + public function addFailure( PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time ) { + $this->add_failure( $test, $e, $time ); + } + + /** + * Incomplete test. + * + * @param PHPUnit_Framework_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( PHPUnit_Framework_Test $test, Exception $e, $time ) { + $this->add_incomplete_test( $test, $e, $time ); + } + + /** + * Risky test. + * + * @param PHPUnit_Framework_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( PHPUnit_Framework_Test $test, Exception $e, $time ) { + $this->add_risky_test( $test, $e, $time ); + } + + /** + * Skipped test. + * + * @param PHPUnit_Framework_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( PHPUnit_Framework_Test $test, Exception $e, $time ) { + $this->add_skipped_test( $test, $e, $time ); + } + + /** + * A test suite started. + * + * @param PHPUnit_Framework_TestSuite $suite Test suite object. + * + * @return void + */ + public function startTestSuite( PHPUnit_Framework_TestSuite $suite ) { + $this->start_test_suite( $suite ); + } + + /** + * A test suite ended. + * + * @param PHPUnit_Framework_TestSuite $suite Test suite object. + * + * @return void + */ + public function endTestSuite( PHPUnit_Framework_TestSuite $suite ) { + $this->end_test_suite( $suite ); + } + + /** + * A test started. + * + * @param PHPUnit_Framework_Test $test Test object. + * + * @return void + */ + public function startTest( PHPUnit_Framework_Test $test ) { + $this->start_test( $test ); + } + + /** + * A test ended. + * + * @param PHPUnit_Framework_Test $test Test object. + * @param float $time Execution time of this test. + * + * @return void + */ + public function endTest( PHPUnit_Framework_Test $test, $time ) { + $this->end_test( $test, $time ); + } +} diff --git a/tests/TestListeners/TestListenerTest.php b/tests/TestListeners/TestListenerTest.php index 5d2969c..9584854 100644 --- a/tests/TestListeners/TestListenerTest.php +++ b/tests/TestListeners/TestListenerTest.php @@ -68,8 +68,14 @@ public function testError() { /** * Test that the TestListener add_warning() method is called. * + * Note: Prior to PHPUnit 6, PHPUnit did not have `addWarning()` support. + * Interestingly enough, it does seem to work on PHPUnit 5, just don't ask me how. + * + * @requires PHPUnit 5 + * * @return void */ + #[RequiresPhpunit( '5' )] public function testWarning() { $test = $this->getTestObject( 'Warning' ); $test->run( $this->result ); @@ -110,8 +116,13 @@ public function testIncomplete() { /** * Test that the TestListener add_risky_test() method is called. * + * Note: It appears that the PHPUnit native recording of risky tests prior to PHPUnit 6 is buggy. + * + * @requires PHPUnit 6 + * * @return void */ + #[RequiresPhpunit( '6' )] public function testRisky() { $test = $this->getTestObject( 'Risky' ); $test->run( $this->result );