From 05318b7b13e290c592306d1e029e04b9e9f8fa6e Mon Sep 17 00:00:00 2001 From: Joel Male Date: Tue, 3 Sep 2024 21:41:45 +1000 Subject: [PATCH] working on deprecations and code cleanup --- src/Cart.php | 72 +++++++-- tests/Unit/CartConditionsTest.php | 245 +++++++++++++++--------------- 2 files changed, 177 insertions(+), 140 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 435211c..1b7cfee 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -297,13 +297,29 @@ public function clear() /** * Adds a condition to the cart * - * @throws InvalidConditionException + * @deprecated Use addCondition or addConditions instead */ public function condition(array|CartCondition $condition): self { if (is_array($condition)) { foreach ($condition as $c) { - $this->condition($c); + $this->addCondition($c); + } + + return $this; + } + + return $this->addCondition($condition); + } + + /** + * Add a condition to the cart + */ + public function addCondition(CartCondition $condition): self + { + if (is_array($condition)) { + foreach ($condition as $c) { + $this->addCondition($c); } return $this; @@ -332,6 +348,18 @@ public function condition(array|CartCondition $condition): self return $this; } + /** + * Add multiple conditions to the cart + */ + public function addConditions(array $conditions): self + { + foreach ($conditions as $condition) { + $this->addCondition($condition); + } + + return $this; + } + /** * get conditions applied on the cart * @@ -384,32 +412,48 @@ public function getConditionsByType($type) /** * Remove all the condition with the $type specified - * Please Note that this will only remove condition added on cart bases, not those conditions added - * specifically on an per item bases - * - * @return $this */ - public function removeConditionsByType($type) + public function removeConditionsByType(string $type) { $this->getConditionsByType($type)->each(function ($condition) { - $this->removeCartCondition($condition->getName()); + $this->removeCondition($condition->getName()); }); } /** - * removes a condition on a cart by condition name, - * this can only remove conditions that are added on cart bases not conditions that are added on an item/product. - * If you wish to remove a condition that has been added for a specific item/product, you may - * use the removeItemCondition(itemId, conditionName) method instead. + * Remove a condition from the cart * - * @return void + * @deprecated Use removeCondition instead */ - public function removeCartCondition($conditionName) + public function removeCartCondition(string $conditionName) + { + $this->removeCondition($conditionName); + } + + /** + * Remove a condition from the cart + */ + public function removeCondition(string $conditionName, $clearItemConditions = false) { $conditions = $this->getConditions(); $conditions->pull($conditionName); + if ($clearItemConditions) { + // check if condition name is on any item's conditions + $items = $this->getContent(); + + foreach ($items as $item) { + if (isset($item['conditions'])) { + foreach ($item['conditions'] as $condition) { + if ($condition->getName() === $conditionName) { + $this->removeItemCondition($item['id'], $conditionName); + } + } + } + } + } + $this->saveConditions($conditions); } diff --git a/tests/Unit/CartConditionsTest.php b/tests/Unit/CartConditionsTest.php index 7c38b98..25eb64f 100644 --- a/tests/Unit/CartConditionsTest.php +++ b/tests/Unit/CartConditionsTest.php @@ -792,40 +792,6 @@ expect($giftCard->getCalculatedValue())->toEqual(0, 'Gift Card value should be 0'); }); - test('can be cleared', function () { - $siteWideDiscount = new CartCondition([ - 'name' => 'Site Wide Discount', - 'type' => 'discount', - 'target' => 'total', - 'value' => '-5%', - ]); - - $giftCard = new CartCondition([ - 'name' => 'Gift Card', - 'type' => 'gift_card', - 'target' => 'total', - 'value' => '-25', - ]); - - $item = [ - 'id' => 1, - 'name' => 'Backpack', - 'price' => 168.72, - 'quantity' => 1, - 'attributes' => [], - ]; - - $this->cart->add($item); - - $this->cart->condition([$siteWideDiscount, $giftCard]); - - expect($this->cart->getConditions()->count())->toEqual(2, 'Cart should have two conditions'); - - $this->cart->clearCartConditions(); - - 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([ [ @@ -1085,6 +1051,44 @@ expect($this->cart->getConditions(active: true))->toHaveCount(1, 'Cart should have 1 condition'); }); + + test('can be removed by condition name', function () { + $shipping = new CartCondition([ + 'name' => 'Shipping', + 'type' => 'shipping', + 'target' => 'total', + 'value' => '12', + ]); + + $tax = new CartCondition([ + 'name' => 'Tax', + 'type' => 'tax', + 'target' => 'total', + 'value' => '2.50', + ]); + + $item = [ + 'id' => 456, + 'name' => 'Sample Item 1', + 'price' => 100, + 'quantity' => 1, + 'attributes' => [], + ]; + + $this->cart->add($item); + + $this->cart->addConditions([$shipping, $tax]); + + // let's prove first we have now two conditions in the cart + expect($this->cart->getConditions()->count())->toEqual(2, 'Cart should have two conditions'); + + // now let's remove a specific condition by condition name + $this->cart->removeCondition('Tax'); + + // cart should have now only 1 condition + expect($this->cart->getConditions()->count())->toEqual(1, 'Cart should have one condition'); + expect($this->cart->getConditions()->first()->getName())->toEqual('Shipping'); + }); }); describe('item level conditions', function () { @@ -1252,110 +1256,66 @@ expect($this->cart->get(1)->getPriceSumWithConditions())->toEqual(0.00, "The item's price should be prevented from going below zero."); }); - // test('get cart condition by condition name', function () { - // $itemCondition1 = new CartCondition([ - // 'name' => 'SALE 5%', - // 'type' => 'sale', - // 'target' => 'total', - // 'value' => '-5%', - // ]); - // $itemCondition2 = new CartCondition([ - // 'name' => 'Item Gift Pack 25.00', - // 'type' => 'promo', - // 'target' => 'total', - // 'value' => '-25', - // ]); + test('can be removed by name when calling removeCondition with clearItemConditions: true', function () { + $itemCondition = new CartCondition([ + 'name' => 'Item Discount', + 'type' => 'discount', + 'value' => '-5%', + ]); - // $item = [ - // 'id' => 456, - // 'name' => 'Sample Item 1', - // 'price' => 100, - // 'quantity' => 1, - // 'attributes' => [], - // ]; + $item = [ + 'id' => 1, + 'name' => 'Backpack', + 'price' => 168.72, + 'quantity' => 1, + 'attributes' => [], + 'conditions' => [$itemCondition], + ]; - // $this->cart->add($item); + $this->cart->add($item); - // $this->cart->condition([$itemCondition1, $itemCondition2]); + expect($this->cart->getConditions())->toHaveCount(0, 'Cart should have no conditions'); + expect($this->cart->get(1)['conditions'])->toHaveCount(1, 'Cart item should have one condition'); - // // get a condition applied on cart by condition name - // $condition = $this->cart->getCondition($itemCondition1->getName()); + $this->cart->removeCondition( + conditionName: 'Item Discount', + clearItemConditions: true + ); - // expect('SALE 5%')->toEqual($condition->getName()); - // expect('total')->toEqual($condition->getTarget()); - // expect('sale')->toEqual($condition->getType()); - // expect('-5%')->toEqual($condition->getValue()); - // }); + expect($this->cart->getConditions()->count())->toEqual(0, 'Cart should have no conditions now'); + expect($this->cart->get(1)['conditions'])->toHaveCount(0, 'Cart items should have no conditions now'); + }); - // test('remove cart condition by condition name', function () { - // $itemCondition1 = new CartCondition([ - // 'name' => 'SALE 5%', - // 'type' => 'sale', - // 'target' => 'total', - // 'value' => '-5%', - // ]); - // $itemCondition2 = new CartCondition([ - // 'name' => 'Item Gift Pack 25.00', - // 'type' => 'promo', - // 'target' => 'total', - // 'value' => '-25', - // ]); + test('can be removed by condition name', function () { + $heavyItemSurcharge = new CartCondition([ + 'name' => 'Heavy Item Surcharge', + 'type' => 'surcharge', + 'value' => '10', + ]); - // $item = [ - // 'id' => 456, - // 'name' => 'Sample Item 1', - // 'price' => 100, - // 'quantity' => 1, - // 'attributes' => [], - // ]; + $tax = new CartCondition([ + 'name' => 'Tax', + 'type' => 'tax', + 'value' => '10%', + ]); - // $this->cart->add($item); + $item = [ + 'id' => 12, + 'name' => 'Beer (1 box)', + 'price' => 410.60, + 'quantity' => 1, + 'attributes' => [], + 'conditions' => [$heavyItemSurcharge, $tax], + ]; - // $this->cart->condition([$itemCondition1, $itemCondition2]); + $this->cart->add($item); - // // let's prove first we have now two conditions in the cart - // expect($this->cart->getConditions()->count())->toEqual(2, 'Cart should have two conditions'); + expect($this->cart->get(12)['conditions'])->toHaveCount(2, 'Item should have two conditions'); - // // now let's remove a specific condition by condition name - // $this->cart->removeCartCondition('SALE 5%'); + $this->cart->removeItemCondition(12, 'Tax'); - // // cart should have now only 1 condition - // expect($this->cart->getConditions()->count())->toEqual(1, 'Cart should have one condition'); - // expect($this->cart->getConditions()->first()->getName())->toEqual('Item Gift Pack 25.00'); - // }); - - // test('remove item condition by condition name', function () { - // $itemCondition1 = new CartCondition([ - // 'name' => 'SALE 5%', - // 'type' => 'sale', - // 'value' => '-5%', - // ]); - // $itemCondition2 = new CartCondition([ - // 'name' => 'Item Gift Pack 25.00', - // 'type' => 'promo', - // 'value' => '-25', - // ]); - - // $item = [ - // 'id' => 456, - // 'name' => 'Sample Item 1', - // 'price' => 100, - // 'quantity' => 1, - // 'attributes' => [], - // 'conditions' => [$itemCondition1, $itemCondition2], - // ]; - - // $this->cart->add($item); - - // // let's very first the item has 2 conditions in it - // expect($this->cart->get(456)['conditions'])->toHaveCount(2, 'Item should have two conditions'); - - // // now let's remove a condition on that item using the condition name - // $this->cart->removeItemCondition(456, 'SALE 5%'); - - // // now we should have only 1 condition left on that item - // expect($this->cart->get(456)['conditions'])->toHaveCount(1, 'Item should have one condition left'); - // }); + expect($this->cart->get(12)['conditions'])->toHaveCount(1, 'Item should have one condition'); + })->only(); // test('remove item condition by condition name scenario two', function () { // // NOTE: in this scenario, we will add the conditions not in array format @@ -1772,4 +1732,37 @@ expect($this->cart->getConditions()->count())->toEqual(0, 'Cart should have no conditions now'); expect($this->cart->get(1)['conditions'])->toHaveCount(0, 'Cart items should have no conditions now'); }); + + // test('remove item condition by condition name', function () { + // $itemCondition1 = new CartCondition([ + // 'name' => 'SALE 5%', + // 'type' => 'sale', + // 'value' => '-5%', + // ]); + // $itemCondition2 = new CartCondition([ + // 'name' => 'Item Gift Pack 25.00', + // 'type' => 'promo', + // 'value' => '-25', + // ]); + + // $item = [ + // 'id' => 456, + // 'name' => 'Sample Item 1', + // 'price' => 100, + // 'quantity' => 1, + // 'attributes' => [], + // 'conditions' => [$itemCondition1, $itemCondition2], + // ]; + + // $this->cart->add($item); + + // // let's very first the item has 2 conditions in it + // expect($this->cart->get(456)['conditions'])->toHaveCount(2, 'Item should have two conditions'); + + // // now let's remove a condition on that item using the condition name + // $this->cart->removeItemCondition(456, 'SALE 5%'); + + // // now we should have only 1 condition left on that item + // expect($this->cart->get(456)['conditions'])->toHaveCount(1, 'Item should have one condition left'); + // }); });