From 0a9bcc28321c419561bc74d4970b6ea79955af2b Mon Sep 17 00:00:00 2001 From: Joel Male Date: Fri, 16 Aug 2024 16:49:59 +1000 Subject: [PATCH] quantities now support decimals (so adding 1.1, or setting it to 1.1) --- src/Cart.php | 18 +++------ tests/Unit/CartTest.php | 84 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 82 insertions(+), 20 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 4fa2761..6728576 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -226,18 +226,10 @@ public function update($id, $data) $item = $cart->get($id); foreach ($data as $key => $value) { - // if the key is currently "quantity" we will need to check if an arithmetic - // symbol is present so we can decide if the update of quantity is being added - // or being reduced. if ($key == 'quantity') { - // we will check if quantity value provided is array, - // if it is, we will need to check if a key "relative" is set - // and we will evaluate its value if true or false, - // this tells us how to treat the quantity value if it should be updated - // relatively to its current quantity value or just totally replace the value if (is_array($value)) { if (isset($value['relative'])) { - if ((bool) $value['relative']) { + if (isset($value['relative']) && $value['relative'] === true) { $item = $this->updateQuantityRelative($item, $key, $value['value']); } else { $item = $this->updateQuantityNotRelative($item, $key, $value['value']); @@ -791,7 +783,7 @@ protected function itemHasConditions($item) protected function updateQuantityRelative($item, $key, $value) { if (preg_match('/\-/', $value) == 1) { - $value = (int) str_replace('-', '', $value); + $value = (float) str_replace('-', '', $value); // we will not allowed to reduced quantity to 0, so if the given value // would result to item quantity of 0, we will not do it. @@ -799,9 +791,9 @@ protected function updateQuantityRelative($item, $key, $value) $item[$key] -= $value; } } elseif (preg_match('/\+/', $value) == 1) { - $item[$key] += (int) str_replace('+', '', $value); + $item[$key] += (float) str_replace('+', '', $value); } else { - $item[$key] += (int) $value; + $item[$key] += (float) $value; } return $item; @@ -814,7 +806,7 @@ protected function updateQuantityRelative($item, $key, $value) */ protected function updateQuantityNotRelative($item, $key, $value) { - $item[$key] = (int) $value; + $item[$key] = (float) $value; return $item; } diff --git a/tests/Unit/CartTest.php b/tests/Unit/CartTest.php index c0a9d32..c9ff6e3 100644 --- a/tests/Unit/CartTest.php +++ b/tests/Unit/CartTest.php @@ -113,6 +113,13 @@ expect($this->cart->getTotal())->toEqual(341.95, 'Cart should have a total of 341.95'); }); + test('cart item prices should be normalized when added to cart', function () { + // add a price in a string format should be converted to float + $this->cart->add(1, 'Book of Languages Item', '100.99', 2, []); + + expect($this->cart->getContent()->first()['price'])->toBeFloat('Cart price should be a float'); + }); + test('can update a cart item with new attributes and it should be still instance of item attribute collection', function () { $item = [ 'id' => 1, @@ -206,6 +213,76 @@ expect($items->first()->id)->toEqual(50, 'First item should have ID of 1'); expect($items->last()->id)->toEqual(2, 'Last item should have ID of 2'); }); + + test('can update existing items', function () { + $items = [ + [ + 'id' => 1, + 'name' => 'Magic The Gathering Card Pack', + 'price' => 12.99, + 'quantity' => 3, + 'attributes' => [], + ], + [ + 'id' => 2, + 'name' => 'Magic The Gathering Card Case', + 'price' => 39.99, + 'quantity' => 1, + 'attributes' => [], + ], + ]; + + $this->cart->add($items); + + $item = $this->cart->get(1); + + expect($item['name'])->toEqual('Magic The Gathering Card Pack', 'Item name should be "Magic The Gathering Card Pack"'); + expect($item['price'])->toEqual(12.99, 'Item price should be "12.99"'); + expect($item['quantity'])->toEqual(3, 'Item quantity should be 3'); + + // when cart's item quantity is updated, the subtotal should be updated as well + $this->cart->update(1, [ + 'name' => 'Magic The Gathering Card Packs', + 'quantity' => 2, + 'price' => 10.20, + ]); + + $item = $this->cart->get(1); + + expect($item['name'])->toEqual('Magic The Gathering Card Packs', 'Item name should be "Magic The Gathering Card Packs"'); + expect($item['price'])->toEqual(10.20, 'Item price should be 10.20'); + expect($item['quantity'])->toEqual(5, 'Item quantity should be 2'); + }); + + test('can update existing item quanties with decimals', function () { + $items = [ + 'id' => 1, + 'name' => 'Fertilizer', + 'price' => 30, + 'quantity' => 7.8, + 'attributes' => [ + 'unit' => 'kilogram', + ], + ]; + + $this->cart->add($items); + + $item = $this->cart->get(1); + + expect($item['quantity'])->toEqual(7.8, 'Item quantity should be 7.8'); + + // when cart's item quantity is updated, the subtotal should be updated as well + $this->cart->update(1, [ + 'quantity' => [ + 'relative' => true, + 'value' => 2.1, + ], + ]); + + $item = $this->cart->get(1); + + expect($item['quantity'])->toEqual(9.9, 'Item quantity should be 9.9'); + }); }); test('cart items attributes', function () { @@ -298,13 +375,6 @@ expect($item['quantity'])->toEqual(5, 'Item quantity should be 5'); }); -test('item price should be normalized when added to cart', function () { - // add a price in a string format should be converted to float - $this->cart->add(455, 'Sample Item', '100.99', 2, []); - - expect($this->cart->getContent()->first()['price'])->toBeFloat('Cart price should be a float'); -}); - test('it removes an item on cart by item id', function () { $items = [ [