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

AssertClosedResource: fix compatibility with PHPUnit 8/9 PHAR files #161

Merged
merged 1 commit into from
Apr 5, 2024
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
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();
}
}