From f6447ad4d9667c370ca847922fc0a2ec58fda8e2 Mon Sep 17 00:00:00 2001 From: Joel Male Date: Tue, 20 Aug 2024 16:57:06 +1000 Subject: [PATCH 1/2] feat: add failing tests --- tests/Unit/CartConditionsTest.php | 90 +++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/tests/Unit/CartConditionsTest.php b/tests/Unit/CartConditionsTest.php index 6553623..4d43a8d 100644 --- a/tests/Unit/CartConditionsTest.php +++ b/tests/Unit/CartConditionsTest.php @@ -825,6 +825,96 @@ expect($this->cart->getConditions()->count())->toEqual(0, 'Cart should have no conditions now'); }); + + test('calculate the subtotal correctly when one condition makes it $0, and the other adds $10', function () { + $this->cart->add([ + [ + 'id' => 456, + 'name' => 'Sample Item 1', + 'price' => 67.99, + 'quantity' => 1, + 'attributes' => [], + ], + [ + 'id' => 568, + 'name' => 'Sample Item 2', + 'price' => 69.25, + 'quantity' => 1, + 'attributes' => [], + ], + ]); + + expect($this->cart->getSubTotal())->toEqual(137.24, 'Cart should have sub total of 137.24'); + expect($this->cart->getTotal())->toEqual(137.24, 'Cart should have total of 137.24'); + + // add conditions + $couponCode = new CartCondition([ + 'name' => 'Coupon code', + 'type' => 'discount', + 'target' => 'subtotal', + 'order' => '1', + 'value' => '-100%', + ]); + + $shipping = new CartCondition([ + 'name' => 'Shipping', + 'type' => 'shipping', + 'target' => 'subtotal', + 'order' => '2', + 'value' => '10', + ]); + + $this->cart->condition($couponCode); + $this->cart->condition($shipping); + + expect($this->cart->getSubTotal())->toEqual(10.00, 'Cart should have sub total of 10.00'); + expect($this->cart->getTotal())->toEqual(10.00, 'Cart should have a total of 10.00'); + }); + + test('calculate the subtotal correctly when one condition makes it $0 (with dollars), and the other adds $10', function () { + $this->cart->add([ + [ + 'id' => 456, + 'name' => 'Sample Item 1', + 'price' => 67.99, + 'quantity' => 1, + 'attributes' => [], + ], + [ + 'id' => 568, + 'name' => 'Sample Item 2', + 'price' => 69.25, + 'quantity' => 1, + 'attributes' => [], + ], + ]); + + expect($this->cart->getSubTotal())->toEqual(137.24, 'Cart should have sub total of 137.24'); + expect($this->cart->getTotal())->toEqual(137.24, 'Cart should have total of 137.24'); + + // add conditions + $couponCode = new CartCondition([ + 'name' => 'Coupon code', + 'type' => 'discount', + 'target' => 'subtotal', + 'order' => '1', + 'value' => '-137.24', + ]); + + $shipping = new CartCondition([ + 'name' => 'Shipping', + 'type' => 'shipping', + 'target' => 'subtotal', + 'order' => '2', + 'value' => '10', + ]); + + $this->cart->condition($couponCode); + $this->cart->condition($shipping); + + expect($this->cart->getSubTotal())->toEqual(10.00, 'Cart should have sub total of 10.00'); + expect($this->cart->getTotal())->toEqual(10.00, 'Cart should have a total of 10.00'); + }); }); describe('item level conditions', function () { From 3673f360b7bad0036cbd3c13575d407d0d595344 Mon Sep 17 00:00:00 2001 From: Joel Male Date: Tue, 20 Aug 2024 16:57:11 +1000 Subject: [PATCH 2/2] feat: fix tests --- src/CartCondition.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/CartCondition.php b/src/CartCondition.php index 344240d..a511cc6 100644 --- a/src/CartCondition.php +++ b/src/CartCondition.php @@ -145,12 +145,6 @@ public function getCalculatedValue($totalOrSubTotalOrPrice = null): int|float */ protected function apply($totalOrSubTotalOrPrice, $conditionValue) { - if ($totalOrSubTotalOrPrice <= 0) { - $this->parsedRawValue = 0; - - return; - } - // if value has a percentage sign on it, we will get first // its percentage then we will evaluate again if the value // has a minus or plus sign so we can decide what to do with the