Skip to content

feat: add support for PHPUnit 11 #17

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

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion .composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

"null", "true", "false",
"static", "self", "parent",
"array", "string", "int", "float", "bool", "iterable", "callable", "void"
"array", "string", "int", "float", "bool", "iterable", "callable", "void", "mixed"
]
}
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v3
- uses: shivammathur/setup-php@v2
with:
php-version: '8.1'
php-version: '8.2'
coverage: none
- run: composer update --no-progress
- run: composer update --no-progress --working-dir=dev-tools
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": "^7.0 || ^8.0",
"phpunit/phpunit": "^6.5.14 || ^7.5.20 || ^8.0 || ^9.0 || ^10.0"
"phpunit/phpunit": "^6.5.14 || ^7.5.20 || ^8.0 || ^9.0 || ^10.0 || ^11.0"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 1 addition & 1 deletion dev-tools/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"php": "^8.1"
"php": "^8.2"
},
"require-dev": {
"ergebnis/composer-normalize": "*",
Expand Down
12 changes: 8 additions & 4 deletions src/Constraint/IsIdenticalString.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@

namespace PhpCsFixer\PhpunitConstraintIsIdenticalString\Constraint;

if (version_compare(\PHPUnit\Runner\Version::id(), '7.0.0') < 0) {
use PHPUnit\Runner\Version;

if (version_compare(Version::id(), '7.0.0') < 0) {
class_alias(IsIdenticalStringForV5::class, IsIdenticalString::class);
} elseif (version_compare(\PHPUnit\Runner\Version::id(), '8.0.0') < 0) {
} elseif (version_compare(Version::id(), '8.0.0') < 0) {
class_alias(IsIdenticalStringForV7::class, IsIdenticalString::class);
} elseif (version_compare(\PHPUnit\Runner\Version::id(), '9.0.0') < 0) {
} elseif (version_compare(Version::id(), '9.0.0') < 0) {
class_alias(IsIdenticalStringForV8::class, IsIdenticalString::class);
} else {
} elseif (version_compare(Version::id(), '11.0.0') < 0) {
class_alias(IsIdenticalStringForV9::class, IsIdenticalString::class);
} else {
class_alias(IsIdenticalStringForV11::class, IsIdenticalString::class);
}
71 changes: 71 additions & 0 deletions src/Constraint/IsIdenticalStringForV11.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of PHP CS Fixer / PHPUnit Constraint IsIdenticalString.
*
* (c) Dariusz Rumiński <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace PhpCsFixer\PhpunitConstraintIsIdenticalString\Constraint;

use PHPUnit\Framework\Constraint\Constraint;
use PHPUnit\Framework\Constraint\IsIdentical;
use PHPUnit\Framework\ExpectationFailedException;

/**
* @internal
*/
final readonly class IsIdenticalStringForV11 extends Constraint
{
private IsIdentical $isIdentical;

public function __construct(private mixed $value)
{
$this->isIdentical = new IsIdentical($this->value);
}

public function evaluate(mixed $other, string $description = '', bool $returnResult = false): ?bool
{
try {
return $this->isIdentical->evaluate($other, $description, $returnResult);
} catch (ExpectationFailedException $exception) {
$message = $exception->getMessage();

$additionalFailureDescription = $this->additionalFailureDescription($other);

if ($additionalFailureDescription) {
$message .= "\n".$additionalFailureDescription;
}

throw new ExpectationFailedException(
$message,
$exception->getComparisonFailure(),
$exception
);
}
}

public function toString(): string
{
return $this->isIdentical->toString();
}

protected function additionalFailureDescription(mixed $other): string
{
if (
$other === $this->value
|| preg_replace('/(\r\n|\n\r|\r)/', "\n", $other) !== preg_replace('/(\r\n|\n\r|\r)/', "\n", $this->value)
) {
return '';
}

return ' #Warning: Strings contain different line endings! Debug using remapping ["\r" => "R", "\n" => "N", "\t" => "T"]:'
."\n"
.' -'.str_replace(["\r", "\n", "\t"], ['R', 'N', 'T'], $other)
."\n"
.' +'.str_replace(["\r", "\n", "\t"], ['R', 'N', 'T'], $this->value);
}
}