Skip to content

Commit

Permalink
AssertIgnoringLineEndings: fix compatibility with PHPUnit 8/9/10 PHAR…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
jrfnl committed Apr 5, 2024
1 parent 1fb6984 commit e03879e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
24 changes: 22 additions & 2 deletions src/Polyfills/AssertIgnoringLineEndings.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Yoast\PHPUnitPolyfills\Polyfills;

use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar;
use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar_Old;
use PHPUnitPHAR\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar;
use SebastianBergmann\Exporter\Exporter;
use TypeError;

Expand Down Expand Up @@ -56,7 +57,7 @@ final public static function assertStringEqualsStringIgnoringLineEndings( $expec
}

$expected = self::normalizeLineEndingsForIgnoringLineEndingsAssertions( (string) $expected );
$exporter = \class_exists( Exporter::class ) ? new Exporter() : new Exporter_In_Phar();
$exporter = self::getPHPUnitExporterObjectForIgnoringLineEndings();
$msg = \sprintf(
'Failed asserting that %s is equal to "%s" ignoring line endings.',
$exporter->export( $actual ),
Expand Down Expand Up @@ -128,4 +129,23 @@ private static function normalizeLineEndingsForIgnoringLineEndingsAssertions( $v
]
);
}

/**
* Helper function to obtain an instance of the Exporter class.
*
* @return SebastianBergmann\Exporter\Exporter|PHPUnitPHAR\SebastianBergmann\Exporter\Exporter|PHPUnit\SebastianBergmann\Exporter\Exporter
*/
private static function getPHPUnitExporterObjectForIgnoringLineEndings() {
if ( \class_exists( Exporter::class ) ) {
// Composer install or really old PHAR files.
return new Exporter();
}
elseif ( \class_exists( Exporter_In_Phar::class ) ) {
// PHPUnit PHAR file for 8.5.38+, 9.6.19+, 10.5.17+ and 11.0.10+.
return new Exporter_In_Phar();
}

// PHPUnit PHAR file for < 8.5.38, < 9.6.19, < 10.5.17 and < 11.0.10.
return new Exporter_In_Phar_Old();
}
}
30 changes: 26 additions & 4 deletions tests/Polyfills/AssertIgnoringLineEndingsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\TestCase;
use PHPUnit\Runner\Version as PHPUnit_Version;
use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar;
use PHPUnit\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar_Old;
use PHPUnit_Framework_AssertionFailedError;
use PHPUnitPHAR\SebastianBergmann\Exporter\Exporter as Exporter_In_Phar;
use SebastianBergmann\Exporter\Exporter;
use stdClass;
use TypeError;
Expand Down Expand Up @@ -140,7 +141,7 @@ public static function dataAssertStringEqualsStringIgnoringLineEndingsTypeVariat
*/
public function testAssertStringEqualsStringIgnoringLineEndingsFails( $expected, $actual ) {

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

$exporter = \class_exists( Exporter::class ) ? new Exporter() : new Exporter_In_Phar();
$exporter = self::getPHPUnitExporterObjectForIgnoringLineEndingsForTests();
$msg = \sprintf(
'Failed asserting that %s is equal to "%s" ignoring line endings.',
$exporter->export( $actual ),
Expand Down Expand Up @@ -316,7 +317,7 @@ public function testAssertStringContainsStringIgnoringLineEndingsBug5279( $needl
* @return void
*/
public function testAssertStringContainsStringIgnoringLineEndingsFails( $needle, $haystack ) {
$exporter = \class_exists( Exporter::class ) ? new Exporter() : new Exporter_In_Phar();
$exporter = self::getPHPUnitExporterObjectForIgnoringLineEndingsForTests();
$pattern = \sprintf(
'`^Failed asserting that %1$s%3$s contains "%2$s"%3$s\.`',
\preg_quote( $exporter->export( $haystack ), '`' ),
Expand Down Expand Up @@ -389,4 +390,25 @@ private static function normalizeLineEndings( $value ) {
]
);
}

/**
* Helper function to obtain an instance of the Exporter class.
*
* Note: the helper from the trait is accessible, but may not be available if the "empty" trait is being loaded.
*
* @return SebastianBergmann\Exporter\Exporter|PHPUnitPHAR\SebastianBergmann\Exporter\Exporter|PHPUnit\SebastianBergmann\Exporter\Exporter
*/
private static function getPHPUnitExporterObjectForIgnoringLineEndingsForTests() {
if ( \class_exists( Exporter::class ) ) {
// Composer install or really old PHAR files.
return new Exporter();
}
elseif ( \class_exists( Exporter_In_Phar::class ) ) {
// PHPUnit PHAR file for 8.5.38+, 9.6.19+, 10.5.17+ and 11.0.10+.
return new Exporter_In_Phar();
}

// PHPUnit PHAR file for < 8.5.38, < 9.6.19, < 10.5.17 and < 11.0.10.
return new Exporter_In_Phar_Old();
}
}

0 comments on commit e03879e

Please sign in to comment.