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

Fix PHPUnit comparator under PHPUnit 10 #746

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 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
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@
"php-http/mock-client": "^1.4.1",
"phpbench/phpbench": "^1.2.5",
"phpspec/phpspec": "^7.3",
"phpunit/phpunit": "^9.5.4",
"phpunit/phpunit": "^9.5.4 || ^10",
"psalm/plugin-phpunit": "^0.18.4",
"psr/cache": "^1.0.1",
"vimeo/psalm": "~5.3.0"
"vimeo/psalm": "~5.5.0"
},
"suggest": {
"ext-gmp": "Calculate without integer limits",
Expand Down Expand Up @@ -91,7 +91,7 @@
"clean": "rm -rf build/ vendor/",
"test": [
"vendor/bin/phpspec run",
"vendor/bin/phpunit -v",
"vendor/bin/phpunit",
"vendor/bin/phpbench run",
"vendor/bin/psalm",
"vendor/bin/phpcs"
Expand Down
2 changes: 1 addition & 1 deletion src/Calculator/BcMathCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,6 @@ public static function mod(string $amount, string $divisor): string
throw InvalidArgumentException::moduloByZero();
}

return bcmod($amount, $divisor) ?? '0';
return bcmod($amount, $divisor);
}
}
3 changes: 0 additions & 3 deletions src/Currencies/CryptoCurrencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,12 @@ private function getCurrencies(): array
* symbol: non-empty-string,
* minorUnit: positive-int|0
* }>
*
* @psalm-suppress MoreSpecificReturnType do not specify all keys and values
*/
private function loadCurrencies(): array
{
$file = __DIR__ . '/../../resources/binance.php';

if (is_file($file)) {
/** @psalm-suppress LessSpecificReturnStatement */
return require $file;
}

Expand Down
3 changes: 0 additions & 3 deletions src/Currencies/ISOCurrencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,12 @@ private function getCurrencies(): array
* minorUnit: positive-int|0,
* numericCode: positive-int
* }>
*
* @psalm-suppress MoreSpecificReturnType do not specify all keys and values
*/
private function loadCurrencies(): array
{
$file = __DIR__ . '/../../resources/currency.php';

if (is_file($file)) {
/** @psalm-suppress LessSpecificReturnStatement */
return require $file;
}

Expand Down
42 changes: 37 additions & 5 deletions src/PHPUnit/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
use Money\Formatter\IntlMoneyFormatter;
use Money\Money;
use NumberFormatter;
use ReflectionMethod;
use SebastianBergmann\Comparator\ComparisonFailure;

use function assert;
use function method_exists;

/**
* The comparator is for comparing Money objects in PHPUnit tests.
Expand All @@ -27,11 +29,25 @@
*/
final class Comparator extends \SebastianBergmann\Comparator\Comparator
{
private bool $isComparatorVersion5;

private IntlMoneyFormatter $formatter;

public function __construct()
{
parent::__construct();
// PHPUnit 10 + sebastian/comparator:5 remove the parent class
// constructor. Call conditionally if detected to keep working on
// previous versions.
if (method_exists(parent::class, '__construct')) {
parent::__construct();
}

// Similarly, comparator:5 changed the constructor signature of
// ComparisonFailure. This needs to be detected so the correct version
// can be used depending on installed tools.
$cfConstructor = new ReflectionMethod(ComparisonFailure::class, '__construct');
$parameterCount = $cfConstructor->getNumberOfParameters();
$this->isComparatorVersion5 = $parameterCount === 5;

$currencies = new AggregateCurrencies([
new ISOCurrencies(),
Expand All @@ -43,15 +59,21 @@ public function __construct()
}

/** {@inheritDoc} */
public function accepts($expected, $actual)
public function accepts(mixed $expected, mixed $actual): bool
{
return $expected instanceof Money && $actual instanceof Money;
}

/** {@inheritDoc} */
/**
* {@inheritDoc}
*
* @param float $delta
* @param bool $canonicalize
* @param bool $ignoreCase
*/
public function assertEquals(
$expected,
$actual,
mixed $expected,
mixed $actual,
$delta = 0.0,
$canonicalize = false,
$ignoreCase = false
Expand All @@ -60,6 +82,16 @@ public function assertEquals(
assert($actual instanceof Money);

if (! $expected->equals($actual)) {
// Handle signature change in different versions; see notes in
// constructor.
if ($this->isComparatorVersion5) {
throw new ComparisonFailure($expected, $actual, $this->formatter->format($expected), $this->formatter->format($actual), 'Failed asserting that two Money objects are equal.');
}

/**
* @psalm-suppress TooManyArguments
* @psalm-suppress InvalidArgument
*/
throw new ComparisonFailure($expected, $actual, $this->formatter->format($expected), $this->formatter->format($actual), false, 'Failed asserting that two Money objects are equal.');
}
}
Expand Down