Skip to content

Commit 7334c24

Browse files
Merge pull request moneyphp#30 from camspiers/currency-pair-exception
Added better exception for failure to parse currency pair ISO string
2 parents 7ffda0d + 6b51878 commit 7334c24

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

lib/Money/CurrencyPair.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ public static function createFromIso($iso)
5252

5353
$matches = array();
5454
if (!preg_match($pattern, $iso, $matches)) {
55-
// @todo better exception
56-
throw new \Exception();
55+
throw new InvalidArgumentException(
56+
sprintf(
57+
"Can't create currency pair from ISO string '%s', format of string is invalid",
58+
$iso
59+
)
60+
);
5761
}
5862

5963
return new static(new Currency($matches[1]), new Currency($matches[2]), $matches[3]);

tests/Money/Tests/CurrencyPairTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,14 @@ public function ParsesIso()
3838
$expected = new CurrencyPair(new Currency('EUR'), new Currency('USD'), 1.2500);
3939
$this->assertEquals($expected, $pair);
4040
}
41+
42+
/**
43+
* @test
44+
* @expectedException \Money\InvalidArgumentException
45+
* @expectedExceptionMessage Can't create currency pair from ISO string '1.2500', format of string is invalid
46+
*/
47+
public function ParsesIsoWithException()
48+
{
49+
CurrencyPair::createFromIso('1.2500');
50+
}
4151
}

0 commit comments

Comments
 (0)