From 4c67318244cbcde4eb330b5474a0a347ca886029 Mon Sep 17 00:00:00 2001 From: Eric Stern Date: Thu, 30 Mar 2023 11:27:09 -0700 Subject: [PATCH 1/4] chore: upgrade PHPUnit 9 => 10 changes for sebastian/comparator --- composer.json | 6 ++--- phpunit.xml.dist | 24 +++++++++---------- src/PHPUnit/Comparator.php | 18 ++++++++------ tests/AggregateExamples.php | 8 +++---- tests/Calculator/BcMathCalculatorTest.php | 2 +- tests/Calculator/CalculatorTestCase.php | 24 +++++++++---------- tests/Calculator/GmpCalculatorTest.php | 2 +- tests/ConverterTest.php | 2 +- tests/Currencies/CryptoCurrenciesTest.php | 2 +- tests/Currencies/CurrencyListTest.php | 2 +- tests/Currencies/ISOCurrenciesTest.php | 2 +- tests/Exchange/SwapExchangeTest.php | 2 +- tests/Formatter/BitcoinMoneyFormatterTest.php | 2 +- tests/MoneyFactoryTest.php | 2 +- tests/MoneyTest.php | 18 +++++++------- tests/NumberTest.php | 8 +++---- tests/Parser/BitcoinMoneyParserTest.php | 2 +- tests/Parser/DecimalMoneyParserTest.php | 2 +- .../Parser/IntlLocalizedDecimalParserTest.php | 2 +- tests/Parser/IntlMoneyParserTest.php | 2 +- tests/RoundExamples.php | 2 +- 21 files changed, 68 insertions(+), 66 deletions(-) diff --git a/composer.json b/composer.json index 5588b085..2d2679be 100644 --- a/composer.json +++ b/composer.json @@ -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" @@ -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", diff --git a/phpunit.xml.dist b/phpunit.xml.dist index b4d5ed76..9f3d4218 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -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" > - - - tests/ - - - - - src - - + + + tests/ + + + + + + src + + diff --git a/src/PHPUnit/Comparator.php b/src/PHPUnit/Comparator.php index 28a5d4ea..8f211478 100644 --- a/src/PHPUnit/Comparator.php +++ b/src/PHPUnit/Comparator.php @@ -31,8 +31,6 @@ final class Comparator extends \SebastianBergmann\Comparator\Comparator public function __construct() { - parent::__construct(); - $currencies = new AggregateCurrencies([ new ISOCurrencies(), new BitcoinCurrencies(), @@ -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 @@ -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.'); } } } diff --git a/tests/AggregateExamples.php b/tests/AggregateExamples.php index 59befdfd..d6cc67f6 100644 --- a/tests/AggregateExamples.php +++ b/tests/AggregateExamples.php @@ -9,7 +9,7 @@ trait AggregateExamples { /** @psalm-return non-empty-list, Money}> */ - public function sumExamples(): array + public static function sumExamples(): array { return [ [[Money::EUR(5), Money::EUR(10), Money::EUR(15)], Money::EUR(30)], @@ -19,7 +19,7 @@ public function sumExamples(): array } /** @psalm-return non-empty-list, Money}> */ - public function minExamples(): array + public static function minExamples(): array { return [ [[Money::EUR(5), Money::EUR(10), Money::EUR(15)], Money::EUR(5)], @@ -29,7 +29,7 @@ public function minExamples(): array } /** @psalm-return non-empty-list, Money}> */ - public function maxExamples(): array + public static function maxExamples(): array { return [ [[Money::EUR(5), Money::EUR(10), Money::EUR(15)], Money::EUR(15)], @@ -39,7 +39,7 @@ public function maxExamples(): array } /** @psalm-return non-empty-list, Money}> */ - public function avgExamples(): array + public static function avgExamples(): array { return [ [[Money::EUR(5), Money::EUR(10), Money::EUR(15)], Money::EUR(10)], diff --git a/tests/Calculator/BcMathCalculatorTest.php b/tests/Calculator/BcMathCalculatorTest.php index 9d4d0391..f219d483 100644 --- a/tests/Calculator/BcMathCalculatorTest.php +++ b/tests/Calculator/BcMathCalculatorTest.php @@ -116,7 +116,7 @@ public function itRefusesToModuloByZeroWhenDivisorIsTooSmallToCompare(): void /** * @psalm-return array> */ - public function compareLessExamples(): array + public static function compareLessExamples(): array { return array_merge( parent::compareLessExamples(), diff --git a/tests/Calculator/CalculatorTestCase.php b/tests/Calculator/CalculatorTestCase.php index ac80e61d..80ca1c54 100644 --- a/tests/Calculator/CalculatorTestCase.php +++ b/tests/Calculator/CalculatorTestCase.php @@ -250,7 +250,7 @@ public function itRefusesToModuloByNegativeZero(): void * numeric-string * }> */ - public function additionExamples(): array + public static function additionExamples(): array { return [ [1, 1, '2'], @@ -265,7 +265,7 @@ public function additionExamples(): array * numeric-string * }> */ - public function subtractionExamples(): array + public static function subtractionExamples(): array { return [ [1, 1, '0'], @@ -280,7 +280,7 @@ public function subtractionExamples(): array * numeric-string * }> */ - public function multiplicationExamples(): array + public static function multiplicationExamples(): array { return [ [1, 1.5, '1.5'], @@ -306,7 +306,7 @@ public function multiplicationExamples(): array * numeric-string * }> */ - public function divisionExamples(): array + public static function divisionExamples(): array { return [ [6, 3, '2'], @@ -333,7 +333,7 @@ public function divisionExamples(): array * numeric-string * }> */ - public function divisionExactExamples(): array + public static function divisionExactExamples(): array { return [ [6, 3, '2'], @@ -352,7 +352,7 @@ public function divisionExactExamples(): array * numeric-string * }> */ - public function ceilExamples(): array + public static function ceilExamples(): array { return [ [1.2, '2'], @@ -367,7 +367,7 @@ public function ceilExamples(): array * numeric-string * }> */ - public function floorExamples(): array + public static function floorExamples(): array { return [ [2.7, '2'], @@ -382,7 +382,7 @@ public function floorExamples(): array * numeric-string * }> */ - public function absoluteExamples(): array + public static function absoluteExamples(): array { return [ [2, '2'], @@ -398,7 +398,7 @@ public function absoluteExamples(): array * numeric-string * }> */ - public function shareExamples(): array + public static function shareExamples(): array { return [ [10, 2, 4, '5'], @@ -408,7 +408,7 @@ public function shareExamples(): array /** * @psalm-return array> */ - public function compareLessExamples(): array + public static function compareLessExamples(): array { return [ [0, 1], @@ -427,7 +427,7 @@ public function compareLessExamples(): array * int|numeric-string * }> */ - public function compareEqualExamples(): array + public static function compareEqualExamples(): array { return [ [1, 1], @@ -443,7 +443,7 @@ public function compareEqualExamples(): array * numeric-string * }> */ - public function modExamples(): array + public static function modExamples(): array { return [ [11, 5, '1'], diff --git a/tests/Calculator/GmpCalculatorTest.php b/tests/Calculator/GmpCalculatorTest.php index a2388d76..60e2d8c8 100644 --- a/tests/Calculator/GmpCalculatorTest.php +++ b/tests/Calculator/GmpCalculatorTest.php @@ -58,7 +58,7 @@ public function it_divides_bug538(): void /** * @psalm-return array> */ - public function compareLessExamples(): array + public static function compareLessExamples(): array { return array_merge( parent::compareLessExamples(), diff --git a/tests/ConverterTest.php b/tests/ConverterTest.php index c853d0e7..6dad6ca5 100644 --- a/tests/ConverterTest.php +++ b/tests/ConverterTest.php @@ -231,7 +231,7 @@ public function itThrowsWhenConvertingAgainstTheWrongBaseCurrency(): void * positive-int|0 * }> */ - public function convertExamples(): array + public static function convertExamples(): array { return [ ['USD', 'JPY', 2, 0, 101, 100, 101], diff --git a/tests/Currencies/CryptoCurrenciesTest.php b/tests/Currencies/CryptoCurrenciesTest.php index dabf8e7e..4843d017 100644 --- a/tests/Currencies/CryptoCurrenciesTest.php +++ b/tests/Currencies/CryptoCurrenciesTest.php @@ -68,7 +68,7 @@ public function itIsIterable(): void /** * @psalm-return non-empty-list */ - public function currencyCodeExamples(): array + public static function currencyCodeExamples(): array { /** @psalm-var non-empty-array $currencies */ $currencies = require __DIR__ . '/../../resources/binance.php'; diff --git a/tests/Currencies/CurrencyListTest.php b/tests/Currencies/CurrencyListTest.php index 1d6e073a..911c77c9 100644 --- a/tests/Currencies/CurrencyListTest.php +++ b/tests/Currencies/CurrencyListTest.php @@ -72,7 +72,7 @@ public function itIsIterable(): void } /** @psalm-return non-empty-list */ - public function currencyCodeExamples(): array + public static function currencyCodeExamples(): array { $currencies = array_keys(self::CORRECT_CURRENCIES); diff --git a/tests/Currencies/ISOCurrenciesTest.php b/tests/Currencies/ISOCurrenciesTest.php index 856862b2..726e58f3 100644 --- a/tests/Currencies/ISOCurrenciesTest.php +++ b/tests/Currencies/ISOCurrenciesTest.php @@ -93,7 +93,7 @@ public function itIsIterable(): void /** * @psalm-return non-empty-list */ - public function currencyCodeExamples(): array + public static function currencyCodeExamples(): array { /** @psalm-var non-empty-array $currencies */ $currencies = require __DIR__ . '/../../resources/currency.php'; diff --git a/tests/Exchange/SwapExchangeTest.php b/tests/Exchange/SwapExchangeTest.php index a9978fab..bc31e694 100644 --- a/tests/Exchange/SwapExchangeTest.php +++ b/tests/Exchange/SwapExchangeTest.php @@ -22,7 +22,7 @@ final class SwapExchangeTest extends TestCase * numeric-string * }> */ - public function exchangeRateExamples(): array + public static function exchangeRateExamples(): array { return [ [1.25, '1.25000000000000'], diff --git a/tests/Formatter/BitcoinMoneyFormatterTest.php b/tests/Formatter/BitcoinMoneyFormatterTest.php index 85be82c0..eda46162 100644 --- a/tests/Formatter/BitcoinMoneyFormatterTest.php +++ b/tests/Formatter/BitcoinMoneyFormatterTest.php @@ -44,7 +44,7 @@ public function itFormatsMoney(int $value, string $formatted, int $fractionDigit * positive-int|0 * }> */ - public function bitcoinExamples(): array + public static function bitcoinExamples(): array { return [ [100000000000, "\xC9\x831000.00", 2], diff --git a/tests/MoneyFactoryTest.php b/tests/MoneyFactoryTest.php index 0d952435..80c5e6e6 100644 --- a/tests/MoneyFactoryTest.php +++ b/tests/MoneyFactoryTest.php @@ -28,7 +28,7 @@ public function itCreatesMoneyUsingFactories(Currency $currency): void } /** @psalm-return list */ - public function currencyExamples(): array + public static function currencyExamples(): array { $currencies = new AggregateCurrencies([ new ISOCurrencies(), diff --git a/tests/MoneyTest.php b/tests/MoneyTest.php index 1827ba8d..a6aeff39 100644 --- a/tests/MoneyTest.php +++ b/tests/MoneyTest.php @@ -458,7 +458,7 @@ public function itThrowsWhenComparingDifferentCurrencies(): void * bool * }> */ - public function equalityExamples(): array + public static function equalityExamples(): array { return [ [10, new Currency(self::CURRENCY), true], @@ -475,7 +475,7 @@ public function equalityExamples(): array * int * }> */ - public function comparisonExamples(): array + public static function comparisonExamples(): array { return [ [self::AMOUNT, 0], @@ -491,7 +491,7 @@ public function comparisonExamples(): array * non-empty-array * }> */ - public function allocationExamples(): array + public static function allocationExamples(): array { return [ [100, [1, 1, 1], [34, 33, 33]], @@ -521,7 +521,7 @@ public function allocationExamples(): array * non-empty-list * }> */ - public function allocationTargetExamples(): array + public static function allocationTargetExamples(): array { return [ [15, 2, [8, 7]], @@ -539,7 +539,7 @@ public function allocationTargetExamples(): array * bool * }> */ - public function comparatorExamples(): array + public static function comparatorExamples(): array { return [ [1, false, true, false], @@ -557,7 +557,7 @@ public function comparatorExamples(): array * positive-int|0 * }> */ - public function absoluteExamples(): array + public static function absoluteExamples(): array { return [ [1, 1], @@ -575,7 +575,7 @@ public function absoluteExamples(): array * int * }> */ - public function negativeExamples(): array + public static function negativeExamples(): array { return [ [1, -1], @@ -594,7 +594,7 @@ public function negativeExamples(): array * numeric-string * }> */ - public function modExamples(): array + public static function modExamples(): array { return [ [11, 5, '1'], @@ -612,7 +612,7 @@ public function modExamples(): array * int * }> */ - public function roundToUnitExamples(): array + public static function roundToUnitExamples(): array { return [ [510, 2, 500, Money::ROUND_HALF_UP], diff --git a/tests/NumberTest.php b/tests/NumberTest.php index 08e5aa47..fe5e797e 100644 --- a/tests/NumberTest.php +++ b/tests/NumberTest.php @@ -104,7 +104,7 @@ public function itCreatesANumberFromAFloatingPointValue(): void * the {@see PHP_INT_MAX} operations below cannot be inferred to numeric-string * concatenation of {@see PHP_INT_MAX} is disallowed by type checker, but valid in this scenario */ - public function numberExamples(): array + public static function numberExamples(): array { return [ ['0', false, false, true, false, '0', ''], @@ -162,7 +162,7 @@ public function numberExamples(): array } /** @psalm-return non-empty-list */ - public function invalidNumberExamples() + public static function invalidNumberExamples(): array { return [ [''], @@ -187,7 +187,7 @@ public function invalidNumberExamples() * numeric-string * }> */ - public function base10Examples() + public static function base10Examples(): array { return [ ['0', 10, '0'], @@ -208,7 +208,7 @@ public function base10Examples() } /** @psalm-return non-empty-list */ - public function numericExamples(): array + public static function numericExamples(): array { return [ [1], diff --git a/tests/Parser/BitcoinMoneyParserTest.php b/tests/Parser/BitcoinMoneyParserTest.php index 4a7eb1f0..47954852 100644 --- a/tests/Parser/BitcoinMoneyParserTest.php +++ b/tests/Parser/BitcoinMoneyParserTest.php @@ -51,7 +51,7 @@ public function forceCurrencyWorks(): void * int|numeric-string * }> */ - public function bitcoinExamples(): array + public static function bitcoinExamples(): array { return [ ["\xC9\x831000.00", 100000], diff --git a/tests/Parser/DecimalMoneyParserTest.php b/tests/Parser/DecimalMoneyParserTest.php index 28fdd9f0..184146f2 100644 --- a/tests/Parser/DecimalMoneyParserTest.php +++ b/tests/Parser/DecimalMoneyParserTest.php @@ -62,7 +62,7 @@ public function itThrowsAnExceptionUponInvalidInputs($input): void * int * }> */ - public function formattedMoneyExamples(): array + public static function formattedMoneyExamples(): array { return [ ['1000.50', 'USD', 2, 100050], diff --git a/tests/Parser/IntlLocalizedDecimalParserTest.php b/tests/Parser/IntlLocalizedDecimalParserTest.php index 7bf4d9c0..168ffd32 100644 --- a/tests/Parser/IntlLocalizedDecimalParserTest.php +++ b/tests/Parser/IntlLocalizedDecimalParserTest.php @@ -118,7 +118,7 @@ public function it_does_not_support_invalid_decimal(): void * non-empty-string * }> */ - public function formattedMoneyExamples() + public static function formattedMoneyExamples(): array { return [ ['1000.50', 100050, 'en_US'], diff --git a/tests/Parser/IntlMoneyParserTest.php b/tests/Parser/IntlMoneyParserTest.php index a1b726f0..1f68901d 100644 --- a/tests/Parser/IntlMoneyParserTest.php +++ b/tests/Parser/IntlMoneyParserTest.php @@ -126,7 +126,7 @@ public function itSupportsFractionDigitsWithDifferentStyleAndPattern(): void * int * }> */ - public function formattedMoneyExamples(): array + public static function formattedMoneyExamples(): array { return [ ['$1000.50', 100050], diff --git a/tests/RoundExamples.php b/tests/RoundExamples.php index 46bebd2d..3d1adfda 100644 --- a/tests/RoundExamples.php +++ b/tests/RoundExamples.php @@ -20,7 +20,7 @@ trait RoundExamples * 2: numeric-string * }> */ - public function roundingExamples(): array + public static function roundingExamples(): array { return [ ['2.6', Money::ROUND_HALF_EVEN, '3'], From b19212c45615aa9a1cc9cf98d6e766a5211aa52e Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Sat, 27 Jan 2024 19:41:28 +0100 Subject: [PATCH 2/4] chore: upgrade PHPUnit 9 => 10 replace setLocale() calls with setlocale() --- tests/Calculator/LocaleAwareBcMathCalculatorTest.php | 4 +++- tests/Calculator/LocaleAwareGmpCalculatorTest.php | 4 +++- tests/ConverterTest.php | 3 ++- tests/MoneyTest.php | 3 ++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/tests/Calculator/LocaleAwareBcMathCalculatorTest.php b/tests/Calculator/LocaleAwareBcMathCalculatorTest.php index a31d4641..d0a57090 100644 --- a/tests/Calculator/LocaleAwareBcMathCalculatorTest.php +++ b/tests/Calculator/LocaleAwareBcMathCalculatorTest.php @@ -4,6 +4,8 @@ namespace Tests\Money\Calculator; +use function setlocale; + use const LC_ALL; /** @covers \Money\Calculator\BcMathCalculator */ @@ -13,6 +15,6 @@ public function setUp(): void { parent::setUp(); - $this->setLocale(LC_ALL, 'ru_RU.UTF-8'); + setlocale(LC_ALL, 'ru_RU.UTF-8'); } } diff --git a/tests/Calculator/LocaleAwareGmpCalculatorTest.php b/tests/Calculator/LocaleAwareGmpCalculatorTest.php index 19fff94d..aca32ab9 100644 --- a/tests/Calculator/LocaleAwareGmpCalculatorTest.php +++ b/tests/Calculator/LocaleAwareGmpCalculatorTest.php @@ -4,6 +4,8 @@ namespace Tests\Money\Calculator; +use function setlocale; + use const LC_ALL; /** @covers \Money\Calculator\GmpCalculator */ @@ -13,6 +15,6 @@ public function setUp(): void { parent::setUp(); - $this->setLocale(LC_ALL, 'ru_RU.UTF-8'); + setlocale(LC_ALL, 'ru_RU.UTF-8'); } } diff --git a/tests/ConverterTest.php b/tests/ConverterTest.php index 6dad6ca5..cab4134d 100644 --- a/tests/ConverterTest.php +++ b/tests/ConverterTest.php @@ -13,6 +13,7 @@ use Money\Money; use PHPUnit\Framework\TestCase; +use function setlocale; use function sprintf; use const LC_ALL; @@ -192,7 +193,7 @@ public function itConvertsToADifferentCurrencyWhenDecimalSeparatorIsComma( $amount, $expectedAmount ): void { - $this->setLocale(LC_ALL, 'ru_RU.UTF-8'); + setlocale(LC_ALL, 'ru_RU.UTF-8'); $this->itConvertsToADifferentCurrency( $baseCurrencyCode, diff --git a/tests/MoneyTest.php b/tests/MoneyTest.php index a6aeff39..a80ad7d8 100644 --- a/tests/MoneyTest.php +++ b/tests/MoneyTest.php @@ -10,6 +10,7 @@ use PHPUnit\Framework\TestCase; use function json_encode; +use function setlocale; use const LC_ALL; use const PHP_INT_MAX; @@ -99,7 +100,7 @@ public function itMultipliesTheAmount(int|string $multiplier, int $roundingMode, */ public function itMultipliesTheAmountWithLocaleThatUsesCommaSeparator(): void { - $this->setLocale(LC_ALL, 'es_ES.utf8'); + setlocale(LC_ALL, 'es_ES.utf8'); $money = new Money(100, new Currency(self::CURRENCY)); $money = $money->multiply('0.1'); From e58c49bdf5ab9b362c2a57cd747d24fedb99efb7 Mon Sep 17 00:00:00 2001 From: Christopher Georg Date: Fri, 2 Feb 2024 14:46:08 +0100 Subject: [PATCH 3/4] chore: upgrade PHPUnit 9 => 10 --- tests/ConverterTest.php | 25 +++++++++++++------------ tests/Locale.php | 23 +++++++++++++++++++++++ tests/MoneyTest.php | 14 +++++++------- 3 files changed, 43 insertions(+), 19 deletions(-) create mode 100644 tests/Locale.php diff --git a/tests/ConverterTest.php b/tests/ConverterTest.php index cab4134d..6fdcc8a0 100644 --- a/tests/ConverterTest.php +++ b/tests/ConverterTest.php @@ -13,7 +13,6 @@ use Money\Money; use PHPUnit\Framework\TestCase; -use function setlocale; use function sprintf; use const LC_ALL; @@ -21,6 +20,8 @@ /** @covers \Money\Converter */ final class ConverterTest extends TestCase { + use Locale; + /** * @psalm-param non-empty-string $baseCurrencyCode * @psalm-param non-empty-string $counterCurrencyCode @@ -193,17 +194,17 @@ public function itConvertsToADifferentCurrencyWhenDecimalSeparatorIsComma( $amount, $expectedAmount ): void { - setlocale(LC_ALL, 'ru_RU.UTF-8'); - - $this->itConvertsToADifferentCurrency( - $baseCurrencyCode, - $counterCurrencyCode, - $subunitBase, - $subunitCounter, - $ratio, - $amount, - $expectedAmount - ); + self::runLocaleAware(LC_ALL, 'ru_RU.UTF-8', function () use ($expectedAmount, $amount, $ratio, $subunitCounter, $subunitBase, $counterCurrencyCode, $baseCurrencyCode): void { + $this->itConvertsToADifferentCurrency( + $baseCurrencyCode, + $counterCurrencyCode, + $subunitBase, + $subunitCounter, + $ratio, + $amount, + $expectedAmount + ); + }); } /** diff --git a/tests/Locale.php b/tests/Locale.php new file mode 100644 index 00000000..22c223d6 --- /dev/null +++ b/tests/Locale.php @@ -0,0 +1,23 @@ +multiply('0.1'); - $money = new Money(100, new Currency(self::CURRENCY)); - $money = $money->multiply('0.1'); - - self::assertInstanceOf(Money::class, $money); - self::assertEquals('10', $money->getAmount()); + self::assertInstanceOf(Money::class, $money); + self::assertEquals('10', $money->getAmount()); + }); } /** From b34c2301f42c4896b3b08dbd24f0f040149d0915 Mon Sep 17 00:00:00 2001 From: Frederik Bosch Date: Tue, 6 Feb 2024 10:07:27 +0100 Subject: [PATCH 4/4] move locale aware tests abstract calculator --- tests/Calculator/CalculatorTestCase.php | 40 +++++++++++++++++++ .../LocaleAwareBcMathCalculatorTest.php | 20 ---------- .../LocaleAwareGmpCalculatorTest.php | 20 ---------- 3 files changed, 40 insertions(+), 40 deletions(-) delete mode 100644 tests/Calculator/LocaleAwareBcMathCalculatorTest.php delete mode 100644 tests/Calculator/LocaleAwareGmpCalculatorTest.php diff --git a/tests/Calculator/CalculatorTestCase.php b/tests/Calculator/CalculatorTestCase.php index 80ca1c54..640cde2a 100644 --- a/tests/Calculator/CalculatorTestCase.php +++ b/tests/Calculator/CalculatorTestCase.php @@ -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 @@ -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)); + }); } /** @@ -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)); + }); } /** @@ -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)); + }); } /** @@ -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)); + }); } /** @@ -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)); + }); } /** @@ -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)); + }); } /** @@ -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)); + }); } /** @@ -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)); + }); } /** @@ -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 */ diff --git a/tests/Calculator/LocaleAwareBcMathCalculatorTest.php b/tests/Calculator/LocaleAwareBcMathCalculatorTest.php deleted file mode 100644 index d0a57090..00000000 --- a/tests/Calculator/LocaleAwareBcMathCalculatorTest.php +++ /dev/null @@ -1,20 +0,0 @@ -