22
33namespace DMS \PHPUnitExtensions \ArraySubset ;
44
5+ use PHPUnit \Runner \Version as PHPUnit_Version ;
6+ use PHPUnit_Runner_Version ;
7+
58if (\class_exists ('DMS\PHPUnitExtensions\ArraySubset\Autoload ' , false ) === false ) {
69
710 /**
@@ -25,49 +28,51 @@ public static function load($className)
2528 return false ;
2629 }
2730
31+ $ loadPolyfill = \version_compare (self ::getPHPUnitVersion (), '8.0.0 ' , '>= ' );
32+
2833 switch ($ className ) {
2934 case 'DMS\PHPUnitExtensions\ArraySubset\ArraySubsetAsserts ' :
30- if (\method_exists ( ' \PHPUnit\Framework\Assert ' , ' assertArraySubset ' ) === false ) {
31- // PHPUnit >= 9 .0.0.
35+ if ($ loadPolyfill === true ) {
36+ // PHPUnit >= 8 .0.0.
3237 require_once __DIR__ . '/src/ArraySubsetAsserts.php ' ;
3338
3439 return true ;
3540 }
3641
37- // PHPUnit < 9 .0.0.
42+ // PHPUnit < 8 .0.0.
3843 require_once __DIR__ . '/src/ArraySubsetAssertsEmpty.php ' ;
3944
4045 return true ;
4146
4247 case 'DMS\PHPUnitExtensions\ArraySubset\Assert ' :
43- if (\method_exists ( ' \PHPUnit\Framework\Assert ' , ' assertArraySubset ' ) === false ) {
44- // PHPUnit >= 9 .0.0.
48+ if ($ loadPolyfill === true ) {
49+ // PHPUnit >= 8 .0.0.
4550 require_once __DIR__ . '/src/Assert.php ' ;
4651
4752 return true ;
4853 }
4954
50- // PHPUnit < 9 .0.0.
55+ // PHPUnit < 8 .0.0.
5156 require_once __DIR__ . '/src/AssertFallThrough.php ' ;
5257
5358 return true ;
5459
5560 /*
56- * Handle arbitrary additional classes via PSR-4, but only allow loading on PHPUnit >= 9 .0.0,
57- * as additional classes should only ever _need_ to be loaded when using PHPUnit >= 9 .0.0.
61+ * Handle arbitrary additional classes via PSR-4, but only allow loading on PHPUnit >= 8 .0.0,
62+ * as additional classes should only ever _need_ to be loaded when using PHPUnit >= 8 .0.0.
5863 */
5964 default :
60- if (\method_exists ( ' \PHPUnit\Framework\Assert ' , ' assertArraySubset ' ) === true ) {
65+ if ($ loadPolyfill === false ) {
6166 // PHPUnit < 9.0.0.
6267 throw new \RuntimeException (
6368 \sprintf (
64- 'Using class "%s" is only supported in combination with PHPUnit >= 9 .0.0 ' ,
69+ 'Using class "%s" is only supported in combination with PHPUnit >= 8 .0.0 ' ,
6570 $ className
6671 )
6772 );
6873 }
6974
70- // PHPUnit >= 9 .0.0.
75+ // PHPUnit >= 8 .0.0.
7176 $ file = \realpath (
7277 __DIR__ . \DIRECTORY_SEPARATOR
7378 . 'src ' . \DIRECTORY_SEPARATOR
@@ -83,6 +88,27 @@ public static function load($className)
8388
8489 return false ;
8590 }
91+
92+ /**
93+ * Retrieve the PHPUnit version id.
94+ *
95+ * As both the pre-PHPUnit 6 class, as well as the PHPUnit 6+ class contain the `id()` function,
96+ * this should work independently of whether or not another library may have aliased the class.
97+ *
98+ * @return string Version number as a string.
99+ */
100+ public static function getPHPUnitVersion ()
101+ {
102+ if (\class_exists ('\PHPUnit\Runner\Version ' )) {
103+ return PHPUnit_Version::id ();
104+ }
105+
106+ if (\class_exists ('\PHPUnit_Runner_Version ' )) {
107+ return PHPUnit_Runner_Version::id ();
108+ }
109+
110+ return '0 ' ;
111+ }
86112 }
87113
88114 \spl_autoload_register (__NAMESPACE__ . '\Autoload::load ' );
0 commit comments