From 98474d9b9851acecd8fbf806c776149dabb1ff4f Mon Sep 17 00:00:00 2001 From: Lionel Guichard Date: Mon, 6 May 2024 22:16:12 +0200 Subject: [PATCH] Fix - Method remove into ShippingModifiers (#1600) The ShippingModifiers store modifiers in collection, the method remove tried to remove the key that didn't exist. --------- Co-authored-by: Glenn Jacobs --- packages/core/src/Base/ShippingModifiers.php | 2 +- .../tests/Unit/Base/ShippingModifiersTest.php | 61 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 packages/core/tests/Unit/Base/ShippingModifiersTest.php diff --git a/packages/core/src/Base/ShippingModifiers.php b/packages/core/src/Base/ShippingModifiers.php index 3f1b06e3db..d89fda1cf0 100644 --- a/packages/core/src/Base/ShippingModifiers.php +++ b/packages/core/src/Base/ShippingModifiers.php @@ -48,6 +48,6 @@ public function add($modifier) */ public function remove($modifier) { - $this->modifiers->forget($modifier); + $this->modifiers = $this->modifiers->reject(fn ($value) => $value == $modifier); } } diff --git a/packages/core/tests/Unit/Base/ShippingModifiersTest.php b/packages/core/tests/Unit/Base/ShippingModifiersTest.php new file mode 100644 index 0000000000..4aa2cb1dd5 --- /dev/null +++ b/packages/core/tests/Unit/Base/ShippingModifiersTest.php @@ -0,0 +1,61 @@ +create([ + 'decimal_places' => 2, + ]); + + $this->cart = Cart::factory()->create([ + 'currency_id' => $currency->id, + ]); + + $this->class = new class extends ShippingModifier + { + public function handle(Cart $cart) + { + // + } + }; + + $this->shippingModifiers = new ShippingModifiers(); + } + + /** @test */ + public function can_add_modifier() + { + $this->shippingModifiers->add($this->class::class); + + $this->assertCount(1, $this->shippingModifiers->getModifiers()); + } + + + public function can_remove_modifier() + { + $this->shippingModifiers->remove($this->class::class); + + $this->assertCount(0, $this->shippingModifiers->getModifiers()); + } + +}