diff --git a/src/Polyfills/AssertClosedResource.php b/src/Polyfills/AssertClosedResource.php index 799b6d9..c468386 100644 --- a/src/Polyfills/AssertClosedResource.php +++ b/src/Polyfills/AssertClosedResource.php @@ -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; @@ -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 !== '' ) { @@ -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)'; @@ -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(); + } }