Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.0 | Drop support for PHPUnit < 6.4 #193

Merged
merged 5 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 8 additions & 18 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ jobs:
include:
# Test against a version on the low-end of the PHPUnit versions supported for each PHP version.
# Using the Composer `--prefer-lowest` option is, unfortunately, not viable, as
# it would result PHP 7.0 - 7.4 all using PHPUnit 5.7.21, which is not the intention.
# it would result PHP 7.0 - 7.4 all using PHPUnit 6.4.4, which is not the intention.
# It also would run into trouble with PHP 8.5.12 being used on PHP 8.0+, while the
# 8.5.12 release still contained a bug which makes it incompatible with PHP 8.1+,
# even though it officially allows for it..
- php: '7.0'
phpunit: '5.7.27'
phpunit: '6.4.4'
coverage: true
experimental: false
- php: '7.1'
phpunit: '5.7.21'
phpunit: '~6.4.4'
coverage: true
experimental: false
- php: '7.2'
phpunit: '6.3.1'
# - PHPUnit 7.0 seems to have an issue with something related to TestListeners, so using PHPUnit 7.1 instead for "low".
phpunit: '~7.1.0'
coverage: true
experimental: false
- php: '7.3'
Expand Down Expand Up @@ -206,24 +207,13 @@ jobs:
# and very select other combinations.
# This should be sufficient to record the coverage for the PHAR specific code.

# PHPUnit 5 is only supported for PHPUnit 5.7.21-latest.
# PHPUnit 6 is only supported for PHPUnit 6.4.4-latest on the officially supported PHP versions.
- php: '7.0'
phpunit: '5.7.21'
- php: '7.0'
phpunit: '5'
- php: '7.1'
phpunit: '5.7.21'
- php: '7.1'
phpunit: '5'
coverage: true

# PHPUnit 6 is fully supported for the officially supported PHP versions.
- php: '7.0'
phpunit: '6.0'
phpunit: '6.4'
- php: '7.0'
phpunit: '6'
- php: '7.2'
phpunit: '6.0'
phpunit: '6.4'
- php: '7.2'
phpunit: '6'
coverage: true
Expand Down
45 changes: 17 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Requirements
------------

* PHP 7.0 or higher.
* [PHPUnit] 5.7 - 10.x (automatically required via Composer).
* [PHPUnit] 6.4 - 10.x (automatically required via Composer).

[PHPUnit]: https://packagist.org/packages/phpunit/phpunit

Expand Down Expand Up @@ -65,7 +65,7 @@ Why use the PHPUnit Polyfills?

This library is set up to allow for creating PHPUnit cross-version compatible tests by offering a number of polyfills for functionality which was introduced, split up or renamed in PHPUnit.

### Write your tests for PHPUnit 10.x and run them on PHPUnit 5.7 - 10.x
### Write your tests for PHPUnit 10.x and run them on PHPUnit 6.4 - 10.x

The polyfills have been setup to allow tests to be _forward_-compatible. What that means is, that your tests can use the assertions supported by the _latest_ PHPUnit version, even when running on older PHPUnit versions.

Expand Down Expand Up @@ -163,11 +163,11 @@ You will need to make sure to `use` any additional traits needed for the polyfil

| PHPUnit | When `use`-ing this trait | You also need to `use` this trait |
| --------- | --------------------------- | --------------------------------- |
| 5.7 < 7.5 | `AssertIgnoringLineEndings` | `AssertStringContains` |
| 6.4 < 7.5 | `AssertIgnoringLineEndings` | `AssertStringContains` |

