Skip to content

Commit

Permalink
Revert "Drop support for PHPUnit < 6.4 [2]"
Browse files Browse the repository at this point in the history
This reverts commit f1850a7.
  • Loading branch information
jrfnl committed Jul 17, 2024
1 parent e3fd3ff commit 176cd96
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 14 deletions.
29 changes: 16 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 {}

Expand Down
23 changes: 22 additions & 1 deletion phpunitpolyfills-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
155 changes: 155 additions & 0 deletions src/TestListeners/TestListenerDefaultImplementationPHPUnitLte5.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<?php

namespace Yoast\PHPUnitPolyfills\TestListeners;

use Exception;
use PHPUnit_Framework_AssertionFailedError;
use PHPUnit_Framework_Test;
use PHPUnit_Framework_TestSuite;
use PHPUnit_Framework_Warning;

/**
* Basic TestListener implementation for use with PHPUnit 5.x.
*
* This TestListener trait uses renamed (snakecase) methods for all standard methods in
* a TestListener to get round the method signature changes in various PHPUnit versions.
*
* When using this TestListener trait, the snake_case method names need to be used to implement
* the listener functionality.
*
* {@internal While in essence this trait is no different from the PHPUnit 6.x version, this
* version is necessary as the class/interface name type declarations used in the PHPUnit 6.x
* file are based on the namespaced names. As both the namespaced names as well as the
* non-namespaced names exist in PHPUnit 5.7.21+, we cannot create class aliases to
* get round the signature mismatch and need this trait using the old names instead.}
*/
trait TestListenerDefaultImplementation {

use TestListenerSnakeCaseMethods;

/**
* An error occurred.
*
* @param PHPUnit_Framework_Test $test Test object.
* @param Exception $e Instance of the error encountered.
* @param float $time Execution time of this test.
*
* @return void
*/
public function addError( PHPUnit_Framework_Test $test, Exception $e, $time ) {
$this->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 );
}
}
11 changes: 11 additions & 0 deletions tests/TestListeners/TestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit 176cd96

Please sign in to comment.