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(); + } }