Skip to content

Commit af16eb9

Browse files
pieterocpdmaicher
andcommitted
Add in phpunit 11 support
Credited to dmaicher Co-authored-by: dmaicher <dmaicher@users.noreply.github.com>
1 parent 1aa0d83 commit af16eb9

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "This package provides ArraySubset and related asserts once deprecated in PHPUnit 8",
44
"type": "library",
55
"require": {
6-
"phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0",
6+
"phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0 || ^11.0",
77
"php": "^5.4 || ^7.0 || ^8.0"
88
},
99
"license": "MIT",

src/Constraint/ArraySubset.php

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@
88
use ArrayObject;
99
use PHPUnit\Framework\Constraint\Constraint;
1010
use PHPUnit\Framework\ExpectationFailedException;
11-
use PHPUnit\SebastianBergmann\Comparator\ComparisonFailure as Phar_ComparisonFailure;
11+
use PHPUnit\SebastianBergmann\Comparator\ComparisonFailure as ComparisonFailure_In_Phar_Old;
12+
use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar_Old;
13+
use PHPUnitPHAR\SebastianBergmann\Comparator\ComparisonFailure as ComparisonFailure_In_Phar;
14+
use PHPUnitPHAR\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar;
1215
use SebastianBergmann\Comparator\ComparisonFailure;
16+
use SebastianBergmann\Exporter\Exporter;
1317
use SebastianBergmann\RecursionContext\InvalidArgumentException;
1418
use Traversable;
1519

@@ -84,12 +88,7 @@ public function evaluate($other, string $description = '', bool $returnResult =
8488
return null;
8589
}
8690

87-
// Support use of this library when running PHPUnit as a Phar.
88-
if (class_exists(Phar_ComparisonFailure::class) === true) {
89-
$class = Phar_ComparisonFailure::class;
90-
} else {
91-
$class = ComparisonFailure::class;
92-
}
91+
$class = self::getPHPUnitComparisonFailure();
9392

9493
$f = new $class(
9594
$patched,
@@ -107,7 +106,51 @@ public function evaluate($other, string $description = '', bool $returnResult =
107106
*/
108107
public function toString(): string
109108
{
110-
return 'has the subset ' . $this->exporter()->export($this->subset);
109+
$exporter = self::getPHPUnitExporterObject();
110+
111+
return 'has the subset ' . $exporter->export($this->subset);
112+
}
113+
114+
/**
115+
* Helper function to obtain an instance of the Exporter class.
116+
*
117+
* @return SebastianBergmann\Exporter\Exporter|PHPUnitPHAR\SebastianBergmann\Exporter\Exporter|PHPUnit\SebastianBergmann\Exporter\Exporter
118+
*/
119+
private static function getPHPUnitExporterObject()
120+
{
121+
if (class_exists('SebastianBergmann\Comparator\ComparisonFailure')) {
122+
// Composer install or really old PHAR files.
123+
return new Exporter();
124+
}
125+
126+
if (class_exists('PHPUnitPHAR\SebastianBergmann\Comparator\ComparisonFailure')) {
127+
// PHPUnit PHAR file for 8.5.38+, 9.6.19+, 10.5.17+ and 11.0.10+.
128+
return new Exporter_In_Phar();
129+
}
130+
131+
// PHPUnit PHAR file for < 8.5.38, < 9.6.19, < 10.5.17 and < 11.0.10.
132+
return new Exporter_In_Phar_Old();
133+
}
134+
135+
/**
136+
* Helper function to obtain the class name of the ComparisonFailure class.
137+
*
138+
* @return string;
139+
*/
140+
private static function getPHPUnitComparisonFailure()
141+
{
142+
if (class_exists('SebastianBergmann\Exporter\Exporter')) {
143+
// Composer install or really old PHAR files.
144+
return ComparisonFailure::class;
145+
}
146+
147+
if (class_exists('PHPUnitPHAR\SebastianBergmann\Exporter\Exporter')) {
148+
// PHPUnit PHAR file for 8.5.38+, 9.6.19+, 10.5.17+ and 11.0.10+.
149+
return ComparisonFailure_In_Phar::class;
150+
}
151+
152+
// PHPUnit PHAR file for < 8.5.38, < 9.6.19, < 10.5.17 and < 11.0.10.
153+
return ComparisonFailure_In_Phar_Old::class;
111154
}
112155

113156
/**

0 commit comments

Comments
 (0)