Repository navigation
Throw CurrencyMismatchException when adding/subtracting Money with different currencies - #12
Conversation
…fferent currencies Add a CurrencyMismatchException class and currency validation to Money::add() and Money::subtract() methods. Operations on Money objects with different currencies now throw an exception instead of silently using the first operand's currency. Closes #11 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Code Review
This pull request introduces a new CurrencyMismatchException and updates the add and subtract methods in the Money class to throw this exception when performing operations on objects with mismatched currencies. Corresponding unit tests have also been added. The feedback suggests improving these tests by asserting the specific exception message in addition to the exception class to prevent false positives.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| public function test_add_currency_mismatch() { | ||
| $this->expectException( CurrencyMismatchException::class ); | ||
|
|
||
| $eur = new Money( 100, 'EUR' ); | ||
| $usd = new Money( 50, 'USD' ); | ||
|
|
||
| $eur->add( $usd ); | ||
| } |
There was a problem hiding this comment.
It is highly recommended to assert the exception message in addition to the exception class. This ensures that the exception is thrown with the expected message and details (e.g., the specific currency codes), preventing false positives in tests.
public function test_add_currency_mismatch() {
$this->expectException( CurrencyMismatchException::class );
$this->expectExceptionMessage( 'Cannot perform arithmetic operation on Money objects with different currencies: EUR and USD.' );
$eur = new Money( 100, 'EUR' );
$usd = new Money( 50, 'USD' );
$eur->add( $usd );
}| public function test_subtract_currency_mismatch() { | ||
| $this->expectException( CurrencyMismatchException::class ); | ||
|
|
||
| $eur = new Money( 100, 'EUR' ); | ||
| $usd = new Money( 50, 'USD' ); | ||
|
|
||
| $eur->subtract( $usd ); | ||
| } |
There was a problem hiding this comment.
It is highly recommended to assert the exception message in addition to the exception class. This ensures that the exception is thrown with the expected message and details (e.g., the specific currency codes), preventing false positives in tests.
public function test_subtract_currency_mismatch() {
$this->expectException( CurrencyMismatchException::class );
$this->expectExceptionMessage( 'Cannot perform arithmetic operation on Money objects with different currencies: EUR and USD.' );
$eur = new Money( 100, 'EUR' );
$usd = new Money( 50, 'USD' );
$eur->subtract( $usd );
}Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Summary
Adds currency validation to
Money::add()andMoney::subtract()— operations on Money objects with different currencies now throw aCurrencyMismatchExceptioninstead of silently using the first operand's currency.Changes
CurrencyMismatchException— extends\InvalidArgumentExceptionwith a descriptive message showing both currency codes.Money::add()— throws if the addend has a different currency.Money::subtract()— throws if the subtrahend has a different currency.test_add_currency_mismatchandtest_subtract_currency_mismatch.This follows the same pattern as moneyphp/money.
Closes #11