From baff7b538649a64795ad1ef9249766d66096c037 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 5 Apr 2024 17:11:29 +0200 Subject: [PATCH] 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 --- src/Polyfills/AssertIgnoringLineEndings.php | 24 +++++++++++++-- .../AssertIgnoringLineEndingsTest.php | 30 ++++++++++++++++--- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/src/Polyfills/AssertIgnoringLineEndings.php b/src/Polyfills/AssertIgnoringLineEndings.php index 609abd5..2750ec4 100644 --- a/src/Polyfills/AssertIgnoringLineEndings.php +++ b/src/Polyfills/AssertIgnoringLineEndings.php @@ -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; @@ -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 ), @@ -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(); + } } diff --git a/tests/Polyfills/AssertIgnoringLineEndingsTest.php b/tests/Polyfills/AssertIgnoringLineEndingsTest.php index b4493c4..ab4fe15 100644 --- a/tests/Polyfills/AssertIgnoringLineEndingsTest.php +++ b/tests/Polyfills/AssertIgnoringLineEndingsTest.php @@ -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; @@ -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 ), @@ -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 ), @@ -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 ), '`' ), @@ -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(); + } }