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

Autoload: improve/stabilize version check for TestCase #59

Merged
merged 1 commit into from
Oct 3, 2021
Merged
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
27 changes: 23 additions & 4 deletions phpunitpolyfills-autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Yoast\PHPUnitPolyfills;

use PHPUnit\Runner\Version as PHPUnit_Version;
use PHPUnit_Runner_Version;

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

Expand Down Expand Up @@ -426,9 +427,7 @@ public static function loadAssertObjectEquals() {
* @return void
*/
public static function loadTestCase() {
if ( \class_exists( '\PHPUnit_Runner_Version' ) === true
|| \version_compare( PHPUnit_Version::id(), '8.0.0', '<' )
) {
if ( \version_compare( self::getPHPUnitVersion(), '8.0.0', '<' ) ) {
// PHPUnit < 8.0.0.
require_once __DIR__ . '/src/TestCases/TestCasePHPUnitLte7.php';
return;
Expand All @@ -444,7 +443,7 @@ public static function loadTestCase() {
* @return void
*/
public static function loadTestListenerDefaultImplementation() {
if ( \class_exists( '\PHPUnit_Runner_Version' ) === true ) {
if ( \version_compare( self::getPHPUnitVersion(), '6.0.0', '<' ) ) {
/*
* Alias one particular PHPUnit 4/5 class to its PHPUnit >= 6 name.
*
Expand Down Expand Up @@ -474,6 +473,26 @@ public static function loadTestListenerDefaultImplementation() {
// PHPUnit >= 7.0.0.
require_once __DIR__ . '/src/TestListeners/TestListenerDefaultImplementationPHPUnitGte7.php';
}

/**
* 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';
}
}

\spl_autoload_register( __NAMESPACE__ . '\Autoload::load' );
Expand Down