_**Note: this only applies to the stand-alone use of the traits. The [`TestCase` classes](#testcases) provided by this library already take care of this automatically.**_

Code example for a test using the `AssertIgnoringLineEndings` trait, which needs to be able to run on PHPUnit 5.7:
Code example for a test using the `AssertIgnoringLineEndings` trait, which needs to be able to run on PHPUnit 6.4:
```php
<?php

Expand Down Expand Up @@ -198,14 +198,6 @@ Features

### Polyfill traits

#### PHPUnit < 6.4.0: `Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject`

Polyfills the [`TestCase::expectExceptionObject()`] method to test all aspects of an `Exception` by passing an object to the method.

This method was introduced in PHPUnit 6.4.0.

[`TestCase::expectExceptionObject()`]: https://docs.phpunit.de/en/main/writing-tests-for-phpunit.html#testing-exceptions

#### PHPUnit < 7.5.0: `Yoast\PHPUnitPolyfills\Polyfills\AssertIsType`

Polyfills the following methods:
Expand Down Expand Up @@ -590,7 +582,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 5.x-9.x TestListener polyfill implementation.
> The below documentation is for the PHPUnit 6.x-9.x TestListener polyfill implementation.

[polyfill-ticket]: https://github.com/Yoast/PHPUnit-Polyfills/issues/128

Expand All @@ -609,24 +601,21 @@ 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 | 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)` | |
| 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)` |

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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"require": {
"php": ">=7.0",
"phpunit/phpunit": "^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0"
"phpunit/phpunit": "^6.4.4 || ^7.0 || ^8.0 || ^9.0 || ^10.0"
},
"require-dev": {
"php-parallel-lint/php-console-highlighter": "^1.0.0",
Expand Down
52 changes: 1 addition & 51 deletions phpunitpolyfills-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\Version as PHPUnit_Version;
use PHPUnit_Runner_Version;

if ( \class_exists( 'Yoast\PHPUnitPolyfills\Autoload', false ) === false ) {

Expand Down Expand Up @@ -35,10 +34,6 @@ public static function load( $className ) {
}

switch ( $className ) {
case 'Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject':
self::loadExpectExceptionObject();
return true;

case 'Yoast\PHPUnitPolyfills\Polyfills\AssertIsType':
self::loadAssertIsType();
return true;
Expand Down Expand Up @@ -115,23 +110,6 @@ public static function load( $className ) {
return false;
}

/**
* Load the ExpectExceptionObject polyfill or an empty trait with the same name
* if a PHPUnit version is used which already contains this functionality.
*
* @return void
*/
public static function loadExpectExceptionObject() {
if ( \method_exists( TestCase::class, 'expectExceptionObject' ) === false ) {
// PHPUnit < 6.4.0.
require_once __DIR__ . '/src/Polyfills/ExpectExceptionObject.php';
return;
}

// PHPUnit >= 6.4.0.
require_once __DIR__ . '/src/Polyfills/ExpectExceptionObject_Empty.php';
}

/**
* Load the AssertIsType polyfill or an empty trait with the same name
* if a PHPUnit version is used which already contains this functionality.
Expand Down Expand Up @@ -358,29 +336,8 @@ 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.0.0 < 7.0.0.
// PHPUnit 6.4.4 < 7.0.0.
require_once __DIR__ . '/src/TestListeners/TestListenerDefaultImplementationPHPUnit6.php';
return;
}
Expand All @@ -392,20 +349,13 @@ public static function loadTestListenerDefaultImplementation() {
/**
* Retrieve the PHPUnit version id.
*
* As both the pre-PHPUnit 6 class, as well as the PHPUnit 6+ class contain the `id()` function,
* this should work independently of whether or not another library may have aliased the class.
*
* @return string Version number as a string.
*/
public static function getPHPUnitVersion() {
if ( \class_exists( '\PHPUnit\Runner\Version' ) ) {
return PHPUnit_Version::id();
}

if ( \class_exists( '\PHPUnit_Runner_Version' ) ) {
return PHPUnit_Runner_Version::id();
}

return '0';
}
}
Expand Down
34 changes: 0 additions & 34 deletions src/Polyfills/AssertStringContains.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@
* Use of Assert::assertContains() and Assert::assertNotContains() with string haystacks was
* deprecated in PHPUnit 7.5.0 and removed in PHPUnit 9.0.0.
*
* Note: this polyfill accounts for a bug in PHPUnit < 6.4.2.
* Prior to PHPUnit 6.4.2, when the $needle was an empty string, a PHP native
* "mb_strpos(): Empty delimiter" warning would be thrown, which would result
* in the test failing.
* This polyfill prevents that warning and emulates the PHPUnit >= 6.4.2 behaviour.
*
* @link https://github.com/sebastianbergmann/phpunit/issues/3422
* @link https://github.com/sebastianbergmann/phpunit/issues/2520
* @link https://github.com/sebastianbergmann/phpunit/pull/2778
Expand All @@ -32,11 +26,6 @@ trait AssertStringContains {
* @return void
*/
final public static function assertStringContainsString( $needle, $haystack, $message = '' ) {
if ( $needle === '' ) {
static::assertSame( $needle, $needle, $message );
return;
}

static::assertContains( $needle, $haystack, $message );
}

Expand All @@ -50,11 +39,6 @@ final public static function assertStringContainsString( $needle, $haystack, $me
* @return void
*/
final public static function assertStringContainsStringIgnoringCase( $needle, $haystack, $message = '' ) {
if ( $needle === '' ) {
static::assertSame( $needle, $needle, $message );
return;
}

static::assertContains( $needle, $haystack, $message, true );
}

Expand All @@ -68,15 +52,6 @@ final public static function assertStringContainsStringIgnoringCase( $needle, $h
* @return void
*/
final public static function assertStringNotContainsString( $needle, $haystack, $message = '' ) {
if ( $needle === '' ) {
$msg = "Failed asserting that '{$haystack}' does not contain \"\".";
if ( $message !== '' ) {
$msg = $message . \PHP_EOL . $msg;
}

static::fail( $msg );
}

static::assertNotContains( $needle, $haystack, $message );
}

Expand All @@ -90,15 +65,6 @@ final public static function assertStringNotContainsString( $needle, $haystack,
* @return void
*/
final public static function assertStringNotContainsStringIgnoringCase( $needle, $haystack, $message = '' ) {
if ( $needle === '' ) {
$msg = "Failed asserting that '{$haystack}' does not contain \"\".";
if ( $message !== '' ) {
$msg = $message . \PHP_EOL . $msg;
}

static::fail( $msg );
}

static::assertNotContains( $needle, $haystack, $message, true );
}
}
28 changes: 0 additions & 28 deletions src/Polyfills/ExpectExceptionObject.php

This file was deleted.

8 changes: 0 additions & 8 deletions src/Polyfills/ExpectExceptionObject_Empty.php

This file was deleted.

2 changes: 0 additions & 2 deletions src/TestCases/TestCasePHPUnitLte7.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains;
use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject;

/**
* Basic test case for use with PHPUnit <= 7.
Expand All @@ -42,7 +41,6 @@ abstract class TestCase extends PHPUnit_TestCase {
use AssertStringContains;
use EqualToSpecializations;
use ExpectExceptionMessageMatches;
use ExpectExceptionObject;

/**
* This method is called before the first test of this test class is run.
Expand Down
2 changes: 0 additions & 2 deletions src/TestCases/XTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Yoast\PHPUnitPolyfills\Polyfills\AssertStringContains;
use Yoast\PHPUnitPolyfills\Polyfills\EqualToSpecializations;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionMessageMatches;
use Yoast\PHPUnitPolyfills\Polyfills\ExpectExceptionObject;

/**
* Basic test case for use with PHPUnit cross-version.
Expand Down Expand Up @@ -48,7 +47,6 @@ abstract class XTestCase extends PHPUnit_TestCase {
use AssertStringContains;
use EqualToSpecializations;
use ExpectExceptionMessageMatches;
use ExpectExceptionObject;

/**
* This method is called before the first test of this test class is run.
Expand Down
Loading
Loading