Commit 65a4a0b5 authored by bojanz's avatar bojanz

Issue #3027073 by mradcliffe, bojanz: Exceptions thrown by Price need to have a message

parent 3a98a2f4
......@@ -296,7 +296,7 @@ final class Price {
*/
protected function assertCurrencyCodeFormat($currency_code) {
if (strlen($currency_code) != '3') {
throw new \InvalidArgumentException();
throw new \InvalidArgumentException(sprintf('Invalid currency code "%s".', $currency_code));
}
}
......@@ -313,7 +313,7 @@ final class Price {
*/
protected function assertSameCurrency(Price $first_price, Price $second_price) {
if ($first_price->getCurrencyCode() != $second_price->getCurrencyCode()) {
throw new CurrencyMismatchException();
throw new CurrencyMismatchException(sprintf('The provided prices have mismatched currencies: %s, %s.', $first_price->__toString(), $second_price->__toString()));
}
}
......
......@@ -36,6 +36,7 @@ class PriceTest extends UnitTestCase {
*/
public function testCreateFromInvalidArray() {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Price::fromArray() called with a malformed array.');
$price = Price::fromArray([]);
}
......@@ -69,7 +70,8 @@ class PriceTest extends UnitTestCase {
*/
public function testInvalidCurrencyCode() {
$this->expectException(\InvalidArgumentException::class);
$price = new Price('10', 'INVALID');
$this->expectExceptionMessage('Invalid currency code "TEST".');
$price = new Price('10', 'TEST');
}
/**
......@@ -87,6 +89,28 @@ class PriceTest extends UnitTestCase {
$this->assertEquals(['number' => '10', 'currency_code' => 'USD'], $this->price->toArray());
}
/**
* Tests addition with mismatched currencies.
*
* ::covers add.
*/
public function testAddWithMismatchedCurrencies() {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The provided prices have mismatched currencies: 10 USD, 5 EUR.');
$this->price->add(new Price('5', 'EUR'));
}
/**
* Tests subtraction with mismatched currencies.
*
* ::covers subtract.
*/
public function testSubtractWithMismatchedCurrencies() {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('The provided prices have mismatched currencies: 10 USD, 4 EUR.');
$this->price->subtract(new Price('4', 'EUR'));
}
/**
* Tests the arithmetic methods.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment