Skip to content

Commit

Permalink
Undefined Index "digits" (#430)
Browse files Browse the repository at this point in the history
* add test for handling parsing of invalid inputs

* add failing test case for invalid input

* check digits were found in regex
  • Loading branch information
rodnaph authored and frederikbosch committed Dec 11, 2017
1 parent 27f8dd6 commit 57d415a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Parser/DecimalMoneyParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function parse($money, $forceCurrency = null)

$subunit = $this->currencies->subunitFor($currency);

if (!preg_match(self::DECIMAL_PATTERN, $decimal, $matches)) {
if (!preg_match(self::DECIMAL_PATTERN, $decimal, $matches) || !isset($matches['digits'])) {
throw new ParserException(sprintf(
'Cannot parse "%s" to Money.',
$decimal
Expand Down
26 changes: 26 additions & 0 deletions tests/Parser/DecimalMoneyParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,30 @@ public static function formattedMoneyExamples()
['-9.99', 'USD', 2, -999],
];
}

public static function invalidMoneyExamples()
{
return [
['INVALID'],
['.'],
];
}

/**
* @dataProvider invalidMoneyExamples
* @expectedException \Money\Exception\ParserException
*/
public function testInvalidInputsThrowParseException($input)
{
$currencies = $this->prophesize(Currencies::class);

$currencies->subunitFor(Argument::allOf(
Argument::type(Currency::class),
Argument::which('getCode', 'USD')
))->willReturn(2);

$parser = new DecimalMoneyParser($currencies->reveal());

$parser->parse($input, 'USD')->getAmount();
}
}

0 comments on commit 57d415a

Please sign in to comment.