Skip to content

Commit baff7b5

Browse files
committed
AssertIgnoringLineEndings: fix compatibility with PHPUnit 8/9/10 PHAR files
PHPUnit 8.5..38, 9.6.19 and 10.5.17 contain a change in the PHAR files. In particular, a change in how external dependencies included in the packaged PHAR files are prefixed to prevent conflicts with potentially Composer installed dependencies on the same packages. In practice, the prefix for these external dependencies which is being added when the PHAR is being build has changed from `PHPUnit\\` to `PHPUnitPHAR\\`. This impacts the `AssertIgnoringLineEndings` polyfill which uses the `SebastianBergmann\Exporter\Exporter` class from the external `Exporter` dependency. This commit fixes the issue. Refs: * https://github.com/sebastianbergmann/phpunit/releases/tag/8.5.38 * https://github.com/sebastianbergmann/phpunit/releases/tag/9.6.19 * https://github.com/sebastianbergmann/phpunit/releases/tag/10.5.17
1 parent 1fb6984 commit baff7b5

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

src/Polyfills/AssertIgnoringLineEndings.php

Lines changed: 22 additions & 2 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 TypeError;
89

@@ -56,7 +57,7 @@ final public static function assertStringEqualsStringIgnoringLineEndings( $expec
5657
}
5758

5859
$expected = self::normalizeLineEndingsForIgnoringLineEndingsAssertions( (string) $expected );
59-
$exporter = \class_exists( Exporter::class ) ? new Exporter() : new Exporter_In_Phar();
60+
$exporter = self::getPHPUnitExporterObjectForIgnoringLineEndings();
6061
$msg = \sprintf(
6162
'Failed asserting that %s is equal to "%s" ignoring line endings.',
6263
$exporter->export( $actual ),
@@ -128,4 +129,23 @@ private static function normalizeLineEndingsForIgnoringLineEndingsAssertions( $v
128129
]
129130
);
130131
}
132+
133+
/**
134+
* Helper function to obtain an instance of the Exporter class.
135+
*
136+
* @return SebastianBergmann\Exporter\Exporter|PHPUnitPHAR\SebastianBergmann\Exporter\Exporter|PHPUnit\SebastianBergmann\Exporter\Exporter
137+
*/
138+
private static function getPHPUnitExporterObjectForIgnoringLineEndings() {
139+
if ( \class_exists( Exporter::class ) ) {
140+
// Composer install or really old PHAR files.
141+
return new Exporter();
142+
}
143+
elseif ( \class_exists( Exporter_In_Phar::class ) ) {
144+
// PHPUnit PHAR file for 8.5.38+, 9.6.19+, 10.5.17+ and 11.0.10+.
145+
return new Exporter_In_Phar();
146+
}
147+
148+
// PHPUnit PHAR file for < 8.5.38, < 9.6.19, < 10.5.17 and < 11.0.10.
149+
return new Exporter_In_Phar_Old();
150+
}
131151
}

tests/Polyfills/AssertIgnoringLineEndingsTest.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
use PHPUnit\Framework\AssertionFailedError;
66
use PHPUnit\Framework\TestCase;
77
use PHPUnit\Runner\Version as PHPUnit_Version;
8-
use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar;
8+
use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar_Old;
99
use PHPUnit_Framework_AssertionFailedError;
10+
use PHPUnitPHAR\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar;
1011
use SebastianBergmann\Exporter\Exporter;
1112
use stdClass;
1213
use TypeError;
@@ -140,7 +141,7 @@ public static function dataAssertStringEqualsStringIgnoringLineEndingsTypeVariat
140141
*/
141142
public function testAssertStringEqualsStringIgnoringLineEndingsFails( $expected, $actual ) {
142143

143-
$exporter = \class_exists( Exporter::class ) ? new Exporter() : new Exporter_In_Phar();
144+
$exporter = self::getPHPUnitExporterObjectForIgnoringLineEndingsForTests();
144145
$msg = \sprintf(
145146
'Failed asserting that %s is equal to "%s" ignoring line endings.',
146147
$exporter->export( $actual ),
@@ -179,7 +180,7 @@ public function testAssertStringEqualsStringIgnoringLineEndingsFailsWithCustomMe
179180
$actual = 'ab';
180181
$expected = "a b\n";
181182

182-
$exporter = \class_exists( Exporter::class ) ? new Exporter() : new Exporter_In_Phar();
183+
$exporter = self::getPHPUnitExporterObjectForIgnoringLineEndingsForTests();
183184
$msg = \sprintf(
184185
'Failed asserting that %s is equal to "%s" ignoring line endings.',
185186
$exporter->export( $actual ),
@@ -316,7 +317,7 @@ public function testAssertStringContainsStringIgnoringLineEndingsBug5279( $needl
316317
* @return void
317318
*/
318319
public function testAssertStringContainsStringIgnoringLineEndingsFails( $needle, $haystack ) {
319-
$exporter = \class_exists( Exporter::class ) ? new Exporter() : new Exporter_In_Phar();
320+
$exporter = self::getPHPUnitExporterObjectForIgnoringLineEndingsForTests();
320321
$pattern = \sprintf(
321322
'`^Failed asserting that %1$s%3$s contains "%2$s"%3$s\.`',
322323
\preg_quote( $exporter->export( $haystack ), '`' ),
@@ -389,4 +390,25 @@ private static function normalizeLineEndings( $value ) {
389390
]
390391
);
391392
}
393+
394+
/**
395+
* Helper function to obtain an instance of the Exporter class.
396+
*
397+
* Note: the helper from the trait is accessible, but may not be available if the "empty" trait is being loaded.
398+
*
399+
* @return SebastianBergmann\Exporter\Exporter|PHPUnitPHAR\SebastianBergmann\Exporter\Exporter|PHPUnit\SebastianBergmann\Exporter\Exporter
400+
*/
401+
private static function getPHPUnitExporterObjectForIgnoringLineEndingsForTests() {
402+
if ( \class_exists( Exporter::class ) ) {
403+
// Composer install or really old PHAR files.
404+
return new Exporter();
405+
}
406+
elseif ( \class_exists( Exporter_In_Phar::class ) ) {
407+
// PHPUnit PHAR file for 8.5.38+, 9.6.19+, 10.5.17+ and 11.0.10+.
408+
return new Exporter_In_Phar();
409+
}
410+
411+
// PHPUnit PHAR file for < 8.5.38, < 9.6.19, < 10.5.17 and < 11.0.10.
412+
return new Exporter_In_Phar_Old();
413+
}
392414
}

0 commit comments

Comments
 (0)