Skip to content

Commit

Permalink
Merge pull request #784 from moneyphp/phpunit_10
Browse files Browse the repository at this point in the history
Phpunit 10
  • Loading branch information
frederikbosch authored Feb 6, 2024
2 parents 4c1a502 + b34c230 commit dfdb760
Show file tree
Hide file tree
Showing 24 changed files with 151 additions and 119 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"php-http/message": "^1.16.0",
"php-http/mock-client": "^1.6.0",
"phpbench/phpbench": "^1.2.5",
"phpunit/phpunit": "^9.5.4",
"phpunit/phpunit": "^10.5.9",
"psalm/plugin-phpunit": "^0.18.4",
"psr/cache": "^1.0.1 || ^2.0 || ^3.0",
"vimeo/psalm": "~5.20.0"
Expand Down Expand Up @@ -88,13 +88,13 @@
],
"clean": "rm -rf build/ vendor/",
"test": [
"vendor/bin/phpunit -v",
"vendor/bin/phpbench run",
"vendor/bin/phpunit",
"vendor/bin/psalm",
"vendor/bin/phpcs"
],
"test-coverage": [
"vendor/bin/phpunit -v --coverage-text --coverage-clover=build/unit_coverage.xml"
"vendor/bin/phpunit --coverage-text --coverage-clover=build/unit_coverage.xml"
],
"update-currencies": [
"cp vendor/moneyphp/iso-currencies/resources/current.php resources/currency.php",
Expand Down
24 changes: 11 additions & 13 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="./vendor/autoload.php"
colors="true"
verbose="true"
columns="max"
beStrictAboutTestsThatDoNotTestAnything="true"
beStrictAboutResourceUsageDuringSmallTests="true"
beStrictAboutChangesToGlobalState="true"
>
<testsuites>
<testsuite name="Money Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>

<coverage>
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
<testsuites>
<testsuite name="Money Test Suite">
<directory>tests/</directory>
</testsuite>
</testsuites>

<source>
<include>
<directory>src</directory>
</include>
</source>
</phpunit>
18 changes: 11 additions & 7 deletions src/PHPUnit/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ final class Comparator extends \SebastianBergmann\Comparator\Comparator

public function __construct()
{
parent::__construct();

$currencies = new AggregateCurrencies([
new ISOCurrencies(),
new BitcoinCurrencies(),
Expand All @@ -43,15 +41,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,7 +64,7 @@ public function assertEquals(
assert($actual instanceof Money);

if (! $expected->equals($actual)) {
throw new ComparisonFailure($expected, $actual, $this->formatter->format($expected), $this->formatter->format($actual), false, 'Failed asserting that two Money objects are equal.');
throw new ComparisonFailure($expected, $actual, $this->formatter->format($expected), $this->formatter->format($actual), 'Failed asserting that two Money objects are equal.');
}
}
}
8 changes: 4 additions & 4 deletions tests/AggregateExamples.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
trait AggregateExamples
{
/** @psalm-return non-empty-list<array{non-empty-list<Money>, Money}> */
public function sumExamples(): array
public static function sumExamples(): array
{
return [
[[Money::EUR(5), Money::EUR(10), Money::EUR(15)], Money::EUR(30)],
Expand All @@ -19,7 +19,7 @@ public function sumExamples(): array
}

/** @psalm-return non-empty-list<array{non-empty-list<Money>, Money}> */
public function minExamples(): array
public static function minExamples(): array
{
return [
[[Money::EUR(5), Money::EUR(10), Money::EUR(15)], Money::EUR(5)],
Expand All @@ -29,7 +29,7 @@ public function minExamples(): array
}

/** @psalm-return non-empty-list<array{non-empty-list<Money>, Money}> */
public function maxExamples(): array
public static function maxExamples(): array
{
return [
[[Money::EUR(5), Money::EUR(10), Money::EUR(15)], Money::EUR(15)],
Expand All @@ -39,7 +39,7 @@ public function maxExamples(): array
}

/** @psalm-return non-empty-list<array{non-empty-list<Money>, Money}> */
public function avgExamples(): array
public static function avgExamples(): array
{
return [
[[Money::EUR(5), Money::EUR(10), Money::EUR(15)], Money::EUR(10)],
Expand Down
2 changes: 1 addition & 1 deletion tests/Calculator/BcMathCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public function itRefusesToModuloByZeroWhenDivisorIsTooSmallToCompare(): void
/**
* @psalm-return array<int,array<int|numeric-string>>
*/
public function compareLessExamples(): array
public static function compareLessExamples(): array
{
return array_merge(
parent::compareLessExamples(),
Expand Down
64 changes: 52 additions & 12 deletions tests/Calculator/CalculatorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
use Money\Exception\InvalidArgumentException;
use Money\Money;
use PHPUnit\Framework\TestCase;
use Tests\Money\Locale;
use Tests\Money\RoundExamples;

use function preg_replace;
use function rtrim;
use function substr;

use const LC_ALL;

abstract class CalculatorTestCase extends TestCase
{
use RoundExamples;
use Locale;

/**
* @return Calculator
Expand All @@ -35,6 +39,10 @@ abstract protected function getCalculator(): string;
public function itAddsTwoValues(int $value1, int $value2, string $expected): void
{
self::assertEqualNumber($expected, $this->getCalculator()::add((string) $value1, (string) $value2));

self::runLocaleAware(LC_ALL, 'ru_RU.UTF-8', function () use ($value1, $value2, $expected): void {
self::assertEqualNumber($expected, $this->getCalculator()::add((string) $value1, (string) $value2));
});
}

/**
Expand All @@ -48,6 +56,10 @@ public function itAddsTwoValues(int $value1, int $value2, string $expected): voi
public function itSubtractsAValueFromAnother(int $value1, int $value2, string $expected): void
{
self::assertEqualNumber($expected, $this->getCalculator()::subtract((string) $value1, (string) $value2));

self::runLocaleAware(LC_ALL, 'ru_RU.UTF-8', function () use ($value1, $value2, $expected): void {
self::assertEqualNumber($expected, $this->getCalculator()::subtract((string) $value1, (string) $value2));
});
}

/**
Expand All @@ -61,6 +73,10 @@ public function itSubtractsAValueFromAnother(int $value1, int $value2, string $e
public function itMultipliesAValueByAnother(int|string $value1, float $value2, string $expected): void
{
self::assertEqualNumber($expected, $this->getCalculator()::multiply((string) $value1, (string) $value2));

self::runLocaleAware(LC_ALL, 'ru_RU.UTF-8', function () use ($value1, $value2, $expected): void {
self::assertEqualNumber($expected, $this->getCalculator()::multiply((string) $value1, (string) $value2));
});
}

/**
Expand Down Expand Up @@ -96,6 +112,10 @@ public function itDividesAValueByAnother(int|string $value1, int|float $value2,
public function itDividesAValueByAnotherExact(int $value1, int|float $value2, string $expected): void
{
self::assertEqualNumber($expected, $this->getCalculator()::divide((string) $value1, (string) $value2));

self::runLocaleAware(LC_ALL, 'ru_RU.UTF-8', function () use ($value1, $value2, $expected): void {
self::assertEqualNumber($expected, $this->getCalculator()::divide((string) $value1, (string) $value2));
});
}

/**
Expand All @@ -108,6 +128,10 @@ public function itDividesAValueByAnotherExact(int $value1, int|float $value2, st
public function itCeilsAValue(float $value, string $expected): void
{
self::assertEquals($expected, $this->getCalculator()::ceil((string) $value));

self::runLocaleAware(LC_ALL, 'ru_RU.UTF-8', function () use ($value, $expected): void {
self::assertEqualNumber($expected, $this->getCalculator()::ceil((string) $value));
});
}

/**
Expand All @@ -120,6 +144,10 @@ public function itCeilsAValue(float $value, string $expected): void
public function itFloorsAValue(float $value, string $expected): void
{
self::assertEquals($expected, $this->getCalculator()::floor((string) $value));

self::runLocaleAware(LC_ALL, 'ru_RU.UTF-8', function () use ($value, $expected): void {
self::assertEqualNumber($expected, $this->getCalculator()::floor((string) $value));
});
}

/**
Expand All @@ -132,6 +160,10 @@ public function itFloorsAValue(float $value, string $expected): void
public function itCalculatesTheAbsoluteValue(int $value, string $expected): void
{
self::assertEquals($expected, $this->getCalculator()::absolute((string) $value));

self::runLocaleAware(LC_ALL, 'ru_RU.UTF-8', function () use ($value, $expected): void {
self::assertEqualNumber($expected, $this->getCalculator()::absolute((string) $value));
});
}

/**
Expand Down Expand Up @@ -159,6 +191,10 @@ public function itSharesAValue(int $value, int $ratio, int $total, string $expec
public function itRoundsAValue(int|string $value, int $mode, string $expected): void
{
self::assertEquals($expected, $this->getCalculator()::round((string) $value, $mode));

self::runLocaleAware(LC_ALL, 'ru_RU.UTF-8', function () use ($value, $mode, $expected): void {
self::assertEqualNumber($expected, $this->getCalculator()::round((string) $value, $mode));
});
}

/**
Expand Down Expand Up @@ -201,6 +237,10 @@ public function itComparesValues(int|string $left, int|string $right): void
public function itCalculatesTheModulusOfAValue(int $left, int $right, string $expected): void
{
self::assertEquals($expected, $this->getCalculator()::mod((string) $left, (string) $right));

self::runLocaleAware(LC_ALL, 'ru_RU.UTF-8', function () use ($left, $right, $expected): void {
self::assertEqualNumber($expected, $this->getCalculator()::mod((string) $left, (string) $right));
});
}

/** @test */
Expand Down Expand Up @@ -250,7 +290,7 @@ public function itRefusesToModuloByNegativeZero(): void
* numeric-string
* }>
*/
public function additionExamples(): array
public static function additionExamples(): array
{
return [
[1, 1, '2'],
Expand All @@ -265,7 +305,7 @@ public function additionExamples(): array
* numeric-string
* }>
*/
public function subtractionExamples(): array
public static function subtractionExamples(): array
{
return [
[1, 1, '0'],
Expand All @@ -280,7 +320,7 @@ public function subtractionExamples(): array
* numeric-string
* }>
*/
public function multiplicationExamples(): array
public static function multiplicationExamples(): array
{
return [
[1, 1.5, '1.5'],
Expand All @@ -306,7 +346,7 @@ public function multiplicationExamples(): array
* numeric-string
* }>
*/
public function divisionExamples(): array
public static function divisionExamples(): array
{
return [
[6, 3, '2'],
Expand All @@ -333,7 +373,7 @@ public function divisionExamples(): array
* numeric-string
* }>
*/
public function divisionExactExamples(): array
public static function divisionExactExamples(): array
{
return [
[6, 3, '2'],
Expand All @@ -352,7 +392,7 @@ public function divisionExactExamples(): array
* numeric-string
* }>
*/
public function ceilExamples(): array
public static function ceilExamples(): array
{
return [
[1.2, '2'],
Expand All @@ -367,7 +407,7 @@ public function ceilExamples(): array
* numeric-string
* }>
*/
public function floorExamples(): array
public static function floorExamples(): array
{
return [
[2.7, '2'],
Expand All @@ -382,7 +422,7 @@ public function floorExamples(): array
* numeric-string
* }>
*/
public function absoluteExamples(): array
public static function absoluteExamples(): array
{
return [
[2, '2'],
Expand All @@ -398,7 +438,7 @@ public function absoluteExamples(): array
* numeric-string
* }>
*/
public function shareExamples(): array
public static function shareExamples(): array
{
return [
[10, 2, 4, '5'],
Expand All @@ -408,7 +448,7 @@ public function shareExamples(): array
/**
* @psalm-return array<int,array<int|numeric-string>>
*/
public function compareLessExamples(): array
public static function compareLessExamples(): array
{
return [
[0, 1],
Expand All @@ -427,7 +467,7 @@ public function compareLessExamples(): array
* int|numeric-string
* }>
*/
public function compareEqualExamples(): array
public static function compareEqualExamples(): array
{
return [
[1, 1],
Expand All @@ -443,7 +483,7 @@ public function compareEqualExamples(): array
* numeric-string
* }>
*/
public function modExamples(): array
public static function modExamples(): array
{
return [
[11, 5, '1'],
Expand Down
2 changes: 1 addition & 1 deletion tests/Calculator/GmpCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function it_divides_bug538(): void
/**
* @psalm-return array<int,array<int|numeric-string>>
*/
public function compareLessExamples(): array
public static function compareLessExamples(): array
{
return array_merge(
parent::compareLessExamples(),
Expand Down
Loading

0 comments on commit dfdb760

Please sign in to comment.