Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AssertIgnoringLineEndings: fix compatibility with PHPUnit 8/9/10 PHAR files #164

Merged
merged 1 commit into from
Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}