Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/CurrencyMismatchException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Currency Mismatch Exception
*
* @author Pronamic <info@pronamic.eu>
* @copyright 2005-2026 Pronamic
* @license GPL-3.0-or-later
* @package Pronamic\WordPress\Money
*/

namespace Pronamic\WordPress\Money;

/**
* Currency Mismatch Exception
*
* @author Remco Tolsma
*/
class CurrencyMismatchException extends \InvalidArgumentException {
/**
* Create a new currency mismatch exception.
*
* @param Currency $a First currency.
* @param Currency $b Second currency.
* @return self
*/
public static function create( Currency $a, Currency $b ): self {
return new self(
\sprintf(
'Cannot perform arithmetic operation on Money objects with different currencies: %s and %s.',
$a->get_alphabetic_code(),
$b->get_alphabetic_code()
)
);
}
}
10 changes: 10 additions & 0 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,13 @@ public function jsonSerialize(): object {
*
* @param Money $addend Addend.
* @return Money
* @throws CurrencyMismatchException Throws exception if currencies don't match.
*/
public function add( Money $addend ) {
if ( $this->currency->get_alphabetic_code() !== $addend->get_currency()->get_alphabetic_code() ) {
throw CurrencyMismatchException::create( $this->currency, $addend->get_currency() );
}

$result = $this->amount->add( $addend->get_number() );

return new self( $result, $this->currency );
Expand All @@ -292,8 +297,13 @@ public function add( Money $addend ) {
* @link https://github.com/moneyphp/money/blob/v3.2.1/src/Money.php#L235-L255
* @param Money $subtrahend Subtrahend.
* @return Money
* @throws CurrencyMismatchException Throws exception if currencies don't match.
*/
public function subtract( Money $subtrahend ) {
if ( $this->currency->get_alphabetic_code() !== $subtrahend->get_currency()->get_alphabetic_code() ) {
throw CurrencyMismatchException::create( $this->currency, $subtrahend->get_currency() );
}

$result = $this->amount->subtract( $subtrahend->get_number() );

return new self( $result, $this->currency );
Expand Down
24 changes: 24 additions & 0 deletions tests/src/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,30 @@ public function test_add() {
$this->assertEquals( 100, $money_3->get_value() );
}

/**
* Test add with currency mismatch.
*/
public function test_add_currency_mismatch() {
$this->expectException( CurrencyMismatchException::class );

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

$eur->add( $usd );
}
Comment on lines +269 to +276

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


/**
* Test subtract with currency mismatch.
*/
public function test_subtract_currency_mismatch() {
$this->expectException( CurrencyMismatchException::class );

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

$eur->subtract( $usd );
}
Comment on lines +281 to +288

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


/**
* Test JSON.
*/
Expand Down
Loading