Skip to content

Commit

Permalink
Merge pull request #164 from Yoast/feature/support-phpunit-8.5.38-9.6…
Browse files Browse the repository at this point in the history
….19_-10.5.17

AssertIgnoringLineEndings: fix compatibility with PHPUnit 8/9/10 PHAR files
  • Loading branch information
jrfnl authored Apr 5, 2024
2 parents 1fb6984 + baff7b5 commit d2a5a0a
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 d2a5a0a

Please sign in to comment.