Skip to content

Commit 51c2415

Browse files
authored
Merge pull request #161 from Yoast/feature/support-phpunit-8.5.38-9.6.19
AssertClosedResource: fix compatibility with PHPUnit 8/9 PHAR files
2 parents 3380dcc + ee15e22 commit 51c2415

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/Polyfills/AssertClosedResource.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace Yoast\PHPUnitPolyfills\Polyfills;
44

5-
use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar;
5+
use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar_Old;
6+
use PHPUnitPHAR\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar;
67
use SebastianBergmann\Exporter\Exporter;
78
use Yoast\PHPUnitPolyfills\Helpers\ResourceHelper;
89

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

3132
if ( $message !== '' ) {
@@ -44,7 +45,7 @@ public static function assertIsClosedResource( $actual, $message = '' ) {
4445
* @return void
4546
*/
4647
public static function assertIsNotClosedResource( $actual, $message = '' ) {
47-
$exporter = \class_exists( 'SebastianBergmann\Exporter\Exporter' ) ? new Exporter() : new Exporter_In_Phar();
48+
$exporter = self::getPHPUnitExporterObject();
4849
$type = $exporter->export( $actual );
4950
if ( $type === 'NULL' ) {
5051
$type = 'resource (closed)';
@@ -77,4 +78,23 @@ public static function assertIsNotClosedResource( $actual, $message = '' ) {
7778
public static function shouldClosedResourceAssertionBeSkipped( $actual ) {
7879
return ( ResourceHelper::isResourceStateReliable( $actual ) === false );
7980
}
81+
82+
/**
83+
* Helper function to obtain an instance of the Exporter class.
84+
*
85+
* @return SebastianBergmann\Exporter\Exporter|PHPUnitPHAR\SebastianBergmann\Exporter\Exporter|PHPUnit\SebastianBergmann\Exporter\Exporter
86+
*/
87+
private static function getPHPUnitExporterObject() {
88+
if ( \class_exists( 'SebastianBergmann\Exporter\Exporter' ) ) {
89+
// Composer install or really old PHAR files.
90+
return new Exporter();
91+
}
92+
elseif ( \class_exists( 'PHPUnitPHAR\SebastianBergmann\Exporter\Exporter' ) ) {
93+
// PHPUnit PHAR file for 8.5.38+, 9.6.19+, 10.5.17+ and 11.0.10+.
94+
return new Exporter_In_Phar();
95+
}
96+
97+
// PHPUnit PHAR file for < 8.5.38, < 9.6.19, < 10.5.17 and < 11.0.10.
98+
return new Exporter_In_Phar_Old();
99+
}
80100
}

0 commit comments

Comments
 (0)