Skip to content

Commit

Permalink
Improve tests of parsers (#586)
Browse files Browse the repository at this point in the history
* Improve tests of parsers

* Fix the codestyle

* Remove unused uses

* Revert changes regarding prophesize

* update actions
  • Loading branch information
gmponos committed Mar 16, 2021
1 parent 05e1af8 commit a44e2f2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 37 deletions.
3 changes: 1 addition & 2 deletions tests/Parser/DecimalMoneyParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();
}

Expand Down
96 changes: 64 additions & 32 deletions tests/Parser/IntlLocalizedDecimalParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
}

Expand All @@ -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));
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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'],
];
}
}
6 changes: 3 additions & 3 deletions tests/Parser/IntlMoneyParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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));
}

/**
Expand Down

0 comments on commit a44e2f2

Please sign in to comment.