diff --git a/tests/Parser/DecimalMoneyParserTest.php b/tests/Parser/DecimalMoneyParserTest.php index 364d6ce9..4a4ff06f 100644 --- a/tests/Parser/DecimalMoneyParserTest.php +++ b/tests/Parser/DecimalMoneyParserTest.php @@ -35,8 +35,6 @@ 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->subunitFor(Argument::allOf( @@ -46,6 +44,7 @@ public function it_throws_an_exception_upon_invalid_inputs($input) $parser = new DecimalMoneyParser($currencies->reveal()); + $this->expectException(ParserException::class); $parser->parse($input, new Currency('USD'))->getAmount(); } diff --git a/tests/Parser/IntlLocalizedDecimalParserTest.php b/tests/Parser/IntlLocalizedDecimalParserTest.php index b9bd204e..4ba73386 100644 --- a/tests/Parser/IntlLocalizedDecimalParserTest.php +++ b/tests/Parser/IntlLocalizedDecimalParserTest.php @@ -17,9 +17,9 @@ 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); + $formatter = new \NumberFormatter($locale, \NumberFormatter::DECIMAL); $currencies = $this->prophesize(Currencies::class); @@ -40,13 +40,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 +59,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 +69,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 +88,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 +118,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..981f6394 100644 --- a/tests/Parser/IntlMoneyParserTest.php +++ b/tests/Parser/IntlMoneyParserTest.php @@ -42,14 +42,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 +64,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)); } /**