Skip to content

Commit

Permalink
Merge pull request #59 from Yoast/feature/autoload-tweak
Browse files Browse the repository at this point in the history
Autoload: improve/stabilize version check for TestCase
  • Loading branch information
jrfnl authored Oct 3, 2021
2 parents ce8ae4a + 24ea563 commit 981f0ce
Showing 1 changed file with 23 additions and 4 deletions.
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

0 comments on commit 981f0ce

Please sign in to comment.