2
2
3
3
namespace Yoast \PHPUnitPolyfills \Polyfills ;
4
4
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 ;
6
7
use SebastianBergmann \Exporter \Exporter ;
7
8
use Yoast \PHPUnitPolyfills \Helpers \ResourceHelper ;
8
9
@@ -25,7 +26,7 @@ trait AssertClosedResource {
25
26
* @return void
26
27
*/
27
28
public static function assertIsClosedResource ( $ actual , $ message = '' ) {
28
- $ exporter = \class_exists ( ' SebastianBergmann\Exporter\Exporter ' ) ? new Exporter () : new Exporter_In_Phar ();
29
+ $ exporter = self :: getPHPUnitExporterObject ();
29
30
$ msg = \sprintf ( 'Failed asserting that %s is of type "resource (closed)" ' , $ exporter ->export ( $ actual ) );
30
31
31
32
if ( $ message !== '' ) {
@@ -44,7 +45,7 @@ public static function assertIsClosedResource( $actual, $message = '' ) {
44
45
* @return void
45
46
*/
46
47
public static function assertIsNotClosedResource ( $ actual , $ message = '' ) {
47
- $ exporter = \class_exists ( ' SebastianBergmann\Exporter\Exporter ' ) ? new Exporter () : new Exporter_In_Phar ();
48
+ $ exporter = self :: getPHPUnitExporterObject ();
48
49
$ type = $ exporter ->export ( $ actual );
49
50
if ( $ type === 'NULL ' ) {
50
51
$ type = 'resource (closed) ' ;
@@ -77,4 +78,23 @@ public static function assertIsNotClosedResource( $actual, $message = '' ) {
77
78
public static function shouldClosedResourceAssertionBeSkipped ( $ actual ) {
78
79
return ( ResourceHelper::isResourceStateReliable ( $ actual ) === false );
79
80
}
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
+ }
80
100
}
0 commit comments