From ce52d056cbcedfff47829ec78a36aad96dd49a13 Mon Sep 17 00:00:00 2001 From: Mponos George Date: Sat, 28 Dec 2019 12:52:26 +0200 Subject: [PATCH 1/5] Improve tests of parsers --- tests/Parser/DecimalMoneyParserTest.php | 36 ++---- .../Parser/IntlLocalizedDecimalParserTest.php | 111 +++++++++++------- tests/Parser/IntlMoneyParserTest.php | 20 ++-- 3 files changed, 90 insertions(+), 77 deletions(-) diff --git a/tests/Parser/DecimalMoneyParserTest.php b/tests/Parser/DecimalMoneyParserTest.php index 364d6ce9..94bc22ba 100644 --- a/tests/Parser/DecimalMoneyParserTest.php +++ b/tests/Parser/DecimalMoneyParserTest.php @@ -17,14 +17,11 @@ final class DecimalMoneyParserTest extends TestCase */ public function it_parses_money($decimal, $currency, $subunit, $result) { - $currencies = $this->prophesize(Currencies::class); + $currencies = new Currencies\CurrencyList([ + $currency => $subunit + ]); - $currencies->subunitFor(Argument::allOf( - Argument::type(Currency::class), - Argument::which('getCode', $currency) - ))->willReturn($subunit); - - $parser = new DecimalMoneyParser($currencies->reveal()); + $parser = new DecimalMoneyParser($currencies); $this->assertEquals($result, $parser->parse($decimal, new Currency($currency))->getAmount()); } @@ -35,17 +32,13 @@ public function it_parses_money($decimal, $currency, $subunit, $result) */ public function it_throws_an_exception_upon_invalid_inputs($input) { - $this->expectException(ParserException::class); - - $currencies = $this->prophesize(Currencies::class); + $currencies = new Currencies\CurrencyList([ + 'USD' => 2 + ]); - $currencies->subunitFor(Argument::allOf( - Argument::type(Currency::class), - Argument::which('getCode', 'USD') - ))->willReturn(2); - - $parser = new DecimalMoneyParser($currencies->reveal()); + $parser = new DecimalMoneyParser($currencies); + $this->expectException(ParserException::class); $parser->parse($input, new Currency('USD'))->getAmount(); } @@ -56,14 +49,11 @@ public function it_throws_an_exception_upon_invalid_inputs($input) */ public function it_accepts_only_a_currency_object() { - $currencies = $this->prophesize(Currencies::class); - - $currencies->subunitFor(Argument::allOf( - Argument::type(Currency::class), - Argument::which('getCode', 'USD') - ))->willReturn(2); + $currencies = new Currencies\CurrencyList([ + 'USD' => 2 + ]); - $parser = new DecimalMoneyParser($currencies->reveal()); + $parser = new DecimalMoneyParser($currencies); $parser->parse('1.0', 'USD')->getAmount(); } diff --git a/tests/Parser/IntlLocalizedDecimalParserTest.php b/tests/Parser/IntlLocalizedDecimalParserTest.php index b9bd204e..1c7167fa 100644 --- a/tests/Parser/IntlLocalizedDecimalParserTest.php +++ b/tests/Parser/IntlLocalizedDecimalParserTest.php @@ -9,7 +9,6 @@ use Money\Money; use Money\Parser\IntlLocalizedDecimalParser; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; final class IntlLocalizedDecimalParserTest extends TestCase { @@ -17,21 +16,17 @@ final class IntlLocalizedDecimalParserTest extends TestCase * @dataProvider formattedMoneyExamples * @test */ - public function it_parses_money($string, $units) + public function it_parses_money($string, $units, $locale) { - $formatter = new \NumberFormatter('en_US', \NumberFormatter::DECIMAL); - - $currencies = $this->prophesize(Currencies::class); + $formatter = new \NumberFormatter($locale, \NumberFormatter::DECIMAL); - $currencies->subunitFor(Argument::allOf( - Argument::type(Currency::class), - Argument::which('getCode', 'USD') - ))->willReturn(2); + $currencies = new Currencies\CurrencyList([ + 'USD' => 2 + ]); - $currencyCode = 'USD'; - $currency = new Currency($currencyCode); + $currency = new Currency('USD'); - $parser = new IntlLocalizedDecimalParser($formatter, $currencies->reveal()); + $parser = new IntlLocalizedDecimalParser($formatter, $currencies); $this->assertEquals($units, $parser->parse($string, $currency)->getAmount()); } @@ -40,13 +35,12 @@ public function it_parses_money($string, $units) */ public function it_cannot_convert_string_to_units() { - $this->expectException(ParserException::class); - $formatter = new \NumberFormatter('en_US', \NumberFormatter::DECIMAL); - $currencyCode = 'USD'; - $currency = new Currency($currencyCode); + $currency = new Currency('USD'); $parser = new IntlLocalizedDecimalParser($formatter, new ISOCurrencies()); + + $this->expectException(ParserException::class); $parser->parse('THIS_IS_NOT_CONVERTABLE_TO_UNIT', $currency); } @@ -60,7 +54,7 @@ public function it_works_with_all_kinds_of_locales() $parser = new IntlLocalizedDecimalParser($formatter, new ISOCurrencies()); $money = $parser->parse('1000.00', new Currency('CAD')); - $this->assertEquals(Money::CAD(100000), $money); + $this->assertTrue(Money::CAD(100000)->equals($money)); } /** @@ -70,13 +64,12 @@ public function it_accepts_a_forced_currency() { $formatter = new \NumberFormatter('en_US', \NumberFormatter::DECIMAL); - $currencyCode = 'CAD'; - $currency = new Currency($currencyCode); + $currency = new Currency('CAD'); $parser = new IntlLocalizedDecimalParser($formatter, new ISOCurrencies()); $money = $parser->parse('1000.00', $currency); - $this->assertEquals('100000', $money->getAmount()); - $this->assertEquals('CAD', $money->getCurrency()->getCode()); + $this->assertSame('100000', $money->getAmount()); + $this->assertSame('CAD', $money->getCurrency()->getCode()); } /** @@ -90,7 +83,18 @@ public function it_supports_fraction_digits() $parser = new IntlLocalizedDecimalParser($formatter, new ISOCurrencies()); $money = $parser->parse('1000.005', new Currency('USD')); - $this->assertEquals('100001', $money->getAmount()); + $this->assertSame('100001', $money->getAmount()); + } + + public function it_does_not_support_invalid_decimal() + { + $formatter = new \NumberFormatter('en_US', \NumberFormatter::DECIMAL); + $formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, 3); + + $parser = new IntlLocalizedDecimalParser($formatter, new ISOCurrencies()); + $money = $parser->parse('1000,005', new Currency('USD')); + + $this->assertSame('100001', $money->getAmount()); } /** @@ -109,26 +113,49 @@ public function it_accepts_only_a_currency_object() public function formattedMoneyExamples() { return [ - ['1000.50', 100050], - ['1000.00', 100000], - ['1000.0', 100000], - ['1000.00', 100000], - ['0.01', 1], - ['0.00', 0], - ['1', 100], - ['-1000', -100000], - ['-1000.0', -100000], - ['-1000.00', -100000], - ['-0.01', -1], - ['-1', -100], - ['1000', 100000], - ['1000.0', 100000], - ['1000.00', 100000], - ['0.01', 1], - ['1', 100], - ['.99', 99], - ['-.99', -99], - ['99.', 9900], + ['1000.50', 100050, 'en_US'], + ['1000.00', 100000, 'en_US'], + ['1000.0', 100000, 'en_US'], + ['1000.00', 100000, 'en_US'], + ['1,000.50', 100050, 'en_US'], + ['1,000.00', 100000, 'en_US'], + ['1,000.0', 100000, 'en_US'], + ['1,000.00', 100000, 'en_US'], + ['0.01', 1, 'en_US'], + ['0.00', 0, 'en_US'], + ['1', 100, 'en_US'], + ['-1000', -100000, 'en_US'], + ['-1000.0', -100000, 'en_US'], + ['-1000.00', -100000, 'en_US'], + ['-1,000', -100000, 'en_US'], + ['-1,000.0', -100000, 'en_US'], + ['-1,000.00', -100000, 'en_US'], + ['-0.01', -1, 'en_US'], + ['-1', -100, 'en_US'], + ['1000', 100000, 'en_US'], + ['1000.0', 100000, 'en_US'], + ['1000.00', 100000, 'en_US'], + ['0.01', 1, 'en_US'], + ['1', 100, 'en_US'], + ['.99', 99, 'en_US'], + ['-.99', -99, 'en_US'], + ['99.', 9900, 'en_US'], + ['1000,50', 100050, 'el_GR'], + ['1000,00', 100000, 'el_GR'], + ['1000,0', 100000, 'el_GR'], + ['1000,00', 100000, 'el_GR'], + ['1.000,50', 100050, 'el_GR'], + ['1.000,00', 100000, 'el_GR'], + ['1.000,0', 100000, 'el_GR'], + ['1.000,00', 100000, 'el_GR'], + ['-1000,50', -100050, 'el_GR'], + ['-1000,00', -100000, 'el_GR'], + ['-1000,0', -100000, 'el_GR'], + ['-1000,00', -100000, 'el_GR'], + ['-1.000,50', -100050, 'el_GR'], + ['-1.000,00', -100000, 'el_GR'], + ['-1.000,0', -100000, 'el_GR'], + ['-1.000,00', -100000, 'el_GR'], ]; } } diff --git a/tests/Parser/IntlMoneyParserTest.php b/tests/Parser/IntlMoneyParserTest.php index 1b23b7bb..ed0bec1e 100644 --- a/tests/Parser/IntlMoneyParserTest.php +++ b/tests/Parser/IntlMoneyParserTest.php @@ -23,17 +23,13 @@ public function it_parses_money($string, $units) $formatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY); $formatter->setPattern('¤#,##0.00;-¤#,##0.00'); - $currencies = $this->prophesize(Currencies::class); + $currencies = new Currencies\CurrencyList([ + 'USD' => 2 + ]); - $currencies->subunitFor(Argument::allOf( - Argument::type(Currency::class), - Argument::which('getCode', 'USD') - ))->willReturn(2); + $currency = new Currency('USD'); - $currencyCode = 'USD'; - $currency = new Currency($currencyCode); - - $parser = new IntlMoneyParser($formatter, $currencies->reveal()); + $parser = new IntlMoneyParser($formatter, $currencies); $this->assertEquals($units, $parser->parse($string, $currency)->getAmount()); } @@ -42,14 +38,14 @@ public function it_parses_money($string, $units) */ public function it_cannot_convert_string_to_units() { - $this->expectException(ParserException::class); - $formatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY); $formatter->setPattern('¤#,##0.00;-¤#,##0.00'); $currencyCode = 'USD'; $currency = new Currency($currencyCode); $parser = new IntlMoneyParser($formatter, new ISOCurrencies()); + + $this->expectException(ParserException::class); $parser->parse('THIS_IS_NOT_CONVERTABLE_TO_UNIT', $currency); } @@ -64,7 +60,7 @@ public function it_works_with_all_kinds_of_locales() $parser = new IntlMoneyParser($formatter, new ISOCurrencies()); $money = $parser->parse('$1000.00'); - $this->assertEquals(Money::CAD(100000), $money); + $this->assertTrue(Money::CAD(100000)->equals($money)); } /** From 4723d4a360899b7df120d37859fa6fb18a2730d3 Mon Sep 17 00:00:00 2001 From: Mponos George Date: Sat, 28 Dec 2019 12:55:47 +0200 Subject: [PATCH 2/5] Fix the codestyle --- tests/Parser/DecimalMoneyParserTest.php | 6 +++--- tests/Parser/IntlLocalizedDecimalParserTest.php | 2 +- tests/Parser/IntlMoneyParserTest.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Parser/DecimalMoneyParserTest.php b/tests/Parser/DecimalMoneyParserTest.php index 94bc22ba..336dab0f 100644 --- a/tests/Parser/DecimalMoneyParserTest.php +++ b/tests/Parser/DecimalMoneyParserTest.php @@ -18,7 +18,7 @@ final class DecimalMoneyParserTest extends TestCase public function it_parses_money($decimal, $currency, $subunit, $result) { $currencies = new Currencies\CurrencyList([ - $currency => $subunit + $currency => $subunit, ]); $parser = new DecimalMoneyParser($currencies); @@ -33,7 +33,7 @@ public function it_parses_money($decimal, $currency, $subunit, $result) public function it_throws_an_exception_upon_invalid_inputs($input) { $currencies = new Currencies\CurrencyList([ - 'USD' => 2 + 'USD' => 2, ]); $parser = new DecimalMoneyParser($currencies); @@ -50,7 +50,7 @@ public function it_throws_an_exception_upon_invalid_inputs($input) public function it_accepts_only_a_currency_object() { $currencies = new Currencies\CurrencyList([ - 'USD' => 2 + 'USD' => 2, ]); $parser = new DecimalMoneyParser($currencies); diff --git a/tests/Parser/IntlLocalizedDecimalParserTest.php b/tests/Parser/IntlLocalizedDecimalParserTest.php index 1c7167fa..a50e3527 100644 --- a/tests/Parser/IntlLocalizedDecimalParserTest.php +++ b/tests/Parser/IntlLocalizedDecimalParserTest.php @@ -21,7 +21,7 @@ public function it_parses_money($string, $units, $locale) $formatter = new \NumberFormatter($locale, \NumberFormatter::DECIMAL); $currencies = new Currencies\CurrencyList([ - 'USD' => 2 + 'USD' => 2, ]); $currency = new Currency('USD'); diff --git a/tests/Parser/IntlMoneyParserTest.php b/tests/Parser/IntlMoneyParserTest.php index ed0bec1e..690bb50f 100644 --- a/tests/Parser/IntlMoneyParserTest.php +++ b/tests/Parser/IntlMoneyParserTest.php @@ -24,7 +24,7 @@ public function it_parses_money($string, $units) $formatter->setPattern('¤#,##0.00;-¤#,##0.00'); $currencies = new Currencies\CurrencyList([ - 'USD' => 2 + 'USD' => 2, ]); $currency = new Currency('USD'); From 15f25af153620d51376021bafc6c9cbde74cbe7c Mon Sep 17 00:00:00 2001 From: Mponos George Date: Sat, 28 Dec 2019 12:57:33 +0200 Subject: [PATCH 3/5] Remove unused uses --- tests/Parser/DecimalMoneyParserTest.php | 1 - tests/Parser/IntlMoneyParserTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/tests/Parser/DecimalMoneyParserTest.php b/tests/Parser/DecimalMoneyParserTest.php index 336dab0f..9c1465e7 100644 --- a/tests/Parser/DecimalMoneyParserTest.php +++ b/tests/Parser/DecimalMoneyParserTest.php @@ -7,7 +7,6 @@ use Money\Exception\ParserException; use Money\Parser\DecimalMoneyParser; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; final class DecimalMoneyParserTest extends TestCase { diff --git a/tests/Parser/IntlMoneyParserTest.php b/tests/Parser/IntlMoneyParserTest.php index 690bb50f..db21016e 100644 --- a/tests/Parser/IntlMoneyParserTest.php +++ b/tests/Parser/IntlMoneyParserTest.php @@ -9,7 +9,6 @@ use Money\Money; use Money\Parser\IntlMoneyParser; use PHPUnit\Framework\TestCase; -use Prophecy\Argument; final class IntlMoneyParserTest extends TestCase { From 1c4527b9375ab8ac1aa4d81f3aab720cbac31832 Mon Sep 17 00:00:00 2001 From: Mponos George Date: Mon, 30 Dec 2019 19:54:56 +0200 Subject: [PATCH 4/5] Revert changes regarding prophesize --- tests/Parser/DecimalMoneyParserTest.php | 34 ++++++++++++------- .../Parser/IntlLocalizedDecimalParserTest.php | 15 +++++--- tests/Parser/IntlMoneyParserTest.php | 15 +++++--- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/tests/Parser/DecimalMoneyParserTest.php b/tests/Parser/DecimalMoneyParserTest.php index 9c1465e7..4a4ff06f 100644 --- a/tests/Parser/DecimalMoneyParserTest.php +++ b/tests/Parser/DecimalMoneyParserTest.php @@ -7,6 +7,7 @@ use Money\Exception\ParserException; use Money\Parser\DecimalMoneyParser; use PHPUnit\Framework\TestCase; +use Prophecy\Argument; final class DecimalMoneyParserTest extends TestCase { @@ -16,11 +17,14 @@ final class DecimalMoneyParserTest extends TestCase */ public function it_parses_money($decimal, $currency, $subunit, $result) { - $currencies = new Currencies\CurrencyList([ - $currency => $subunit, - ]); + $currencies = $this->prophesize(Currencies::class); - $parser = new DecimalMoneyParser($currencies); + $currencies->subunitFor(Argument::allOf( + Argument::type(Currency::class), + Argument::which('getCode', $currency) + ))->willReturn($subunit); + + $parser = new DecimalMoneyParser($currencies->reveal()); $this->assertEquals($result, $parser->parse($decimal, new Currency($currency))->getAmount()); } @@ -31,11 +35,14 @@ public function it_parses_money($decimal, $currency, $subunit, $result) */ public function it_throws_an_exception_upon_invalid_inputs($input) { - $currencies = new Currencies\CurrencyList([ - 'USD' => 2, - ]); + $currencies = $this->prophesize(Currencies::class); + + $currencies->subunitFor(Argument::allOf( + Argument::type(Currency::class), + Argument::which('getCode', 'USD') + ))->willReturn(2); - $parser = new DecimalMoneyParser($currencies); + $parser = new DecimalMoneyParser($currencies->reveal()); $this->expectException(ParserException::class); $parser->parse($input, new Currency('USD'))->getAmount(); @@ -48,11 +55,14 @@ public function it_throws_an_exception_upon_invalid_inputs($input) */ public function it_accepts_only_a_currency_object() { - $currencies = new Currencies\CurrencyList([ - 'USD' => 2, - ]); + $currencies = $this->prophesize(Currencies::class); + + $currencies->subunitFor(Argument::allOf( + Argument::type(Currency::class), + Argument::which('getCode', 'USD') + ))->willReturn(2); - $parser = new DecimalMoneyParser($currencies); + $parser = new DecimalMoneyParser($currencies->reveal()); $parser->parse('1.0', 'USD')->getAmount(); } diff --git a/tests/Parser/IntlLocalizedDecimalParserTest.php b/tests/Parser/IntlLocalizedDecimalParserTest.php index a50e3527..4ba73386 100644 --- a/tests/Parser/IntlLocalizedDecimalParserTest.php +++ b/tests/Parser/IntlLocalizedDecimalParserTest.php @@ -9,6 +9,7 @@ use Money\Money; use Money\Parser\IntlLocalizedDecimalParser; use PHPUnit\Framework\TestCase; +use Prophecy\Argument; final class IntlLocalizedDecimalParserTest extends TestCase { @@ -20,13 +21,17 @@ public function it_parses_money($string, $units, $locale) { $formatter = new \NumberFormatter($locale, \NumberFormatter::DECIMAL); - $currencies = new Currencies\CurrencyList([ - 'USD' => 2, - ]); + $currencies = $this->prophesize(Currencies::class); - $currency = new Currency('USD'); + $currencies->subunitFor(Argument::allOf( + Argument::type(Currency::class), + Argument::which('getCode', 'USD') + ))->willReturn(2); + + $currencyCode = 'USD'; + $currency = new Currency($currencyCode); - $parser = new IntlLocalizedDecimalParser($formatter, $currencies); + $parser = new IntlLocalizedDecimalParser($formatter, $currencies->reveal()); $this->assertEquals($units, $parser->parse($string, $currency)->getAmount()); } diff --git a/tests/Parser/IntlMoneyParserTest.php b/tests/Parser/IntlMoneyParserTest.php index db21016e..981f6394 100644 --- a/tests/Parser/IntlMoneyParserTest.php +++ b/tests/Parser/IntlMoneyParserTest.php @@ -9,6 +9,7 @@ use Money\Money; use Money\Parser\IntlMoneyParser; use PHPUnit\Framework\TestCase; +use Prophecy\Argument; final class IntlMoneyParserTest extends TestCase { @@ -22,13 +23,17 @@ public function it_parses_money($string, $units) $formatter = new \NumberFormatter('en_US', \NumberFormatter::CURRENCY); $formatter->setPattern('¤#,##0.00;-¤#,##0.00'); - $currencies = new Currencies\CurrencyList([ - 'USD' => 2, - ]); + $currencies = $this->prophesize(Currencies::class); - $currency = new Currency('USD'); + $currencies->subunitFor(Argument::allOf( + Argument::type(Currency::class), + Argument::which('getCode', 'USD') + ))->willReturn(2); - $parser = new IntlMoneyParser($formatter, $currencies); + $currencyCode = 'USD'; + $currency = new Currency($currencyCode); + + $parser = new IntlMoneyParser($formatter, $currencies->reveal()); $this->assertEquals($units, $parser->parse($string, $currency)->getAmount()); } From adff78019767b3f89b9c5f666883e205d0a14969 Mon Sep 17 00:00:00 2001 From: Mponos George Date: Sat, 20 Jun 2020 13:25:54 +0300 Subject: [PATCH 5/5] update actions --- .github/workflows/ci.yml | 2 +- .github/workflows/static.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 20a9ae48..44182290 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,7 @@ jobs: steps: - name: Set up PHP - uses: shivammathur/setup-php@1.6.2 + uses: shivammathur/setup-php@1.7.0 with: php-version: '5.6' extensions: bcmath, gmp, intl, dom, mbstring diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index b49ad135..bb0bcf9a 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -35,7 +35,7 @@ jobs: uses: actions/checkout@v1 - name: PHP-CS-Fixer - uses: docker://oskarstark/php-cs-fixer-ga:2.14.0 + uses: docker://oskarstark/php-cs-fixer-ga:2.16.3.1 with: args: --dry-run --diff-format udiff