From 949f754e1134368f2df176fa0deb0a15c86cb113 Mon Sep 17 00:00:00 2001 From: Adrien Leloup Date: Mon, 16 Sep 2024 09:55:28 +0200 Subject: [PATCH] Use RationalMoney for modifier totals --- src/Concerns/HasModifiers.php | 10 +++++----- tests/Unit/HasModifiersTest.php | 18 ++++++++++-------- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/Concerns/HasModifiers.php b/src/Concerns/HasModifiers.php index 2aa1391..0ba6569 100644 --- a/src/Concerns/HasModifiers.php +++ b/src/Concerns/HasModifiers.php @@ -6,7 +6,7 @@ use Whitecube\Price\Modifier; use Whitecube\Price\PriceAmendable; use Brick\Money\AbstractMoney; -use Brick\Money\Money; +use Brick\Money\RationalMoney; trait HasModifiers { @@ -101,7 +101,7 @@ public function modifications(bool $perUnit = false, ?string $type = null): arra /** * Return the modification total for all discounts */ - public function discounts(bool $perUnit = false): Money + public function discounts(bool $perUnit = false): RationalMoney { return $this->modifiers($perUnit, Modifier::TYPE_DISCOUNT); } @@ -109,7 +109,7 @@ public function discounts(bool $perUnit = false): Money /** * Return the modification total for all taxes */ - public function taxes(bool $perUnit = false): Money + public function taxes(bool $perUnit = false): RationalMoney { return $this->modifiers($perUnit, Modifier::TYPE_TAX); } @@ -117,9 +117,9 @@ public function taxes(bool $perUnit = false): Money /** * Return the modification total for a given type */ - public function modifiers(bool $perUnit = false, ?string $type = null): Money + public function modifiers(bool $perUnit = false, ?string $type = null): RationalMoney { - $amount = Money::zero($this->currency()); + $amount = RationalMoney::of(0, $this->currency()); foreach ($this->modifications($perUnit, $type) as $modification) { $amount = $amount->plus($modification['amount']); diff --git a/tests/Unit/HasModifiersTest.php b/tests/Unit/HasModifiersTest.php index fb9bce4..3f4c4b4 100644 --- a/tests/Unit/HasModifiersTest.php +++ b/tests/Unit/HasModifiersTest.php @@ -2,6 +2,8 @@ namespace Tests\Unit; +use Brick\Math\RoundingMode; +use Brick\Money\Context\DefaultContext; use Brick\Money\Money; use Whitecube\Price\Price; use Whitecube\Price\Modifier; @@ -312,13 +314,13 @@ ->addModifier('something', CustomAmendableModifier::class, Money::ofMinor(100, 'EUR')) ->addModifier('custom', AmendableModifier::class); - expect($price->discounts()->__toString())->toBe('EUR -3.00'); - expect($price->discounts(true)->__toString())->toBe('EUR -1.50'); + expect($price->discounts()->to(new DefaultContext, RoundingMode::HALF_UP)->__toString())->toBe('EUR -3.00'); + expect($price->discounts(true)->to(new DefaultContext, RoundingMode::HALF_UP)->__toString())->toBe('EUR -1.50'); - expect($price->taxes()->__toString())->toBe('EUR 3.50'); - expect($price->taxes(true)->__toString())->toBe('EUR 1.75'); + expect($price->taxes()->to(new DefaultContext, RoundingMode::HALF_UP)->__toString())->toBe('EUR 3.50'); + expect($price->taxes(true)->to(new DefaultContext, RoundingMode::HALF_UP)->__toString())->toBe('EUR 1.75'); - expect($price->modifiers()->__toString())->toBe('EUR 7.63'); - expect($price->modifiers(true)->__toString())->toBe('EUR 3.81'); - expect($price->modifiers(false, 'custom')->__toString())->toBe('EUR 5.13'); -}); + expect($price->modifiers()->to(new DefaultContext, RoundingMode::HALF_UP)->__toString())->toBe('EUR 7.63'); + expect($price->modifiers(true)->to(new DefaultContext, RoundingMode::HALF_UP)->__toString())->toBe('EUR 3.81'); + expect($price->modifiers(false, 'custom')->to(new DefaultContext, RoundingMode::HALF_UP)->__toString())->toBe('EUR 5.13'); +}); \ No newline at end of file