Skip to content

Throw CurrencyMismatchException when adding/subtracting Money with different currencies - #12

Merged
remcotolsma merged 3 commits into
mainfrom
remcotolsma-currency-mismatch-errors
Jun 23, 2026
Merged

remcotolsma merged 3 commits into
mainfrom
remcotolsma-currency-mismatch-errors

Conversation

@remcotolsma

Copy link
Copy Markdown
Member

Summary

Adds currency validation to Money::add() and Money::subtract() — operations on Money objects with different currencies now throw a CurrencyMismatchException instead of silently using the first operand's currency.

Changes

  • New class CurrencyMismatchException — extends \InvalidArgumentException with 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.
  • Tests — added test_add_currency_mismatch and test_subtract_currency_mismatch.

This follows the same pattern as moneyphp/money.

Closes #11

…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>

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread tests/src/MoneyTest.php
Comment on lines +269 to +276
public function test_add_currency_mismatch() {
$this->expectException( CurrencyMismatchException::class );

$eur = new Money( 100, 'EUR' );
$usd = new Money( 50, 'USD' );

$eur->add( $usd );
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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 );
	}

Comment thread tests/src/MoneyTest.php
Comment on lines +281 to +288
public function test_subtract_currency_mismatch() {
$this->expectException( CurrencyMismatchException::class );

$eur = new Money( 100, 'EUR' );
$usd = new Money( 50, 'USD' );

$eur->subtract( $usd );
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

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 );
	}

remcotolsma and others added 2 commits June 23, 2026 12:25
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@remcotolsma
remcotolsma merged commit 442c90a into main Jun 23, 2026
6 of 18 checks passed
@remcotolsma
remcotolsma deleted the remcotolsma-currency-mismatch-errors branch June 23, 2026 10:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error should be thrown during Maths if currencies of both are not same.

1 participant