Skip to content

Commit

Permalink
Merge pull request #161 from Yoast/feature/support-phpunit-8.5.38-9.6.19
Browse files Browse the repository at this point in the history
AssertClosedResource: fix compatibility with PHPUnit 8/9 PHAR files
  • Loading branch information
jrfnl committed Apr 5, 2024
2 parents 3380dcc + ee15e22 commit 51c2415
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/Polyfills/AssertClosedResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Yoast\PHPUnitPolyfills\Polyfills;

use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar;
use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar_Old;
use PHPUnitPHAR\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar;
use SebastianBergmann\Exporter\Exporter;
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;

Expand All @@ -25,7 +26,7 @@ trait AssertClosedResource {
* @return void
*/
public static function assertIsClosedResource( $actual, $message = '' ) {
$exporter = \class_exists( 'SebastianBergmann\Exporter\Exporter' ) ? new Exporter() : new Exporter_In_Phar();
$exporter = self::getPHPUnitExporterObject();
$msg = \sprintf( 'Failed asserting that %s is of type "resource (closed)"', $exporter->export( $actual ) );

if ( $message !== '' ) {
Expand All @@ -44,7 +45,7 @@ public static function assertIsClosedResource( $actual, $message = '' ) {
* @return void
*/
public static function assertIsNotClosedResource( $actual, $message = '' ) {
$exporter = \class_exists( 'SebastianBergmann\Exporter\Exporter' ) ? new Exporter() : new Exporter_In_Phar();
$exporter = self::getPHPUnitExporterObject();
$type = $exporter->export( $actual );
if ( $type === 'NULL' ) {
$type = 'resource (closed)';
Expand Down Expand Up @@ -77,4 +78,23 @@ public static function assertIsNotClosedResource( $actual, $message = '' ) {
public static function shouldClosedResourceAssertionBeSkipped( $actual ) {
return ( ResourceHelper::isResourceStateReliable( $actual ) === false );
}

/**
* Helper function to obtain an instance of the Exporter class.
*
* @return SebastianBergmann\Exporter\Exporter|PHPUnitPHAR\SebastianBergmann\Exporter\Exporter|PHPUnit\SebastianBergmann\Exporter\Exporter
*/
private static function getPHPUnitExporterObject() {
if ( \class_exists( 'SebastianBergmann\Exporter\Exporter' ) ) {
// Composer install or really old PHAR files.
return new Exporter();
}
elseif ( \class_exists( 'PHPUnitPHAR\SebastianBergmann\Exporter\Exporter' ) ) {
// PHPUnit PHAR file for 8.5.38+, 9.6.19+, 10.5.17+ and 11.0.10+.
return new Exporter_In_Phar();
}

// PHPUnit PHAR file for < 8.5.38, < 9.6.19, < 10.5.17 and < 11.0.10.
return new Exporter_In_Phar_Old();
}
}

0 comments on commit 51c2415

Please sign in to comment.