2
2
3
3
namespace DMS \PHPUnitExtensions \ArraySubset ;
4
4
5
+ use PHPUnit \Runner \Version as PHPUnit_Version ;
6
+ use PHPUnit_Runner_Version ;
7
+
5
8
if (\class_exists ('DMS\PHPUnitExtensions\ArraySubset\Autoload ' , false ) === false ) {
6
9
7
10
/**
@@ -25,49 +28,51 @@ public static function load($className)
25
28
return false ;
26
29
}
27
30
31
+ $ loadPolyfill = \version_compare (self ::getPHPUnitVersion (), '8.0.0 ' , '>= ' );
32
+
28
33
switch ($ className ) {
29
34
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.
32
37
require_once __DIR__ . '/src/ArraySubsetAsserts.php ' ;
33
38
34
39
return true ;
35
40
}
36
41
37
- // PHPUnit < 9 .0.0.
42
+ // PHPUnit < 8 .0.0.
38
43
require_once __DIR__ . '/src/ArraySubsetAssertsEmpty.php ' ;
39
44
40
45
return true ;
41
46
42
47
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.
45
50
require_once __DIR__ . '/src/Assert.php ' ;
46
51
47
52
return true ;
48
53
}
49
54
50
- // PHPUnit < 9 .0.0.
55
+ // PHPUnit < 8 .0.0.
51
56
require_once __DIR__ . '/src/AssertFallThrough.php ' ;
52
57
53
58
return true ;
54
59
55
60
/*
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.
58
63
*/
59
64
default :
60
- if (\method_exists ( ' \PHPUnit\Framework\Assert ' , ' assertArraySubset ' ) === true ) {
65
+ if ($ loadPolyfill === false ) {
61
66
// PHPUnit < 9.0.0.
62
67
throw new \RuntimeException (
63
68
\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 ' ,
65
70
$ className
66
71
)
67
72
);
68
73
}
69
74
70
- // PHPUnit >= 9 .0.0.
75
+ // PHPUnit >= 8 .0.0.
71
76
$ file = \realpath (
72
77
__DIR__ . \DIRECTORY_SEPARATOR
73
78
. 'src ' . \DIRECTORY_SEPARATOR
@@ -83,6 +88,27 @@ public static function load($className)
83
88
84
89
return false ;
85
90
}
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
+ }
86
112
}
87
113
88
114
\spl_autoload_register (__NAMESPACE__ . '\Autoload::load ' );
0 commit comments