From ee15e228be7fb3136afc4113d15f9ad25bc009a9 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 5 Apr 2024 14:39:45 +0200 Subject: [PATCH] AssertClosedResource: fix compatibility with PHPUnit 8/9 PHAR files PHPUnit 8.5..38 and 9.6.19 contain a change in the PHAR files. In particular, a change in how external dependencies included in the packaged PHAR files are prefixed to prevent conflicts with potentially Composer installed dependencies on the same packages. In practice, the prefix for these external dependencies which is being added when the PHAR is being build has changed from `PHPUnit\\` to `PHPUnitPHAR\\`. This impacts the `AssertClosedResource` polyfill which uses the `SebastianBergmann\Exporter\Exporter` class from the external `Exporter` dependency. This commit fixes the issue. Refs: * https://github.com/sebastianbergmann/phpunit/releases/tag/8.5.38 * https://github.com/sebastianbergmann/phpunit/releases/tag/9.6.19 --- src/Polyfills/AssertClosedResource.php | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) 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(); + } }