From 0151eb5c97e9fd9b20f761db1c6d023e9d2c2fe4 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 18 Jan 2022 00:34:19 +0100 Subject: [PATCH 001/156] Add MoneyPHP --- composer.json | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 0074708..1ebb609 100644 --- a/composer.json +++ b/composer.json @@ -17,13 +17,14 @@ ], "require": { "php": "^7.4|^8.0", - "illuminate/support": "5.8.*||^6.0||^7.0||^8.0", - "illuminate/session": "5.8.*||^6.0||^7.0||^8.0", - "illuminate/events": "5.8.*||^6.0||^7.0||^8.0", - "nesbot/carbon": "^2.0" + "illuminate/support": "5.8.*|^6.0|^7.0|^8.0", + "illuminate/session": "5.8.*|^6.0|^7.0|^8.0", + "illuminate/events": "5.8.*|^6.0|^7.0|^8.0", + "nesbot/carbon": "^2.0", + "moneyphp/money": "^3.3.0|^4.0.0" }, "require-dev": { - "phpunit/phpunit": "~8.0||~9.0", + "phpunit/phpunit": "~8.0|~9.0", "mockery/mockery": "^1.0", "orchestra/testbench": "^3.8|^4.0|^5.0|^6.0" }, From 23325c2bb718dfc16f07bae661ecff23fe961d21 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 18 Jan 2022 00:50:01 +0100 Subject: [PATCH 002/156] Update CartItem.php --- src/CartItem.php | 110 ++++++++++++++++++----------------------------- 1 file changed, 43 insertions(+), 67 deletions(-) diff --git a/src/CartItem.php b/src/CartItem.php index c231cce..2bdf990 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -9,6 +9,7 @@ use Gloudemans\Shoppingcart\Exceptions\InvalidCalculatorException; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Support\Arr; +use Money\Money; use ReflectionClass; /** @@ -27,10 +28,8 @@ class CartItem implements Arrayable, Jsonable { /** * The rowID of the cart item. - * - * @var string */ - public $rowId; + public string $rowId; /** * The ID of the cart item. @@ -55,10 +54,8 @@ class CartItem implements Arrayable, Jsonable /** * The price without TAX of the cart item. - * - * @var float */ - public $price; + public Money $price; /** * The weight of the product. @@ -74,10 +71,8 @@ class CartItem implements Arrayable, Jsonable /** * The tax rate for the cart item. - * - * @var int|float */ - public $taxRate = 0; + public float $taxRate = 0; /** * The FQN of the associated model. @@ -88,10 +83,8 @@ class CartItem implements Arrayable, Jsonable /** * The discount rate for the cart item. - * - * @var float */ - private $discountRate = 0; + private Money $discount = 0; /** * The cart instance of the cart item. @@ -107,25 +100,16 @@ class CartItem implements Arrayable, Jsonable * @param float $weight * @param array $options */ - public function __construct($id, string $name, $price, $weight = 0, array $options = []) + public function __construct($id, string $name, Money $price, int $weight = 0, array $options = []) { - if (empty($id)) { + if (!is_string($id) && !is_int($id)) { throw new \InvalidArgumentException('Please supply a valid identifier.'); } - if (empty($name)) { - throw new \InvalidArgumentException('Please supply a valid name.'); - } - if (strlen($price) < 0 || !is_numeric($price)) { - throw new \InvalidArgumentException('Please supply a valid price.'); - } - if (strlen($weight) < 0 || !is_numeric($weight)) { - throw new \InvalidArgumentException('Please supply a valid weight.'); - } $this->id = $id; $this->name = $name; - $this->price = floatval($price); - $this->weight = floatval($weight); + $this->price = $price; + $this->weight = $weight; $this->options = new CartItemOptions($options); $this->rowId = $this->generateRowId($id, $options); } @@ -340,7 +324,7 @@ class CartItem implements Arrayable, Jsonable * * @return \Gloudemans\Shoppingcart\CartItem */ - public function associate($model) + public function associate($model) : self { $this->associatedModel = is_string($model) ? $model : get_class($model); @@ -354,7 +338,7 @@ class CartItem implements Arrayable, Jsonable * * @return \Gloudemans\Shoppingcart\CartItem */ - public function setTaxRate($taxRate) + public function setTaxRate($taxRate) : self { $this->taxRate = $taxRate; @@ -363,14 +347,10 @@ class CartItem implements Arrayable, Jsonable /** * Set the discount rate. - * - * @param int|float $discountRate - * - * @return \Gloudemans\Shoppingcart\CartItem */ - public function setDiscountRate($discountRate) + public function setDiscount(Money $discount) : self { - $this->discountRate = $discountRate; + $this->discount = $discount; return $this; } @@ -382,12 +362,21 @@ class CartItem implements Arrayable, Jsonable * * @return \Gloudemans\Shoppingcart\CartItem */ - public function setInstance(?string $instance) + public function setInstance(?string $instance) : self { $this->instance = $instance; return $this; } + + /** + * Getter for the raw internal discount rate. + * Should be used in calculators. + */ + public function getDiscount() : Money + { + return $this->discount; + } /** * Get an attribute from the cart item or get the associated model. @@ -434,7 +423,7 @@ class CartItem implements Arrayable, Jsonable * * @return \Gloudemans\Shoppingcart\CartItem */ - public static function fromBuyable(Buyable $item, array $options = []) + public static function fromBuyable(Buyable $item, array $options = []) : self { return new self($item->getBuyableIdentifier($options), $item->getBuyableDescription($options), $item->getBuyablePrice($options), $item->getBuyableWeight($options), $options); } @@ -446,7 +435,7 @@ class CartItem implements Arrayable, Jsonable * * @return \Gloudemans\Shoppingcart\CartItem */ - public static function fromArray(array $attributes) + public static function fromArray(array $attributes) : self { $options = Arr::get($attributes, 'options', []); @@ -463,26 +452,11 @@ class CartItem implements Arrayable, Jsonable * * @return \Gloudemans\Shoppingcart\CartItem */ - public static function fromAttributes($id, string $name, $price, $weight, array $options = []) + public static function fromAttributes($id, string $name, Money $price, $weight, array $options = []) : self { return new self($id, $name, $price, $weight, $options); } - /** - * Generate a unique id for the cart item. - * - * @param string $id - * @param array $options - * - * @return string - */ - protected function generateRowId($id, array $options) - { - ksort($options); - - return md5($id.serialize($options)); - } - /** * Get the instance as an array. * @@ -497,9 +471,7 @@ class CartItem implements Arrayable, Jsonable 'qty' => $this->qty, 'price' => $this->price, 'weight' => $this->weight, - 'options' => is_object($this->options) - ? $this->options->toArray() - : $this->options, + 'options' => $this->options->toArray(), 'discount' => $this->discount, 'tax' => $this->tax, 'subtotal' => $this->subtotal, @@ -517,6 +489,21 @@ class CartItem implements Arrayable, Jsonable { return json_encode($this->toArray(), $options); } + + /** + * Generate a unique id for the cart item. + * + * @param string $id + * @param array $options + * + * @return string + */ + protected function generateRowId(string $id, array $options) : string + { + ksort($options); + + return md5($id.serialize($options)); + } /** * Get the formatted number. @@ -528,7 +515,7 @@ class CartItem implements Arrayable, Jsonable * * @return string */ - private function numberFormat($value, ?int $decimals, ?string $decimalPoint, ?string $thousandSeperator) + private function numberFormat($value, ?int $decimals, ?string $decimalPoint, ?string $thousandSeperator) : string { if (is_null($decimals)) { $decimals = config('cart.format.decimals', 2); @@ -544,15 +531,4 @@ class CartItem implements Arrayable, Jsonable return number_format($value, $decimals, $decimalPoint, $thousandSeperator); } - - /** - * Getter for the raw internal discount rate. - * Should be used in calculators. - * - * @return float - */ - public function getDiscountRate() - { - return $this->discountRate; - } } From 83f6d94a9478e03dbc7f753ffcd55eaafa4c5ddf Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 18 Jan 2022 00:54:05 +0100 Subject: [PATCH 003/156] Update Cart.php --- src/Cart.php | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 17f2487..2fe8649 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -54,10 +54,8 @@ class Cart /** * Defines the discount percentage. - * - * @var float */ - private $discount = 0; + private ?Money $discount = null; /** * Defines the tax rate. @@ -124,7 +122,7 @@ class Cart * * @return \Gloudemans\Shoppingcart\CartItem */ - public function add($id, ?string $name = null, $qty = null, $price = null, $weight = 0, array $options = []) + public function add($id, ?string $name = null, $qty = null, ?Money $price = null, $weight = 0, array $options = []) { if ($this->isMulti($id)) { return array_map(function ($item) { @@ -751,7 +749,7 @@ class Cart * * @return \Gloudemans\Shoppingcart\CartItem */ - private function createCartItem($id, ?string $name = null, $qty, $price, $weight, array $options) : CartItem + private function createCartItem($id, ?string $name = null, int $qty, ?Money $price = null, int $weight = 0, array $options = []) : CartItem { if ($id instanceof Buyable) { $cartItem = CartItem::fromBuyable($id, $qty ?: []); @@ -777,7 +775,7 @@ class Cart * * @return bool */ - private function isMulti($item) + private function isMulti($item) : bool { if (!is_array($item)) { return false; @@ -791,7 +789,7 @@ class Cart * * @return bool */ - private function storedCartInstanceWithIdentifierExists($instance, $identifier) + private function storedCartInstanceWithIdentifierExists(string $instance, string $identifier) : bool { return $this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance'=> $instance])->exists(); } @@ -811,7 +809,7 @@ class Cart * * @return string */ - private function getTableName() + private function getTableName() : string { return config('cart.database.table', 'shoppingcart'); } @@ -821,7 +819,7 @@ class Cart * * @return string */ - private function getConnectionName() + private function getConnectionName() : string { $connection = config('cart.database.connection'); @@ -838,7 +836,7 @@ class Cart * * @return string */ - private function numberFormat($value, ?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) + private function numberFormat($value, ?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) : string { if (is_null($decimals)) { $decimals = config('cart.format.decimals', 2); From d838fcddfb741030d29fc60e80e3729282afa5ef Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 18 Jan 2022 00:55:15 +0100 Subject: [PATCH 004/156] Update InstanceIdentifier.php --- src/Contracts/InstanceIdentifier.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Contracts/InstanceIdentifier.php b/src/Contracts/InstanceIdentifier.php index 6c30cc2..e99caeb 100644 --- a/src/Contracts/InstanceIdentifier.php +++ b/src/Contracts/InstanceIdentifier.php @@ -2,6 +2,8 @@ namespace Gloudemans\Shoppingcart\Contracts; +use Money\Money; + interface InstanceIdentifier { /** @@ -16,5 +18,5 @@ interface InstanceIdentifier * * @return int|string */ - public function getInstanceGlobalDiscount(); + public function getInstanceGlobalDiscount() : Money; } From 4021baf038ea32c3d3aab08f04338963fd109cde Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 18 Jan 2022 00:57:19 +0100 Subject: [PATCH 005/156] Update cart.php --- src/Config/cart.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Config/cart.php b/src/Config/cart.php index 42f667a..bfc5c43 100644 --- a/src/Config/cart.php +++ b/src/Config/cart.php @@ -1,5 +1,7 @@ \Gloudemans\Shoppingcart\Calculation\DefaultCalculator::class, + + /* + |-------------------------------------------------------------------------- + | Rounding strategy + |-------------------------------------------------------------------------- + | + | This default value is used to select the method to calculate prices and taxes + | If true the item price is managed as a gross price, so taxes will be calculated by separation/exclusion + | + */ + + 'rounding' => Money::ROUND_UP, /* |-------------------------------------------------------------------------- From 211d4f40229cd062d96443e29345bb199fa13bf2 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 18 Jan 2022 01:05:19 +0100 Subject: [PATCH 006/156] Update CartItem.php --- src/CartItem.php | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/CartItem.php b/src/CartItem.php index 2bdf990..3804e81 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -84,7 +84,7 @@ class CartItem implements Arrayable, Jsonable /** * The discount rate for the cart item. */ - private Money $discount = 0; + public float $discountRate = 0; /** * The cart instance of the cart item. @@ -348,9 +348,9 @@ class CartItem implements Arrayable, Jsonable /** * Set the discount rate. */ - public function setDiscount(Money $discount) : self + public function setDiscount(float $discount) : self { - $this->discount = $discount; + $this->discountRate = $discountRate; return $this; } @@ -369,15 +369,6 @@ class CartItem implements Arrayable, Jsonable return $this; } - /** - * Getter for the raw internal discount rate. - * Should be used in calculators. - */ - public function getDiscount() : Money - { - return $this->discount; - } - /** * Get an attribute from the cart item or get the associated model. * From c4a7c7a65b3419ec4266bfdc30a7ff7ddf97aa8b Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 18 Jan 2022 01:14:44 +0100 Subject: [PATCH 007/156] Update DefaultCalculator.php --- src/Calculation/DefaultCalculator.php | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Calculation/DefaultCalculator.php b/src/Calculation/DefaultCalculator.php index d95f7a7..6cface8 100644 --- a/src/Calculation/DefaultCalculator.php +++ b/src/Calculation/DefaultCalculator.php @@ -4,6 +4,7 @@ namespace Gloudemans\Shoppingcart\Calculation; use Gloudemans\Shoppingcart\CartItem; use Gloudemans\Shoppingcart\Contracts\Calculator; +use Money\Money; class DefaultCalculator implements Calculator { @@ -13,23 +14,24 @@ class DefaultCalculator implements Calculator switch ($attribute) { case 'discount': - return $cartItem->price * ($cartItem->getDiscountRate() / 100); + return $cartItem->price->multiply($cartItem->discountRate, config('cart.rounding', Money::ROUND_UP)); case 'tax': - return round($cartItem->priceTarget * ($cartItem->taxRate / 100), $decimals); + return $cartItem->priceTarget->multiply($cartItem->taxRate + 1, config('cart.rounding', Money::ROUND_UP)); case 'priceTax': - return round($cartItem->priceTarget + $cartItem->tax, $decimals); + return $cartItem->priceTarget->add($cartItem->tax); case 'discountTotal': - return round($cartItem->discount * $cartItem->qty, $decimals); + return $cartItem->discount->multiply($cartItem->qty, config('cart.rounding', Money::ROUND_UP)); case 'priceTotal': - return round($cartItem->price * $cartItem->qty, $decimals); + return $cartItem->price->multiply($cartItem->qty, config('cart.rounding', Money::ROUND_UP)); case 'subtotal': - return max(round($cartItem->priceTotal - $cartItem->discountTotal, $decimals), 0); + $subtotal = $cartItem->priceTotal->subtract($cartItem->discountTotal); + return $subtotal->isPositive() ? $subtotal : new Money(0, $this->price->getCurrency()); case 'priceTarget': - return round(($cartItem->priceTotal - $cartItem->discountTotal) / $cartItem->qty, $decimals); + return $cartItem->priceTotal->subtract($cartItem->discountTotal)->divide($cartItem->qty); case 'taxTotal': - return round($cartItem->subtotal * ($cartItem->taxRate / 100), $decimals); + return $cartItem->subtotal->multiply($cartItem->taxRate + 1, config('cart.rounding', Money::ROUND_UP)); case 'total': - return round($cartItem->subtotal + $cartItem->taxTotal, $decimals); + return $cartItem->subtotal->add($cartItem->taxTotal); default: return; } From fa35766660f60f0805b2e7c5fabde6c1985c136a Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 18 Jan 2022 01:14:54 +0100 Subject: [PATCH 008/156] Update DefaultCalculator.php --- src/Calculation/DefaultCalculator.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Calculation/DefaultCalculator.php b/src/Calculation/DefaultCalculator.php index 6cface8..daac9c7 100644 --- a/src/Calculation/DefaultCalculator.php +++ b/src/Calculation/DefaultCalculator.php @@ -10,8 +10,6 @@ class DefaultCalculator implements Calculator { public static function getAttribute(string $attribute, CartItem $cartItem) { - $decimals = config('cart.format.decimals', 2); - switch ($attribute) { case 'discount': return $cartItem->price->multiply($cartItem->discountRate, config('cart.rounding', Money::ROUND_UP)); From c6a60334ee6ccf57f52c9d03dada9a26e957d771 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 18 Jan 2022 01:16:35 +0100 Subject: [PATCH 009/156] Update DefaultCalculator.php --- src/Calculation/DefaultCalculator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Calculation/DefaultCalculator.php b/src/Calculation/DefaultCalculator.php index daac9c7..a9afba6 100644 --- a/src/Calculation/DefaultCalculator.php +++ b/src/Calculation/DefaultCalculator.php @@ -14,7 +14,7 @@ class DefaultCalculator implements Calculator case 'discount': return $cartItem->price->multiply($cartItem->discountRate, config('cart.rounding', Money::ROUND_UP)); case 'tax': - return $cartItem->priceTarget->multiply($cartItem->taxRate + 1, config('cart.rounding', Money::ROUND_UP)); + return $cartItem->priceTarget->multiply($cartItem->taxRate + 1, config('cart.rounding', Money::ROUND_UP)); case 'priceTax': return $cartItem->priceTarget->add($cartItem->tax); case 'discountTotal': @@ -27,7 +27,7 @@ class DefaultCalculator implements Calculator case 'priceTarget': return $cartItem->priceTotal->subtract($cartItem->discountTotal)->divide($cartItem->qty); case 'taxTotal': - return $cartItem->subtotal->multiply($cartItem->taxRate + 1, config('cart.rounding', Money::ROUND_UP)); + return $cartItem->subtotal->multiply($cartItem->taxRate + 1, config('cart.rounding', Money::ROUND_UP)); case 'total': return $cartItem->subtotal->add($cartItem->taxTotal); default: From 717c8f56675e0a38d9f11d2cc3b64aea2639c681 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 18 Jan 2022 01:16:55 +0100 Subject: [PATCH 010/156] Update Buyable.php --- src/Contracts/Buyable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Contracts/Buyable.php b/src/Contracts/Buyable.php index 4666b36..a6baff5 100644 --- a/src/Contracts/Buyable.php +++ b/src/Contracts/Buyable.php @@ -14,7 +14,7 @@ interface Buyable /** * Get the description or title of the Buyable item. */ - public function getBuyableDescription() : ?string; + public function getBuyableDescription(): ?string; /** * Get the price of the Buyable item. From fbab509c803c79c33eea8530bb8ea341570747a6 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 18 Jan 2022 01:17:05 +0100 Subject: [PATCH 011/156] Update InstanceIdentifier.php --- src/Contracts/InstanceIdentifier.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Contracts/InstanceIdentifier.php b/src/Contracts/InstanceIdentifier.php index e99caeb..d2acbcf 100644 --- a/src/Contracts/InstanceIdentifier.php +++ b/src/Contracts/InstanceIdentifier.php @@ -18,5 +18,5 @@ interface InstanceIdentifier * * @return int|string */ - public function getInstanceGlobalDiscount() : Money; + public function getInstanceGlobalDiscount(): Money; } From c7e3774f9f401e0e665cee7b023b49ff8e9497f0 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 18 Jan 2022 01:17:19 +0100 Subject: [PATCH 012/156] Update CanBeBought.php --- src/CanBeBought.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CanBeBought.php b/src/CanBeBought.php index 544f36b..f11e71a 100644 --- a/src/CanBeBought.php +++ b/src/CanBeBought.php @@ -19,7 +19,7 @@ trait CanBeBought * * @return string */ - public function getBuyableDescription() : ?string + public function getBuyableDescription(): ?string { if (($name = $this->getAttribute('name'))) { return $name; From ca22d5d2f55e14630cc5364b61f41ac4d442db6b Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 18 Jan 2022 01:21:59 +0100 Subject: [PATCH 013/156] Update Cart.php --- src/Cart.php | 98 +++++++++++++++++++++++++--------------------------- 1 file changed, 47 insertions(+), 51 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 2fe8649..bca8d8b 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -86,7 +86,7 @@ class Cart * * @return \Gloudemans\Shoppingcart\Cart */ - public function instance($instance = null) + public function instance($instance = null): self { $instance = $instance ?: self::DEFAULT_INSTANCE; @@ -105,7 +105,7 @@ class Cart * * @return string */ - public function currentInstance() + public function currentInstance(): string { return str_replace('cart.', '', $this->instance); } @@ -122,7 +122,7 @@ class Cart * * @return \Gloudemans\Shoppingcart\CartItem */ - public function add($id, ?string $name = null, $qty = null, ?Money $price = null, $weight = 0, array $options = []) + public function add($id, ?string $name = null, $qty = null, ?Money $price = null, $weight = 0, array $options = []): CartItem { if ($this->isMulti($id)) { return array_map(function ($item) { @@ -145,7 +145,7 @@ class Cart * * @return \Gloudemans\Shoppingcart\CartItem The CartItem */ - public function addCartItem(CartItem $item, bool $keepDiscount = false, bool $keepTax = false, bool $dispatchEvent = true) + public function addCartItem(CartItem $item, bool $keepDiscount = false, bool $keepTax = false, bool $dispatchEvent = true): CartItem { if (!$keepDiscount) { $item->setDiscountRate($this->discount); @@ -184,7 +184,7 @@ class Cart * * @return \Gloudemans\Shoppingcart\CartItem */ - public function update(string $rowId, $qty) + public function update(string $rowId, $qty): ?CartItem { $cartItem = $this->get($rowId); @@ -212,7 +212,7 @@ class Cart if ($cartItem->qty <= 0) { $this->remove($cartItem->rowId); - return; + return null; } else { if (isset($itemOldIndex)) { $content = $content->slice(0, $itemOldIndex) @@ -239,7 +239,7 @@ class Cart * * @return void */ - public function remove(string $rowId) + public function remove(string $rowId): void { $cartItem = $this->get($rowId); @@ -261,7 +261,7 @@ class Cart * * @return \Gloudemans\Shoppingcart\CartItem */ - public function get(string $rowId) + public function get(string $rowId): CartItem { $content = $this->getContent(); @@ -277,7 +277,7 @@ class Cart * * @return void */ - public function destroy() + public function destroy(): void { $this->session->remove($this->instance); } @@ -287,7 +287,7 @@ class Cart * * @return \Illuminate\Support\Collection */ - public function content() + public function content(): Collection { if (is_null($this->session->get($this->instance))) { return new Collection([]); @@ -298,10 +298,8 @@ class Cart /** * Get the total quantity of all CartItems in the cart. - * - * @return int|float */ - public function count() + public function count(): int { return $this->getContent()->sum('qty'); } @@ -310,7 +308,7 @@ class Cart * Get the amount of CartItems in the Cart. * Keep in mind that this does NOT count quantity. */ - public function countItems() : int + public function countItems(): int { return $this->getContent()->count(); } @@ -318,7 +316,7 @@ class Cart /** * Get the total price of the items in the cart. */ - public function totalFloat() : float + public function totalFloat(): float { return $this->getContent()->reduce(function ($total, CartItem $cartItem) { return $total + $cartItem->total; @@ -328,7 +326,7 @@ class Cart /** * Get the total price of the items in the cart as formatted string. */ - public function total(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) : string + public function total(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string { return $this->numberFormat($this->totalFloat(), $decimals, $decimalPoint, $thousandSeperator); } @@ -336,7 +334,7 @@ class Cart /** * Get the total tax of the items in the cart. */ - public function taxFloat() : float + public function taxFloat(): float { return $this->getContent()->reduce(function ($tax, CartItem $cartItem) { return $tax + $cartItem->taxTotal; @@ -346,7 +344,7 @@ class Cart /** * Get the total tax of the items in the cart as formatted string. */ - public function tax(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) : string + public function tax(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string { return $this->numberFormat($this->taxFloat(), $decimals, $decimalPoint, $thousandSeperator); } @@ -354,7 +352,7 @@ class Cart /** * Get the subtotal (total - tax) of the items in the cart. */ - public function subtotalFloat() : float + public function subtotalFloat(): float { return $this->getContent()->reduce(function ($subTotal, CartItem $cartItem) { return $subTotal + $cartItem->subtotal; @@ -370,7 +368,7 @@ class Cart * * @return string */ - public function subtotal(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) : string + public function subtotal(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string { return $this->numberFormat($this->subtotalFloat(), $decimals, $decimalPoint, $thousandSeperator); } @@ -380,7 +378,7 @@ class Cart * * @return float */ - public function discountFloat() : float + public function discountFloat(): float { return $this->getContent()->reduce(function ($discount, CartItem $cartItem) { return $discount + $cartItem->discountTotal; @@ -390,7 +388,7 @@ class Cart /** * Get the discount of the items in the cart as formatted string. */ - public function discount(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) : string + public function discount(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string { return $this->numberFormat($this->discountFloat(), $decimals, $decimalPoint, $thousandSeperator); } @@ -398,7 +396,7 @@ class Cart /** * Get the price of the items in the cart (not rounded). */ - public function initialFloat() : float + public function initialFloat(): float { return $this->getContent()->reduce(function ($initial, CartItem $cartItem) { return $initial + ($cartItem->qty * $cartItem->price); @@ -408,7 +406,7 @@ class Cart /** * Get the price of the items in the cart as formatted string. */ - public function initial(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) : string + public function initial(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string { return $this->numberFormat($this->initialFloat(), $decimals, $decimalPoint, $thousandSeperator); } @@ -416,7 +414,7 @@ class Cart /** * Get the price of the items in the cart (previously rounded). */ - public function priceTotalFloat() : float + public function priceTotalFloat(): float { return $this->getContent()->reduce(function ($initial, CartItem $cartItem) { return $initial + $cartItem->priceTotal; @@ -426,7 +424,7 @@ class Cart /** * Get the price of the items in the cart as formatted string. */ - public function priceTotal(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) : string + public function priceTotal(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string { return $this->numberFormat($this->priceTotalFloat(), $decimals, $decimalPoint, $thousandSeperator); } @@ -434,7 +432,7 @@ class Cart /** * Get the total weight of the items in the cart. */ - public function weightFloat() : float + public function weightFloat(): float { return $this->getContent()->reduce(function ($total, CartItem $cartItem) { return $total + ($cartItem->qty * $cartItem->weight); @@ -450,7 +448,7 @@ class Cart * * @return string */ - public function weight(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) : string + public function weight(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string { return $this->numberFormat($this->weightFloat(), $decimals, $decimalPoint, $thousandSeperator); } @@ -462,7 +460,7 @@ class Cart * * @return \Illuminate\Support\Collection */ - public function search(Closure $search) : Collection + public function search(Closure $search): Collection { return $this->getContent()->filter($search); } @@ -475,7 +473,7 @@ class Cart * * @return void */ - public function associate(string $rowId, $model) + public function associate(string $rowId, $model): void { if (is_string($model) && !class_exists($model)) { throw new UnknownModelException("The supplied model {$model} does not exist."); @@ -500,7 +498,7 @@ class Cart * * @return void */ - public function setTax(string $rowId, $taxRate) + public function setTax(string $rowId, $taxRate): void { $cartItem = $this->get($rowId); @@ -519,7 +517,7 @@ class Cart * * @param float $discount */ - public function setGlobalTax($taxRate) + public function setGlobalTax($taxRate): void { $this->taxRate = $taxRate; @@ -539,7 +537,7 @@ class Cart * * @return void */ - public function setDiscount(string $rowId, $discount) + public function setDiscount(string $rowId, $discount): void { $cartItem = $this->get($rowId); @@ -560,7 +558,7 @@ class Cart * * @return void */ - public function setGlobalDiscount($discount) + public function setGlobalDiscount($discount): void { $this->discount = $discount; @@ -579,7 +577,7 @@ class Cart * * @return void */ - public function store($identifier) + public function store($identifier): void { $content = $this->getContent(); @@ -611,7 +609,7 @@ class Cart * * @return void */ - public function restore($identifier) + public function restore($identifier): void { if ($identifier instanceof InstanceIdentifier) { $identifier = $identifier->getInstanceIdentifier(); @@ -655,7 +653,7 @@ class Cart * * @return void */ - public function erase($identifier) + public function erase($identifier): void { if ($identifier instanceof InstanceIdentifier) { $identifier = $identifier->getInstanceIdentifier(); @@ -682,7 +680,7 @@ class Cart * * @return bool */ - public function merge($identifier, bool $keepDiscount = false, bool $keepTax = false, bool $dispatchAdd = true, $instance = self::DEFAULT_INSTANCE) + public function merge($identifier, bool $keepDiscount = false, bool $keepTax = false, bool $dispatchAdd = true, $instance = self::DEFAULT_INSTANCE): bool { if (!$this->storedCartInstanceWithIdentifierExists($instance, $identifier)) { return false; @@ -706,10 +704,8 @@ class Cart * Magic method to make accessing the total, tax and subtotal properties possible. * * @param string $attribute - * - * @return float|null */ - public function __get(string $attribute) + public function __get(string $attribute): ?Money { switch ($attribute) { case 'total': @@ -719,7 +715,7 @@ class Cart case 'subtotal': return $this->subtotal(); default: - return; + return null; } } @@ -728,7 +724,7 @@ class Cart * * @return \Illuminate\Support\Collection */ - protected function getContent() : Collection + protected function getContent(): Collection { if ($this->session->has($this->instance)) { return $this->session->get($this->instance); @@ -749,7 +745,7 @@ class Cart * * @return \Gloudemans\Shoppingcart\CartItem */ - private function createCartItem($id, ?string $name = null, int $qty, ?Money $price = null, int $weight = 0, array $options = []) : CartItem + private function createCartItem($id, ?string $name = null, int $qty, ?Money $price = null, int $weight = 0, array $options = []): CartItem { if ($id instanceof Buyable) { $cartItem = CartItem::fromBuyable($id, $qty ?: []); @@ -775,7 +771,7 @@ class Cart * * @return bool */ - private function isMulti($item) : bool + private function isMulti($item): bool { if (!is_array($item)) { return false; @@ -789,7 +785,7 @@ class Cart * * @return bool */ - private function storedCartInstanceWithIdentifierExists(string $instance, string $identifier) : bool + private function storedCartInstanceWithIdentifierExists(string $instance, string $identifier): bool { return $this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance'=> $instance])->exists(); } @@ -809,7 +805,7 @@ class Cart * * @return string */ - private function getTableName() : string + private function getTableName(): string { return config('cart.database.table', 'shoppingcart'); } @@ -819,7 +815,7 @@ class Cart * * @return string */ - private function getConnectionName() : string + private function getConnectionName(): string { $connection = config('cart.database.connection'); @@ -836,7 +832,7 @@ class Cart * * @return string */ - private function numberFormat($value, ?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) : string + private function numberFormat($value, ?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string { if (is_null($decimals)) { $decimals = config('cart.format.decimals', 2); @@ -858,7 +854,7 @@ class Cart * * @return \Carbon\Carbon|null */ - public function createdAt() : ?Carbon + public function createdAt(): ?Carbon { return $this->createdAt; } @@ -868,7 +864,7 @@ class Cart * * @return \Carbon\Carbon|null */ - public function updatedAt() : ?Carbon + public function updatedAt(): ?Carbon { return $this->updatedAt; } From ace1dc89f2103a52df24dc56531eeecab921cbbe Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 18 Jan 2022 01:27:36 +0100 Subject: [PATCH 014/156] Update InstanceIdentifier.php --- src/Contracts/InstanceIdentifier.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Contracts/InstanceIdentifier.php b/src/Contracts/InstanceIdentifier.php index d2acbcf..16662eb 100644 --- a/src/Contracts/InstanceIdentifier.php +++ b/src/Contracts/InstanceIdentifier.php @@ -18,5 +18,5 @@ interface InstanceIdentifier * * @return int|string */ - public function getInstanceGlobalDiscount(): Money; + public function getInstanceGlobalDiscount(): float; } From 4b1f78a5e9e8b69e4dfd35f6d590ed26dac805e4 Mon Sep 17 00:00:00 2001 From: Patrick Date: Tue, 18 Jan 2022 01:28:25 +0100 Subject: [PATCH 015/156] Update Identifiable.php --- tests/Fixtures/Identifiable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Fixtures/Identifiable.php b/tests/Fixtures/Identifiable.php index e2d498f..2c8100c 100644 --- a/tests/Fixtures/Identifiable.php +++ b/tests/Fixtures/Identifiable.php @@ -44,7 +44,7 @@ class Identifiable implements InstanceIdentifier * * @return int|string */ - public function getInstanceGlobalDiscount() + public function getInstanceGlobalDiscount(): float { return $this->discountRate; } From 06a828208b0521c06ebb855392deb733a7c21c19 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 3 Feb 2022 00:00:30 +0100 Subject: [PATCH 016/156] Update CartItem.php --- src/CartItem.php | 185 +---------------------------------------------- 1 file changed, 1 insertion(+), 184 deletions(-) diff --git a/src/CartItem.php b/src/CartItem.php index 3804e81..2b2ca7f 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -114,162 +114,6 @@ class CartItem implements Arrayable, Jsonable $this->rowId = $this->generateRowId($id, $options); } - /** - * Returns the formatted weight. - * - * @param int $decimals - * @param string $decimalPoint - * @param string $thousandSeperator - * - * @return string - */ - public function weight(int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) - { - return $this->numberFormat($this->weight, $decimals, $decimalPoint, $thousandSeperator); - } - - /** - * Returns the formatted price without TAX. - * - * @param int $decimals - * @param string $decimalPoint - * @param string $thousandSeperator - * - * @return string - */ - public function price(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) - { - return $this->numberFormat($this->price, $decimals, $decimalPoint, $thousandSeperator); - } - - /** - * Returns the formatted price with discount applied. - * - * @param int $decimals - * @param string $decimalPoint - * @param string $thousandSeperator - * - * @return string - */ - public function priceTarget(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) - { - return $this->numberFormat($this->priceTarget, $decimals, $decimalPoint, $thousandSeperator); - } - - /** - * Returns the formatted price with TAX. - * - * @param int $decimals - * @param string $decimalPoint - * @param string $thousandSeperator - * - * @return string - */ - public function priceTax(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) - { - return $this->numberFormat($this->priceTax, $decimals, $decimalPoint, $thousandSeperator); - } - - /** - * Returns the formatted subtotal. - * Subtotal is price for whole CartItem without TAX. - * - * @param int $decimals - * @param string $decimalPoint - * @param string $thousandSeperator - * - * @return string - */ - public function subtotal(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) - { - return $this->numberFormat($this->subtotal, $decimals, $decimalPoint, $thousandSeperator); - } - - /** - * Returns the formatted total. - * Total is price for whole CartItem with TAX. - * - * @param int $decimals - * @param string $decimalPoint - * @param string $thousandSeperator - * - * @return string - */ - public function total(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) - { - return $this->numberFormat($this->total, $decimals, $decimalPoint, $thousandSeperator); - } - - /** - * Returns the formatted tax. - * - * @param int $decimals - * @param string $decimalPoint - * @param string $thousandSeperator - * - * @return string - */ - public function tax(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) - { - return $this->numberFormat($this->tax, $decimals, $decimalPoint, $thousandSeperator); - } - - /** - * Returns the formatted tax. - * - * @param int $decimals - * @param string $decimalPoint - * @param string $thousandSeperator - * - * @return string - */ - public function taxTotal(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) - { - return $this->numberFormat($this->taxTotal, $decimals, $decimalPoint, $thousandSeperator); - } - - /** - * Returns the formatted discount. - * - * @param int $decimals - * @param string $decimalPoint - * @param string $thousandSeperator - * - * @return string - */ - public function discount(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) - { - return $this->numberFormat($this->discount, $decimals, $decimalPoint, $thousandSeperator); - } - - /** - * Returns the formatted total discount for this cart item. - * - * @param int $decimals - * @param string $decimalPoint - * @param string $thousandSeperator - * - * @return string - */ - public function discountTotal(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) - { - return $this->numberFormat($this->discountTotal, $decimals, $decimalPoint, $thousandSeperator); - } - - /** - * Returns the formatted total price for this cart item. - * - * @param int $decimals - * @param string $decimalPoint - * @param string $thousandSeperator - * - * @return string - */ - public function priceTotal(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null) - { - return $this->numberFormat($this->priceTotal, $decimals, $decimalPoint, $thousandSeperator); - } - /** * Set the quantity for this cart item. * @@ -493,33 +337,6 @@ class CartItem implements Arrayable, Jsonable { ksort($options); - return md5($id.serialize($options)); - } - - /** - * Get the formatted number. - * - * @param float $value - * @param int $decimals - * @param string $decimalPoint - * @param string $thousandSeperator - * - * @return string - */ - private function numberFormat($value, ?int $decimals, ?string $decimalPoint, ?string $thousandSeperator) : string - { - if (is_null($decimals)) { - $decimals = config('cart.format.decimals', 2); - } - - if (is_null($decimalPoint)) { - $decimalPoint = config('cart.format.decimal_point', '.'); - } - - if (is_null($thousandSeperator)) { - $thousandSeperator = config('cart.format.thousand_separator', ','); - } - - return number_format($value, $decimals, $decimalPoint, $thousandSeperator); + return md5($id . serialize($options)); } } From 5e6adedd7008453b57767b0630cbf3baa8fed215 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 3 Feb 2022 00:14:15 +0100 Subject: [PATCH 017/156] Update Cart.php --- src/Cart.php | 188 +++++++++++---------------------------------------- 1 file changed, 38 insertions(+), 150 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index bca8d8b..bda4149 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -3,6 +3,7 @@ namespace Gloudemans\Shoppingcart; use Carbon\Carbon; +use Money\Money; use Closure; use Gloudemans\Shoppingcart\Contracts\Buyable; use Gloudemans\Shoppingcart\Contracts\InstanceIdentifier; @@ -33,24 +34,18 @@ class Cart /** * Holds the current cart instance. - * - * @var string */ private string $instance; /** * Holds the creation date of the cart. - * - * @var mixed */ - private $createdAt; + private ?Carbon $createdAt = null; /** * Holds the update date of the cart. - * - * @var mixed */ - private $updatedAt; + private ?Carbon $updatedAt = null; /** * Defines the discount percentage. @@ -316,61 +311,31 @@ class Cart /** * Get the total price of the items in the cart. */ - public function totalFloat(): float + public function totalFloat(): Money { - return $this->getContent()->reduce(function ($total, CartItem $cartItem) { - return $total + $cartItem->total; - }, 0); + return $this->getContent()->reduce(function (Money $total, CartItem $cartItem) { + return $total->add($cartItem->total); + }, new Money()); } - - /** - * Get the total price of the items in the cart as formatted string. - */ - public function total(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string - { - return $this->numberFormat($this->totalFloat(), $decimals, $decimalPoint, $thousandSeperator); - } - + /** * Get the total tax of the items in the cart. */ public function taxFloat(): float { - return $this->getContent()->reduce(function ($tax, CartItem $cartItem) { - return $tax + $cartItem->taxTotal; - }, 0); - } - - /** - * Get the total tax of the items in the cart as formatted string. - */ - public function tax(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string - { - return $this->numberFormat($this->taxFloat(), $decimals, $decimalPoint, $thousandSeperator); + return $this->getContent()->reduce(function (Money $tax, CartItem $cartItem) { + return $tax->add($cartItem->taxTotal); + }, new Money()); } /** * Get the subtotal (total - tax) of the items in the cart. */ - public function subtotalFloat(): float + public function subtotal(): Money { - return $this->getContent()->reduce(function ($subTotal, CartItem $cartItem) { - return $subTotal + $cartItem->subtotal; - }, 0); - } - - /** - * Get the subtotal (total - tax) of the items in the cart as formatted string. - * - * @param int $decimals - * @param string $decimalPoint - * @param string $thousandSeperator - * - * @return string - */ - public function subtotal(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string - { - return $this->numberFormat($this->subtotalFloat(), $decimals, $decimalPoint, $thousandSeperator); + return $this->getContent()->reduce(function (Money $subTotal, CartItem $cartItem) { + return $subTotal->add($cartItem->subtotal); + }, new Money()); } /** @@ -378,19 +343,11 @@ class Cart * * @return float */ - public function discountFloat(): float + public function discount(): float { - return $this->getContent()->reduce(function ($discount, CartItem $cartItem) { - return $discount + $cartItem->discountTotal; - }, 0); - } - - /** - * Get the discount of the items in the cart as formatted string. - */ - public function discount(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string - { - return $this->numberFormat($this->discountFloat(), $decimals, $decimalPoint, $thousandSeperator); + return $this->getContent()->reduce(function (Money $discount, CartItem $cartItem) { + return $discount->add($cartItem->discountTotal); + }, new Money()); } /** @@ -398,61 +355,31 @@ class Cart */ public function initialFloat(): float { - return $this->getContent()->reduce(function ($initial, CartItem $cartItem) { - return $initial + ($cartItem->qty * $cartItem->price); - }, 0); - } - - /** - * Get the price of the items in the cart as formatted string. - */ - public function initial(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string - { - return $this->numberFormat($this->initialFloat(), $decimals, $decimalPoint, $thousandSeperator); + return $this->getContent()->reduce(function (Money $initial, CartItem $cartItem) { + return $initial->add($cartItem->price->multiply($cartItem->qty)); + }, new Money()); } /** * Get the price of the items in the cart (previously rounded). */ - public function priceTotalFloat(): float + public function priceTotal(): Money { - return $this->getContent()->reduce(function ($initial, CartItem $cartItem) { - return $initial + $cartItem->priceTotal; - }, 0); - } - - /** - * Get the price of the items in the cart as formatted string. - */ - public function priceTotal(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string - { - return $this->numberFormat($this->priceTotalFloat(), $decimals, $decimalPoint, $thousandSeperator); + return $this->getContent()->reduce(function (Money $initial, CartItem $cartItem) { + return $initial->add($cartItem->priceTotal) + }, new Money()); } /** * Get the total weight of the items in the cart. */ - public function weightFloat(): float + public function weight(): float { - return $this->getContent()->reduce(function ($total, CartItem $cartItem) { + return $this->getContent()->reduce(function (float $total, CartItem $cartItem) { return $total + ($cartItem->qty * $cartItem->weight); }, 0); } - /** - * Get the total weight of the items in the cart. - * - * @param int $decimals - * @param string $decimalPoint - * @param string $thousandSeperator - * - * @return string - */ - public function weight(?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string - { - return $this->numberFormat($this->weightFloat(), $decimals, $decimalPoint, $thousandSeperator); - } - /** * Search the cart content for a cart item matching the given search closure. * @@ -591,7 +518,7 @@ class Cart throw new CartAlreadyStoredException("A cart with identifier {$identifier} was already stored."); } - $this->getConnection()->table($this->getTableName())->insert([ + $this->getConnection()->table(self::getTableName())->insert([ 'identifier' => $identifier, 'instance' => $instance, 'content' => serialize($content), @@ -621,7 +548,7 @@ class Cart return; } - $stored = $this->getConnection()->table($this->getTableName()) + $stored = $this->getConnection()->table(self::getTableName()) ->where(['identifier'=> $identifier, 'instance' => $currentInstance])->first(); $storedContent = unserialize(data_get($stored, 'content')); @@ -643,7 +570,7 @@ class Cart $this->createdAt = Carbon::parse(data_get($stored, 'created_at')); $this->updatedAt = Carbon::parse(data_get($stored, 'updated_at')); - $this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance' => $currentInstance])->delete(); + $this->getConnection()->table(self::getTableName())->where(['identifier' => $identifier, 'instance' => $currentInstance])->delete(); } /** @@ -665,7 +592,7 @@ class Cart return; } - $this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance' => $instance])->delete(); + $this->getConnection()->table(self::getTableName())->where(['identifier' => $identifier, 'instance' => $instance])->delete(); $this->events->dispatch('cart.erased'); } @@ -686,7 +613,7 @@ class Cart return false; } - $stored = $this->getConnection()->table($this->getTableName()) + $stored = $this->getConnection()->table(self::getTableName()) ->where(['identifier'=> $identifier, 'instance'=> $instance])->first(); $storedContent = unserialize($stored->content); @@ -721,8 +648,6 @@ class Cart /** * Get the carts content, if there is no cart content set yet, return a new empty Collection. - * - * @return \Illuminate\Support\Collection */ protected function getContent(): Collection { @@ -742,8 +667,6 @@ class Cart * @param float $price * @param float $weight * @param array $options - * - * @return \Gloudemans\Shoppingcart\CartItem */ private function createCartItem($id, ?string $name = null, int $qty, ?Money $price = null, int $weight = 0, array $options = []): CartItem { @@ -768,12 +691,10 @@ class Cart * Check if the item is a multidimensional array or an array of Buyables. * * @param mixed $item - * - * @return bool */ private function isMulti($item): bool { - if (!is_array($item)) { + if (! is_array($item)) { return false; } @@ -782,20 +703,16 @@ class Cart /** * @param $identifier - * - * @return bool */ private function storedCartInstanceWithIdentifierExists(string $instance, string $identifier): bool { - return $this->getConnection()->table($this->getTableName())->where(['identifier' => $identifier, 'instance'=> $instance])->exists(); + return $this->getConnection()->table(self::getTableName())->where(['identifier' => $identifier, 'instance'=> $instance])->exists(); } /** * Get the database connection. - * - * @return \Illuminate\Database\Connection */ - private function getConnection() + private function getConnection(): \Illuminate\Database\Connection { return app(DatabaseManager::class)->connection($this->getConnectionName()); } @@ -805,50 +722,21 @@ class Cart * * @return string */ - private function getTableName(): string + private static function getTableName(): string { return config('cart.database.table', 'shoppingcart'); } /** * Get the database connection name. - * - * @return string */ - private function getConnectionName(): string + private function getConnectionName(): ?string { $connection = config('cart.database.connection'); return is_null($connection) ? config('database.default') : $connection; } - /** - * Get the Formatted number. - * - * @param $value - * @param $decimals - * @param $decimalPoint - * @param $thousandSeperator - * - * @return string - */ - private function numberFormat($value, ?int $decimals = null, ?string $decimalPoint = null, ?string $thousandSeperator = null): string - { - if (is_null($decimals)) { - $decimals = config('cart.format.decimals', 2); - } - - if (is_null($decimalPoint)) { - $decimalPoint = config('cart.format.decimal_point', '.'); - } - - if (is_null($thousandSeperator)) { - $thousandSeperator = config('cart.format.thousand_separator', ','); - } - - return number_format($value, $decimals, $decimalPoint, $thousandSeperator); - } - /** * Get the creation date of the cart (db context). * From 1b866e50e65b0527a3fc10ac4d39f7d87a9bff37 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 3 Feb 2022 00:18:36 +0100 Subject: [PATCH 018/156] Update Cart.php --- src/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cart.php b/src/Cart.php index bda4149..aae56b5 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -366,7 +366,7 @@ class Cart public function priceTotal(): Money { return $this->getContent()->reduce(function (Money $initial, CartItem $cartItem) { - return $initial->add($cartItem->priceTotal) + return $initial->add($cartItem->priceTotal); }, new Money()); } From ad95835db0faacecc4ccc1a035af27c0b8f091ee Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 3 Feb 2022 00:22:07 +0100 Subject: [PATCH 019/156] Update Buyable.php --- src/Contracts/Buyable.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Contracts/Buyable.php b/src/Contracts/Buyable.php index a6baff5..65e4e3d 100644 --- a/src/Contracts/Buyable.php +++ b/src/Contracts/Buyable.php @@ -2,6 +2,8 @@ namespace Gloudemans\Shoppingcart\Contracts; +use Money\Money; + interface Buyable { /** @@ -18,15 +20,11 @@ interface Buyable /** * Get the price of the Buyable item. - * - * @return float */ - public function getBuyablePrice(); + public function getBuyablePrice(): Money; /** * Get the weight of the Buyable item. - * - * @return float */ - public function getBuyableWeight(); + public function getBuyableWeight(): int; } From aa125ddc87deba7b6338cef31ad60f0df18d9e9e Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 3 Feb 2022 00:22:57 +0100 Subject: [PATCH 020/156] Update InstanceIdentifier.php --- src/Contracts/InstanceIdentifier.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Contracts/InstanceIdentifier.php b/src/Contracts/InstanceIdentifier.php index 16662eb..9c82ab3 100644 --- a/src/Contracts/InstanceIdentifier.php +++ b/src/Contracts/InstanceIdentifier.php @@ -2,8 +2,6 @@ namespace Gloudemans\Shoppingcart\Contracts; -use Money\Money; - interface InstanceIdentifier { /** @@ -15,8 +13,6 @@ interface InstanceIdentifier /** * Get the unique identifier to load the Cart from. - * - * @return int|string */ public function getInstanceGlobalDiscount(): float; } From 019bdb4e5c2f7d13828cbfbd6eff77365f8001ab Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 3 Feb 2022 00:23:37 +0100 Subject: [PATCH 021/156] Update ShoppingcartServiceProvider.php --- src/ShoppingcartServiceProvider.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/ShoppingcartServiceProvider.php b/src/ShoppingcartServiceProvider.php index 2e6b1b5..b5c6fda 100644 --- a/src/ShoppingcartServiceProvider.php +++ b/src/ShoppingcartServiceProvider.php @@ -22,12 +22,6 @@ class ShoppingcartServiceProvider extends ServiceProvider $this->publishes([__DIR__.'/Config/cart.php' => config_path('cart.php')], 'config'); - $this->app['events']->listen(Logout::class, function () { - if ($this->app['config']->get('cart.destroy_on_logout')) { - $this->app->make(SessionManager::class)->forget('cart'); - } - }); - $this->publishes([ realpath(__DIR__.'/Database/migrations') => $this->app->databasePath().'/migrations', ], 'migrations'); From a816a21a278bd5637177bbad064f0ec806ff28e4 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 3 Feb 2022 00:23:49 +0100 Subject: [PATCH 022/156] Update cart.php --- src/Config/cart.php | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Config/cart.php b/src/Config/cart.php index bfc5c43..1f06222 100644 --- a/src/Config/cart.php +++ b/src/Config/cart.php @@ -58,18 +58,6 @@ return [ ], - /* - |-------------------------------------------------------------------------- - | Destroy the cart on user logout - |-------------------------------------------------------------------------- - | - | When this option is set to 'true' the cart will automatically - | destroy all cart instances when the user logs out. - | - */ - - 'destroy_on_logout' => false, - /* |-------------------------------------------------------------------------- | Default number format From bc91ea1976a5acc8205dedc68a22c2d980502630 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 3 Feb 2022 00:24:57 +0100 Subject: [PATCH 023/156] Update CanBeBought.php --- src/CanBeBought.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/CanBeBought.php b/src/CanBeBought.php index f11e71a..093f927 100644 --- a/src/CanBeBought.php +++ b/src/CanBeBought.php @@ -2,6 +2,8 @@ namespace Gloudemans\Shoppingcart; +use Money\Money; + trait CanBeBought { /** @@ -16,8 +18,6 @@ trait CanBeBought /** * Get the name, title or description of the Buyable item. - * - * @return string */ public function getBuyableDescription(): ?string { @@ -34,10 +34,8 @@ trait CanBeBought /** * Get the price of the Buyable item. - * - * @return float */ - public function getBuyablePrice() + public function getBuyablePrice(): Money { if (($price = $this->getAttribute('price'))) { return $price; @@ -46,10 +44,8 @@ trait CanBeBought /** * Get the weight of the Buyable item. - * - * @return float */ - public function getBuyableWeight() + public function getBuyableWeight(): float { if (($weight = $this->getAttribute('weight'))) { return $weight; From 679578d4e259a6ebdcc6592b129fd8cabc1a18e4 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 3 Feb 2022 00:32:37 +0100 Subject: [PATCH 024/156] Create 2022_02_03_000000_remove_content_from_shoppingcart_table.php --- ...remove_content_from_shoppingcart_table.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/Database/migrations/2022_02_03_000000_remove_content_from_shoppingcart_table.php diff --git a/src/Database/migrations/2022_02_03_000000_remove_content_from_shoppingcart_table.php b/src/Database/migrations/2022_02_03_000000_remove_content_from_shoppingcart_table.php new file mode 100644 index 0000000..c926142 --- /dev/null +++ b/src/Database/migrations/2022_02_03_000000_remove_content_from_shoppingcart_table.php @@ -0,0 +1,27 @@ +dropColumn('content'); + }); + } + + /** + * Reverse the migrations. + */ + public function down() + { + Schema::table(config('cart.database.table'), function (Blueprint $table) { + $table->longText('content'); + }); + } From b018ce007fb550c520587b683947f27dad275a02 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 3 Feb 2022 00:33:27 +0100 Subject: [PATCH 025/156] Delete 2022_02_03_000000_remove_content_from_shoppingcart_table.php --- ...remove_content_from_shoppingcart_table.php | 27 ------------------- 1 file changed, 27 deletions(-) delete mode 100644 src/Database/migrations/2022_02_03_000000_remove_content_from_shoppingcart_table.php diff --git a/src/Database/migrations/2022_02_03_000000_remove_content_from_shoppingcart_table.php b/src/Database/migrations/2022_02_03_000000_remove_content_from_shoppingcart_table.php deleted file mode 100644 index c926142..0000000 --- a/src/Database/migrations/2022_02_03_000000_remove_content_from_shoppingcart_table.php +++ /dev/null @@ -1,27 +0,0 @@ -dropColumn('content'); - }); - } - - /** - * Reverse the migrations. - */ - public function down() - { - Schema::table(config('cart.database.table'), function (Blueprint $table) { - $table->longText('content'); - }); - } From 036b6a8fbefd05daae81b459dae9866060e50727 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 3 Feb 2022 00:34:42 +0100 Subject: [PATCH 026/156] Update BuyableProduct.php --- tests/Fixtures/BuyableProduct.php | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/tests/Fixtures/BuyableProduct.php b/tests/Fixtures/BuyableProduct.php index 576a9cb..e2db9f6 100644 --- a/tests/Fixtures/BuyableProduct.php +++ b/tests/Fixtures/BuyableProduct.php @@ -4,6 +4,7 @@ namespace Gloudemans\Tests\Shoppingcart\Fixtures; use Gloudemans\Shoppingcart\Contracts\Buyable; use Illuminate\Database\Eloquent\Model; +use Money\Money; class BuyableProduct extends Model implements Buyable { @@ -50,20 +51,16 @@ class BuyableProduct extends Model implements Buyable /** * Get the price of the Buyable item. - * - * @return float */ - public function getBuyablePrice() + public function getBuyablePrice(): Money { - return $this->price; + return new Money($this->price); } /** * Get the price of the Buyable item. - * - * @return float */ - public function getBuyableWeight() + public function getBuyableWeight(): float { return $this->weight; } From 5024b485546455ae1c4befb3058278fc9aa58d83 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 3 Feb 2022 00:35:38 +0100 Subject: [PATCH 027/156] Update CanBeBought.php --- src/CanBeBought.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CanBeBought.php b/src/CanBeBought.php index 093f927..a3abba6 100644 --- a/src/CanBeBought.php +++ b/src/CanBeBought.php @@ -38,7 +38,7 @@ trait CanBeBought public function getBuyablePrice(): Money { if (($price = $this->getAttribute('price'))) { - return $price; + return new Money($price); } } From cbcc8efaa05a388a3821a04ecd3835d17e859510 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 3 Feb 2022 00:38:05 +0100 Subject: [PATCH 028/156] Update BuyableProduct.php --- tests/Fixtures/BuyableProduct.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Fixtures/BuyableProduct.php b/tests/Fixtures/BuyableProduct.php index e2db9f6..800ba1a 100644 --- a/tests/Fixtures/BuyableProduct.php +++ b/tests/Fixtures/BuyableProduct.php @@ -60,7 +60,7 @@ class BuyableProduct extends Model implements Buyable /** * Get the price of the Buyable item. */ - public function getBuyableWeight(): float + public function getBuyableWeight(): int { return $this->weight; } From 1cba3e7beb4bafbbeca7fbc7cbb68444a8205755 Mon Sep 17 00:00:00 2001 From: Patrick Date: Thu, 3 Feb 2022 00:39:17 +0100 Subject: [PATCH 029/156] Update CanBeBought.php --- src/CanBeBought.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CanBeBought.php b/src/CanBeBought.php index a3abba6..a5cbd44 100644 --- a/src/CanBeBought.php +++ b/src/CanBeBought.php @@ -45,7 +45,7 @@ trait CanBeBought /** * Get the weight of the Buyable item. */ - public function getBuyableWeight(): float + public function getBuyableWeight(): int { if (($weight = $this->getAttribute('weight'))) { return $weight; From 1f0df693a33ed9d25becb406a91fc128b632ca99 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 17:30:30 +0100 Subject: [PATCH 030/156] Update CartItemTest.php --- tests/CartItemTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index 04ddf19..c344f8b 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -5,6 +5,7 @@ namespace Gloudemans\Tests\Shoppingcart; use Gloudemans\Shoppingcart\CartItem; use Gloudemans\Shoppingcart\ShoppingcartServiceProvider; use Orchestra\Testbench\TestCase; +use Money\Money; class CartItemTest extends TestCase { @@ -23,7 +24,7 @@ class CartItemTest extends TestCase /** @test */ public function it_can_be_cast_to_an_array() { - $cartItem = new CartItem(1, 'Some item', 10.00, 550, ['size' => 'XL', 'color' => 'red']); + $cartItem = new CartItem(1, 'Some item', new Money(10.00), 550, ['size' => 'XL', 'color' => 'red']); $cartItem->setQuantity(2); $this->assertEquals([ @@ -46,7 +47,7 @@ class CartItemTest extends TestCase /** @test */ public function it_can_be_cast_to_json() { - $cartItem = new CartItem(1, 'Some item', 10.00, 550, ['size' => 'XL', 'color' => 'red']); + $cartItem = new CartItem(1, 'Some item', new Money(10.00), 550, ['size' => 'XL', 'color' => 'red']); $cartItem->setQuantity(2); $this->assertJson($cartItem->toJson()); @@ -59,7 +60,7 @@ class CartItemTest extends TestCase /** @test */ public function it_formats_price_total_correctly() { - $cartItem = new CartItem(1, 'Some item', 10.00, 550, ['size' => 'XL', 'color' => 'red']); + $cartItem = new CartItem(1, 'Some item', new Money(10.00), 550, ['size' => 'XL', 'color' => 'red']); $cartItem->setQuantity(2); $this->assertSame('20.00', $cartItem->priceTotal()); From ff2b124e6b2d7785ef28e3c2d585d8101ddae224 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 17:35:48 +0100 Subject: [PATCH 031/156] Update CanBeBought.php --- src/CanBeBought.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CanBeBought.php b/src/CanBeBought.php index a5cbd44..cb529d0 100644 --- a/src/CanBeBought.php +++ b/src/CanBeBought.php @@ -37,8 +37,8 @@ trait CanBeBought */ public function getBuyablePrice(): Money { - if (($price = $this->getAttribute('price'))) { - return new Money($price); + if (($price = $this->getAttribute('price')) && ($currency = $this->getAttribute('currency'))) { + return new Money($price, $currency); } } From a4f1b8cb9f1f0f7c66a09dd59b5d0bc8854181cb Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 17:36:34 +0100 Subject: [PATCH 032/156] Update BuyableProduct.php --- tests/Fixtures/BuyableProduct.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/Fixtures/BuyableProduct.php b/tests/Fixtures/BuyableProduct.php index 800ba1a..435219b 100644 --- a/tests/Fixtures/BuyableProduct.php +++ b/tests/Fixtures/BuyableProduct.php @@ -23,10 +23,11 @@ class BuyableProduct extends Model implements Buyable ]; protected $attributes = [ - 'id' => 1, - 'name' => 'Item name', - 'price' => 10.00, - 'weight' => 0, + 'id' => 1, + 'name' => 'Item name', + 'price' => 10.00, + 'currency' => 'USD', + 'weight' => 0, ]; /** From 5b4d779182743a3219a8bc6352943cef38bd99ef Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 17:36:45 +0100 Subject: [PATCH 033/156] Update BuyableProduct.php --- tests/Fixtures/BuyableProduct.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Fixtures/BuyableProduct.php b/tests/Fixtures/BuyableProduct.php index 435219b..fc317e5 100644 --- a/tests/Fixtures/BuyableProduct.php +++ b/tests/Fixtures/BuyableProduct.php @@ -55,7 +55,7 @@ class BuyableProduct extends Model implements Buyable */ public function getBuyablePrice(): Money { - return new Money($this->price); + return new Money($this->price, $this->currency); } /** From a6a0b1f802e5d8b060cb3c6208ea9cc3a7ea9ecf Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 17:37:07 +0100 Subject: [PATCH 034/156] Update BuyableProductTrait.php --- tests/Fixtures/BuyableProductTrait.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/Fixtures/BuyableProductTrait.php b/tests/Fixtures/BuyableProductTrait.php index 29e39f3..01dc681 100644 --- a/tests/Fixtures/BuyableProductTrait.php +++ b/tests/Fixtures/BuyableProductTrait.php @@ -24,9 +24,10 @@ class BuyableProductTrait extends Model implements Buyable ]; protected $attributes = [ - 'id' => 1, - 'name' => 'Item name', - 'price' => 10.00, - 'weight' => 0, + 'id' => 1, + 'name' => 'Item name', + 'price' => 10.00, + 'currency' => 'USD', + 'weight' => 0, ]; } From b2a6bcc56843ab051ffc43cd3fd71dedd4074644 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 17:37:19 +0100 Subject: [PATCH 035/156] Update BuyableProductTrait.php --- tests/Fixtures/BuyableProductTrait.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Fixtures/BuyableProductTrait.php b/tests/Fixtures/BuyableProductTrait.php index 01dc681..d9d3572 100644 --- a/tests/Fixtures/BuyableProductTrait.php +++ b/tests/Fixtures/BuyableProductTrait.php @@ -20,6 +20,7 @@ class BuyableProductTrait extends Model implements Buyable 'title', 'description', 'price', + 'currency', 'weight', ]; From 1ce9babb61c71de398e53fb8b5713a962e2e55a6 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 17:37:37 +0100 Subject: [PATCH 036/156] Update BuyableProduct.php --- tests/Fixtures/BuyableProduct.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Fixtures/BuyableProduct.php b/tests/Fixtures/BuyableProduct.php index fc317e5..b28699c 100644 --- a/tests/Fixtures/BuyableProduct.php +++ b/tests/Fixtures/BuyableProduct.php @@ -19,6 +19,7 @@ class BuyableProduct extends Model implements Buyable 'title', 'description', 'price', + 'currency', 'weight', ]; From 8df3c393b272393c9e56e5d09129dfc88109266b Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 17:40:16 +0100 Subject: [PATCH 037/156] Update CartItemTest.php --- tests/CartItemTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index c344f8b..e9dc32b 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -24,7 +24,7 @@ class CartItemTest extends TestCase /** @test */ public function it_can_be_cast_to_an_array() { - $cartItem = new CartItem(1, 'Some item', new Money(10.00), 550, ['size' => 'XL', 'color' => 'red']); + $cartItem = new CartItem(1, 'Some item', new Money(10.00, 'USD'), 550, ['size' => 'XL', 'color' => 'red']); $cartItem->setQuantity(2); $this->assertEquals([ @@ -47,7 +47,7 @@ class CartItemTest extends TestCase /** @test */ public function it_can_be_cast_to_json() { - $cartItem = new CartItem(1, 'Some item', new Money(10.00), 550, ['size' => 'XL', 'color' => 'red']); + $cartItem = new CartItem(1, 'Some item', new Money(10.00, 'USD'), 550, ['size' => 'XL', 'color' => 'red']); $cartItem->setQuantity(2); $this->assertJson($cartItem->toJson()); @@ -60,7 +60,7 @@ class CartItemTest extends TestCase /** @test */ public function it_formats_price_total_correctly() { - $cartItem = new CartItem(1, 'Some item', new Money(10.00), 550, ['size' => 'XL', 'color' => 'red']); + $cartItem = new CartItem(1, 'Some item', new Money(10.00, 'USD'), 550, ['size' => 'XL', 'color' => 'red']); $cartItem->setQuantity(2); $this->assertSame('20.00', $cartItem->priceTotal()); From bfc2cccfa9e266083b5a4f6a4f2585f8fb94d6f7 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 17:47:14 +0100 Subject: [PATCH 038/156] Update CanBeBought.php --- src/CanBeBought.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/CanBeBought.php b/src/CanBeBought.php index cb529d0..b2138cc 100644 --- a/src/CanBeBought.php +++ b/src/CanBeBought.php @@ -3,6 +3,7 @@ namespace Gloudemans\Shoppingcart; use Money\Money; +use Money\Currency; trait CanBeBought { @@ -38,7 +39,7 @@ trait CanBeBought public function getBuyablePrice(): Money { if (($price = $this->getAttribute('price')) && ($currency = $this->getAttribute('currency'))) { - return new Money($price, $currency); + return new Money($price, new Currency($currency)); } } From 89e4c8d0ef0c83cb8884d05de68e1067fb1cbfb0 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 17:48:12 +0100 Subject: [PATCH 039/156] Update BuyableProduct.php --- tests/Fixtures/BuyableProduct.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Fixtures/BuyableProduct.php b/tests/Fixtures/BuyableProduct.php index b28699c..513c215 100644 --- a/tests/Fixtures/BuyableProduct.php +++ b/tests/Fixtures/BuyableProduct.php @@ -5,6 +5,7 @@ namespace Gloudemans\Tests\Shoppingcart\Fixtures; use Gloudemans\Shoppingcart\Contracts\Buyable; use Illuminate\Database\Eloquent\Model; use Money\Money; +use Money\Currency; class BuyableProduct extends Model implements Buyable { @@ -56,7 +57,7 @@ class BuyableProduct extends Model implements Buyable */ public function getBuyablePrice(): Money { - return new Money($this->price, $this->currency); + return new Money($this->price, new Currency($this->currency)); } /** From fdc228832f41675845993147cf2b7e6cdbadbc30 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 17:48:53 +0100 Subject: [PATCH 040/156] Update CartItemTest.php --- tests/CartItemTest.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index e9dc32b..ff82821 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -6,6 +6,7 @@ use Gloudemans\Shoppingcart\CartItem; use Gloudemans\Shoppingcart\ShoppingcartServiceProvider; use Orchestra\Testbench\TestCase; use Money\Money; +use Money\Currency; class CartItemTest extends TestCase { @@ -24,7 +25,7 @@ class CartItemTest extends TestCase /** @test */ public function it_can_be_cast_to_an_array() { - $cartItem = new CartItem(1, 'Some item', new Money(10.00, 'USD'), 550, ['size' => 'XL', 'color' => 'red']); + $cartItem = new CartItem(1, 'Some item', new Money(10.00, new Currency('USD')), 550, ['size' => 'XL', 'color' => 'red']); $cartItem->setQuantity(2); $this->assertEquals([ @@ -47,7 +48,7 @@ class CartItemTest extends TestCase /** @test */ public function it_can_be_cast_to_json() { - $cartItem = new CartItem(1, 'Some item', new Money(10.00, 'USD'), 550, ['size' => 'XL', 'color' => 'red']); + $cartItem = new CartItem(1, 'Some item', new Money(10.00, new Currency('USD')), 550, ['size' => 'XL', 'color' => 'red']); $cartItem->setQuantity(2); $this->assertJson($cartItem->toJson()); @@ -60,7 +61,7 @@ class CartItemTest extends TestCase /** @test */ public function it_formats_price_total_correctly() { - $cartItem = new CartItem(1, 'Some item', new Money(10.00, 'USD'), 550, ['size' => 'XL', 'color' => 'red']); + $cartItem = new CartItem(1, 'Some item', new Money(10.00, new Currency('USD')), 550, ['size' => 'XL', 'color' => 'red']); $cartItem->setQuantity(2); $this->assertSame('20.00', $cartItem->priceTotal()); From 22f7d62c36a9fc5089958bd3ac485b7e3ac55729 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 17:51:10 +0100 Subject: [PATCH 041/156] Update CartItemTest.php --- tests/CartItemTest.php | 9 --------- 1 file changed, 9 deletions(-) diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index ff82821..9bfd693 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -57,13 +57,4 @@ class CartItemTest extends TestCase $this->assertEquals($json, $cartItem->toJson()); } - - /** @test */ - public function it_formats_price_total_correctly() - { - $cartItem = new CartItem(1, 'Some item', new Money(10.00, new Currency('USD')), 550, ['size' => 'XL', 'color' => 'red']); - $cartItem->setQuantity(2); - - $this->assertSame('20.00', $cartItem->priceTotal()); - } } From 321f41dbfdbe0725f42ebdbe620cd9c5348c589e Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 17:53:34 +0100 Subject: [PATCH 042/156] Update Cart.php --- src/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cart.php b/src/Cart.php index aae56b5..54e2d32 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -117,7 +117,7 @@ class Cart * * @return \Gloudemans\Shoppingcart\CartItem */ - public function add($id, ?string $name = null, $qty = null, ?Money $price = null, $weight = 0, array $options = []): CartItem + public function add($id, ?string $name = null, $qty = 1, ?Money $price = null, $weight = 0, array $options = []): CartItem { if ($this->isMulti($id)) { return array_map(function ($item) { From 25c5841fc1582fe6bbf9228a3d6fc700d495d518 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 17:55:32 +0100 Subject: [PATCH 043/156] Update Cart.php --- src/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cart.php b/src/Cart.php index 54e2d32..0bd4bf2 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -117,7 +117,7 @@ class Cart * * @return \Gloudemans\Shoppingcart\CartItem */ - public function add($id, ?string $name = null, $qty = 1, ?Money $price = null, $weight = 0, array $options = []): CartItem + public function add($id, ?string $name = null, ?int $qty = null, ?Money $price = null, ?int $weight = null, array $options = []): CartItem { if ($this->isMulti($id)) { return array_map(function ($item) { From 1b3140589b56614bb82c7edec82658f8b99fb449 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 17:59:07 +0100 Subject: [PATCH 044/156] Update CartItem.php --- src/CartItem.php | 49 +++++------------------------------------------- 1 file changed, 5 insertions(+), 44 deletions(-) diff --git a/src/CartItem.php b/src/CartItem.php index 2b2ca7f..8e4e7e7 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -130,12 +130,8 @@ class CartItem implements Arrayable, Jsonable /** * Update the cart item from a Buyable. - * - * @param \Gloudemans\Shoppingcart\Contracts\Buyable $item - * - * @return void */ - public function updateFromBuyable(Buyable $item) + public function updateFromBuyable(Buyable $item): void { $this->id = $item->getBuyableIdentifier($this->options); $this->name = $item->getBuyableDescription($this->options); @@ -144,12 +140,8 @@ class CartItem implements Arrayable, Jsonable /** * Update the cart item from an array. - * - * @param array $attributes - * - * @return void */ - public function updateFromArray(array $attributes) + public function updateFromArray(array $attributes): void { $this->id = Arr::get($attributes, 'id', $this->id); $this->qty = Arr::get($attributes, 'qty', $this->qty); @@ -165,8 +157,6 @@ class CartItem implements Arrayable, Jsonable * Associate the cart item with the given model. * * @param mixed $model - * - * @return \Gloudemans\Shoppingcart\CartItem */ public function associate($model) : self { @@ -177,12 +167,8 @@ class CartItem implements Arrayable, Jsonable /** * Set the tax rate. - * - * @param int|float $taxRate - * - * @return \Gloudemans\Shoppingcart\CartItem */ - public function setTaxRate($taxRate) : self + public function setTaxRate(float $taxRate) : self { $this->taxRate = $taxRate; @@ -201,10 +187,6 @@ class CartItem implements Arrayable, Jsonable /** * Set cart instance. - * - * @param null|string $instance - * - * @return \Gloudemans\Shoppingcart\CartItem */ public function setInstance(?string $instance) : self { @@ -216,11 +198,9 @@ class CartItem implements Arrayable, Jsonable /** * Get an attribute from the cart item or get the associated model. * - * @param string $attribute - * * @return mixed */ - public function __get($attribute) + public function __get(string $attribute) { if (property_exists($this, $attribute)) { return $this->{$attribute}; @@ -252,11 +232,6 @@ class CartItem implements Arrayable, Jsonable /** * Create a new instance from a Buyable. - * - * @param \Gloudemans\Shoppingcart\Contracts\Buyable $item - * @param array $options - * - * @return \Gloudemans\Shoppingcart\CartItem */ public static function fromBuyable(Buyable $item, array $options = []) : self { @@ -265,10 +240,6 @@ class CartItem implements Arrayable, Jsonable /** * Create a new instance from the given array. - * - * @param array $attributes - * - * @return \Gloudemans\Shoppingcart\CartItem */ public static function fromArray(array $attributes) : self { @@ -281,13 +252,8 @@ class CartItem implements Arrayable, Jsonable * Create a new instance from the given attributes. * * @param int|string $id - * @param string $name - * @param float $price - * @param array $options - * - * @return \Gloudemans\Shoppingcart\CartItem */ - public static function fromAttributes($id, string $name, Money $price, $weight, array $options = []) : self + public static function fromAttributes($id, string $name, Money $price, int $weight, array $options = []) : self { return new self($id, $name, $price, $weight, $options); } @@ -327,11 +293,6 @@ class CartItem implements Arrayable, Jsonable /** * Generate a unique id for the cart item. - * - * @param string $id - * @param array $options - * - * @return string */ protected function generateRowId(string $id, array $options) : string { From b1c9c522919f3d319e554df7434e4c0083515cc7 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 18:02:24 +0100 Subject: [PATCH 045/156] Update CartItem.php --- src/CartItem.php | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/src/CartItem.php b/src/CartItem.php index 8e4e7e7..938f79c 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -40,10 +40,8 @@ class CartItem implements Arrayable, Jsonable /** * The quantity for this cart item. - * - * @var int|float */ - public $qty; + public int $qty; /** * The name of the cart item. @@ -116,15 +114,9 @@ class CartItem implements Arrayable, Jsonable /** * Set the quantity for this cart item. - * - * @param int|float $qty */ - public function setQuantity($qty) + public function setQuantity(int $qty) { - if (empty($qty) || !is_numeric($qty)) { - throw new \InvalidArgumentException('Please supply a valid quantity.'); - } - $this->qty = $qty; } @@ -233,7 +225,7 @@ class CartItem implements Arrayable, Jsonable /** * Create a new instance from a Buyable. */ - public static function fromBuyable(Buyable $item, array $options = []) : self + public static function fromBuyable(Buyable $item, CartItemOptions $options = new CartItemOptions([])) : self { return new self($item->getBuyableIdentifier($options), $item->getBuyableDescription($options), $item->getBuyablePrice($options), $item->getBuyableWeight($options), $options); } From f7779e0797cf1b9bb48b0e7f68dd19da555cc4d5 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 18:03:14 +0100 Subject: [PATCH 046/156] Update Buyable.php --- src/Contracts/Buyable.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Contracts/Buyable.php b/src/Contracts/Buyable.php index 65e4e3d..675e378 100644 --- a/src/Contracts/Buyable.php +++ b/src/Contracts/Buyable.php @@ -2,6 +2,7 @@ namespace Gloudemans\Shoppingcart\Contracts; +use Gloudemans\Shoppingcart\CartItemOptions; use Money\Money; interface Buyable @@ -11,20 +12,20 @@ interface Buyable * * @return int|string */ - public function getBuyableIdentifier(); + public function getBuyableIdentifier(CartItemOptions $options); /** * Get the description or title of the Buyable item. */ - public function getBuyableDescription(): ?string; + public function getBuyableDescription(CartItemOptions $options): ?string; /** * Get the price of the Buyable item. */ - public function getBuyablePrice(): Money; + public function getBuyablePrice(CartItemOptions $options): Money; /** * Get the weight of the Buyable item. */ - public function getBuyableWeight(): int; + public function getBuyableWeight(CartItemOptions $options): int; } From 3b3f17b301adb90dfd7bdef9cad93e571a311f6a Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 18:04:14 +0100 Subject: [PATCH 047/156] Update BuyableProduct.php --- tests/Fixtures/BuyableProduct.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/Fixtures/BuyableProduct.php b/tests/Fixtures/BuyableProduct.php index 513c215..ae7cedc 100644 --- a/tests/Fixtures/BuyableProduct.php +++ b/tests/Fixtures/BuyableProduct.php @@ -3,6 +3,7 @@ namespace Gloudemans\Tests\Shoppingcart\Fixtures; use Gloudemans\Shoppingcart\Contracts\Buyable; +use Gloudemans\Shoppingcart\CartItemOptions; use Illuminate\Database\Eloquent\Model; use Money\Money; use Money\Currency; @@ -37,7 +38,7 @@ class BuyableProduct extends Model implements Buyable * * @return int|string */ - public function getBuyableIdentifier() + public function getBuyableIdentifier(CartItemOptions $options) { return $this->id; } @@ -47,7 +48,7 @@ class BuyableProduct extends Model implements Buyable * * @return string */ - public function getBuyableDescription() : ?string + public function getBuyableDescription(CartItemOptions $options) : ?string { return $this->name; } @@ -55,7 +56,7 @@ class BuyableProduct extends Model implements Buyable /** * Get the price of the Buyable item. */ - public function getBuyablePrice(): Money + public function getBuyablePrice(CartItemOptions $options): Money { return new Money($this->price, new Currency($this->currency)); } @@ -63,7 +64,7 @@ class BuyableProduct extends Model implements Buyable /** * Get the price of the Buyable item. */ - public function getBuyableWeight(): int + public function getBuyableWeight(CartItemOptions $options): int { return $this->weight; } From a2ac34de5afe15243591211f3aa4410a46f7436d Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 18:05:59 +0100 Subject: [PATCH 048/156] Update CanBeBought.php --- src/CanBeBought.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/CanBeBought.php b/src/CanBeBought.php index b2138cc..9088fb0 100644 --- a/src/CanBeBought.php +++ b/src/CanBeBought.php @@ -2,6 +2,7 @@ namespace Gloudemans\Shoppingcart; +use Gloudemans\Shoppingcart\CartItemOptions; use Money\Money; use Money\Currency; @@ -12,7 +13,7 @@ trait CanBeBought * * @return int|string */ - public function getBuyableIdentifier() + public function getBuyableIdentifier(CartItemOptions $options) { return method_exists($this, 'getKey') ? $this->getKey() : $this->id; } @@ -20,7 +21,7 @@ trait CanBeBought /** * Get the name, title or description of the Buyable item. */ - public function getBuyableDescription(): ?string + public function getBuyableDescription(CartItemOptions $options): ?string { if (($name = $this->getAttribute('name'))) { return $name; @@ -36,7 +37,7 @@ trait CanBeBought /** * Get the price of the Buyable item. */ - public function getBuyablePrice(): Money + public function getBuyablePrice(CartItemOptions $options): Money { if (($price = $this->getAttribute('price')) && ($currency = $this->getAttribute('currency'))) { return new Money($price, new Currency($currency)); @@ -46,7 +47,7 @@ trait CanBeBought /** * Get the weight of the Buyable item. */ - public function getBuyableWeight(): int + public function getBuyableWeight(CartItemOptions $options): int { if (($weight = $this->getAttribute('weight'))) { return $weight; From 158947d1f122bb52c0724d096204126d9ebb2a75 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 18:40:01 +0100 Subject: [PATCH 049/156] Update Cart.php --- src/Cart.php | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 0bd4bf2..7380c5e 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -5,6 +5,7 @@ namespace Gloudemans\Shoppingcart; use Carbon\Carbon; use Money\Money; use Closure; +use Iterable; use Gloudemans\Shoppingcart\Contracts\Buyable; use Gloudemans\Shoppingcart\Contracts\InstanceIdentifier; use Gloudemans\Shoppingcart\Exceptions\CartAlreadyStoredException; @@ -117,17 +118,26 @@ class Cart * * @return \Gloudemans\Shoppingcart\CartItem */ - public function add($id, ?string $name = null, ?int $qty = null, ?Money $price = null, ?int $weight = null, array $options = []): CartItem + public function add(int|string|Buyable|Iterable $id, ?string $name = null, ?int $qty = null, ?Money $price = null, ?int $weight = null, array $options = []): CartItem { - if ($this->isMulti($id)) { + /* Allow adding a CartItem by raw parameters */ + if (is_int($id) || is_string($id)) { + return $this->addCartItem($this->createCartItem($id, $name, $qty, $price, $weight, $options)); + } + /* Also allow passing a Buyable instance, get data from the instance rather than parameters */ + else if ($id instanceof Buyable) { + return $this->addCartItem($this->createCartItem($id); + } + /* Also allow passing multiple definitions at the same time, simply call same method and collec return value */ + else if (is_iterable($id)) { return array_map(function ($item) { return $this->add($item); }, $id); } - - $cartItem = $this->createCartItem($id, $name, $qty, $price, $weight, $options); - - return $this->addCartItem($cartItem); + /* Due to PHP8 union types this should never happen */ + else { + throw new InvalidArgumentException('$id must be of type int|string|Buyable|Iterable') + } } /** From af886fdd710951035af18be599c37c6da304a4f0 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 18:54:01 +0100 Subject: [PATCH 050/156] Update CartItem.php --- src/CartItem.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/CartItem.php b/src/CartItem.php index 938f79c..5851be9 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -98,7 +98,7 @@ class CartItem implements Arrayable, Jsonable * @param float $weight * @param array $options */ - public function __construct($id, string $name, Money $price, int $weight = 0, array $options = []) + public function __construct($id, string $name, Money $price, int $qty, int $weight = 0, array $options = []) { if (!is_string($id) && !is_int($id)) { throw new \InvalidArgumentException('Please supply a valid identifier.'); @@ -107,6 +107,7 @@ class CartItem implements Arrayable, Jsonable $this->id = $id; $this->name = $name; $this->price = $price; + $this->qty = $qty; $this->weight = $weight; $this->options = new CartItemOptions($options); $this->rowId = $this->generateRowId($id, $options); @@ -225,9 +226,9 @@ class CartItem implements Arrayable, Jsonable /** * Create a new instance from a Buyable. */ - public static function fromBuyable(Buyable $item, CartItemOptions $options = new CartItemOptions([])) : self + public static function fromBuyable(Buyable $item, int $qty = 1, CartItemOptions $options = new CartItemOptions([])) : self { - return new self($item->getBuyableIdentifier($options), $item->getBuyableDescription($options), $item->getBuyablePrice($options), $item->getBuyableWeight($options), $options); + return new self($item->getBuyableIdentifier($options), $item->getBuyableDescription($options), $item->getBuyablePrice($options), $qty, $item->getBuyableWeight($options), $options); } /** From 80a0eb5a5cd81d9b48a777e434ef9c04f6f54e8c Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 18:59:09 +0100 Subject: [PATCH 051/156] Update Cart.php --- src/Cart.php | 70 ++++++++++++++++++---------------------------------- 1 file changed, 24 insertions(+), 46 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 7380c5e..8c6b529 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -118,15 +118,34 @@ class Cart * * @return \Gloudemans\Shoppingcart\CartItem */ - public function add(int|string|Buyable|Iterable $id, ?string $name = null, ?int $qty = null, ?Money $price = null, ?int $weight = null, array $options = []): CartItem + public function add(int|string|Buyable|Iterable $id, null|string|int $nameOrQty = null, null|int|array $qtyOrOptions = null, ?Money $price = null, ?int $weight = null, array $options = []): CartItem { /* Allow adding a CartItem by raw parameters */ if (is_int($id) || is_string($id)) { - return $this->addCartItem($this->createCartItem($id, $name, $qty, $price, $weight, $options)); + if (! is_null($nameOrQty) && ! is_string($nameOrQty)) { + throw new InvalidArgumentException('$nameOrQty must be of string (name) or null when adding with raw parameters') + } + + if (! is_null($qtyOrOptions) && ! is_int($qtyOrOptions)) { + throw new InvalidArgumentException('$nameOrQty must be of int (quantity) or null when adding with raw parameters') + } + + return $this->addCartItem(CartItem::fromAttributes($id, $nameOrQty, $price, $qtyOrOptions ?: 1, $weight, $options)); } /* Also allow passing a Buyable instance, get data from the instance rather than parameters */ else if ($id instanceof Buyable) { - return $this->addCartItem($this->createCartItem($id); + if (! is_int($nameOrQty)) { + throw new InvalidArgumentException('$nameOrQty must be of int (quantity) when adding a Buyable instance') + } + + if (! is_null($qtyOrOptions) && ! is_array($qtyOrOptions)) { + throw new InvalidArgumentException('$qtyOrOptions must be of array (options) or null when adding a Buyable instance') + } + + $cartItem = CartItem::fromBuyable($id, $nameOrQty ?: 1, $qtyOrOptions ?: []); + $cartItem->associate($id); + + return $this->addCartItem($cartItem); } /* Also allow passing multiple definitions at the same time, simply call same method and collec return value */ else if (is_iterable($id)) { @@ -152,6 +171,8 @@ class Cart */ public function addCartItem(CartItem $item, bool $keepDiscount = false, bool $keepTax = false, bool $dispatchEvent = true): CartItem { + $cartItem->setInstance($this->currentInstance()); + if (!$keepDiscount) { $item->setDiscountRate($this->discount); } @@ -668,49 +689,6 @@ class Cart return new Collection(); } - /** - * Create a new CartItem from the supplied attributes. - * - * @param mixed $id - * @param mixed $name - * @param int|float $qty - * @param float $price - * @param float $weight - * @param array $options - */ - private function createCartItem($id, ?string $name = null, int $qty, ?Money $price = null, int $weight = 0, array $options = []): CartItem - { - if ($id instanceof Buyable) { - $cartItem = CartItem::fromBuyable($id, $qty ?: []); - $cartItem->setQuantity($name ?: 1); - $cartItem->associate($id); - } elseif (is_array($id)) { - $cartItem = CartItem::fromArray($id); - $cartItem->setQuantity($id['qty']); - } else { - $cartItem = CartItem::fromAttributes($id, $name, $price, $weight, $options); - $cartItem->setQuantity($qty); - } - - $cartItem->setInstance($this->currentInstance()); - - return $cartItem; - } - - /** - * Check if the item is a multidimensional array or an array of Buyables. - * - * @param mixed $item - */ - private function isMulti($item): bool - { - if (! is_array($item)) { - return false; - } - - return is_array(head($item)) || head($item) instanceof Buyable; - } - /** * @param $identifier */ From 1798774a66ff7a59707595dbc528586dfed62b6d Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:02:47 +0100 Subject: [PATCH 052/156] Update Cart.php --- src/Cart.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 8c6b529..c418776 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -123,11 +123,11 @@ class Cart /* Allow adding a CartItem by raw parameters */ if (is_int($id) || is_string($id)) { if (! is_null($nameOrQty) && ! is_string($nameOrQty)) { - throw new InvalidArgumentException('$nameOrQty must be of string (name) or null when adding with raw parameters') + throw new InvalidArgumentException('$nameOrQty must be of string (name) or null when adding with raw parameters'); } if (! is_null($qtyOrOptions) && ! is_int($qtyOrOptions)) { - throw new InvalidArgumentException('$nameOrQty must be of int (quantity) or null when adding with raw parameters') + throw new InvalidArgumentException('$nameOrQty must be of int (quantity) or null when adding with raw parameters'); } return $this->addCartItem(CartItem::fromAttributes($id, $nameOrQty, $price, $qtyOrOptions ?: 1, $weight, $options)); @@ -135,11 +135,11 @@ class Cart /* Also allow passing a Buyable instance, get data from the instance rather than parameters */ else if ($id instanceof Buyable) { if (! is_int($nameOrQty)) { - throw new InvalidArgumentException('$nameOrQty must be of int (quantity) when adding a Buyable instance') + throw new InvalidArgumentException('$nameOrQty must be of int (quantity) when adding a Buyable instance'); } if (! is_null($qtyOrOptions) && ! is_array($qtyOrOptions)) { - throw new InvalidArgumentException('$qtyOrOptions must be of array (options) or null when adding a Buyable instance') + throw new InvalidArgumentException('$qtyOrOptions must be of array (options) or null when adding a Buyable instance'); } $cartItem = CartItem::fromBuyable($id, $nameOrQty ?: 1, $qtyOrOptions ?: []); @@ -155,7 +155,7 @@ class Cart } /* Due to PHP8 union types this should never happen */ else { - throw new InvalidArgumentException('$id must be of type int|string|Buyable|Iterable') + throw new InvalidArgumentException('$id must be of type int|string|Buyable|Iterable'); } } From 300d868640c850e1a125259a40816af9fac0da61 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:05:26 +0100 Subject: [PATCH 053/156] Update CartItem.php --- src/CartItem.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/CartItem.php b/src/CartItem.php index 5851be9..9f316fd 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -226,8 +226,9 @@ class CartItem implements Arrayable, Jsonable /** * Create a new instance from a Buyable. */ - public static function fromBuyable(Buyable $item, int $qty = 1, CartItemOptions $options = new CartItemOptions([])) : self + public static function fromBuyable(Buyable $item, int $qty = 1, ?CartItemOptions $options = null) : self { + $options = $options ?: new CartItemOptions([]) return new self($item->getBuyableIdentifier($options), $item->getBuyableDescription($options), $item->getBuyablePrice($options), $qty, $item->getBuyableWeight($options), $options); } From 9b7846f948360bd057f377dfbe37ed3db2898398 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:06:32 +0100 Subject: [PATCH 054/156] Update Cart.php --- src/Cart.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index c418776..bca8711 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -4,8 +4,8 @@ namespace Gloudemans\Shoppingcart; use Carbon\Carbon; use Money\Money; -use Closure; -use Iterable; +use \Closure; +use \Iterable; use Gloudemans\Shoppingcart\Contracts\Buyable; use Gloudemans\Shoppingcart\Contracts\InstanceIdentifier; use Gloudemans\Shoppingcart\Exceptions\CartAlreadyStoredException; From d9c8e57280ba2243103ebb1344cda77a917ef347 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:08:02 +0100 Subject: [PATCH 055/156] Update Cart.php --- src/Cart.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index bca8711..a591bfa 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -4,8 +4,7 @@ namespace Gloudemans\Shoppingcart; use Carbon\Carbon; use Money\Money; -use \Closure; -use \Iterable; +use Closure; use Gloudemans\Shoppingcart\Contracts\Buyable; use Gloudemans\Shoppingcart\Contracts\InstanceIdentifier; use Gloudemans\Shoppingcart\Exceptions\CartAlreadyStoredException; @@ -118,7 +117,7 @@ class Cart * * @return \Gloudemans\Shoppingcart\CartItem */ - public function add(int|string|Buyable|Iterable $id, null|string|int $nameOrQty = null, null|int|array $qtyOrOptions = null, ?Money $price = null, ?int $weight = null, array $options = []): CartItem + public function add(int|string|Buyable|iterable $id, null|string|int $nameOrQty = null, null|int|array $qtyOrOptions = null, ?Money $price = null, ?int $weight = null, array $options = []): CartItem { /* Allow adding a CartItem by raw parameters */ if (is_int($id) || is_string($id)) { From 9f9df6a8f0b0c53c47090a04420caf957a20b1ab Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:08:55 +0100 Subject: [PATCH 056/156] Update CartItem.php --- src/CartItem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CartItem.php b/src/CartItem.php index 9f316fd..35b3814 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -228,7 +228,7 @@ class CartItem implements Arrayable, Jsonable */ public static function fromBuyable(Buyable $item, int $qty = 1, ?CartItemOptions $options = null) : self { - $options = $options ?: new CartItemOptions([]) + $options = $options ?: new CartItemOptions([]); return new self($item->getBuyableIdentifier($options), $item->getBuyableDescription($options), $item->getBuyablePrice($options), $qty, $item->getBuyableWeight($options), $options); } From 98ce5be39a7588e0d05b0f8e697c1fd05d732338 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:12:10 +0100 Subject: [PATCH 057/156] Update CartItemTest.php --- tests/CartItemTest.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index 9bfd693..7f99ec2 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -25,8 +25,7 @@ class CartItemTest extends TestCase /** @test */ public function it_can_be_cast_to_an_array() { - $cartItem = new CartItem(1, 'Some item', new Money(10.00, new Currency('USD')), 550, ['size' => 'XL', 'color' => 'red']); - $cartItem->setQuantity(2); + $cartItem = new CartItem(1, 'Some item', new Money(10.00, new Currency('USD')), 550, 2, ['size' => 'XL', 'color' => 'red']); $this->assertEquals([ 'id' => 1, @@ -48,8 +47,7 @@ class CartItemTest extends TestCase /** @test */ public function it_can_be_cast_to_json() { - $cartItem = new CartItem(1, 'Some item', new Money(10.00, new Currency('USD')), 550, ['size' => 'XL', 'color' => 'red']); - $cartItem->setQuantity(2); + $cartItem = new CartItem(1, 'Some item', new Money(10.00, new Currency('USD')), 550, 2, ['size' => 'XL', 'color' => 'red']); $this->assertJson($cartItem->toJson()); From df8c8c3cb61090706dc1ea014898a479bb722488 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:12:37 +0100 Subject: [PATCH 058/156] Update Cart.php --- src/Cart.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Cart.php b/src/Cart.php index a591bfa..1ad97f7 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -5,6 +5,7 @@ namespace Gloudemans\Shoppingcart; use Carbon\Carbon; use Money\Money; use Closure; +use \InvalidArgumentException; use Gloudemans\Shoppingcart\Contracts\Buyable; use Gloudemans\Shoppingcart\Contracts\InstanceIdentifier; use Gloudemans\Shoppingcart\Exceptions\CartAlreadyStoredException; From 7571e6092219848f95e9dca95ce95cb4b324f883 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:12:50 +0100 Subject: [PATCH 059/156] Update Cart.php --- src/Cart.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 1ad97f7..776d1d8 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -2,10 +2,10 @@ namespace Gloudemans\Shoppingcart; +use \Closure; +use \InvalidArgumentException; use Carbon\Carbon; use Money\Money; -use Closure; -use \InvalidArgumentException; use Gloudemans\Shoppingcart\Contracts\Buyable; use Gloudemans\Shoppingcart\Contracts\InstanceIdentifier; use Gloudemans\Shoppingcart\Exceptions\CartAlreadyStoredException; From 1df06502af1eda7bfd02ee78bc34e613e72742c0 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:13:00 +0100 Subject: [PATCH 060/156] Update Cart.php --- src/Cart.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 776d1d8..62b9817 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -2,8 +2,8 @@ namespace Gloudemans\Shoppingcart; -use \Closure; -use \InvalidArgumentException; +use Closure; +use InvalidArgumentException; use Carbon\Carbon; use Money\Money; use Gloudemans\Shoppingcart\Contracts\Buyable; From 3bbaadfea553e9105611619ea113690988180344 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:15:07 +0100 Subject: [PATCH 061/156] Update Cart.php --- src/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cart.php b/src/Cart.php index 62b9817..db45319 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -134,7 +134,7 @@ class Cart } /* Also allow passing a Buyable instance, get data from the instance rather than parameters */ else if ($id instanceof Buyable) { - if (! is_int($nameOrQty)) { + if (! is_null($qtyOrOptions) && ! is_int($nameOrQty)) { throw new InvalidArgumentException('$nameOrQty must be of int (quantity) when adding a Buyable instance'); } From f73b44bbf77e5a1892515b52c2a739b43b3e5abe Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:16:38 +0100 Subject: [PATCH 062/156] Update Cart.php --- src/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cart.php b/src/Cart.php index db45319..f1b0291 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -142,7 +142,7 @@ class Cart throw new InvalidArgumentException('$qtyOrOptions must be of array (options) or null when adding a Buyable instance'); } - $cartItem = CartItem::fromBuyable($id, $nameOrQty ?: 1, $qtyOrOptions ?: []); + $cartItem = CartItem::fromBuyable($id, $nameOrQty ?: 1, new CartItemOptions($qtyOrOptions ?: [])); $cartItem->associate($id); return $this->addCartItem($cartItem); From e885e7d70d5d1f2b1aca3af65129dd2d5dd8cb7e Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:18:44 +0100 Subject: [PATCH 063/156] Update CartItem.php --- src/CartItem.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/CartItem.php b/src/CartItem.php index 35b3814..826e5c4 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -98,7 +98,7 @@ class CartItem implements Arrayable, Jsonable * @param float $weight * @param array $options */ - public function __construct($id, string $name, Money $price, int $qty, int $weight = 0, array $options = []) + public function __construct($id, string $name, Money $price, int $qty = 1, int $weight = 0, ?CartItemOptions $options = null) { if (!is_string($id) && !is_int($id)) { throw new \InvalidArgumentException('Please supply a valid identifier.'); @@ -109,7 +109,7 @@ class CartItem implements Arrayable, Jsonable $this->price = $price; $this->qty = $qty; $this->weight = $weight; - $this->options = new CartItemOptions($options); + $this->options = $options ?: new CartItemOptions([]); $this->rowId = $this->generateRowId($id, $options); } From 8b9335fb5bc1bf99c99dddea4baa83bf1d1860bb Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:20:40 +0100 Subject: [PATCH 064/156] Update CartItemTest.php --- tests/CartItemTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index 7f99ec2..3823296 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -3,6 +3,7 @@ namespace Gloudemans\Tests\Shoppingcart; use Gloudemans\Shoppingcart\CartItem; +use Gloudemans\Shoppingcart\CartItemOptions; use Gloudemans\Shoppingcart\ShoppingcartServiceProvider; use Orchestra\Testbench\TestCase; use Money\Money; @@ -25,7 +26,7 @@ class CartItemTest extends TestCase /** @test */ public function it_can_be_cast_to_an_array() { - $cartItem = new CartItem(1, 'Some item', new Money(10.00, new Currency('USD')), 550, 2, ['size' => 'XL', 'color' => 'red']); + $cartItem = new CartItem(1, 'Some item', new Money(10.00, new Currency('USD')), 550, 2, new CartItemOptions(['size' => 'XL', 'color' => 'red'])); $this->assertEquals([ 'id' => 1, @@ -47,7 +48,7 @@ class CartItemTest extends TestCase /** @test */ public function it_can_be_cast_to_json() { - $cartItem = new CartItem(1, 'Some item', new Money(10.00, new Currency('USD')), 550, 2, ['size' => 'XL', 'color' => 'red']); + $cartItem = new CartItem(1, 'Some item', new Money(10.00, new Currency('USD')), 550, 2, new CartItemOptions(['size' => 'XL', 'color' => 'red'])); $this->assertJson($cartItem->toJson()); From edc4a5125878bf182123e794045c124d8af43787 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:22:07 +0100 Subject: [PATCH 065/156] Update CartItem.php --- src/CartItem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CartItem.php b/src/CartItem.php index 826e5c4..6e82f73 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -110,7 +110,7 @@ class CartItem implements Arrayable, Jsonable $this->qty = $qty; $this->weight = $weight; $this->options = $options ?: new CartItemOptions([]); - $this->rowId = $this->generateRowId($id, $options); + $this->rowId = $this->generateRowId($id, $options->toArray()); } /** From 177bf6a5d9c3b2f7941637b1033d823f6b03774b Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:23:02 +0100 Subject: [PATCH 066/156] Update Cart.php --- src/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cart.php b/src/Cart.php index f1b0291..bc75702 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -171,7 +171,7 @@ class Cart */ public function addCartItem(CartItem $item, bool $keepDiscount = false, bool $keepTax = false, bool $dispatchEvent = true): CartItem { - $cartItem->setInstance($this->currentInstance()); + $item->setInstance($this->currentInstance()); if (!$keepDiscount) { $item->setDiscountRate($this->discount); From 9711c975454d7c472c4f004154e0bd2a51c2c568 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:24:34 +0100 Subject: [PATCH 067/156] Update CartItem.php --- src/CartItem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CartItem.php b/src/CartItem.php index 6e82f73..6efbece 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -171,7 +171,7 @@ class CartItem implements Arrayable, Jsonable /** * Set the discount rate. */ - public function setDiscount(float $discount) : self + public function setDiscountRate(float $discountRate) : self { $this->discountRate = $discountRate; From 4c9c5936254377ee8fc36cf45e9f06b8068a51ba Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:27:40 +0100 Subject: [PATCH 068/156] Update Cart.php --- src/Cart.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index bc75702..3eb9ede 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -51,7 +51,7 @@ class Cart /** * Defines the discount percentage. */ - private ?Money $discount = null; + private float $discount = 0; /** * Defines the tax rate. @@ -173,7 +173,7 @@ class Cart { $item->setInstance($this->currentInstance()); - if (!$keepDiscount) { + if (! $keepDiscount) { $item->setDiscountRate($this->discount); } From 8c3bd2ce19f3f940665faf91bb709e7c7f93be8b Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:27:59 +0100 Subject: [PATCH 069/156] Update Cart.php --- src/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cart.php b/src/Cart.php index 3eb9ede..4d3bbcc 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -516,7 +516,7 @@ class Cart * * @return void */ - public function setGlobalDiscount($discount): void + public function setGlobalDiscount(float $discount): void { $this->discount = $discount; From e9927ccc8f366bc5647dbd283cefeb6703037857 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:30:31 +0100 Subject: [PATCH 070/156] Update Cart.php --- src/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cart.php b/src/Cart.php index 4d3bbcc..29cf5ea 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -118,7 +118,7 @@ class Cart * * @return \Gloudemans\Shoppingcart\CartItem */ - public function add(int|string|Buyable|iterable $id, null|string|int $nameOrQty = null, null|int|array $qtyOrOptions = null, ?Money $price = null, ?int $weight = null, array $options = []): CartItem + public function add(int|string|Buyable|iterable $id, null|string|int $nameOrQty = null, null|int|array $qtyOrOptions = null, ?Money $price = null, ?int $weight = null, array $options = []): CartItem|array { /* Allow adding a CartItem by raw parameters */ if (is_int($id) || is_string($id)) { From 9633f555f19c2d0813de8eb3e277f72d747eea65 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:36:36 +0100 Subject: [PATCH 071/156] Update CartTest.php --- tests/CartTest.php | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 9887270..a0939fd 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -3,6 +3,8 @@ namespace Gloudemans\Tests\Shoppingcart; use Carbon\Carbon; +use Money\Money; +use Money\Currency; use Gloudemans\Shoppingcart\Calculation\GrossPrice; use Gloudemans\Shoppingcart\Cart; use Gloudemans\Shoppingcart\CartItem; @@ -170,7 +172,7 @@ class CartTest extends TestCase $cart = $this->getCart(); - $cart->add(1, 'Test item', 1, 10.00); + $cart->add(1, 'Test item', 1, new Money(10.00, new Currency('USD'))); $this->assertEquals(1, $cart->count()); @@ -184,7 +186,7 @@ class CartTest extends TestCase $cart = $this->getCart(); - $cart->add(['id' => 1, 'name' => 'Test item', 'qty' => 1, 'price' => 10.00, 'weight' => 550]); + $cart->add(['id' => 1, 'name' => 'Test item', 'qty' => 1, 'price' => new Money(10.00, new Currency('USD')), 'weight' => 550]); $this->assertEquals(1, $cart->count()); @@ -199,8 +201,8 @@ class CartTest extends TestCase $cart = $this->getCart(); $cart->add([ - ['id' => 1, 'name' => 'Test item 1', 'qty' => 1, 'price' => 10.00, 'weight' => 550], - ['id' => 2, 'name' => 'Test item 2', 'qty' => 1, 'price' => 10.00, 'weight' => 550], + ['id' => 1, 'name' => 'Test item 1', 'qty' => 1, 'price' => new Money(10.00, new Currency('USD')), 'weight' => 550], + ['id' => 2, 'name' => 'Test item 2', 'qty' => 1, 'price' => new Money(10.00, new Currency('USD')), 'weight' => 550], ]); $this->assertEquals(2, $cart->count()); @@ -238,7 +240,7 @@ class CartTest extends TestCase $cart = $this->getCart(); - $cart->add(null, 'Some title', 1, 10.00); + $cart->add(null, 'Some title', 1, new Money(10.00, new Currency('USD'))); } /** @@ -251,7 +253,7 @@ class CartTest extends TestCase $cart = $this->getCart(); - $cart->add(1, 'Some title', 'invalid', 10.00); + $cart->add(1, 'Some title', 'invalid', new Money(10.00, new Currency('USD'))); } /** @@ -277,7 +279,7 @@ class CartTest extends TestCase $cart = $this->getCart(); - $cart->add(1, 'Some title', 1, 10.00, 'invalid'); + $cart->add(1, 'Some title', 1, new Money(10.00, new Currency('USD')), 'invalid'); } /** @test */ @@ -662,11 +664,11 @@ class CartTest extends TestCase $cart->add(new BuyableProduct([ 'name' => 'Some item', - ]), 1, ['color' => 'red']); + ]), 1, new CartItemOptions(['color' => 'red'])); $cart->add(new BuyableProduct([ 'id' => 2, 'name' => 'Another item', - ]), 1, ['color' => 'blue']); + ]), 1, new CartItemOptions(['color' => 'blue'])); $cartItem = $cart->search(function ($cartItem, $rowId) { return $cartItem->options->color == 'red'; @@ -695,7 +697,7 @@ class CartTest extends TestCase { $cart = $this->getCart(); - $cart->add(1, 'Test item', 1, 10.00); + $cart->add(1, 'Test item', 1, new Money(10.00, new Currency('USD'))); $cart->associate('027c91341fd5cf4d2579b49c4b6a90da', new ProductModel()); @@ -714,7 +716,7 @@ class CartTest extends TestCase $cart = $this->getCart(); - $cart->add(1, 'Test item', 1, 10.00); + $cart->add(1, 'Test item', 1, new Money(10.00, new Currency('USD'))); $cart->associate('027c91341fd5cf4d2579b49c4b6a90da', 'SomeModel'); } @@ -724,7 +726,7 @@ class CartTest extends TestCase { $cart = $this->getCart(); - $cart->add(1, 'Test item', 1, 10.00); + $cart->add(1, 'Test item', 1, new Money(10.00, new Currency('USD'))); $cart->associate('027c91341fd5cf4d2579b49c4b6a90da', new ProductModel()); @@ -1421,7 +1423,7 @@ class CartTest extends TestCase // https://github.com/bumbummen99/LaravelShoppingcart/pull/5 $cart = $this->getCart(); - $cartItem = $cart->add('293ad', 'Product 1', 1, 9.99, 550, ['size' => 'large']); + $cartItem = $cart->add('293ad', 'Product 1', 1, new Money(10.00, new Currency('USD')), 550, new CartItemOptions(['size' => 'large'])); $this->assertEquals(550, $cartItem->weight); $this->assertTrue($cartItem->options->has('size')); @@ -1662,7 +1664,7 @@ class CartTest extends TestCase $cart->add(new BuyableProduct([ 'name' => 'first item', 'price' => 1000, - ]), $qty = 5); + ]), 5); $this->assertEquals(5000, $cart->priceTotalFloat()); } From 80123f243f16b5258dafdb194168ef848f514f38 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:39:28 +0100 Subject: [PATCH 072/156] Update CartItem.php --- src/CartItem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CartItem.php b/src/CartItem.php index 6efbece..b792912 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -247,7 +247,7 @@ class CartItem implements Arrayable, Jsonable * * @param int|string $id */ - public static function fromAttributes($id, string $name, Money $price, int $weight, array $options = []) : self + public static function fromAttributes(int|string $id, string $name, Money $price, int $qty, int $weight, array $options = []) : self { return new self($id, $name, $price, $weight, $options); } From a6f6e0887f9617f35c87678575c3a1fbf1414eab Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:42:15 +0100 Subject: [PATCH 073/156] Update CartItem.php --- src/CartItem.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/CartItem.php b/src/CartItem.php index b792912..d9a0287 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -237,9 +237,8 @@ class CartItem implements Arrayable, Jsonable */ public static function fromArray(array $attributes) : self { - $options = Arr::get($attributes, 'options', []); - - return new self($attributes['id'], $attributes['name'], $attributes['price'], $attributes['weight'], $options); + $options = new CartItemOptions(Arr::get($attributes, 'options', [])); + return new self($attributes['id'], $attributes['name'], $attributes['price'], $attributes['qty'], $attributes['weight'], $options); } /** @@ -247,9 +246,10 @@ class CartItem implements Arrayable, Jsonable * * @param int|string $id */ - public static function fromAttributes(int|string $id, string $name, Money $price, int $qty, int $weight, array $options = []) : self + public static function fromAttributes(int|string $id, string $name, Money $price, int $qty, int $weight, ?CartItemOptions $options = null) : self { - return new self($id, $name, $price, $weight, $options); + $options = $options ?: new CartItemOptions([]); + return new self($id, $name, $price, $qty, $weight, $options); } /** From d4dad63481696ae88296fc48fdc6253ea395806b Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:44:28 +0100 Subject: [PATCH 074/156] Update Cart.php --- src/Cart.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 29cf5ea..b03817f 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -118,7 +118,7 @@ class Cart * * @return \Gloudemans\Shoppingcart\CartItem */ - public function add(int|string|Buyable|iterable $id, null|string|int $nameOrQty = null, null|int|array $qtyOrOptions = null, ?Money $price = null, ?int $weight = null, array $options = []): CartItem|array + public function add(int|string|Buyable|iterable $id, null|string|int $nameOrQty = null, null|int|array $qtyOrOptions = null, ?Money $price = null, ?int $weight = null, ?CartItemOptions $options = null): CartItem|array { /* Allow adding a CartItem by raw parameters */ if (is_int($id) || is_string($id)) { @@ -130,7 +130,7 @@ class Cart throw new InvalidArgumentException('$nameOrQty must be of int (quantity) or null when adding with raw parameters'); } - return $this->addCartItem(CartItem::fromAttributes($id, $nameOrQty, $price, $qtyOrOptions ?: 1, $weight, $options)); + return $this->addCartItem(CartItem::fromAttributes($id, $nameOrQty, $price, $qtyOrOptions ?: 1, $weight, $options ?: new CartItemOptions([]))); } /* Also allow passing a Buyable instance, get data from the instance rather than parameters */ else if ($id instanceof Buyable) { From 05f1d8d5ad2db82b594e0edaefa7c2491def3b6b Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:47:09 +0100 Subject: [PATCH 075/156] Update Cart.php --- src/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cart.php b/src/Cart.php index b03817f..fe4df59 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -130,7 +130,7 @@ class Cart throw new InvalidArgumentException('$nameOrQty must be of int (quantity) or null when adding with raw parameters'); } - return $this->addCartItem(CartItem::fromAttributes($id, $nameOrQty, $price, $qtyOrOptions ?: 1, $weight, $options ?: new CartItemOptions([]))); + return $this->addCartItem(CartItem::fromAttributes($id, $nameOrQty, $price, $qtyOrOptions ?: 1, $weight ?: 0, $options ?: new CartItemOptions([]))); } /* Also allow passing a Buyable instance, get data from the instance rather than parameters */ else if ($id instanceof Buyable) { From 07884cb6a6d59e10df00414aae5f076c1b980d2d Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 19:48:07 +0100 Subject: [PATCH 076/156] Update CartItem.php --- src/CartItem.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/CartItem.php b/src/CartItem.php index d9a0287..cd7b9bc 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -57,10 +57,8 @@ class CartItem implements Arrayable, Jsonable /** * The weight of the product. - * - * @var float */ - public $weight; + public int $weight; /** * The options for this cart item. @@ -246,7 +244,7 @@ class CartItem implements Arrayable, Jsonable * * @param int|string $id */ - public static function fromAttributes(int|string $id, string $name, Money $price, int $qty, int $weight, ?CartItemOptions $options = null) : self + public static function fromAttributes(int|string $id, string $name, Money $price, int $qty = 1, int $weight = 0, ?CartItemOptions $options = null) : self { $options = $options ?: new CartItemOptions([]); return new self($id, $name, $price, $qty, $weight, $options); From 08f4256242aea40648176b2209b3030b3742e70f Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:12:13 +0100 Subject: [PATCH 077/156] Update Cart.php --- src/Cart.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index fe4df59..d85c13d 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -346,7 +346,7 @@ class Cart { return $this->getContent()->reduce(function (Money $total, CartItem $cartItem) { return $total->add($cartItem->total); - }, new Money()); + }, new Money(0, 'USD')); } /** @@ -356,7 +356,7 @@ class Cart { return $this->getContent()->reduce(function (Money $tax, CartItem $cartItem) { return $tax->add($cartItem->taxTotal); - }, new Money()); + }, new Money(0, 'USD')); } /** @@ -366,7 +366,7 @@ class Cart { return $this->getContent()->reduce(function (Money $subTotal, CartItem $cartItem) { return $subTotal->add($cartItem->subtotal); - }, new Money()); + }, new Money(0, 'USD')); } /** @@ -378,7 +378,7 @@ class Cart { return $this->getContent()->reduce(function (Money $discount, CartItem $cartItem) { return $discount->add($cartItem->discountTotal); - }, new Money()); + }, new Money(0, 'USD')); } /** @@ -388,7 +388,7 @@ class Cart { return $this->getContent()->reduce(function (Money $initial, CartItem $cartItem) { return $initial->add($cartItem->price->multiply($cartItem->qty)); - }, new Money()); + }, new Money(0, 'USD')); } /** @@ -398,7 +398,7 @@ class Cart { return $this->getContent()->reduce(function (Money $initial, CartItem $cartItem) { return $initial->add($cartItem->priceTotal); - }, new Money()); + }, new Money(0, 'USD')); } /** From b0de33e35c6cf7746476840b2dd9a4f4870e9637 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:18:17 +0100 Subject: [PATCH 078/156] Update Cart.php --- src/Cart.php | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index d85c13d..8d4786f 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -149,9 +149,18 @@ class Cart } /* Also allow passing multiple definitions at the same time, simply call same method and collec return value */ else if (is_iterable($id)) { - return array_map(function ($item) { - return $this->add($item); - }, $id); + /* Check if this iterable contains instances */ + if (is_array(head($item)) || head($item) instanceof Buyable) { + return array_map(function ($item) { + return $this->add($item); + }, $id); + } + /* Treat the array itself as an instance */ + else { + $cartItem = CartItem::fromArray($id); + + return $this->addCartItem($cartItem); + } } /* Due to PHP8 union types this should never happen */ else { From f41f1215be40371582d946d8b11bd8f6d5c85464 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:20:58 +0100 Subject: [PATCH 079/156] Update Cart.php --- src/Cart.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 8d4786f..8fa524a 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -150,8 +150,8 @@ class Cart /* Also allow passing multiple definitions at the same time, simply call same method and collec return value */ else if (is_iterable($id)) { /* Check if this iterable contains instances */ - if (is_array(head($item)) || head($item) instanceof Buyable) { - return array_map(function ($item) { + if (is_array(head($id)) || head($id) instanceof Buyable) { + return array_map(function (Buyable|iterable $item) { return $this->add($item); }, $id); } From 365a4e3d645a576ced72dc43ae96138861f9f37b Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:23:41 +0100 Subject: [PATCH 080/156] Update Cart.php --- src/Cart.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 8fa524a..fbd18fb 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -355,7 +355,7 @@ class Cart { return $this->getContent()->reduce(function (Money $total, CartItem $cartItem) { return $total->add($cartItem->total); - }, new Money(0, 'USD')); + }, new Money(0, new Currency('USD'))); } /** @@ -365,7 +365,7 @@ class Cart { return $this->getContent()->reduce(function (Money $tax, CartItem $cartItem) { return $tax->add($cartItem->taxTotal); - }, new Money(0, 'USD')); + }, new Money(0, new Currency('USD'))); } /** @@ -375,7 +375,7 @@ class Cart { return $this->getContent()->reduce(function (Money $subTotal, CartItem $cartItem) { return $subTotal->add($cartItem->subtotal); - }, new Money(0, 'USD')); + }, new Money(0, new Currency('USD'))); } /** @@ -387,7 +387,7 @@ class Cart { return $this->getContent()->reduce(function (Money $discount, CartItem $cartItem) { return $discount->add($cartItem->discountTotal); - }, new Money(0, 'USD')); + }, new Money(0, new Currency('USD'))); } /** @@ -397,7 +397,7 @@ class Cart { return $this->getContent()->reduce(function (Money $initial, CartItem $cartItem) { return $initial->add($cartItem->price->multiply($cartItem->qty)); - }, new Money(0, 'USD')); + }, new Money(0, new Currency('USD'))); } /** @@ -407,7 +407,7 @@ class Cart { return $this->getContent()->reduce(function (Money $initial, CartItem $cartItem) { return $initial->add($cartItem->priceTotal); - }, new Money(0, 'USD')); + }, new Money(0, new Currency('USD'))); } /** From 7d565dd000efd394b245e12b02b24da8fe244f85 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:24:37 +0100 Subject: [PATCH 081/156] Update Cart.php --- src/Cart.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Cart.php b/src/Cart.php index fbd18fb..4934fa0 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -6,6 +6,7 @@ use Closure; use InvalidArgumentException; use Carbon\Carbon; use Money\Money; +use Money\Currency; use Gloudemans\Shoppingcart\Contracts\Buyable; use Gloudemans\Shoppingcart\Contracts\InstanceIdentifier; use Gloudemans\Shoppingcart\Exceptions\CartAlreadyStoredException; From 5e30aee09411fd1d7378f8ec0980857e7c3004fe Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:26:36 +0100 Subject: [PATCH 082/156] Update CartTest.php --- tests/CartTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/CartTest.php b/tests/CartTest.php index a0939fd..570c643 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -8,6 +8,7 @@ use Money\Currency; use Gloudemans\Shoppingcart\Calculation\GrossPrice; use Gloudemans\Shoppingcart\Cart; use Gloudemans\Shoppingcart\CartItem; +use Gloudemans\Shoppingcart\CartItemOptions; use Gloudemans\Shoppingcart\ShoppingcartServiceProvider; use Gloudemans\Tests\Shoppingcart\Fixtures\BuyableProduct; use Gloudemans\Tests\Shoppingcart\Fixtures\BuyableProductTrait; From 6664cb8834b272e10053718ea5ba66eb8df01cd8 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:28:06 +0100 Subject: [PATCH 083/156] Update Cart.php --- src/Cart.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cart.php b/src/Cart.php index 4934fa0..19ef338 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -119,7 +119,7 @@ class Cart * * @return \Gloudemans\Shoppingcart\CartItem */ - public function add(int|string|Buyable|iterable $id, null|string|int $nameOrQty = null, null|int|array $qtyOrOptions = null, ?Money $price = null, ?int $weight = null, ?CartItemOptions $options = null): CartItem|array + public function add(int|string|Buyable|iterable $id, null|string|int $nameOrQty = null, null|int|CartItemOptions $qtyOrOptions = null, ?Money $price = null, ?int $weight = null, ?CartItemOptions $options = null): CartItem|array { /* Allow adding a CartItem by raw parameters */ if (is_int($id) || is_string($id)) { From b1a40c96e363d33ea3ff65d5640637832695df36 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:29:57 +0100 Subject: [PATCH 084/156] Update CartTest.php --- tests/CartTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 570c643..ea21b05 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -218,9 +218,7 @@ class CartTest extends TestCase $cart = $this->getCart(); - $options = ['size' => 'XL', 'color' => 'red']; - - $cart->add(new BuyableProduct(), 1, $options); + $cart->add(new BuyableProduct(), 1, new CartItemOptions(['size' => 'XL', 'color' => 'red'])); $cartItem = $cart->get('07d5da5550494c62daf9993cf954303f'); From 95193d5a71fe43cf94eba35fcadc57f49b03a618 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:31:07 +0100 Subject: [PATCH 085/156] Update CartTest.php --- tests/CartTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index ea21b05..d606a70 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -391,7 +391,7 @@ class CartTest extends TestCase $cart->add(new BuyableProduct(), 1, ['color' => 'red']); - $cart->update('ea65e0bdcd1967c4b3149e9e780177c0', ['options' => ['color' => 'blue']]); + $cart->update('ea65e0bdcd1967c4b3149e9e780177c0', ['options' => new CartItemOptions(['color' => 'blue'])]); $this->assertItemsInCart(1, $cart); $this->assertEquals('7e70a1e9aaadd18c72921a07aae5d011', $cart->content()->first()->rowId); @@ -406,7 +406,7 @@ class CartTest extends TestCase $cart->add(new BuyableProduct(), 1, ['color' => 'red']); $cart->add(new BuyableProduct(), 1, ['color' => 'blue']); - $cart->update('7e70a1e9aaadd18c72921a07aae5d011', ['options' => ['color' => 'red']]); + $cart->update('7e70a1e9aaadd18c72921a07aae5d011', ['options' => new CartItemOptions(['color' => 'red'])]); $this->assertItemsInCart(2, $cart); $this->assertRowsInCart(1, $cart); @@ -421,7 +421,7 @@ class CartTest extends TestCase $cart->add(new BuyableProduct(), 1, ['color' => 'green']); $cart->add(new BuyableProduct(), 1, ['color' => 'blue']); - $cart->update($cart->content()->values()[1]->rowId, ['options' => ['color' => 'yellow']]); + $cart->update($cart->content()->values()[1]->rowId, ['options' => new CartItemOptions(['color' => 'yellow'])]); $this->assertRowsInCart(3, $cart); $this->assertEquals('yellow', $cart->content()->values()[1]->options->color); From 7b62728f7e61e18b131cd9986b281cf9ac54de75 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:33:07 +0100 Subject: [PATCH 086/156] Update Cart.php --- src/Cart.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 19ef338..51b9a46 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -124,11 +124,11 @@ class Cart /* Allow adding a CartItem by raw parameters */ if (is_int($id) || is_string($id)) { if (! is_null($nameOrQty) && ! is_string($nameOrQty)) { - throw new InvalidArgumentException('$nameOrQty must be of string (name) or null when adding with raw parameters'); + throw new InvalidArgumentException('$nameOrQty must be of type string (name) or null when adding with raw parameters'); } if (! is_null($qtyOrOptions) && ! is_int($qtyOrOptions)) { - throw new InvalidArgumentException('$nameOrQty must be of int (quantity) or null when adding with raw parameters'); + throw new InvalidArgumentException('$nameOrQty must be of type int (quantity) or null when adding with raw parameters'); } return $this->addCartItem(CartItem::fromAttributes($id, $nameOrQty, $price, $qtyOrOptions ?: 1, $weight ?: 0, $options ?: new CartItemOptions([]))); @@ -136,14 +136,14 @@ class Cart /* Also allow passing a Buyable instance, get data from the instance rather than parameters */ else if ($id instanceof Buyable) { if (! is_null($qtyOrOptions) && ! is_int($nameOrQty)) { - throw new InvalidArgumentException('$nameOrQty must be of int (quantity) when adding a Buyable instance'); + throw new InvalidArgumentException('$nameOrQty must be of type int (quantity) when adding a Buyable instance'); } - if (! is_null($qtyOrOptions) && ! is_array($qtyOrOptions)) { - throw new InvalidArgumentException('$qtyOrOptions must be of array (options) or null when adding a Buyable instance'); + if (! is_null($qtyOrOptions) && ! $qtyOrOptions instanceof CartItemOptions) { + throw new InvalidArgumentException('$qtyOrOptions must be of type CartItemOptions (options) or null when adding a Buyable instance'); } - $cartItem = CartItem::fromBuyable($id, $nameOrQty ?: 1, new CartItemOptions($qtyOrOptions ?: [])); + $cartItem = CartItem::fromBuyable($id, $nameOrQty ?: 1, $qtyOrOptions ?: new CartItemOptions([])); $cartItem->associate($id); return $this->addCartItem($cartItem); From a3889ca37262122dc091fee54e7f814bdf5e0cd8 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:35:24 +0100 Subject: [PATCH 087/156] Update CartTest.php --- tests/CartTest.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index d606a70..e60f31f 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -389,7 +389,7 @@ class CartTest extends TestCase { $cart = $this->getCart(); - $cart->add(new BuyableProduct(), 1, ['color' => 'red']); + $cart->add(new BuyableProduct(), 1, new CartItemOptions(['color' => 'red'])); $cart->update('ea65e0bdcd1967c4b3149e9e780177c0', ['options' => new CartItemOptions(['color' => 'blue'])]); @@ -403,8 +403,8 @@ class CartTest extends TestCase { $cart = $this->getCart(); - $cart->add(new BuyableProduct(), 1, ['color' => 'red']); - $cart->add(new BuyableProduct(), 1, ['color' => 'blue']); + $cart->add(new BuyableProduct(), 1, new CartItemOptions(['color' => 'red'])); + $cart->add(new BuyableProduct(), 1, new CartItemOptions(['color' => 'blue'])); $cart->update('7e70a1e9aaadd18c72921a07aae5d011', ['options' => new CartItemOptions(['color' => 'red'])]); @@ -417,9 +417,9 @@ class CartTest extends TestCase { $cart = $this->getCart(); - $cart->add(new BuyableProduct(), 1, ['color' => 'red']); - $cart->add(new BuyableProduct(), 1, ['color' => 'green']); - $cart->add(new BuyableProduct(), 1, ['color' => 'blue']); + $cart->add(new BuyableProduct(), 1, new CartItemOptions(['color' => 'red'])); + $cart->add(new BuyableProduct(), 1, new CartItemOptions(['color' => 'green'])); + $cart->add(new BuyableProduct(), 1, new CartItemOptions(['color' => 'blue'])); $cart->update($cart->content()->values()[1]->rowId, ['options' => new CartItemOptions(['color' => 'yellow'])]); From 9a99613d3d177c9aa536ab183ffb46ff31a36090 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:38:34 +0100 Subject: [PATCH 088/156] Update CartTest.php --- tests/CartTest.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index e60f31f..fdd5482 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -234,8 +234,7 @@ class CartTest extends TestCase */ public function it_will_validate_the_identifier() { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Please supply a valid identifier.'); + $this->expectException(\TypeError::class); $cart = $this->getCart(); @@ -247,8 +246,7 @@ class CartTest extends TestCase */ public function it_will_validate_the_quantity() { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Please supply a valid quantity.'); + $this->expectException(\TypeError::class); $cart = $this->getCart(); @@ -260,8 +258,7 @@ class CartTest extends TestCase */ public function it_will_validate_the_price() { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Please supply a valid price.'); + $this->expectException(\TypeError::class); $cart = $this->getCart(); @@ -273,8 +270,7 @@ class CartTest extends TestCase */ public function it_will_validate_the_weight() { - $this->expectException(\InvalidArgumentException::class); - $this->expectExceptionMessage('Please supply a valid weight'); + $this->expectException(\TypeError::class); $cart = $this->getCart(); From 30bdedd4d588266b4438cff53ab0f2cb1cf5f31e Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:40:17 +0100 Subject: [PATCH 089/156] Update CartTest.php --- tests/CartTest.php | 83 ---------------------------------------------- 1 file changed, 83 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index fdd5482..75e6a74 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -586,25 +586,6 @@ class CartTest extends TestCase $this->assertEquals(60.00, $cart->subtotal()); } - /** @test */ - public function it_can_return_a_formatted_total() - { - $cart = $this->getCart(); - - $cart->add(new BuyableProduct([ - 'name' => 'First item', - 'price' => 1000.00, - ])); - $cart->add(new BuyableProduct([ - 'id' => 2, - 'name' => 'Second item', - 'price' => 2500.00, - ]), 2); - - $this->assertItemsInCart(3, $cart); - $this->assertEquals('6.000,00', $cart->subtotal(2, ',', '.')); - } - /** @test */ public function it_can_search_the_cart_for_a_specific_item() { @@ -746,21 +727,6 @@ class CartTest extends TestCase $this->assertEquals(29.97, $cartItem->subtotal); } - /** @test */ - public function it_can_return_a_formatted_subtotal() - { - $cart = $this->getCart(); - - $cart->add(new BuyableProduct([ - 'name' => 'Some title', - 'price' => 500, - ]), 3); - - $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - - $this->assertEquals('1.500,00', $cartItem->subtotal(2, ',', '.')); - } - /** @test */ public function it_can_calculate_tax_based_on_the_default_tax_rate_in_the_config() { @@ -791,21 +757,6 @@ class CartTest extends TestCase $this->assertEquals(1.90, $cartItem->tax); } - /** @test */ - public function it_can_return_the_calculated_tax_formatted() - { - $cart = $this->getCart(); - - $cart->add(new BuyableProduct([ - 'name' => 'Some title', - 'price' => 10000.00, - ]), 1); - - $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - - $this->assertEquals('2.100,00', $cartItem->tax(2, ',', '.')); - } - /** @test */ public function it_can_calculate_the_total_tax_for_all_cart_items() { @@ -823,24 +774,6 @@ class CartTest extends TestCase $this->assertEquals(10.50, $cart->tax); } - /** @test */ - public function it_can_return_formatted_total_tax() - { - $cart = $this->getCart(); - - $cart->add(new BuyableProduct([ - 'name' => 'Some title', - 'price' => 1000.00, - ]), 1); - $cart->add(new BuyableProduct([ - 'id' => 2, - 'name' => 'Some title', - 'price' => 2000.00, - ]), 2); - - $this->assertEquals('1.050,00', $cart->tax(2, ',', '.')); - } - /** @test */ public function it_can_access_tax_as_percentage() { @@ -871,22 +804,6 @@ class CartTest extends TestCase $this->assertEquals(50.00, $cart->subtotal); } - /** @test */ - public function it_can_return_formatted_subtotal() - { - $cart = $this->getCart(); - - $cart->add(new BuyableProduct([ - 'price' => 1000.00, - ]), 1); - $cart->add(new BuyableProduct([ - 'id' => 2, - 'price' => 2000.00, - ]), 2); - - $this->assertEquals('5000,00', $cart->subtotal(2, ',', '')); - } - /** @test */ public function it_can_return_cart_formated_numbers_by_config_values() { From f0a1e794404acbac4cd61d5462eee2a693631036 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:41:44 +0100 Subject: [PATCH 090/156] Update CartTest.php --- tests/CartTest.php | 48 ---------------------------------------------- 1 file changed, 48 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 75e6a74..c605984 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -804,54 +804,6 @@ class CartTest extends TestCase $this->assertEquals(50.00, $cart->subtotal); } - /** @test */ - public function it_can_return_cart_formated_numbers_by_config_values() - { - $this->setConfigFormat(2, ',', ''); - - $cart = $this->getCart(); - - $cart->add(new BuyableProduct([ - 'price' => 1000.00, - ]), 1); - $cart->add(new BuyableProduct([ - 'id' => 2, - 'price' => 2000.00, - ]), 2); - - $this->assertEquals('5000,00', $cart->subtotal()); - $this->assertEquals('1050,00', $cart->tax()); - $this->assertEquals('6050,00', $cart->total()); - - $this->assertEquals('5000,00', $cart->subtotal); - $this->assertEquals('1050,00', $cart->tax); - $this->assertEquals('6050,00', $cart->total); - } - - /** @test */ - public function it_can_return_cartItem_formated_numbers_by_config_values() - { - $this->setConfigFormat(2, ',', ''); - - $cart = $this->getCartDiscount(50); - - $cart->add(new BuyableProduct([ - 'price' => 2000.00, - ]), 2); - - $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - - $this->assertEquals('2000,00', $cartItem->price()); - $this->assertEquals('1000,00', $cartItem->discount()); - $this->assertEquals('2000,00', $cartItem->discountTotal()); - $this->assertEquals('1000,00', $cartItem->priceTarget()); - $this->assertEquals('2000,00', $cartItem->subtotal()); - $this->assertEquals('210,00', $cartItem->tax()); - $this->assertEquals('420,00', $cartItem->taxTotal()); - $this->assertEquals('1210,00', $cartItem->priceTax()); - $this->assertEquals('2420,00', $cartItem->total()); - } - /** @test */ public function it_can_store_the_cart_in_a_database() { From a88ac7575e5393a3c58c2d8bb24bab02d10d5cb3 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:42:32 +0100 Subject: [PATCH 091/156] Update CartTest.php --- tests/CartTest.php | 14 -------------- 1 file changed, 14 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index c605984..b388152 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1014,20 +1014,6 @@ class CartTest extends TestCase $this->assertEquals(11.90, $cartItem->total(2)); } - /** @test */ - public function it_will_destroy_the_cart_when_the_user_logs_out_and_the_config_setting_was_set_to_true() - { - $this->app['config']->set('cart.destroy_on_logout', true); - - $this->app->instance(SessionManager::class, Mockery::mock(SessionManager::class, function ($mock) { - $mock->shouldReceive('forget')->once()->with('cart'); - })); - - $user = Mockery::mock(Authenticatable::class); - - \Auth::guard('web')->logout(); - } - /** @test */ public function can_change_tax_globally() { From c6c7a1afdd0db6ca8cef586c31b3970b63825b3c Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:44:37 +0100 Subject: [PATCH 092/156] Update Cart.php --- src/Cart.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 51b9a46..709ac6e 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -352,7 +352,7 @@ class Cart /** * Get the total price of the items in the cart. */ - public function totalFloat(): Money + public function total(): Money { return $this->getContent()->reduce(function (Money $total, CartItem $cartItem) { return $total->add($cartItem->total); @@ -362,7 +362,7 @@ class Cart /** * Get the total tax of the items in the cart. */ - public function taxFloat(): float + public function tax(): float { return $this->getContent()->reduce(function (Money $tax, CartItem $cartItem) { return $tax->add($cartItem->taxTotal); @@ -394,7 +394,7 @@ class Cart /** * Get the price of the items in the cart (not rounded). */ - public function initialFloat(): float + public function initial(): float { return $this->getContent()->reduce(function (Money $initial, CartItem $cartItem) { return $initial->add($cartItem->price->multiply($cartItem->qty)); From 9605395097d78c7a8b81c457be82b685704206f2 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:45:58 +0100 Subject: [PATCH 093/156] Update Cart.php --- src/Cart.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 709ac6e..04d775a 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -362,7 +362,7 @@ class Cart /** * Get the total tax of the items in the cart. */ - public function tax(): float + public function tax(): Money { return $this->getContent()->reduce(function (Money $tax, CartItem $cartItem) { return $tax->add($cartItem->taxTotal); @@ -384,7 +384,7 @@ class Cart * * @return float */ - public function discount(): float + public function discount(): Money { return $this->getContent()->reduce(function (Money $discount, CartItem $cartItem) { return $discount->add($cartItem->discountTotal); @@ -394,7 +394,7 @@ class Cart /** * Get the price of the items in the cart (not rounded). */ - public function initial(): float + public function initial(): Money { return $this->getContent()->reduce(function (Money $initial, CartItem $cartItem) { return $initial->add($cartItem->price->multiply($cartItem->qty)); From 3fc64e8c7992c04d23fe739fd9739ee4caabf2cc Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:48:34 +0100 Subject: [PATCH 094/156] Update CartTest.php --- tests/CartTest.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index b388152..b248c58 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1091,7 +1091,7 @@ class CartTest extends TestCase $cart2->merge('test'); $this->assertEquals('2', $cart2->countItems()); - $this->assertEquals(20, $cart2->totalFloat()); + $this->assertEquals(20, $cart2->total()); $cart3 = $this->getCart(); $cart3->instance('test3'); @@ -1100,7 +1100,7 @@ class CartTest extends TestCase $cart3->merge('test', true); - $this->assertEquals(10, $cart3->totalFloat()); + $this->assertEquals(10, $cart3->total()); } /** @test */ @@ -1132,15 +1132,15 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); $this->assertEquals('10.00', $cart->initial(2)); - $this->assertEquals(10.00, $cart->initialFloat()); + $this->assertEquals(10.00, $cart->initial()); $this->assertEquals('5.00', $cart->discount(2)); - $this->assertEquals(5.00, $cart->discountFloat()); + $this->assertEquals(5.00, $cart->discount()); $this->assertEquals('5.00', $cart->subtotal(2)); - $this->assertEquals(5.00, $cart->subtotalFloat()); + $this->assertEquals(5.00, $cart->subtotal()); $this->assertEquals('0.95', $cart->tax(2)); - $this->assertEquals(0.95, $cart->taxFloat()); + $this->assertEquals(0.95, $cart->tax()); $this->assertEquals('5.95', $cart->total(2)); - $this->assertEquals(5.95, $cart->totalFloat()); + $this->assertEquals(5.95, $cart->total()); } /** @test */ @@ -1189,7 +1189,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 50); $this->assertEquals('500.00', $cart->weight(2)); - $this->assertEquals(500.00, $cart->weightFloat()); + $this->assertEquals(500.00, $cart->weight()); $this->assertEquals(500.00, $cartItem->weightTotal); $this->assertEquals('250.00', $cartItem->weight(2)); } @@ -1311,7 +1311,7 @@ class CartTest extends TestCase Event::assertDispatched('cart.merged'); $this->assertEquals('2', $cart2->countItems()); - $this->assertEquals(20, $cart2->totalFloat()); + $this->assertEquals(20, $cart2->total()); }); } @@ -1345,7 +1345,7 @@ class CartTest extends TestCase Event::assertDispatched('cart.added', 2); Event::assertDispatched('cart.merged'); $this->assertEquals('2', $cart2->countItems()); - $this->assertEquals(20, $cart2->totalFloat()); + $this->assertEquals(20, $cart2->total()); }); } @@ -1377,7 +1377,7 @@ class CartTest extends TestCase $this->assertEquals('253,29', $cart->total()); // check that the sum of cart subvalues matches the total (in order to avoid cart summary to looks wrong) - $this->assertEquals($cart->totalFloat(), $cart->subtotalFloat() + $cart->taxFloat()); + $this->assertEquals($cart->total(), $cart->subtotal()->add($cart->tax())); } /** @test */ @@ -1428,7 +1428,7 @@ class CartTest extends TestCase // check item price total $this->assertEquals(190, $cartItem->priceTotal); // check that the sum of cart subvalues matches the total (in order to avoid cart summary to looks wrong) - $this->assertEquals($cart->totalFloat(), $cart->subtotalFloat() + $cart->taxFloat()); + $this->assertEquals($cart->total(), $cart->subtotal()->add($cart->tax()); } /** @@ -1515,7 +1515,7 @@ class CartTest extends TestCase 'name' => 'first item', 'price' => 1000, ]), 5); - $this->assertEquals(5000, $cart->priceTotalFloat()); + $this->assertEquals(5000, $cart->priceTotal()); } /** @test */ From c61610d22d71c98a7fe217bf7e3ce9cb905e9353 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:49:42 +0100 Subject: [PATCH 095/156] Update CartTest.php --- tests/CartTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index b248c58..996fab5 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1428,7 +1428,7 @@ class CartTest extends TestCase // check item price total $this->assertEquals(190, $cartItem->priceTotal); // check that the sum of cart subvalues matches the total (in order to avoid cart summary to looks wrong) - $this->assertEquals($cart->total(), $cart->subtotal()->add($cart->tax()); + $this->assertEquals($cart->total(), $cart->subtotal()->add($cart->tax())); } /** From b5e58d0d886a0957bac66cda61a079f51e871304 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:53:34 +0100 Subject: [PATCH 096/156] Update CartTest.php --- tests/CartTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 996fab5..a18e15d 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1394,7 +1394,7 @@ class CartTest extends TestCase $cart->setGlobalTax(22); // check net price - $this->assertEquals(81.97, round($cartItem->priceNet, 2)); + $this->assertEquals(new Money(81.97, new Currency('USD')), $cartItem->priceNet); } /** @test */ From 747de2039dae89eaab0556284825ccaf148dfa86 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:54:29 +0100 Subject: [PATCH 097/156] Update CartTest.php --- tests/CartTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index a18e15d..840cf5f 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1423,7 +1423,7 @@ class CartTest extends TestCase $cart->setGlobalTax(22); // check total - $this->assertEquals('254,12', $cart->total()); + $this->assertEquals(new Money('254.12', new Currency('USD')), $cart->total()); // check item price total $this->assertEquals(190, $cartItem->priceTotal); From e3678d7d3c41849ab876529a98b0fcafad36290b Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:55:31 +0100 Subject: [PATCH 098/156] Update DefaultCalculator.php --- src/Calculation/DefaultCalculator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Calculation/DefaultCalculator.php b/src/Calculation/DefaultCalculator.php index a9afba6..22141bb 100644 --- a/src/Calculation/DefaultCalculator.php +++ b/src/Calculation/DefaultCalculator.php @@ -23,7 +23,7 @@ class DefaultCalculator implements Calculator return $cartItem->price->multiply($cartItem->qty, config('cart.rounding', Money::ROUND_UP)); case 'subtotal': $subtotal = $cartItem->priceTotal->subtract($cartItem->discountTotal); - return $subtotal->isPositive() ? $subtotal : new Money(0, $this->price->getCurrency()); + return $subtotal->isPositive() ? $subtotal : new Money(0, $cartItem->price->getCurrency()); case 'priceTarget': return $cartItem->priceTotal->subtract($cartItem->discountTotal)->divide($cartItem->qty); case 'taxTotal': From cde948e9845d351b5119bb1f9ea4c3e275c81a86 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 20:57:53 +0100 Subject: [PATCH 099/156] Update CartTest.php --- tests/CartTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 840cf5f..a61ca2c 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1261,10 +1261,10 @@ class CartTest extends TestCase $cart->setGlobalTax(50); - $this->assertEquals(10.00, $cartItem->price(2)); - $this->assertEquals(5.00, $cart->subtotal(2)); //0.5 qty - $this->assertEquals(7.50, $cart->total(2)); // plus tax - $this->assertEquals(2.50, $cart->tax(2)); // tax of 5 Bucks + $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->price); + $this->assertEquals(new Money(5.00, new Currency('USD')), $cart->subtotal()); //0.5 qty + $this->assertEquals(new Money(7.50, new Currency('USD')), $cart->total()); // plus tax + $this->assertEquals(new Money(2.50, new Currency('USD')), $cart->tax()); // tax of 5 Bucks } /** @test */ From e4c0ddbac372b168853e57e1174bd0e9e50fe626 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:03:17 +0100 Subject: [PATCH 100/156] Update CartTest.php --- tests/CartTest.php | 82 ++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 42 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index a61ca2c..8d97ac0 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -950,15 +950,15 @@ class CartTest extends TestCase $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); - $this->assertEquals(10.00, $cartItem->price(2)); - $this->assertEquals(5.00, $cartItem->discount(2)); - $this->assertEquals(10.00, $cartItem->discountTotal(2)); - $this->assertEquals(5.00, $cartItem->priceTarget(2)); - $this->assertEquals(10.00, $cartItem->subtotal(2)); - $this->assertEquals(0.95, $cartItem->tax(2)); - $this->assertEquals(1.90, $cartItem->taxTotal(2)); - $this->assertEquals(5.95, $cartItem->priceTax(2)); - $this->assertEquals(11.90, $cartItem->total(2)); + $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->price); + $this->assertEquals(new Money(5.00, new Currency('USD')), $cartItem->discount); + $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->discountTotal); + $this->assertEquals(new Money(5.00, new Currency('USD')), $cartItem->priceTarget); + $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->subtotal); + $this->assertEquals(new Money(0.95, new Currency('USD')), $cartItem->tax); + $this->assertEquals(new Money(1.90, new Currency('USD')), $cartItem->taxTotal); + $this->assertEquals(new Money(5.95, new Currency('USD')), $cartItem->priceTax); + $this->assertEquals(new Money(11.90, new Currency('USD')), $cartItem->total); } /** @test */ @@ -975,15 +975,15 @@ class CartTest extends TestCase $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); - $this->assertEquals(10.00, $cartItem->price(2)); - $this->assertEquals(5.00, $cartItem->discount(2)); - $this->assertEquals(10.00, $cartItem->discountTotal(2)); - $this->assertEquals(5.00, $cartItem->priceTarget(2)); - $this->assertEquals(10.00, $cartItem->subtotal(2)); - $this->assertEquals(0.95, $cartItem->tax(2)); - $this->assertEquals(1.90, $cartItem->taxTotal(2)); - $this->assertEquals(5.95, $cartItem->priceTax(2)); - $this->assertEquals(11.90, $cartItem->total(2)); + $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->price); + $this->assertEquals(new Money(5.00, new Currency('USD')), $cartItem->discount); + $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->discountTotal); + $this->assertEquals(new Money(5.00, new Currency('USD')), $cartItem->priceTarget); + $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->subtotal); + $this->assertEquals(new Money(0.95, new Currency('USD')), $cartItem->tax); + $this->assertEquals(new Money(1.90, new Currency('USD')), $cartItem->taxTotal); + $this->assertEquals(new Money(5.95, new Currency('USD')), $cartItem->priceTax); + $this->assertEquals(new Money(11.90, new Currency('USD')), $cartItem->total); } /** @test */ @@ -1003,15 +1003,15 @@ class CartTest extends TestCase $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); - $this->assertEquals(10.00, $cartItem->price(2)); - $this->assertEquals(5.00, $cartItem->discount(2)); - $this->assertEquals(10.00, $cartItem->discountTotal(2)); - $this->assertEquals(5.00, $cartItem->priceTarget(2)); - $this->assertEquals(10.00, $cartItem->subtotal(2)); - $this->assertEquals(0.95, $cartItem->tax(2)); - $this->assertEquals(1.90, $cartItem->taxTotal(2)); - $this->assertEquals(5.95, $cartItem->priceTax(2)); - $this->assertEquals(11.90, $cartItem->total(2)); + $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->price); + $this->assertEquals(new Money(5.00, new Currency('USD')), $cartItem->discount); + $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->discountTotal); + $this->assertEquals(new Money(5.00, new Currency('USD')), $cartItem->priceTarget); + $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->subtotal); + $this->assertEquals(new Money(0.95, new Currency('USD')), $cartItem->tax); + $this->assertEquals(new Money(1.90, new Currency('USD')), $cartItem->taxTotal); + $this->assertEquals(new Money(5.95, new Currency('USD')), $cartItem->priceTax); + $this->assertEquals(new Money(11.90, new Currency('USD')), $cartItem->total); } /** @test */ @@ -1027,7 +1027,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals('20.00', $cartItem->total(2)); + $this->assertEquals(new Money('20.00', new Currency('USD')), $cartItem->total(2)); } /** @test */ @@ -1044,7 +1044,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals('10.00', $cartItem->total(2)); + $this->assertEquals(new Money('10.00', new Currency('USD')), $cartItem->total); } /** @test */ @@ -1059,7 +1059,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals('24.21', $cartItem->total(2)); + $this->assertEquals(new Money('24.21', new Currency('USD')), $cartItem->total); } /** @test */ @@ -1188,10 +1188,8 @@ class CartTest extends TestCase ]), 2); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 50); - $this->assertEquals('500.00', $cart->weight(2)); - $this->assertEquals(500.00, $cart->weight()); + $this->assertEquals(500, $cart->weight); $this->assertEquals(500.00, $cartItem->weightTotal); - $this->assertEquals('250.00', $cartItem->weight(2)); } /** @test */ @@ -1236,15 +1234,15 @@ class CartTest extends TestCase $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); - $this->assertEquals(10.00, $cartItem->price(2)); - $this->assertEquals(5.00, $cartItem->discount(2)); - $this->assertEquals(10.00, $cartItem->discountTotal(2)); - $this->assertEquals(5.00, $cartItem->priceTarget(2)); - $this->assertEquals(10.00, $cartItem->subtotal(2)); - $this->assertEquals(0.95, $cartItem->tax(2)); - $this->assertEquals(1.90, $cartItem->taxTotal(2)); - $this->assertEquals(5.95, $cartItem->priceTax(2)); - $this->assertEquals(11.90, $cartItem->total(2)); + $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->price); + $this->assertEquals(new Money(5.00, new Currency('USD')), $cartItem->discount); + $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->discountTotal); + $this->assertEquals(new Money(5.00, new Currency('USD')), $cartItem->priceTarget); + $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->subtotal); + $this->assertEquals(new Money(0.95, new Currency('USD')), $cartItem->tax); + $this->assertEquals(new Money(1.90, new Currency('USD')), $cartItem->taxTotal); + $this->assertEquals(new Money(5.95, new Currency('USD')), $cartItem->priceTax); + $this->assertEquals(new Money(11.90, new Currency('USD')), $cartItem->total); } /** @test */ From 0918d730065d787a787fc254a4be25c82506bfcd Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:06:30 +0100 Subject: [PATCH 101/156] Update CartTest.php --- tests/CartTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 8d97ac0..b2d78f4 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1027,7 +1027,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(new Money('20.00', new Currency('USD')), $cartItem->total(2)); + $this->assertEquals(new Money('20.00', new Currency('USD')), $cartItem->total); } /** @test */ @@ -1059,7 +1059,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(new Money('24.21', new Currency('USD')), $cartItem->total); + $this->assertEquals(new Money(24.21, new Currency('USD')), $cartItem->total); } /** @test */ From 612c73335a8e45b493d1476f4548f44c2970b65c Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:09:15 +0100 Subject: [PATCH 102/156] Update CartTest.php --- tests/CartTest.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index b2d78f4..e7d6c16 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -583,7 +583,7 @@ class CartTest extends TestCase ]), 2); $this->assertItemsInCart(3, $cart); - $this->assertEquals(60.00, $cart->subtotal()); + $this->assertEquals(new Money(60.00, new Currency('USD')), $cart->subtotal()); } /** @test */ @@ -724,7 +724,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(29.97, $cartItem->subtotal); + $this->assertEquals(new Money(29.97, new Currency('USD')), $cartItem->subtotal); } /** @test */ @@ -738,7 +738,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(2.10, $cartItem->tax); + $this->assertEquals(new Money(2.10, new Currency('USD')), $cartItem->tax); } /** @test */ @@ -754,7 +754,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(1.90, $cartItem->tax); + $this->assertEquals(new Money(1.90, new Currency('USD')), $cartItem->tax); } /** @test */ @@ -771,7 +771,7 @@ class CartTest extends TestCase 'price' => 20.00, ]), 2); - $this->assertEquals(10.50, $cart->tax); + $this->assertEquals(new Money(10.50, new Currency('USD')), $cart->tax); } /** @test */ @@ -787,7 +787,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(19, $cartItem->taxRate); + $this->assertEquals(new Money(19, new Currency('USD')), $cartItem->taxRate); } /** @test */ @@ -801,7 +801,7 @@ class CartTest extends TestCase 'price' => 20.00, ]), 2); - $this->assertEquals(50.00, $cart->subtotal); + $this->assertEquals(new Money(50.00, new Currency('USD')), $cart->subtotal()); } /** @test */ From b825a5728e7fca486c275490154556e01aa87d4f Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:17:55 +0100 Subject: [PATCH 103/156] Update CartTest.php --- tests/CartTest.php | 168 ++++++++++++++++++++------------------------- 1 file changed, 75 insertions(+), 93 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index e7d6c16..c81f6f8 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -173,7 +173,7 @@ class CartTest extends TestCase $cart = $this->getCart(); - $cart->add(1, 'Test item', 1, new Money(10.00, new Currency('USD'))); + $cart->add(1, 'Test item', 1, new Money(1000, new Currency('USD'))); $this->assertEquals(1, $cart->count()); @@ -187,7 +187,7 @@ class CartTest extends TestCase $cart = $this->getCart(); - $cart->add(['id' => 1, 'name' => 'Test item', 'qty' => 1, 'price' => new Money(10.00, new Currency('USD')), 'weight' => 550]); + $cart->add(['id' => 1, 'name' => 'Test item', 'qty' => 1, 'price' => new Money(1000, new Currency('USD')), 'weight' => 550]); $this->assertEquals(1, $cart->count()); @@ -202,8 +202,8 @@ class CartTest extends TestCase $cart = $this->getCart(); $cart->add([ - ['id' => 1, 'name' => 'Test item 1', 'qty' => 1, 'price' => new Money(10.00, new Currency('USD')), 'weight' => 550], - ['id' => 2, 'name' => 'Test item 2', 'qty' => 1, 'price' => new Money(10.00, new Currency('USD')), 'weight' => 550], + ['id' => 1, 'name' => 'Test item 1', 'qty' => 1, 'price' => new Money(1000, new Currency('USD')), 'weight' => 550], + ['id' => 2, 'name' => 'Test item 2', 'qty' => 1, 'price' => new Money(1000, new Currency('USD')), 'weight' => 550], ]); $this->assertEquals(2, $cart->count()); @@ -238,7 +238,7 @@ class CartTest extends TestCase $cart = $this->getCart(); - $cart->add(null, 'Some title', 1, new Money(10.00, new Currency('USD'))); + $cart->add(null, 'Some title', 1, new Money(1000, new Currency('USD'))); } /** @@ -250,7 +250,7 @@ class CartTest extends TestCase $cart = $this->getCart(); - $cart->add(1, 'Some title', 'invalid', new Money(10.00, new Currency('USD'))); + $cart->add(1, 'Some title', 'invalid', new Money(1000, new Currency('USD'))); } /** @@ -274,7 +274,7 @@ class CartTest extends TestCase $cart = $this->getCart(); - $cart->add(1, 'Some title', 1, new Money(10.00, new Currency('USD')), 'invalid'); + $cart->add(1, 'Some title', 1, new Money(1000, new Currency('USD')), 'invalid'); } /** @test */ @@ -583,7 +583,7 @@ class CartTest extends TestCase ]), 2); $this->assertItemsInCart(3, $cart); - $this->assertEquals(new Money(60.00, new Currency('USD')), $cart->subtotal()); + $this->assertEquals(new Money(6000, new Currency('USD')), $cart->subtotal()); } /** @test */ @@ -673,7 +673,7 @@ class CartTest extends TestCase { $cart = $this->getCart(); - $cart->add(1, 'Test item', 1, new Money(10.00, new Currency('USD'))); + $cart->add(1, 'Test item', 1, new Money(1000, new Currency('USD'))); $cart->associate('027c91341fd5cf4d2579b49c4b6a90da', new ProductModel()); @@ -692,7 +692,7 @@ class CartTest extends TestCase $cart = $this->getCart(); - $cart->add(1, 'Test item', 1, new Money(10.00, new Currency('USD'))); + $cart->add(1, 'Test item', 1, new Money(1000, new Currency('USD'))); $cart->associate('027c91341fd5cf4d2579b49c4b6a90da', 'SomeModel'); } @@ -702,7 +702,7 @@ class CartTest extends TestCase { $cart = $this->getCart(); - $cart->add(1, 'Test item', 1, new Money(10.00, new Currency('USD'))); + $cart->add(1, 'Test item', 1, new Money(1000, new Currency('USD'))); $cart->associate('027c91341fd5cf4d2579b49c4b6a90da', new ProductModel()); @@ -724,7 +724,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(new Money(29.97, new Currency('USD')), $cartItem->subtotal); + $this->assertEquals(new Money(2997, new Currency('USD')), $cartItem->subtotal); } /** @test */ @@ -738,7 +738,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(new Money(2.10, new Currency('USD')), $cartItem->tax); + $this->assertEquals(new Money(210, new Currency('USD')), $cartItem->tax); } /** @test */ @@ -754,7 +754,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(new Money(1.90, new Currency('USD')), $cartItem->tax); + $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->tax); } /** @test */ @@ -771,7 +771,7 @@ class CartTest extends TestCase 'price' => 20.00, ]), 2); - $this->assertEquals(new Money(10.50, new Currency('USD')), $cart->tax); + $this->assertEquals(new Money(1050, new Currency('USD')), $cart->tax); } /** @test */ @@ -801,7 +801,7 @@ class CartTest extends TestCase 'price' => 20.00, ]), 2); - $this->assertEquals(new Money(50.00, new Currency('USD')), $cart->subtotal()); + $this->assertEquals(new Money(5000, new Currency('USD')), $cart->subtotal()); } /** @test */ @@ -950,15 +950,15 @@ class CartTest extends TestCase $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); - $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->price); - $this->assertEquals(new Money(5.00, new Currency('USD')), $cartItem->discount); - $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->discountTotal); - $this->assertEquals(new Money(5.00, new Currency('USD')), $cartItem->priceTarget); - $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->subtotal); - $this->assertEquals(new Money(0.95, new Currency('USD')), $cartItem->tax); - $this->assertEquals(new Money(1.90, new Currency('USD')), $cartItem->taxTotal); - $this->assertEquals(new Money(5.95, new Currency('USD')), $cartItem->priceTax); - $this->assertEquals(new Money(11.90, new Currency('USD')), $cartItem->total); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); + $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->discount); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discountTotal); + $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->priceTarget); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal); + $this->assertEquals(new Money(095, new Currency('USD')), $cartItem->tax); + $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->taxTotal); + $this->assertEquals(new Money(595, new Currency('USD')), $cartItem->priceTax); + $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total); } /** @test */ @@ -975,15 +975,15 @@ class CartTest extends TestCase $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); - $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->price); - $this->assertEquals(new Money(5.00, new Currency('USD')), $cartItem->discount); - $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->discountTotal); - $this->assertEquals(new Money(5.00, new Currency('USD')), $cartItem->priceTarget); - $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->subtotal); - $this->assertEquals(new Money(0.95, new Currency('USD')), $cartItem->tax); - $this->assertEquals(new Money(1.90, new Currency('USD')), $cartItem->taxTotal); - $this->assertEquals(new Money(5.95, new Currency('USD')), $cartItem->priceTax); - $this->assertEquals(new Money(11.90, new Currency('USD')), $cartItem->total); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); + $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->discount); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discountTotal); + $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->priceTarget); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal); + $this->assertEquals(new Money(095, new Currency('USD')), $cartItem->tax); + $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->taxTotal); + $this->assertEquals(new Money(595, new Currency('USD')), $cartItem->priceTax); + $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total); } /** @test */ @@ -1003,15 +1003,15 @@ class CartTest extends TestCase $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); - $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->price); - $this->assertEquals(new Money(5.00, new Currency('USD')), $cartItem->discount); - $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->discountTotal); - $this->assertEquals(new Money(5.00, new Currency('USD')), $cartItem->priceTarget); - $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->subtotal); - $this->assertEquals(new Money(0.95, new Currency('USD')), $cartItem->tax); - $this->assertEquals(new Money(1.90, new Currency('USD')), $cartItem->taxTotal); - $this->assertEquals(new Money(5.95, new Currency('USD')), $cartItem->priceTax); - $this->assertEquals(new Money(11.90, new Currency('USD')), $cartItem->total); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); + $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->discount); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discountTotal); + $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->priceTarget); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal); + $this->assertEquals(new Money(095, new Currency('USD')), $cartItem->tax); + $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->taxTotal); + $this->assertEquals(new Money(595, new Currency('USD')), $cartItem->priceTax); + $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total); } /** @test */ @@ -1027,7 +1027,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(new Money('20.00', new Currency('USD')), $cartItem->total); + $this->assertEquals(new Money(2000, new Currency('USD')), $cartItem->total); } /** @test */ @@ -1044,7 +1044,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(new Money('10.00', new Currency('USD')), $cartItem->total); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->total); } /** @test */ @@ -1059,7 +1059,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(new Money(24.21, new Currency('USD')), $cartItem->total); + $this->assertEquals(new Money(2421, new Currency('USD')), $cartItem->total); } /** @test */ @@ -1086,12 +1086,12 @@ class CartTest extends TestCase $cart2->setGlobalTax(0); $cart2->setGlobalDiscount(0); - $this->assertEquals('0', $cart2->countItems()); + $this->assertEquals(new Money(0, new Currency('USD')), $cart2->countItems()); $cart2->merge('test'); $this->assertEquals('2', $cart2->countItems()); - $this->assertEquals(20, $cart2->total()); + $this->assertEquals(new Money(2000, new Currency('USD')), $cart2->total()); $cart3 = $this->getCart(); $cart3->instance('test3'); @@ -1100,7 +1100,7 @@ class CartTest extends TestCase $cart3->merge('test', true); - $this->assertEquals(10, $cart3->total()); + $this->assertEquals(new Money(1000, new Currency('USD')), $cart3->total()); } /** @test */ @@ -1131,16 +1131,11 @@ class CartTest extends TestCase ]), 1); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); - $this->assertEquals('10.00', $cart->initial(2)); - $this->assertEquals(10.00, $cart->initial()); - $this->assertEquals('5.00', $cart->discount(2)); - $this->assertEquals(5.00, $cart->discount()); - $this->assertEquals('5.00', $cart->subtotal(2)); - $this->assertEquals(5.00, $cart->subtotal()); - $this->assertEquals('0.95', $cart->tax(2)); - $this->assertEquals(0.95, $cart->tax()); - $this->assertEquals('5.95', $cart->total(2)); - $this->assertEquals(5.95, $cart->total()); + $this->assertEquals(new Money(1000, new Currency('USD')), $cart->initial()); + $this->assertEquals(new Money(500, new Currency('USD')), $cart->discount()); + $this->assertEquals(new Money(500, new Currency('USD')), $cart->subtotal()); + $this->assertEquals(new Money(95, new Currency('USD')), $cart->tax()); + $this->assertEquals(new Money(595, new Currency('USD')), $cart->total()); } /** @test */ @@ -1234,15 +1229,15 @@ class CartTest extends TestCase $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); - $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->price); - $this->assertEquals(new Money(5.00, new Currency('USD')), $cartItem->discount); - $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->discountTotal); - $this->assertEquals(new Money(5.00, new Currency('USD')), $cartItem->priceTarget); - $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->subtotal); - $this->assertEquals(new Money(0.95, new Currency('USD')), $cartItem->tax); - $this->assertEquals(new Money(1.90, new Currency('USD')), $cartItem->taxTotal); - $this->assertEquals(new Money(5.95, new Currency('USD')), $cartItem->priceTax); - $this->assertEquals(new Money(11.90, new Currency('USD')), $cartItem->total); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); + $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->discount); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discountTotal); + $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->priceTarget); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal); + $this->assertEquals(new Money(095, new Currency('USD')), $cartItem->tax); + $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->taxTotal); + $this->assertEquals(new Money(595, new Currency('USD')), $cartItem->priceTax); + $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total); } /** @test */ @@ -1259,10 +1254,10 @@ class CartTest extends TestCase $cart->setGlobalTax(50); - $this->assertEquals(new Money(10.00, new Currency('USD')), $cartItem->price); - $this->assertEquals(new Money(5.00, new Currency('USD')), $cart->subtotal()); //0.5 qty - $this->assertEquals(new Money(7.50, new Currency('USD')), $cart->total()); // plus tax - $this->assertEquals(new Money(2.50, new Currency('USD')), $cart->tax()); // tax of 5 Bucks + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); + $this->assertEquals(new Money(500, new Currency('USD')), $cart->subtotal()); //0.5 qty + $this->assertEquals(new Money(750, new Currency('USD')), $cart->total()); // plus tax + $this->assertEquals(new Money(250, new Currency('USD')), $cart->tax()); // tax of 5 Bucks } /** @test */ @@ -1271,7 +1266,7 @@ class CartTest extends TestCase // https://github.com/bumbummen99/LaravelShoppingcart/pull/5 $cart = $this->getCart(); - $cartItem = $cart->add('293ad', 'Product 1', 1, new Money(10.00, new Currency('USD')), 550, new CartItemOptions(['size' => 'large'])); + $cartItem = $cart->add('293ad', 'Product 1', 1, new Money(1000, new Currency('USD')), 550, new CartItemOptions(['size' => 'large'])); $this->assertEquals(550, $cartItem->weight); $this->assertTrue($cartItem->options->has('size')); @@ -1336,14 +1331,14 @@ class CartTest extends TestCase $cart2->setGlobalTax(0); $cart2->setGlobalDiscount(0); - $this->assertEquals('0', $cart2->countItems()); + $this->assertEquals(0, $cart2->countItems()); $cart2->merge('test'); Event::assertDispatched('cart.added', 2); Event::assertDispatched('cart.merged'); - $this->assertEquals('2', $cart2->countItems()); - $this->assertEquals(20, $cart2->total()); + $this->assertEquals(2, $cart2->countItems()); + $this->assertEquals(new Money(2000, new Currency('USD')), $cart2->total()); }); } @@ -1372,7 +1367,7 @@ class CartTest extends TestCase $cart->setGlobalTax(22); // check total - $this->assertEquals('253,29', $cart->total()); + $this->assertEquals(new Money(25329, new Currency('USD')), $cart->total()); // check that the sum of cart subvalues matches the total (in order to avoid cart summary to looks wrong) $this->assertEquals($cart->total(), $cart->subtotal()->add($cart->tax())); @@ -1392,7 +1387,7 @@ class CartTest extends TestCase $cart->setGlobalTax(22); // check net price - $this->assertEquals(new Money(81.97, new Currency('USD')), $cartItem->priceNet); + $this->assertEquals(new Money(8197, new Currency('USD')), $cartItem->priceNet); } /** @test */ @@ -1421,7 +1416,7 @@ class CartTest extends TestCase $cart->setGlobalTax(22); // check total - $this->assertEquals(new Money('254.12', new Currency('USD')), $cart->total()); + $this->assertEquals(new Money(25412, new Currency('USD')), $cart->total()); // check item price total $this->assertEquals(190, $cartItem->priceTotal); @@ -1513,20 +1508,7 @@ class CartTest extends TestCase 'name' => 'first item', 'price' => 1000, ]), 5); - $this->assertEquals(5000, $cart->priceTotal()); - } - - /** @test */ - public function it_can_format_the_total_price_of_the_items_in_cart() - { - $cart = $this->getCart(); - - $cart->add(new BuyableProduct([ - 'name' => 'first item', - 'price' => 1000, - ]), 5); - $this->assertEquals('5,000.00', $cart->priceTotal()); - $this->assertEquals('5,000.0000', $cart->priceTotal(4, '.', ',')); + $this->assertEquals(new Money(500000, new Currency('USD')), $cart->priceTotal()); } /** @test */ From 2c272169900133696a9b3107229ce615035de5f7 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:18:07 +0100 Subject: [PATCH 104/156] Update CartItemTest.php --- tests/CartItemTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index 3823296..a073d5c 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -26,7 +26,7 @@ class CartItemTest extends TestCase /** @test */ public function it_can_be_cast_to_an_array() { - $cartItem = new CartItem(1, 'Some item', new Money(10.00, new Currency('USD')), 550, 2, new CartItemOptions(['size' => 'XL', 'color' => 'red'])); + $cartItem = new CartItem(1, 'Some item', new Money(1000, new Currency('USD')), 550, 2, new CartItemOptions(['size' => 'XL', 'color' => 'red'])); $this->assertEquals([ 'id' => 1, @@ -48,7 +48,7 @@ class CartItemTest extends TestCase /** @test */ public function it_can_be_cast_to_json() { - $cartItem = new CartItem(1, 'Some item', new Money(10.00, new Currency('USD')), 550, 2, new CartItemOptions(['size' => 'XL', 'color' => 'red'])); + $cartItem = new CartItem(1, 'Some item', new Money(1000, new Currency('USD')), 550, 2, new CartItemOptions(['size' => 'XL', 'color' => 'red'])); $this->assertJson($cartItem->toJson()); From 2493a90d73c61edd6bc1307a8bb57b0caa2d6a1d Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:19:33 +0100 Subject: [PATCH 105/156] Update CartTest.php --- tests/CartTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index c81f6f8..5613cbf 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -955,7 +955,7 @@ class CartTest extends TestCase $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discountTotal); $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->priceTarget); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal); - $this->assertEquals(new Money(095, new Currency('USD')), $cartItem->tax); + $this->assertEquals(new Money(95, new Currency('USD')), $cartItem->tax); $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->taxTotal); $this->assertEquals(new Money(595, new Currency('USD')), $cartItem->priceTax); $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total); From 7fdda80bd3c1ac8ce2cfefc6425ec7645e121544 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:20:37 +0100 Subject: [PATCH 106/156] Update CartTest.php --- tests/CartTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 5613cbf..be8ab31 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -980,7 +980,7 @@ class CartTest extends TestCase $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discountTotal); $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->priceTarget); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal); - $this->assertEquals(new Money(095, new Currency('USD')), $cartItem->tax); + $this->assertEquals(new Money(95, new Currency('USD')), $cartItem->tax); $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->taxTotal); $this->assertEquals(new Money(595, new Currency('USD')), $cartItem->priceTax); $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total); @@ -1008,7 +1008,7 @@ class CartTest extends TestCase $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discountTotal); $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->priceTarget); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal); - $this->assertEquals(new Money(095, new Currency('USD')), $cartItem->tax); + $this->assertEquals(new Money(95, new Currency('USD')), $cartItem->tax); $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->taxTotal); $this->assertEquals(new Money(595, new Currency('USD')), $cartItem->priceTax); $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total); @@ -1234,7 +1234,7 @@ class CartTest extends TestCase $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discountTotal); $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->priceTarget); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal); - $this->assertEquals(new Money(095, new Currency('USD')), $cartItem->tax); + $this->assertEquals(new Money(95, new Currency('USD')), $cartItem->tax); $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->taxTotal); $this->assertEquals(new Money(595, new Currency('USD')), $cartItem->priceTax); $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total); From 230fd57c924a45726e4d46bf773a5ce2e96f73a7 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:24:19 +0100 Subject: [PATCH 107/156] Update CartItem.php --- src/CartItem.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/CartItem.php b/src/CartItem.php index cd7b9bc..bd68d3f 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -10,6 +10,8 @@ use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Support\Arr; use Money\Money; +use Money\Formatter\DecimalMoneyFormatter; +use Money\Currencies\ISOCurrencies; use ReflectionClass; /** @@ -283,6 +285,14 @@ class CartItem implements Arrayable, Jsonable return json_encode($this->toArray(), $options); } + /** + * Generate a unique id for the cart item. + */ + private function formatMoney(Money $money) : string + { + return (new DecimalMoneyFormatter(new ISOCurrencies()))->format($money); + } + /** * Generate a unique id for the cart item. */ From 47f436b999c6f297b99be50a66426b60778676d4 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:25:09 +0100 Subject: [PATCH 108/156] Update CartItem.php --- src/CartItem.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/CartItem.php b/src/CartItem.php index bd68d3f..9bec287 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -264,12 +264,12 @@ class CartItem implements Arrayable, Jsonable 'id' => $this->id, 'name' => $this->name, 'qty' => $this->qty, - 'price' => $this->price, + 'price' => self::formatMoney($this->price), 'weight' => $this->weight, 'options' => $this->options->toArray(), - 'discount' => $this->discount, - 'tax' => $this->tax, - 'subtotal' => $this->subtotal, + 'discount' => self::formatMoney($this->discount), + 'tax' => self::formatMoney($this->tax), + 'subtotal' => self::formatMoney($this->subtotal), ]; } @@ -288,7 +288,7 @@ class CartItem implements Arrayable, Jsonable /** * Generate a unique id for the cart item. */ - private function formatMoney(Money $money) : string + private static function formatMoney(Money $money) : string { return (new DecimalMoneyFormatter(new ISOCurrencies()))->format($money); } From 892087d2a8dfaa92be6945547a089e22d5a16b2e Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:29:32 +0100 Subject: [PATCH 109/156] Update CartItemTest.php --- tests/CartItemTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index a073d5c..b42838e 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -31,7 +31,7 @@ class CartItemTest extends TestCase $this->assertEquals([ 'id' => 1, 'name' => 'Some item', - 'price' => 10.00, + 'price' => '10.00', 'rowId' => '07d5da5550494c62daf9993cf954303f', 'qty' => 2, 'options' => [ From 87d63e6b5b092cd0bd73a93e9b0e37c06beca510 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:33:44 +0100 Subject: [PATCH 110/156] Update CartTest.php --- tests/CartTest.php | 110 +++++++-------------------------------------- 1 file changed, 15 insertions(+), 95 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index be8ab31..393f43e 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -532,24 +532,24 @@ class CartTest extends TestCase 'id' => 1, 'name' => 'Item name', 'qty' => 1, - 'price' => 10.00, - 'tax' => 2.10, - 'subtotal' => 10.0, + 'price' => '10.00', + 'tax' => '2.10', + 'subtotal' => '10.00', 'options' => [], - 'discount' => 0.0, - 'weight' => 0.0, + 'discount' => '0', + 'weight' => '0.00', ], '370d08585360f5c568b18d1f2e4ca1df' => [ 'rowId' => '370d08585360f5c568b18d1f2e4ca1df', 'id' => 2, 'name' => 'Item name', 'qty' => 1, - 'price' => 10.00, - 'tax' => 2.10, - 'subtotal' => 10.0, + 'price' => '10.00', + 'tax' => '2.10', + 'subtotal' => '10.00', 'options' => [], - 'discount' => 0.0, - 'weight' => 0.0, + 'discount' => '0', + 'weight' => '0.00', ], ], $content->toArray()); } @@ -579,7 +579,7 @@ class CartTest extends TestCase $cart->add(new BuyableProduct([ 'id' => 2, 'name' => 'Second item', - 'price' => 25.00, + 'price' => '25.00', ]), 2); $this->assertItemsInCart(3, $cart); @@ -719,7 +719,7 @@ class CartTest extends TestCase $cart->add(new BuyableProduct([ 'name' => 'Some title', - 'price' => 9.99, + 'price' => '9.99', ]), 3); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); @@ -768,7 +768,7 @@ class CartTest extends TestCase $cart->add(new BuyableProduct([ 'id' => 2, 'name' => 'Some title', - 'price' => 20.00, + 'price' => '20.00', ]), 2); $this->assertEquals(new Money(1050, new Currency('USD')), $cart->tax); @@ -798,7 +798,7 @@ class CartTest extends TestCase $cart->add(new BuyableProduct(), 1); $cart->add(new BuyableProduct([ 'id' => 2, - 'price' => 20.00, + 'price' => '20.00', ]), 2); $this->assertEquals(new Money(5000, new Currency('USD')), $cart->subtotal()); @@ -992,7 +992,7 @@ class CartTest extends TestCase $cart = $this->getCartDiscount(50); $cart->add(new BuyableProduct([ 'name' => 'First item', - 'price' => 5.00, + 'price' => '5.00', ]), 2); $cart->update('027c91341fd5cf4d2579b49c4b6a90da', new BuyableProduct([ @@ -1047,21 +1047,6 @@ class CartTest extends TestCase $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->total); } - /** @test */ - public function cart_has_no_rounding_errors() - { - $cart = $this->getCart(); - - $cart->add(new BuyableProduct([ - 'name' => 'Item', - 'price' => 10.004, - ]), 2); - - $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - - $this->assertEquals(new Money(2421, new Currency('USD')), $cartItem->total); - } - /** @test */ public function it_can_merge_multiple_carts() { @@ -1342,37 +1327,6 @@ class CartTest extends TestCase }); } - /** @test */ - public function it_use_correctly_rounded_values_for_totals_and_cart_summary() - { - $this->setConfigFormat(2, ',', ''); - - $cart = $this->getCartDiscount(6); - - $cartItem = $cart->add(new BuyableProduct([ - 'name' => 'First item', - 'price' => 0.18929, - ]), 1000); - $cart->add(new BuyableProduct([ - 'id' => 2, - 'name' => 'Second item', - 'price' => 4.41632, - ]), 5); - $cart->add(new BuyableProduct([ - 'id' => 3, - 'name' => 'Third item', - 'price' => 0.37995, - ]), 25); - - $cart->setGlobalTax(22); - - // check total - $this->assertEquals(new Money(25329, new Currency('USD')), $cart->total()); - - // check that the sum of cart subvalues matches the total (in order to avoid cart summary to looks wrong) - $this->assertEquals($cart->total(), $cart->subtotal()->add($cart->tax())); - } - /** @test */ public function it_use_gross_price_as_base_price() { @@ -1390,40 +1344,6 @@ class CartTest extends TestCase $this->assertEquals(new Money(8197, new Currency('USD')), $cartItem->priceNet); } - /** @test */ - public function it_use_gross_price_and_it_use_correctly_rounded_values_for_totals_and_cart_summary() - { - $this->setConfigFormat(2, ',', ''); - config(['cart.calculator' => GrossPrice::class]); - - $cart = $this->getCartDiscount(6); - - $cartItem = $cart->add(new BuyableProduct([ - 'name' => 'First item', - 'price' => 0.23093, - ]), 1000); - $cart->add(new BuyableProduct([ - 'id' => 2, - 'name' => 'Second item', - 'price' => 5.38791, - ]), 5); - $cart->add(new BuyableProduct([ - 'id' => 3, - 'name' => 'Third item', - 'price' => 0.46354, - ]), 25); - - $cart->setGlobalTax(22); - - // check total - $this->assertEquals(new Money(25412, new Currency('USD')), $cart->total()); - - // check item price total - $this->assertEquals(190, $cartItem->priceTotal); - // check that the sum of cart subvalues matches the total (in order to avoid cart summary to looks wrong) - $this->assertEquals($cart->total(), $cart->subtotal()->add($cart->tax())); - } - /** * Get an instance of the cart. * From 747ee70edbb4411811d5a2669686dd584398b66d Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:34:00 +0100 Subject: [PATCH 111/156] Update BuyableProduct.php --- tests/Fixtures/BuyableProduct.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Fixtures/BuyableProduct.php b/tests/Fixtures/BuyableProduct.php index ae7cedc..147b421 100644 --- a/tests/Fixtures/BuyableProduct.php +++ b/tests/Fixtures/BuyableProduct.php @@ -28,7 +28,7 @@ class BuyableProduct extends Model implements Buyable protected $attributes = [ 'id' => 1, 'name' => 'Item name', - 'price' => 10.00, + 'price' => '10.00', 'currency' => 'USD', 'weight' => 0, ]; From 43a0aa4c1504082c6a464e482b7bc07d24b27b6a Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:34:10 +0100 Subject: [PATCH 112/156] Update BuyableProductTrait.php --- tests/Fixtures/BuyableProductTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Fixtures/BuyableProductTrait.php b/tests/Fixtures/BuyableProductTrait.php index d9d3572..9ac5bdd 100644 --- a/tests/Fixtures/BuyableProductTrait.php +++ b/tests/Fixtures/BuyableProductTrait.php @@ -27,7 +27,7 @@ class BuyableProductTrait extends Model implements Buyable protected $attributes = [ 'id' => 1, 'name' => 'Item name', - 'price' => 10.00, + 'price' => '10.00', 'currency' => 'USD', 'weight' => 0, ]; From 9d8e45d5d624c53cf18b348ced0104b5dfd363b4 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:37:51 +0100 Subject: [PATCH 113/156] Update CartTest.php --- tests/CartTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 393f43e..0177868 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1428,7 +1428,7 @@ class CartTest extends TestCase 'name' => 'first item', 'price' => 1000, ]), 5); - $this->assertEquals(new Money(500000, new Currency('USD')), $cart->priceTotal()); + $this->assertEquals(new Money(5000, new Currency('USD')), $cart->priceTotal()); } /** @test */ From 0cd1ebff6a24f0d61746d4a41ab506a71ba3ffc8 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:38:06 +0100 Subject: [PATCH 114/156] Update BuyableProduct.php --- tests/Fixtures/BuyableProduct.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Fixtures/BuyableProduct.php b/tests/Fixtures/BuyableProduct.php index 147b421..32dc098 100644 --- a/tests/Fixtures/BuyableProduct.php +++ b/tests/Fixtures/BuyableProduct.php @@ -28,7 +28,7 @@ class BuyableProduct extends Model implements Buyable protected $attributes = [ 'id' => 1, 'name' => 'Item name', - 'price' => '10.00', + 'price' => 1000, 'currency' => 'USD', 'weight' => 0, ]; From ed7269f40d2eb1772110ecf97aaa699330bcad07 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:38:17 +0100 Subject: [PATCH 115/156] Update BuyableProductTrait.php --- tests/Fixtures/BuyableProductTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Fixtures/BuyableProductTrait.php b/tests/Fixtures/BuyableProductTrait.php index 9ac5bdd..17a9811 100644 --- a/tests/Fixtures/BuyableProductTrait.php +++ b/tests/Fixtures/BuyableProductTrait.php @@ -27,7 +27,7 @@ class BuyableProductTrait extends Model implements Buyable protected $attributes = [ 'id' => 1, 'name' => 'Item name', - 'price' => '10.00', + 'price' => 1000, 'currency' => 'USD', 'weight' => 0, ]; From 9f4cb72cf7fccc70fbb4fba6826c6197df87e195 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:45:12 +0100 Subject: [PATCH 116/156] Update CartTest.php --- tests/CartTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 0177868..b2ad71d 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -537,7 +537,7 @@ class CartTest extends TestCase 'subtotal' => '10.00', 'options' => [], 'discount' => '0', - 'weight' => '0.00', + 'weight' => 0, ], '370d08585360f5c568b18d1f2e4ca1df' => [ 'rowId' => '370d08585360f5c568b18d1f2e4ca1df', @@ -549,7 +549,7 @@ class CartTest extends TestCase 'subtotal' => '10.00', 'options' => [], 'discount' => '0', - 'weight' => '0.00', + 'weight' => 0, ], ], $content->toArray()); } From 97c931296cf372f61d384b2f4a8d7e619fd8fd3f Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:49:43 +0100 Subject: [PATCH 117/156] Update CartItemTest.php --- tests/CartItemTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index b42838e..1a10bc5 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -26,7 +26,7 @@ class CartItemTest extends TestCase /** @test */ public function it_can_be_cast_to_an_array() { - $cartItem = new CartItem(1, 'Some item', new Money(1000, new Currency('USD')), 550, 2, new CartItemOptions(['size' => 'XL', 'color' => 'red'])); + $cartItem = new CartItem(1, 'Some item', new Money(1000, new Currency('USD')), 2, 550, new CartItemOptions(['size' => 'XL', 'color' => 'red'])); $this->assertEquals([ 'id' => 1, @@ -48,7 +48,7 @@ class CartItemTest extends TestCase /** @test */ public function it_can_be_cast_to_json() { - $cartItem = new CartItem(1, 'Some item', new Money(1000, new Currency('USD')), 550, 2, new CartItemOptions(['size' => 'XL', 'color' => 'red'])); + $cartItem = new CartItem(1, 'Some item', new Money(1000, new Currency('USD')), 2, 550, new CartItemOptions(['size' => 'XL', 'color' => 'red'])); $this->assertJson($cartItem->toJson()); From 5cc040281aa99521319b8005f5f43e17a46eb730 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:52:12 +0100 Subject: [PATCH 118/156] Update CartTest.php --- tests/CartTest.php | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index b2ad71d..0873226 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1225,26 +1225,6 @@ class CartTest extends TestCase $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total); } - /** @test */ - public function it_does_calculate_correct_results_with_rational_qtys() - { - // https://github.com/Crinsane/LaravelShoppingcart/issues/544 - $cart = $this->getCart(); - - $cart->add(new BuyableProductTrait([ - 'name' => 'First item', - ]), 0.5); - - $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - - $cart->setGlobalTax(50); - - $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); - $this->assertEquals(new Money(500, new Currency('USD')), $cart->subtotal()); //0.5 qty - $this->assertEquals(new Money(750, new Currency('USD')), $cart->total()); // plus tax - $this->assertEquals(new Money(250, new Currency('USD')), $cart->tax()); // tax of 5 Bucks - } - /** @test */ public function it_does_allow_adding_cart_items_with_weight_and_options() { From 1922884abd086b9c938aa1a588dd9432d91f2b76 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:53:50 +0100 Subject: [PATCH 119/156] Update CartItem.php --- src/CartItem.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/CartItem.php b/src/CartItem.php index 9bec287..e50a301 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -89,16 +89,7 @@ class CartItem implements Arrayable, Jsonable */ public ?string $instance = null; - /** - * CartItem constructor. - * - * @param int|string $id - * @param string $name - * @param float $price - * @param float $weight - * @param array $options - */ - public function __construct($id, string $name, Money $price, int $qty = 1, int $weight = 0, ?CartItemOptions $options = null) + public function __construct(int|string $id, string $name, Money $price, int $qty = 1, int $weight = 0, ?CartItemOptions $options = null) { if (!is_string($id) && !is_int($id)) { throw new \InvalidArgumentException('Please supply a valid identifier.'); From 68d4185791470a46204e9a2ce45523327b82f36b Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:54:52 +0100 Subject: [PATCH 120/156] Update CartItemTest.php --- tests/CartItemTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index 1a10bc5..7c24ea9 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -41,7 +41,7 @@ class CartItemTest extends TestCase 'tax' => 0, 'subtotal' => 20.00, 'discount' => 0.0, - 'weight' => 550.0, + 'weight' => 550, ], $cartItem->toArray()); } @@ -52,7 +52,7 @@ class CartItemTest extends TestCase $this->assertJson($cartItem->toJson()); - $json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","qty":2,"price":10,"weight":550,"options":{"size":"XL","color":"red"},"discount":0,"tax":0,"subtotal":20}'; + $json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","qty":2,"price":10.00,"weight":550,"options":{"size":"XL","color":"red"},"discount":0,"tax":0,"subtotal":20}'; $this->assertEquals($json, $cartItem->toJson()); } From 7ae044841ca50953849944d452039ca19c8c5847 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 21:56:03 +0100 Subject: [PATCH 121/156] Update CartItemTest.php --- tests/CartItemTest.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index 7c24ea9..4261f10 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -38,9 +38,9 @@ class CartItemTest extends TestCase 'size' => 'XL', 'color' => 'red', ], - 'tax' => 0, - 'subtotal' => 20.00, - 'discount' => 0.0, + 'tax' => '0.00', + 'subtotal' => '20.00', + 'discount' => '0.00', 'weight' => 550, ], $cartItem->toArray()); } @@ -52,7 +52,7 @@ class CartItemTest extends TestCase $this->assertJson($cartItem->toJson()); - $json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","qty":2,"price":10.00,"weight":550,"options":{"size":"XL","color":"red"},"discount":0,"tax":0,"subtotal":20}'; + $json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","qty":2,"price":10.00,"weight":550,"options":{"size":"XL","color":"red"},"discount":"0.00","tax":"10.00","subtotal":"20.00"}'; $this->assertEquals($json, $cartItem->toJson()); } From 543483b14d51476f402cb9bf7cd8af1296db54f1 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 22:13:42 +0100 Subject: [PATCH 122/156] Update CartTest.php --- tests/CartTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 0873226..a4b1c8d 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -536,7 +536,7 @@ class CartTest extends TestCase 'tax' => '2.10', 'subtotal' => '10.00', 'options' => [], - 'discount' => '0', + 'discount' => '0.00', 'weight' => 0, ], '370d08585360f5c568b18d1f2e4ca1df' => [ @@ -548,7 +548,7 @@ class CartTest extends TestCase 'tax' => '2.10', 'subtotal' => '10.00', 'options' => [], - 'discount' => '0', + 'discount' => '0.00', 'weight' => 0, ], ], $content->toArray()); From 4782c52f4d832172f9f186e4933cfe0ab17866c8 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 22:15:32 +0100 Subject: [PATCH 123/156] Update CartItemTest.php --- tests/CartItemTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index 4261f10..4fb4bb0 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -52,7 +52,7 @@ class CartItemTest extends TestCase $this->assertJson($cartItem->toJson()); - $json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","qty":2,"price":10.00,"weight":550,"options":{"size":"XL","color":"red"},"discount":"0.00","tax":"10.00","subtotal":"20.00"}'; + $json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","qty":2,"price":"10.00","weight":550,"options":{"size":"XL","color":"red"},"discount":"0.00","tax":"10.00","subtotal":"20.00"}'; $this->assertEquals($json, $cartItem->toJson()); } From 75b63a294930672cec8dc684f0c375f29e247c63 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sat, 5 Feb 2022 22:26:46 +0100 Subject: [PATCH 124/156] Fix --- tests/CartTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index a4b1c8d..bb181a9 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -161,7 +161,10 @@ class CartTest extends TestCase $this->assertTrue(is_array($cartItems)); $this->assertCount(2, $cartItems); - $this->assertContainsOnlyInstancesOf(CartItem::class, $cartItems); + $this->assertIsIterable($cartItems); + if (is_iterable($cartItems)) { + $this->assertContainsOnlyInstancesOf(CartItem::class, $cartItems); + } Event::assertDispatched('cart.added'); } From 2b45f9461956e10b5c6c713afff4bdddc622fe2a Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sat, 5 Feb 2022 22:31:32 +0100 Subject: [PATCH 125/156] Fix --- tests/CartTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index bb181a9..79abc89 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1074,7 +1074,7 @@ class CartTest extends TestCase $cart2->setGlobalTax(0); $cart2->setGlobalDiscount(0); - $this->assertEquals(new Money(0, new Currency('USD')), $cart2->countItems()); + $this->assertEquals(0, $cart2->countItems()); $cart2->merge('test'); From 6f8f3f457e44a863e5b2df5c74b96663e990221e Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sat, 5 Feb 2022 22:33:52 +0100 Subject: [PATCH 126/156] Fix --- tests/CartTest.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 79abc89..ca02309 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1170,9 +1170,8 @@ class CartTest extends TestCase 'weight' => 250, ]), 2); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 50); - $this->assertEquals(500, $cart->weight); - $this->assertEquals(500.00, $cartItem->weightTotal); + $this->assertEquals(500, $cart->weight()); + $this->assertEquals(500, $cartItem->weightTotal); } /** @test */ From 9afad78367cb4a72b6596a0a68a7b05e2a9e07d5 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sat, 5 Feb 2022 22:36:54 +0100 Subject: [PATCH 127/156] Fix --- tests/CartTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index ca02309..81cca82 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -1270,8 +1270,8 @@ class CartTest extends TestCase Event::assertNotDispatched('cart.added'); Event::assertDispatched('cart.merged'); - $this->assertEquals('2', $cart2->countItems()); - $this->assertEquals(20, $cart2->total()); + $this->assertEquals(2, $cart2->countItems()); + $this->assertEquals(new Money(2000, new Currency('USD')), $cart2->total()); }); } From 4415db2be6ed67cc828934af89e5d506c1dbe55a Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sat, 5 Feb 2022 22:38:27 +0100 Subject: [PATCH 128/156] Fix --- tests/CartTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 81cca82..91c9b01 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -790,7 +790,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(new Money(19, new Currency('USD')), $cartItem->taxRate); + $this->assertEquals(19.0, $cartItem->taxRate); } /** @test */ From a4322a2ac317b17adc6302d3eb4c1333eec9a8f2 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sat, 5 Feb 2022 23:37:59 +0100 Subject: [PATCH 129/156] Further changes, remove Calculators --- src/Calculation/DefaultCalculator.php | 37 ------- src/Calculation/GrossPrice.php | 39 ------- src/Cart.php | 88 +++++++++------- src/CartItem.php | 142 ++++++++++++++------------ src/Contracts/Calculator.php | 10 -- tests/CartTest.php | 14 +-- 6 files changed, 132 insertions(+), 198 deletions(-) delete mode 100644 src/Calculation/DefaultCalculator.php delete mode 100644 src/Calculation/GrossPrice.php delete mode 100644 src/Contracts/Calculator.php diff --git a/src/Calculation/DefaultCalculator.php b/src/Calculation/DefaultCalculator.php deleted file mode 100644 index 22141bb..0000000 --- a/src/Calculation/DefaultCalculator.php +++ /dev/null @@ -1,37 +0,0 @@ -price->multiply($cartItem->discountRate, config('cart.rounding', Money::ROUND_UP)); - case 'tax': - return $cartItem->priceTarget->multiply($cartItem->taxRate + 1, config('cart.rounding', Money::ROUND_UP)); - case 'priceTax': - return $cartItem->priceTarget->add($cartItem->tax); - case 'discountTotal': - return $cartItem->discount->multiply($cartItem->qty, config('cart.rounding', Money::ROUND_UP)); - case 'priceTotal': - return $cartItem->price->multiply($cartItem->qty, config('cart.rounding', Money::ROUND_UP)); - case 'subtotal': - $subtotal = $cartItem->priceTotal->subtract($cartItem->discountTotal); - return $subtotal->isPositive() ? $subtotal : new Money(0, $cartItem->price->getCurrency()); - case 'priceTarget': - return $cartItem->priceTotal->subtract($cartItem->discountTotal)->divide($cartItem->qty); - case 'taxTotal': - return $cartItem->subtotal->multiply($cartItem->taxRate + 1, config('cart.rounding', Money::ROUND_UP)); - case 'total': - return $cartItem->subtotal->add($cartItem->taxTotal); - default: - return; - } - } -} diff --git a/src/Calculation/GrossPrice.php b/src/Calculation/GrossPrice.php deleted file mode 100644 index 92cc219..0000000 --- a/src/Calculation/GrossPrice.php +++ /dev/null @@ -1,39 +0,0 @@ -price / (1 + ($cartItem->taxRate / 100)), $decimals); - case 'discount': - return $cartItem->priceNet * ($cartItem->getDiscountRate() / 100); - case 'tax': - return round($cartItem->priceTarget * ($cartItem->taxRate / 100), $decimals); - case 'priceTax': - return round($cartItem->priceTarget + $cartItem->tax, $decimals); - case 'discountTotal': - return round($cartItem->discount * $cartItem->qty, $decimals); - case 'priceTotal': - return round($cartItem->priceNet * $cartItem->qty, $decimals); - case 'subtotal': - return max(round($cartItem->priceTotal - $cartItem->discountTotal, $decimals), 0); - case 'priceTarget': - return round(($cartItem->priceTotal - $cartItem->discountTotal) / $cartItem->qty, $decimals); - case 'taxTotal': - return round($cartItem->subtotal * ($cartItem->taxRate / 100), $decimals); - case 'total': - return round($cartItem->subtotal + $cartItem->taxTotal, $decimals); - default: - return; - } - } -} diff --git a/src/Cart.php b/src/Cart.php index 04d775a..625a1ad 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -184,7 +184,7 @@ class Cart $item->setInstance($this->currentInstance()); if (! $keepDiscount) { - $item->setDiscountRate($this->discount); + $item->setDiscount($this->discount); } if (!$keepTax) { @@ -350,33 +350,19 @@ class Cart } /** - * Get the total price of the items in the cart. + * Get the discount of the items in the cart. + * + * @return Money */ - public function total(): Money + public function price(): Money { - return $this->getContent()->reduce(function (Money $total, CartItem $cartItem) { - return $total->add($cartItem->total); + $calculated = $this->getContent()->reduce(function (Money $discount, CartItem $cartItem) { + return $discount->add($cartItem->price()); }, new Money(0, new Currency('USD'))); - } - - /** - * Get the total tax of the items in the cart. - */ - public function tax(): Money - { - return $this->getContent()->reduce(function (Money $tax, CartItem $cartItem) { - return $tax->add($cartItem->taxTotal); - }, new Money(0, new Currency('USD'))); - } - /** - * Get the subtotal (total - tax) of the items in the cart. - */ - public function subtotal(): Money - { - return $this->getContent()->reduce(function (Money $subTotal, CartItem $cartItem) { - return $subTotal->add($cartItem->subtotal); - }, new Money(0, new Currency('USD'))); + if ($calculated instanceof Money) { + return $calculated; + } } /** @@ -386,38 +372,64 @@ class Cart */ public function discount(): Money { - return $this->getContent()->reduce(function (Money $discount, CartItem $cartItem) { - return $discount->add($cartItem->discountTotal); + $calculated = $this->getContent()->reduce(function (Money $discount, CartItem $cartItem) { + return $discount->add($cartItem->discount()); }, new Money(0, new Currency('USD'))); + + if ($calculated instanceof Money) { + return $calculated; + } } /** - * Get the price of the items in the cart (not rounded). + * Get the subtotal (total - tax) of the items in the cart. */ - public function initial(): Money + public function subtotal(): Money { - return $this->getContent()->reduce(function (Money $initial, CartItem $cartItem) { - return $initial->add($cartItem->price->multiply($cartItem->qty)); + $calculated = $this->getContent()->reduce(function (Money $subTotal, CartItem $cartItem) { + return $subTotal->add($cartItem->subtotal()); }, new Money(0, new Currency('USD'))); + + if ($calculated instanceof Money) { + return $calculated; + } + } + + /** + * Get the total tax of the items in the cart. + */ + public function tax(): Money + { + $calculated = $this->getContent()->reduce(function (Money $tax, CartItem $cartItem) { + return $tax->add($cartItem->tax()); + }, new Money(0, new Currency('USD'))); + + if ($calculated instanceof Money) { + return $calculated; + } } /** - * Get the price of the items in the cart (previously rounded). + * Get the total price of the items in the cart. */ - public function priceTotal(): Money + public function total(): Money { - return $this->getContent()->reduce(function (Money $initial, CartItem $cartItem) { - return $initial->add($cartItem->priceTotal); + $calculated = $this->getContent()->reduce(function (Money $total, CartItem $cartItem) { + return $total->add($cartItem->total()); }, new Money(0, new Currency('USD'))); + + if ($calculated instanceof Money) { + return $calculated; + } } /** * Get the total weight of the items in the cart. */ - public function weight(): float + public function weight(): int { - return $this->getContent()->reduce(function (float $total, CartItem $cartItem) { - return $total + ($cartItem->qty * $cartItem->weight); + return $this->getContent()->reduce(function (int $total, CartItem $cartItem) { + return $total + $cartItem->weight(); }, 0); } @@ -509,7 +521,7 @@ class Cart { $cartItem = $this->get($rowId); - $cartItem->setDiscountRate($discount); + $cartItem->setDiscount($discount); $content = $this->getContent(); diff --git a/src/CartItem.php b/src/CartItem.php index e50a301..38cfecb 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -2,30 +2,15 @@ namespace Gloudemans\Shoppingcart; -use Gloudemans\Shoppingcart\Calculation\DefaultCalculator; use Gloudemans\Shoppingcart\Contracts\Buyable; -use Gloudemans\Shoppingcart\Contracts\Calculator; -use Gloudemans\Shoppingcart\Exceptions\InvalidCalculatorException; use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; +use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; use Money\Money; use Money\Formatter\DecimalMoneyFormatter; use Money\Currencies\ISOCurrencies; -use ReflectionClass; -/** - * @property-read mixed discount - * @property-read float discountTotal - * @property-read float priceTarget - * @property-read float priceNet - * @property-read float priceTotal - * @property-read float subtotal - * @property-read float taxTotal - * @property-read float tax - * @property-read float total - * @property-read float priceTax - */ class CartItem implements Arrayable, Jsonable { /** @@ -35,10 +20,8 @@ class CartItem implements Arrayable, Jsonable /** * The ID of the cart item. - * - * @var int|string */ - public $id; + public int|string $id; /** * The quantity for this cart item. @@ -47,8 +30,6 @@ class CartItem implements Arrayable, Jsonable /** * The name of the cart item. - * - * @var string */ public string $name; @@ -74,15 +55,13 @@ class CartItem implements Arrayable, Jsonable /** * The FQN of the associated model. - * - * @var string|null */ - private $associatedModel = null; + public ?string $associatedModel = null; /** * The discount rate for the cart item. */ - public float $discountRate = 0; + public float|Money $discount = 0; /** * The cart instance of the cart item. @@ -91,10 +70,6 @@ class CartItem implements Arrayable, Jsonable public function __construct(int|string $id, string $name, Money $price, int $qty = 1, int $weight = 0, ?CartItemOptions $options = null) { - if (!is_string($id) && !is_int($id)) { - throw new \InvalidArgumentException('Please supply a valid identifier.'); - } - $this->id = $id; $this->name = $name; $this->price = $price; @@ -142,7 +117,7 @@ class CartItem implements Arrayable, Jsonable * * @param mixed $model */ - public function associate($model) : self + public function associate(string|Model $model) : self { $this->associatedModel = is_string($model) ? $model : get_class($model); @@ -162,9 +137,9 @@ class CartItem implements Arrayable, Jsonable /** * Set the discount rate. */ - public function setDiscountRate(float $discountRate) : self + public function setDiscount(float|Money $discount) : self { - $this->discountRate = $discountRate; + $this->discount = $discount; return $this; } @@ -178,40 +153,70 @@ class CartItem implements Arrayable, Jsonable return $this; } - - /** - * Get an attribute from the cart item or get the associated model. - * - * @return mixed - */ - public function __get(string $attribute) + + public function model(): ?Model { - if (property_exists($this, $attribute)) { - return $this->{$attribute}; - } - $decimals = config('cart.format.decimals', 2); - - switch ($attribute) { - case 'model': - if (isset($this->associatedModel)) { - return with(new $this->associatedModel())->find($this->id); - } - // no break - case 'modelFQCN': - if (isset($this->associatedModel)) { - return $this->associatedModel; - } - // no break - case 'weightTotal': - return round($this->weight * $this->qty, $decimals); + if (isset($this->associatedModel)) { + return (new $this->associatedModel())->find($this->id); } - $class = new ReflectionClass(config('cart.calculator', DefaultCalculator::class)); - if (!$class->implementsInterface(Calculator::class)) { - throw new InvalidCalculatorException('The configured Calculator seems to be invalid. Calculators have to implement the Calculator Contract.'); - } + return null; + } - return call_user_func($class->getName().'::getAttribute', $attribute, $this); + /** + * This will is the price of the CartItem considering the set quantity. If you need the raw price + * then simply access the price member. + */ + public function price(): Money + { + return $this->price()->multiply($this->qty); + } + + /** + * This is the discount granted for this CartItem. It is based on the given price and, in case + * discount is a float, multiplied or, in case it is an absolute Money, subtracted. It will return + * a minimum value of 0. + */ + public function discount(): Money + { + if ($this->discount instanceof Money) { + return $this->price()->subtract($this->discountRate); + } else { + return $this->price()->multiply($this->discountRate, config('cart.rounding', Money::ROUND_UP)); + } + } + + /** + * This is the final price of the CartItem but without any tax applied. This does on the + * other hand include any discounts. + */ + public function subtotal(): Money + { + return Money::max(new Money(0, $this->price()->getCurrency()), $this->price()->add($this->discount())); + } + + /** + * This is the tax, based on the subtotal (all previous calculations) and set tax rate + */ + public function tax(): Money + { + return $this->subtotal()->multiply($this->taxRate + 1, config('cart.rounding', Money::ROUND_UP)); + } + + /** + * This is the total price, consisting of the subtotal and tax applied. + */ + public function total(): Money + { + return $this->subtotal()->add($this->tax()); + } + + /** + * This is the total price, consisting of the subtotal and tax applied. + */ + public function weight(): int + { + return $this->qty * $this->weight; } /** @@ -254,13 +259,16 @@ class CartItem implements Arrayable, Jsonable 'rowId' => $this->rowId, 'id' => $this->id, 'name' => $this->name, - 'qty' => $this->qty, 'price' => self::formatMoney($this->price), + 'qty' => $this->qty, 'weight' => $this->weight, 'options' => $this->options->toArray(), - 'discount' => self::formatMoney($this->discount), - 'tax' => self::formatMoney($this->tax), - 'subtotal' => self::formatMoney($this->subtotal), + + /* Calculated attributes */ + 'discount' => self::formatMoney($this->discount()), + 'subtotal' => self::formatMoney($this->subtotal()), + 'tax' => self::formatMoney($this->tax()), + 'total' => self::formatMoney($this->total()), ]; } @@ -293,4 +301,4 @@ class CartItem implements Arrayable, Jsonable return md5($id . serialize($options)); } -} +} \ No newline at end of file diff --git a/src/Contracts/Calculator.php b/src/Contracts/Calculator.php deleted file mode 100644 index 137b1e2..0000000 --- a/src/Contracts/Calculator.php +++ /dev/null @@ -1,10 +0,0 @@ -get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(BuyableProduct::class, $cartItem->modelFQCN); + $this->assertEquals(BuyableProduct::class, $cartItem->associatedModel); } /** @test */ @@ -682,7 +682,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(ProductModel::class, $cartItem->modelFQCN); + $this->assertEquals(ProductModel::class, $cartItem->associatedModel); } /** @@ -711,8 +711,8 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertInstanceOf(ProductModel::class, $cartItem->model); - $this->assertEquals('Some value', $cartItem->model->someValue); + $this->assertInstanceOf(ProductModel::class, $cartItem->model()); + $this->assertEquals('Some value', $cartItem->model()->someValue); } /** @test */ @@ -1134,7 +1134,7 @@ class CartTest extends TestCase 'name' => 'First item', ]), 1); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(50, $cartItem->discountRate); + $this->assertEquals(50, $cartItem->discount); } /** @test */ @@ -1158,7 +1158,7 @@ class CartTest extends TestCase ]), 1); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 50); - $this->assertEquals(50, $cartItem->discountRate); + $this->assertEquals(50, $cartItem->discount); } /** @test */ @@ -1171,7 +1171,7 @@ class CartTest extends TestCase ]), 2); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $this->assertEquals(500, $cart->weight()); - $this->assertEquals(500, $cartItem->weightTotal); + $this->assertEquals(500, $cartItem->weight()); } /** @test */ From b30800729a4fe775f36dc6a7b545947cb54618cb Mon Sep 17 00:00:00 2001 From: Patrick Date: Sat, 5 Feb 2022 23:42:38 +0100 Subject: [PATCH 130/156] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 1117055..20b3bdd 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Run the Composer require command from the Terminal: Now you're ready to start using the shoppingcart in your application. -**As of version 2 of this package it's possibly to use dependency injection to inject an instance of the Cart class into your controller or other class** +**As of version 2 of this package it's possible to use dependency injection to inject an instance of the Cart class into your controller or other class** You definitely should publish the `config` file and take a look at it. From 3f44e9366b50b97815ad63ebd7397b6ca40b0169 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sat, 5 Feb 2022 23:45:16 +0100 Subject: [PATCH 131/156] Remove uneccessary use directives --- tests/CartTest.php | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index f5a9b45..7a55fc6 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -14,11 +14,8 @@ use Gloudemans\Tests\Shoppingcart\Fixtures\BuyableProduct; use Gloudemans\Tests\Shoppingcart\Fixtures\BuyableProductTrait; use Gloudemans\Tests\Shoppingcart\Fixtures\Identifiable; use Gloudemans\Tests\Shoppingcart\Fixtures\ProductModel; -use Illuminate\Contracts\Auth\Authenticatable; -use Illuminate\Session\SessionManager; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Event; -use Mockery; use Orchestra\Testbench\TestCase; class CartTest extends TestCase From f7d6c5df7a3024f6d4037d79c9f0a60f2ad386b2 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sat, 5 Feb 2022 23:50:14 +0100 Subject: [PATCH 132/156] Fix --- src/Config/cart.php | 12 ------------ src/Exceptions/InvalidCalculatorException.php | 9 --------- tests/CartTest.php | 17 ----------------- 3 files changed, 38 deletions(-) delete mode 100644 src/Exceptions/InvalidCalculatorException.php diff --git a/src/Config/cart.php b/src/Config/cart.php index 1f06222..b87faed 100644 --- a/src/Config/cart.php +++ b/src/Config/cart.php @@ -3,18 +3,6 @@ use Money\Money; return [ - - /* - |-------------------------------------------------------------------------- - | Gross price as base price - |-------------------------------------------------------------------------- - | - | This default value is used to select the method to calculate prices and taxes - | If true the item price is managed as a gross price, so taxes will be calculated by separation/exclusion - | - */ - - 'calculator' => \Gloudemans\Shoppingcart\Calculation\DefaultCalculator::class, /* |-------------------------------------------------------------------------- diff --git a/src/Exceptions/InvalidCalculatorException.php b/src/Exceptions/InvalidCalculatorException.php deleted file mode 100644 index abc5370..0000000 --- a/src/Exceptions/InvalidCalculatorException.php +++ /dev/null @@ -1,9 +0,0 @@ -getCartDiscount(0); - config(['cart.calculator' => GrossPrice::class]); - - $cartItem = $cart->add(new BuyableProduct([ - 'name' => 'First item', - 'price' => 100, - ]), 2); - - $cart->setGlobalTax(22); - - // check net price - $this->assertEquals(new Money(8197, new Currency('USD')), $cartItem->priceNet); - } - /** * Get an instance of the cart. * From 9dd494cdc715d3e7a99df255581a5759e1cb9364 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 00:01:56 +0100 Subject: [PATCH 133/156] Further fixes --- src/Cart.php | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 625a1ad..57a3dea 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -14,6 +14,7 @@ use Gloudemans\Shoppingcart\Exceptions\InvalidRowIDException; use Gloudemans\Shoppingcart\Exceptions\UnknownModelException; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\DatabaseManager; +use Illuminate\Database\Eloquent\Model; use Illuminate\Session\SessionManager; use Illuminate\Support\Collection; use Illuminate\Support\Traits\Macroable; @@ -109,17 +110,8 @@ class Cart /** * Add an item to the cart. - * - * @param mixed $id - * @param mixed $name - * @param int|float $qty - * @param float $price - * @param float $weight - * @param array $options - * - * @return \Gloudemans\Shoppingcart\CartItem */ - public function add(int|string|Buyable|iterable $id, null|string|int $nameOrQty = null, null|int|CartItemOptions $qtyOrOptions = null, ?Money $price = null, ?int $weight = null, ?CartItemOptions $options = null): CartItem|array + public function add(int|string|Buyable|array $id, null|string|int $nameOrQty = null, null|int|CartItemOptions $qtyOrOptions = null, ?Money $price = null, ?int $weight = null, ?CartItemOptions $options = null): CartItem|array { /* Allow adding a CartItem by raw parameters */ if (is_int($id) || is_string($id)) { @@ -144,12 +136,15 @@ class Cart } $cartItem = CartItem::fromBuyable($id, $nameOrQty ?: 1, $qtyOrOptions ?: new CartItemOptions([])); - $cartItem->associate($id); + + if ($id instanceof Model) { + $cartItem->associate($id); + } return $this->addCartItem($cartItem); } /* Also allow passing multiple definitions at the same time, simply call same method and collec return value */ - else if (is_iterable($id)) { + else if (is_array($id)) { /* Check if this iterable contains instances */ if (is_array(head($id)) || head($id) instanceof Buyable) { return array_map(function (Buyable|iterable $item) { @@ -305,7 +300,11 @@ class Cart throw new InvalidRowIDException("The cart does not contain rowId {$rowId}."); } - return $content->get($rowId); + $cartItem = $content->get($rowId); + + if ($cartItem instanceof CartItem) { + return $cartItem; + } } /** @@ -362,6 +361,8 @@ class Cart if ($calculated instanceof Money) { return $calculated; + } else { + throw new \TypeError("Calculated price is not an instance of Money"); } } @@ -378,6 +379,8 @@ class Cart if ($calculated instanceof Money) { return $calculated; + } else { + throw new \TypeError("Calculated discount is not an instance of Money"); } } @@ -392,6 +395,8 @@ class Cart if ($calculated instanceof Money) { return $calculated; + } else { + throw new \TypeError("Calculated subtotal is not an instance of Money"); } } @@ -420,6 +425,8 @@ class Cart if ($calculated instanceof Money) { return $calculated; + } else { + throw new \TypeError("Calculated total is not an instance of Money"); } } @@ -428,9 +435,15 @@ class Cart */ public function weight(): int { - return $this->getContent()->reduce(function (int $total, CartItem $cartItem) { + $calculated = $this->getContent()->reduce(function (int $total, CartItem $cartItem) { return $total + $cartItem->weight(); }, 0); + + if (is_int($calculated)) { + return $calculated; + } else { + throw new \TypeError("Calculated weight was not an integer"); + } } /** From 2efb0003ec8c475a77f8aff2ebf27e91301d2e8c Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 00:03:16 +0100 Subject: [PATCH 134/156] Add database as dependency --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 83bbdf0..408df5f 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "illuminate/support": "^6.0|^7.0|^8.0|^9.0", "illuminate/session": "^6.0|^7.0|^8.0|^9.0", "illuminate/events": "^6.0|^7.0|^8.0|^9.0", + "illuminate/database": "^6.0|^7.0|^8.0|^9.0", "nesbot/carbon": "^2.0", "moneyphp/money": "^3.3.0|^4.0.0" }, From c3d33a37f6638c94c16beada68a1480cf727adf5 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 00:13:56 +0100 Subject: [PATCH 135/156] Fix recursion --- src/CartItem.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CartItem.php b/src/CartItem.php index 38cfecb..41f8356 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -169,7 +169,7 @@ class CartItem implements Arrayable, Jsonable */ public function price(): Money { - return $this->price()->multiply($this->qty); + return $this->price->multiply($this->qty); } /** From 66d23444b2bb0a1ae9b290ff8733c3297ccc275e Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 00:27:03 +0100 Subject: [PATCH 136/156] Further fixes --- .gitignore | 3 +- README.md | 2 +- src/Cart.php | 8 ++-- src/CartItem.php | 11 ++--- ...12_23_120000_create_shoppingcart_table.php | 4 +- tests/CartTest.php | 40 +++++++------------ 6 files changed, 29 insertions(+), 39 deletions(-) diff --git a/.gitignore b/.gitignore index b9eaa7d..20dbf72 100644 --- a/.gitignore +++ b/.gitignore @@ -3,4 +3,5 @@ composer.phar composer.lock .DS_Store coverage.xml -.phpunit.result.cache \ No newline at end of file +.phpunit.result.cache +/.vscode \ No newline at end of file diff --git a/README.md b/README.md index 20b3bdd..dc9db69 100644 --- a/README.md +++ b/README.md @@ -611,7 +611,7 @@ class DefaultCalculator implements Calculator { public static function getAttribute(string $attribute, CartItem $cartItem) { - $decimals = config('cart.format.decimals', 2); + $decimals = Config::get('cart.format.decimals', 2); switch ($attribute) { case 'discount': diff --git a/src/Cart.php b/src/Cart.php index 57a3dea..02a2bef 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -72,7 +72,7 @@ class Cart { $this->session = $session; $this->events = $events; - $this->taxRate = config('cart.tax'); + $this->taxRate = Config::get('cart.tax'); $this->instance(self::DEFAULT_INSTANCE); } @@ -747,7 +747,7 @@ class Cart */ private static function getTableName(): string { - return config('cart.database.table', 'shoppingcart'); + return Config::get('cart.database.table', 'shoppingcart'); } /** @@ -755,9 +755,9 @@ class Cart */ private function getConnectionName(): ?string { - $connection = config('cart.database.connection'); + $connection = Config::get('cart.database.connection'); - return is_null($connection) ? config('database.default') : $connection; + return is_null($connection) ? Config::get('database.default') : $connection; } /** diff --git a/src/CartItem.php b/src/CartItem.php index 41f8356..7e4b5fa 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -7,6 +7,7 @@ use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; +use Illuminate\Support\Facades\Config; use Money\Money; use Money\Formatter\DecimalMoneyFormatter; use Money\Currencies\ISOCurrencies; @@ -164,8 +165,8 @@ class CartItem implements Arrayable, Jsonable } /** - * This will is the price of the CartItem considering the set quantity. If you need the raw price - * then simply access the price member. + * This will is the price of the CartItem considering the set quantity. If you need the single + * price just set the parameter to true. */ public function price(): Money { @@ -180,9 +181,9 @@ class CartItem implements Arrayable, Jsonable public function discount(): Money { if ($this->discount instanceof Money) { - return $this->price()->subtract($this->discountRate); + return $this->price()->subtract($this->discount); } else { - return $this->price()->multiply($this->discountRate, config('cart.rounding', Money::ROUND_UP)); + return $this->price()->multiply($this->discount, Config::get('cart.rounding', Money::ROUND_UP)); } } @@ -200,7 +201,7 @@ class CartItem implements Arrayable, Jsonable */ public function tax(): Money { - return $this->subtotal()->multiply($this->taxRate + 1, config('cart.rounding', Money::ROUND_UP)); + return $this->subtotal()->multiply($this->taxRate + 1, Config::get('cart.rounding', Money::ROUND_UP)); } /** diff --git a/src/Database/migrations/2018_12_23_120000_create_shoppingcart_table.php b/src/Database/migrations/2018_12_23_120000_create_shoppingcart_table.php index bc905db..89e8188 100644 --- a/src/Database/migrations/2018_12_23_120000_create_shoppingcart_table.php +++ b/src/Database/migrations/2018_12_23_120000_create_shoppingcart_table.php @@ -11,7 +11,7 @@ class CreateShoppingcartTable extends Migration */ public function up() { - Schema::create(config('cart.database.table'), function (Blueprint $table) { + Schema::create(Config::get('cart.database.table'), function (Blueprint $table) { $table->string('identifier'); $table->string('instance'); $table->longText('content'); @@ -26,6 +26,6 @@ class CreateShoppingcartTable extends Migration */ public function down() { - Schema::drop(config('cart.database.table')); + Schema::drop(Config::get('cart.database.table')); } } diff --git a/tests/CartTest.php b/tests/CartTest.php index cdf56c9..ca54548 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -951,14 +951,10 @@ class CartTest extends TestCase $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); - $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->discount); - $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discountTotal); - $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->priceTarget); - $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal); - $this->assertEquals(new Money(95, new Currency('USD')), $cartItem->tax); - $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->taxTotal); - $this->assertEquals(new Money(595, new Currency('USD')), $cartItem->priceTax); - $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); + $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->subtotal()); + $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->tax()); + $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total()); } /** @test */ @@ -976,14 +972,10 @@ class CartTest extends TestCase $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); - $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->discount); - $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discountTotal); - $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->priceTarget); - $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal); - $this->assertEquals(new Money(95, new Currency('USD')), $cartItem->tax); - $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->taxTotal); - $this->assertEquals(new Money(595, new Currency('USD')), $cartItem->priceTax); - $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal()); + $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->tax()); + $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total()); } /** @test */ @@ -1004,14 +996,10 @@ class CartTest extends TestCase $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); - $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->discount); - $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discountTotal); - $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->priceTarget); - $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal); - $this->assertEquals(new Money(95, new Currency('USD')), $cartItem->tax); - $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->taxTotal); - $this->assertEquals(new Money(595, new Currency('USD')), $cartItem->priceTax); - $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal()); + $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->tax()); + $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total()); } /** @test */ @@ -1027,7 +1015,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(new Money(2000, new Currency('USD')), $cartItem->total); + $this->assertEquals(new Money(2000, new Currency('USD')), $cartItem->total()); } /** @test */ @@ -1044,7 +1032,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->total); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->total()); } /** @test */ From a5c4c64294d5a1236992cb01ae52d7f45e93fb89 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 00:31:27 +0100 Subject: [PATCH 137/156] Fix missing using directive --- src/Cart.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Cart.php b/src/Cart.php index 02a2bef..599b0a4 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -18,6 +18,7 @@ use Illuminate\Database\Eloquent\Model; use Illuminate\Session\SessionManager; use Illuminate\Support\Collection; use Illuminate\Support\Traits\Macroable; +use Illuminate\Support\Facades\Config; class Cart { From 7c88f42ad59b56c80f7d03c0803cd279254484e6 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 00:35:50 +0100 Subject: [PATCH 138/156] Fix format tests --- tests/CartItemTest.php | 3 ++- tests/CartTest.php | 7 +++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index 4fb4bb0..51d99ee 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -40,6 +40,7 @@ class CartItemTest extends TestCase ], 'tax' => '0.00', 'subtotal' => '20.00', + 'total' => '20.00', 'discount' => '0.00', 'weight' => 550, ], $cartItem->toArray()); @@ -52,7 +53,7 @@ class CartItemTest extends TestCase $this->assertJson($cartItem->toJson()); - $json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","qty":2,"price":"10.00","weight":550,"options":{"size":"XL","color":"red"},"discount":"0.00","tax":"10.00","subtotal":"20.00"}'; + $json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","price":"10.00","qty":2,"weight":550,"options":{"size":"XL","color":"red"},"discount":"0.00","subtotal":"20.00","tax":"20.00","total":"40.00"}'; $this->assertEquals($json, $cartItem->toJson()); } diff --git a/tests/CartTest.php b/tests/CartTest.php index ca54548..20ad456 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -533,11 +533,13 @@ class CartTest extends TestCase 'name' => 'Item name', 'qty' => 1, 'price' => '10.00', - 'tax' => '2.10', 'subtotal' => '10.00', + 'tax' => '2.10', + 'total' => '7.90', 'options' => [], 'discount' => '0.00', 'weight' => 0, + ], '370d08585360f5c568b18d1f2e4ca1df' => [ 'rowId' => '370d08585360f5c568b18d1f2e4ca1df', @@ -545,8 +547,9 @@ class CartTest extends TestCase 'name' => 'Item name', 'qty' => 1, 'price' => '10.00', - 'tax' => '2.10', 'subtotal' => '10.00', + 'tax' => '2.10', + 'total' => '7.90', 'options' => [], 'discount' => '0.00', 'weight' => 0, From 4fdc7a55e36a8f24bee7ab65043fa7cd2a09547a Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 00:44:31 +0100 Subject: [PATCH 139/156] Fixed all errors --- src/Cart.php | 4 ++-- tests/CartTest.php | 41 +++++++++++---------------------- tests/Fixtures/ProductModel.php | 4 +++- 3 files changed, 18 insertions(+), 31 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index 599b0a4..ba5a51f 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -558,8 +558,8 @@ class Cart $content = $this->getContent(); if ($content && $content->count()) { - $content->each(function ($item, $key) { - $item->setDiscountRate($this->discount); + $content->each(function (CartItem $item, $key) { + $item->setDiscount($this->discount); }); } } diff --git a/tests/CartTest.php b/tests/CartTest.php index 20ad456..0bde298 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -722,12 +722,12 @@ class CartTest extends TestCase $cart->add(new BuyableProduct([ 'name' => 'Some title', - 'price' => '9.99', + 'price' => 999, ]), 3); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(new Money(2997, new Currency('USD')), $cartItem->subtotal); + $this->assertEquals(new Money(2997, new Currency('USD')), $cartItem->subtotal()); } /** @test */ @@ -741,7 +741,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(new Money(210, new Currency('USD')), $cartItem->tax); + $this->assertEquals(new Money(210, new Currency('USD')), $cartItem->tax()); } /** @test */ @@ -757,7 +757,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->tax); + $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->tax()); } /** @test */ @@ -774,7 +774,7 @@ class CartTest extends TestCase 'price' => '20.00', ]), 2); - $this->assertEquals(new Money(1050, new Currency('USD')), $cart->tax); + $this->assertEquals(new Money(1050, new Currency('USD')), $cart->tax()); } /** @test */ @@ -1107,7 +1107,7 @@ class CartTest extends TestCase ]), 1); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); - $this->assertEquals(new Money(1000, new Currency('USD')), $cart->initial()); + $this->assertEquals(new Money(1000, new Currency('USD')), $cart->price()); $this->assertEquals(new Money(500, new Currency('USD')), $cart->discount()); $this->assertEquals(new Money(500, new Currency('USD')), $cart->subtotal()); $this->assertEquals(new Money(95, new Currency('USD')), $cart->tax()); @@ -1125,18 +1125,6 @@ class CartTest extends TestCase $this->assertEquals(50, $cartItem->discount); } - /** @test */ - public function cant_access_non_existant_propertys() - { - $cart = $this->getCartDiscount(50); - $cart->add(new BuyableProduct([ - 'name' => 'First item', - ]), 1); - $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(null, $cartItem->doesNotExist); - $this->assertEquals(null, $cart->doesNotExist); - } - /** @test */ public function can_set_cart_item_discount() { @@ -1205,14 +1193,10 @@ class CartTest extends TestCase $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); - $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->discount); - $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discountTotal); - $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->priceTarget); - $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal); - $this->assertEquals(new Money(95, new Currency('USD')), $cartItem->tax); - $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->taxTotal); - $this->assertEquals(new Money(595, new Currency('USD')), $cartItem->priceTax); - $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal()); + $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->tax()); + $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total()); } /** @test */ @@ -1221,9 +1205,10 @@ class CartTest extends TestCase // https://github.com/bumbummen99/LaravelShoppingcart/pull/5 $cart = $this->getCart(); - $cartItem = $cart->add('293ad', 'Product 1', 1, new Money(1000, new Currency('USD')), 550, new CartItemOptions(['size' => 'large'])); + $cartItem = $cart->add('293ad', 'Product 1', 2, new Money(1000, new Currency('USD')), 550, new CartItemOptions(['size' => 'large'])); $this->assertEquals(550, $cartItem->weight); + $this->assertEquals(1100, $cartItem->weight()); $this->assertTrue($cartItem->options->has('size')); $this->assertEquals('large', $cartItem->options->size); } @@ -1381,7 +1366,7 @@ class CartTest extends TestCase 'name' => 'first item', 'price' => 1000, ]), 5); - $this->assertEquals(new Money(5000, new Currency('USD')), $cart->priceTotal()); + $this->assertEquals(new Money(5000, new Currency('USD')), $cart->price()); } /** @test */ diff --git a/tests/Fixtures/ProductModel.php b/tests/Fixtures/ProductModel.php index 1939d69..20c57f0 100644 --- a/tests/Fixtures/ProductModel.php +++ b/tests/Fixtures/ProductModel.php @@ -2,7 +2,9 @@ namespace Gloudemans\Tests\Shoppingcart\Fixtures; -class ProductModel +use Illuminate\Database\Eloquent\Model; + +class ProductModel extends Model { public $someValue = 'Some value'; From 0aa43abb6770d22d3f0856ac68cc5005661889e8 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 02:59:32 +0100 Subject: [PATCH 140/156] Fix tests --- src/Cart.php | 7 +- src/CartItem.php | 8 ++- src/Config/cart.php | 2 +- tests/CartItemTest.php | 2 +- tests/CartTest.php | 151 +++++++++++++++++++---------------------- 5 files changed, 79 insertions(+), 91 deletions(-) diff --git a/src/Cart.php b/src/Cart.php index ba5a51f..5fb6910 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -525,13 +525,8 @@ class Cart /** * Set the discount rate for the cart item with the given rowId. - * - * @param string $rowId - * @param int|float $taxRate - * - * @return void */ - public function setDiscount(string $rowId, $discount): void + public function setDiscount(string $rowId, float|Money $discount): void { $cartItem = $this->get($rowId); diff --git a/src/CartItem.php b/src/CartItem.php index 7e4b5fa..c897f8b 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -180,9 +180,11 @@ class CartItem implements Arrayable, Jsonable */ public function discount(): Money { + $price = $this->price(); if ($this->discount instanceof Money) { return $this->price()->subtract($this->discount); } else { + $result = $this->price()->multiply($this->discount, Config::get('cart.rounding', Money::ROUND_UP)); return $this->price()->multiply($this->discount, Config::get('cart.rounding', Money::ROUND_UP)); } } @@ -193,7 +195,8 @@ class CartItem implements Arrayable, Jsonable */ public function subtotal(): Money { - return Money::max(new Money(0, $this->price()->getCurrency()), $this->price()->add($this->discount())); + $subtotal = $this->price()->add($this->discount()); + return Money::max(new Money(0, $this->price->getCurrency()), $this->price()->subtract($this->discount())); } /** @@ -201,7 +204,8 @@ class CartItem implements Arrayable, Jsonable */ public function tax(): Money { - return $this->subtotal()->multiply($this->taxRate + 1, Config::get('cart.rounding', Money::ROUND_UP)); + $tax = $this->subtotal()->multiply($this->taxRate, Config::get('cart.rounding', Money::ROUND_UP)); + return $this->subtotal()->multiply($this->taxRate, Config::get('cart.rounding', Money::ROUND_UP)); } /** diff --git a/src/Config/cart.php b/src/Config/cart.php index b87faed..f3f2bc2 100644 --- a/src/Config/cart.php +++ b/src/Config/cart.php @@ -26,7 +26,7 @@ return [ | */ - 'tax' => 21, + 'tax' => 0.21, /* |-------------------------------------------------------------------------- diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index 51d99ee..88487d2 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -53,7 +53,7 @@ class CartItemTest extends TestCase $this->assertJson($cartItem->toJson()); - $json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","price":"10.00","qty":2,"weight":550,"options":{"size":"XL","color":"red"},"discount":"0.00","subtotal":"20.00","tax":"20.00","total":"40.00"}'; + $json = '{"rowId":"07d5da5550494c62daf9993cf954303f","id":1,"name":"Some item","price":"10.00","qty":2,"weight":550,"options":{"size":"XL","color":"red"},"discount":"0.00","subtotal":"20.00","tax":"0.00","total":"20.00"}'; $this->assertEquals($json, $cartItem->toJson()); } diff --git a/tests/CartTest.php b/tests/CartTest.php index 0bde298..ed841d0 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -5,7 +5,6 @@ namespace Gloudemans\Tests\Shoppingcart; use Carbon\Carbon; use Money\Money; use Money\Currency; -use Gloudemans\Shoppingcart\Calculation\GrossPrice; use Gloudemans\Shoppingcart\Cart; use Gloudemans\Shoppingcart\CartItem; use Gloudemans\Shoppingcart\CartItemOptions; @@ -535,7 +534,7 @@ class CartTest extends TestCase 'price' => '10.00', 'subtotal' => '10.00', 'tax' => '2.10', - 'total' => '7.90', + 'total' => '12.10', 'options' => [], 'discount' => '0.00', 'weight' => 0, @@ -549,7 +548,7 @@ class CartTest extends TestCase 'price' => '10.00', 'subtotal' => '10.00', 'tax' => '2.10', - 'total' => '7.90', + 'total' => '12.10', 'options' => [], 'discount' => '0.00', 'weight' => 0, @@ -582,7 +581,7 @@ class CartTest extends TestCase $cart->add(new BuyableProduct([ 'id' => 2, 'name' => 'Second item', - 'price' => '25.00', + 'price' => 2500, ]), 2); $this->assertItemsInCart(3, $cart); @@ -753,7 +752,7 @@ class CartTest extends TestCase 'name' => 'Some title', ]), 1); - $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); @@ -771,7 +770,7 @@ class CartTest extends TestCase $cart->add(new BuyableProduct([ 'id' => 2, 'name' => 'Some title', - 'price' => '20.00', + 'price' => 2000, ]), 2); $this->assertEquals(new Money(1050, new Currency('USD')), $cart->tax()); @@ -786,11 +785,11 @@ class CartTest extends TestCase 'name' => 'Some title', ]), 1); - $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(19.0, $cartItem->taxRate); + $this->assertEquals(0.19, $cartItem->taxRate); } /** @test */ @@ -801,7 +800,7 @@ class CartTest extends TestCase $cart->add(new BuyableProduct(), 1); $cart->add(new BuyableProduct([ 'id' => 2, - 'price' => '20.00', + 'price' => 2000, ]), 2); $this->assertEquals(new Money(5000, new Currency('USD')), $cart->subtotal()); @@ -943,7 +942,7 @@ class CartTest extends TestCase /** @test */ public function it_can_calculate_all_values() { - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProduct([ 'name' => 'First item', @@ -951,11 +950,11 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); - $this->assertEquals(new Money(500, new Currency('USD')), $cartItem->subtotal()); + $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal()); $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->tax()); $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total()); } @@ -963,7 +962,7 @@ class CartTest extends TestCase /** @test */ public function it_can_calculate_all_values_after_updating_from_array() { - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProduct([ 'name' => 'First item', ]), 1); @@ -972,7 +971,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); @@ -984,7 +983,7 @@ class CartTest extends TestCase /** @test */ public function it_can_calculate_all_values_after_updating_from_buyable() { - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProduct([ 'name' => 'First item', 'price' => '5.00', @@ -996,7 +995,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); @@ -1031,7 +1030,7 @@ class CartTest extends TestCase ]), 2); $cart->setGlobalTax(0); - $cart->setGlobalDiscount(50); + $cart->setGlobalDiscount(0.5); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); @@ -1047,7 +1046,7 @@ class CartTest extends TestCase Event::fake(); - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProduct([ 'name' => 'Item', ]), 1); @@ -1086,7 +1085,7 @@ class CartTest extends TestCase '--database' => 'testing', ]); Event::fake(); - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProduct([ 'name' => 'Item', ]), 1); @@ -1101,12 +1100,11 @@ class CartTest extends TestCase /** @test */ public function cart_can_calculate_all_values() { - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProduct([ 'name' => 'First item', ]), 1); - $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19); $this->assertEquals(new Money(1000, new Currency('USD')), $cart->price()); $this->assertEquals(new Money(500, new Currency('USD')), $cart->discount()); $this->assertEquals(new Money(500, new Currency('USD')), $cart->subtotal()); @@ -1114,17 +1112,6 @@ class CartTest extends TestCase $this->assertEquals(new Money(595, new Currency('USD')), $cart->total()); } - /** @test */ - public function can_access_cart_item_propertys() - { - $cart = $this->getCartDiscount(50); - $cart->add(new BuyableProduct([ - 'name' => 'First item', - ]), 1); - $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(50, $cartItem->discount); - } - /** @test */ public function can_set_cart_item_discount() { @@ -1133,8 +1120,10 @@ class CartTest extends TestCase 'name' => 'First item', ]), 1); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 50); - $this->assertEquals(50, $cartItem->discount); + + $cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 0.5); + + $this->assertEquals(0.5, $cartItem->discount); } /** @test */ @@ -1182,7 +1171,7 @@ class CartTest extends TestCase /** @test */ public function cart_can_create_items_from_models_using_the_canbebought_trait() { - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProductTrait([ 'name' => 'First item', @@ -1190,7 +1179,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 19); + $cart->setTax('027c91341fd5cf4d2579b49c4b6a90da', 0.19); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); @@ -1220,7 +1209,7 @@ class CartTest extends TestCase '--database' => 'testing', ]); - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProduct([ 'name' => 'Item', ]), 1); @@ -1255,7 +1244,7 @@ class CartTest extends TestCase '--database' => 'testing', ]); - $cart = $this->getCartDiscount(50); + $cart = $this->getCartDiscount(0.5); $cart->add(new BuyableProduct([ 'name' => 'Item', ]), 1); @@ -1282,48 +1271,6 @@ class CartTest extends TestCase }); } - /** - * Get an instance of the cart. - * - * @return \Gloudemans\Shoppingcart\Cart - */ - private function getCart() - { - $session = $this->app->make('session'); - $events = $this->app->make('events'); - - return new Cart($session, $events); - } - - /** - * Get an instance of the cart with discount. - * - * @param int $discount - * - * @return \Gloudemans\Shoppingcart\Cart - */ - private function getCartDiscount($discount = 50) - { - $cart = $this->getCart(); - $cart->setGlobalDiscount($discount); - - return $cart; - } - - /** - * Set the config number format. - * - * @param int $decimals - * @param string $decimalPoint - * @param string $thousandSeperator - */ - private function setConfigFormat($decimals, $decimalPoint, $thousandSeperator) - { - $this->app['config']->set('cart.format.decimals', $decimals); - $this->app['config']->set('cart.format.decimal_point', $decimalPoint); - $this->app['config']->set('cart.format.thousand_separator', $thousandSeperator); - } - /** @test */ public function it_can_store__mutiple_instances_of_the_cart_in_a_database() { @@ -1391,4 +1338,46 @@ class CartTest extends TestCase Event::assertDispatched('cart.erased'); $this->assertDatabaseMissing('shoppingcart', ['identifier' => $identifier, 'instance' => Cart::DEFAULT_INSTANCE]); } + + /** + * Get an instance of the cart. + * + * @return \Gloudemans\Shoppingcart\Cart + */ + private function getCart() + { + $session = $this->app->make('session'); + $events = $this->app->make('events'); + + return new Cart($session, $events); + } + + /** + * Get an instance of the cart with discount. + * + * @param int $discount + * + * @return \Gloudemans\Shoppingcart\Cart + */ + private function getCartDiscount(float $discount = 0.5) + { + $cart = $this->getCart(); + $cart->setGlobalDiscount($discount); + + return $cart; + } + + /** + * Set the config number format. + * + * @param int $decimals + * @param string $decimalPoint + * @param string $thousandSeperator + */ + private function setConfigFormat($decimals, $decimalPoint, $thousandSeperator) + { + $this->app['config']->set('cart.format.decimals', $decimals); + $this->app['config']->set('cart.format.decimal_point', $decimalPoint); + $this->app['config']->set('cart.format.thousand_separator', $thousandSeperator); + } } From 1be78257aed7dda9bb6ffe569c1a22576c83de3c Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 6 Feb 2022 13:39:50 +0100 Subject: [PATCH 141/156] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index dc9db69..7de5bb0 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,6 @@ Run the Composer require command from the Terminal: Now you're ready to start using the shoppingcart in your application. -**As of version 2 of this package it's possible to use dependency injection to inject an instance of the Cart class into your controller or other class** - You definitely should publish the `config` file and take a look at it. php artisan vendor:publish --provider="Gloudemans\Shoppingcart\ShoppingcartServiceProvider" --tag="config" From f9c2533746ad4ee1e86ccbc45141682a489e766e Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 13:55:47 +0100 Subject: [PATCH 142/156] Mod test, Document ServiceProvider, --- src/ShoppingcartServiceProvider.php | 8 +++++++- tests/CartTest.php | 9 +++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/ShoppingcartServiceProvider.php b/src/ShoppingcartServiceProvider.php index b5c6fda..7a92bd3 100644 --- a/src/ShoppingcartServiceProvider.php +++ b/src/ShoppingcartServiceProvider.php @@ -15,13 +15,19 @@ class ShoppingcartServiceProvider extends ServiceProvider */ public function register() { + /* Bind Cart class to cart for Facade usage */ $this->app->bind('cart', 'Gloudemans\Shoppingcart\Cart'); + /* Determine where the config file is located */ $config = __DIR__.'/Config/cart.php'; + + /* Use local config */ $this->mergeConfigFrom($config, 'cart'); - $this->publishes([__DIR__.'/Config/cart.php' => config_path('cart.php')], 'config'); + /* Also allow publishing to overwrite local config */ + $this->publishes([$config => config_path('cart.php')], 'config'); + /* Publish included migrations */ $this->publishes([ realpath(__DIR__.'/Database/migrations') => $this->app->databasePath().'/migrations', ], 'migrations'); diff --git a/tests/CartTest.php b/tests/CartTest.php index ed841d0..8d87006 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -30,7 +30,9 @@ class CartTest extends TestCase */ protected function getPackageProviders($app) { - return [ShoppingcartServiceProvider::class]; + return [ + ShoppingcartServiceProvider::class + ]; } /** @@ -522,6 +524,9 @@ class CartTest extends TestCase 'id' => 2, ])); + $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); + $this->assertEquals(config('cart.tax'), $cartItem->taxRate); + $content = $cart->content(); $this->assertInstanceOf(Collection::class, $content); @@ -1120,7 +1125,7 @@ class CartTest extends TestCase 'name' => 'First item', ]), 1); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - + $cart->setDiscount('027c91341fd5cf4d2579b49c4b6a90da', 0.5); $this->assertEquals(0.5, $cartItem->discount); From 08c389d84a6867d67e6928a4aa69171adba43284 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 14:55:47 +0100 Subject: [PATCH 143/156] Refactor ci composer caching and validating --- .github/workflows/php.yml | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 5f86449..5519cd3 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -22,34 +22,26 @@ jobs: env: PHP_VERSION: ${{ matrix.php-versions }} steps: - - name: Checkout - uses: actions/checkout@v2 - - name: Setup PHP uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php-version }} env: runner: ubuntu-18.04 - - - name: Use appropiate Laravel version + + - name: Checkout + uses: actions/checkout@v2 + + - name: Use appropiate Laravel version in composer.json (without installing) run: | - composer require "laravel/framework:${{ matrix.laravel-version }}" --no-interaction --no-update - - - name: Validate composer.json and composer.lock - run: composer validate - - - name: Get composer cache directory - id: composercache - run: echo "::set-output name=dir::$(composer config cache-files-dir)" - - - name: Cache Composer packages - id: composer-cache + composer require --no-update --no-interaction "laravel/framework:${{ matrix.laravel-version }}" + composer validate + + - name: Cache dependencies uses: actions/cache@v2 with: - path: ${{ steps.composercache.outputs.dir }} - key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }} - restore-keys: ${{ runner.os }}-composer- + path: ~/.composer/cache/files + key: dependencies-laravel-${{ matrix.laravel-version }}-php-${{ matrix.php-version }}-composer-${{ hashFiles('composer.json') }} - name: Install composer packages run: composer install --prefer-dist --no-interaction From 68337d27da284aceb64b84e111103fbc9d17284b Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 14:57:31 +0100 Subject: [PATCH 144/156] Mod test --- tests/CartTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 8d87006..9d6c1f7 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -525,7 +525,8 @@ class CartTest extends TestCase ])); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(config('cart.tax'), $cartItem->taxRate); + $this->assertEquals(0.21, $cartItem->taxRate); + $this->assertEquals(0.21, $cartItem->taxRate); $content = $cart->content(); From 3832e67f347345e993c2d60aaf36f2f8f82101d6 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 15:12:57 +0100 Subject: [PATCH 145/156] Mod test --- tests/CartTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/CartTest.php b/tests/CartTest.php index 9d6c1f7..5f8f148 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -527,6 +527,7 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $this->assertEquals(0.21, $cartItem->taxRate); $this->assertEquals(0.21, $cartItem->taxRate); + $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->tax()); $content = $cart->content(); From 8c4203d206ef6ec4e8542d9ce7787b057e1b54e4 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 15:16:14 +0100 Subject: [PATCH 146/156] Mod test --- tests/CartTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/CartTest.php b/tests/CartTest.php index 5f8f148..8a69507 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -527,7 +527,10 @@ class CartTest extends TestCase $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); $this->assertEquals(0.21, $cartItem->taxRate); $this->assertEquals(0.21, $cartItem->taxRate); + $this->assertEquals(new Money(1000, $cartItem->price->getCurrency()), $cartItem->price()); + $this->assertEquals(new Money(1000, $cartItem->price->getCurrency()), $cartItem->subtotal()); $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->tax()); + $this->assertEquals(new Money(1200, $cartItem->price->getCurrency()), $cartItem->total()); $content = $cart->content(); From 54cd0ac876b0f51965ee481680eec70381d019d5 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 15:18:04 +0100 Subject: [PATCH 147/156] Mod test --- tests/CartTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 8a69507..edbd279 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -525,7 +525,7 @@ class CartTest extends TestCase ])); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(0.21, $cartItem->taxRate); + $this->assertEquals(0.21, config('cart.tax')); $this->assertEquals(0.21, $cartItem->taxRate); $this->assertEquals(new Money(1000, $cartItem->price->getCurrency()), $cartItem->price()); $this->assertEquals(new Money(1000, $cartItem->price->getCurrency()), $cartItem->subtotal()); From f505dd70f82c2e9e690c1b2ebee92cd7b76feb5a Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 15:18:20 +0100 Subject: [PATCH 148/156] Mod test --- tests/CartTest.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index edbd279..84c1638 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -525,10 +525,11 @@ class CartTest extends TestCase ])); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - $this->assertEquals(0.21, config('cart.tax')); - $this->assertEquals(0.21, $cartItem->taxRate); + $this->assertEquals(new Money(1000, $cartItem->price->getCurrency()), $cartItem->price()); $this->assertEquals(new Money(1000, $cartItem->price->getCurrency()), $cartItem->subtotal()); + $this->assertEquals(0.21, config('cart.tax')); + $this->assertEquals(0.21, $cartItem->taxRate); $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->tax()); $this->assertEquals(new Money(1200, $cartItem->price->getCurrency()), $cartItem->total()); From fd8c4e6726e66e555a3d65ae9abad848d00bbc0a Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 15:21:25 +0100 Subject: [PATCH 149/156] Mod test --- tests/CartTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 84c1638..5411a84 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -530,7 +530,8 @@ class CartTest extends TestCase $this->assertEquals(new Money(1000, $cartItem->price->getCurrency()), $cartItem->subtotal()); $this->assertEquals(0.21, config('cart.tax')); $this->assertEquals(0.21, $cartItem->taxRate); - $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->tax()); + $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->subtotal()->multiply($cartItem->taxRate), config('cart.rounding', Money::ROUND_UP), 'calculate tax manually'); + $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->tax(), 'calculate tax on CartItem'); $this->assertEquals(new Money(1200, $cartItem->price->getCurrency()), $cartItem->total()); $content = $cart->content(); From 600a32a06cd7a558a3544b39d5dd6a02e5e1fe71 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 15:26:27 +0100 Subject: [PATCH 150/156] Mod ci install extensions --- .github/workflows/php.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/php.yml b/.github/workflows/php.yml index 5519cd3..9652373 100644 --- a/.github/workflows/php.yml +++ b/.github/workflows/php.yml @@ -26,6 +26,7 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php-version }} + extensions: bcmath,gmp,intl env: runner: ubuntu-18.04 @@ -36,7 +37,7 @@ jobs: run: | composer require --no-update --no-interaction "laravel/framework:${{ matrix.laravel-version }}" composer validate - + - name: Cache dependencies uses: actions/cache@v2 with: From 326271619d3322b718e6477c9d69b4b0bdabee6f Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 15:31:22 +0100 Subject: [PATCH 151/156] Mod test --- tests/CartTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 5411a84..194d165 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -530,8 +530,8 @@ class CartTest extends TestCase $this->assertEquals(new Money(1000, $cartItem->price->getCurrency()), $cartItem->subtotal()); $this->assertEquals(0.21, config('cart.tax')); $this->assertEquals(0.21, $cartItem->taxRate); - $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->subtotal()->multiply($cartItem->taxRate), config('cart.rounding', Money::ROUND_UP), 'calculate tax manually'); - $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->tax(), 'calculate tax on CartItem'); + $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->subtotal()->multiply($cartItem->taxRate), config('cart.rounding', Money::ROUND_UP), 'Could not calculate tax manually'); + $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->tax(), 'Could not calculate tax on CartItem'); $this->assertEquals(new Money(1200, $cartItem->price->getCurrency()), $cartItem->total()); $content = $cart->content(); From 71863739fbdae934f13dcc0702c0378806fe62ce Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 15:53:33 +0100 Subject: [PATCH 152/156] Mod test --- tests/CartTest.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 194d165..4aad168 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -70,6 +70,16 @@ class CartTest extends TestCase }); } + /** @test */ + public function i_am_not_insane() + { + $cart = $this->getCart(); + + $price = new Money(1000, new Currency('USD')); + + $this->assertEquals(new Money(210, new Currency('USD')), $price->multiply(0.21)); + } + /** @test */ public function it_has_a_default_instance() { @@ -532,7 +542,7 @@ class CartTest extends TestCase $this->assertEquals(0.21, $cartItem->taxRate); $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->subtotal()->multiply($cartItem->taxRate), config('cart.rounding', Money::ROUND_UP), 'Could not calculate tax manually'); $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->tax(), 'Could not calculate tax on CartItem'); - $this->assertEquals(new Money(1200, $cartItem->price->getCurrency()), $cartItem->total()); + $this->assertEquals(new Money(1210, $cartItem->price->getCurrency()), $cartItem->total()); $content = $cart->content(); From f4e19fb1981753534722b494efbabd7c5ffeb445 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 6 Feb 2022 19:18:27 +0100 Subject: [PATCH 153/156] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 408df5f..8a36115 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "illuminate/events": "^6.0|^7.0|^8.0|^9.0", "illuminate/database": "^6.0|^7.0|^8.0|^9.0", "nesbot/carbon": "^2.0", - "moneyphp/money": "^3.3.0|^4.0.0" + "moneyphp/money": "^4.0.0" }, "require-dev": { "phpunit/phpunit": "~8.0|~9.0", From 9808bd623f0eaf7c7ec78b6f206f99b89502f298 Mon Sep 17 00:00:00 2001 From: Patrick Date: Sun, 6 Feb 2022 19:21:53 +0100 Subject: [PATCH 154/156] Update CartItem.php Changes according to moneyphp/money@30da0b34afe41464f31cefaac9986fd2612fc404 --- src/CartItem.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/CartItem.php b/src/CartItem.php index c897f8b..3559480 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -180,12 +180,10 @@ class CartItem implements Arrayable, Jsonable */ public function discount(): Money { - $price = $this->price(); if ($this->discount instanceof Money) { return $this->price()->subtract($this->discount); } else { - $result = $this->price()->multiply($this->discount, Config::get('cart.rounding', Money::ROUND_UP)); - return $this->price()->multiply($this->discount, Config::get('cart.rounding', Money::ROUND_UP)); + return $this->price()->multiply(sprintf('%.14F', $this->discount), Config::get('cart.rounding', Money::ROUND_UP)); } } @@ -195,7 +193,6 @@ class CartItem implements Arrayable, Jsonable */ public function subtotal(): Money { - $subtotal = $this->price()->add($this->discount()); return Money::max(new Money(0, $this->price->getCurrency()), $this->price()->subtract($this->discount())); } @@ -204,8 +201,7 @@ class CartItem implements Arrayable, Jsonable */ public function tax(): Money { - $tax = $this->subtotal()->multiply($this->taxRate, Config::get('cart.rounding', Money::ROUND_UP)); - return $this->subtotal()->multiply($this->taxRate, Config::get('cart.rounding', Money::ROUND_UP)); + return $this->subtotal()->multiply(sprintf('%.14F', $this->taxRate), Config::get('cart.rounding', Money::ROUND_UP)); } /** @@ -306,4 +302,4 @@ class CartItem implements Arrayable, Jsonable return md5($id . serialize($options)); } -} \ No newline at end of file +} From c4fb20271fb4f4c7cb0e858876246c25e319d210 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 19:26:18 +0100 Subject: [PATCH 155/156] Fix tests --- tests/CartTest.php | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-) diff --git a/tests/CartTest.php b/tests/CartTest.php index 4aad168..2e75695 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -70,16 +70,6 @@ class CartTest extends TestCase }); } - /** @test */ - public function i_am_not_insane() - { - $cart = $this->getCart(); - - $price = new Money(1000, new Currency('USD')); - - $this->assertEquals(new Money(210, new Currency('USD')), $price->multiply(0.21)); - } - /** @test */ public function it_has_a_default_instance() { @@ -538,10 +528,7 @@ class CartTest extends TestCase $this->assertEquals(new Money(1000, $cartItem->price->getCurrency()), $cartItem->price()); $this->assertEquals(new Money(1000, $cartItem->price->getCurrency()), $cartItem->subtotal()); - $this->assertEquals(0.21, config('cart.tax')); - $this->assertEquals(0.21, $cartItem->taxRate); - $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->subtotal()->multiply($cartItem->taxRate), config('cart.rounding', Money::ROUND_UP), 'Could not calculate tax manually'); - $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->tax(), 'Could not calculate tax on CartItem'); + $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->tax()); $this->assertEquals(new Money(1210, $cartItem->price->getCurrency()), $cartItem->total()); $content = $cart->content(); From 5d557885843bb5faf9d3fbc2834bdd62658d0368 Mon Sep 17 00:00:00 2001 From: Patrick Henninger Date: Sun, 6 Feb 2022 19:28:56 +0100 Subject: [PATCH 156/156] StyleCI --- src/CanBeBought.php | 9 +++--- src/Cart.php | 50 ++++++++++++++--------------- src/CartItem.php | 37 +++++++++++---------- src/Config/cart.php | 2 +- src/ShoppingcartServiceProvider.php | 2 -- tests/CartItemTest.php | 4 +-- tests/CartTest.php | 12 +++---- tests/Fixtures/BuyableProduct.php | 6 ++-- tests/Fixtures/ProductModel.php | 2 +- 9 files changed, 62 insertions(+), 62 deletions(-) diff --git a/src/CanBeBought.php b/src/CanBeBought.php index 9088fb0..eb9810f 100644 --- a/src/CanBeBought.php +++ b/src/CanBeBought.php @@ -2,9 +2,8 @@ namespace Gloudemans\Shoppingcart; -use Gloudemans\Shoppingcart\CartItemOptions; -use Money\Money; use Money\Currency; +use Money\Money; trait CanBeBought { @@ -25,13 +24,13 @@ trait CanBeBought { if (($name = $this->getAttribute('name'))) { return $name; - } else if (($title = $this->getAttribute('title'))) { + } elseif (($title = $this->getAttribute('title'))) { return $title; - } else if (($description = $this->getAttribute('description'))) { + } elseif (($description = $this->getAttribute('description'))) { return $description; } else { return null; - } + } } /** diff --git a/src/Cart.php b/src/Cart.php index 5fb6910..72e57e3 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -2,11 +2,8 @@ namespace Gloudemans\Shoppingcart; -use Closure; -use InvalidArgumentException; use Carbon\Carbon; -use Money\Money; -use Money\Currency; +use Closure; use Gloudemans\Shoppingcart\Contracts\Buyable; use Gloudemans\Shoppingcart\Contracts\InstanceIdentifier; use Gloudemans\Shoppingcart\Exceptions\CartAlreadyStoredException; @@ -17,8 +14,11 @@ use Illuminate\Database\DatabaseManager; use Illuminate\Database\Eloquent\Model; use Illuminate\Session\SessionManager; use Illuminate\Support\Collection; -use Illuminate\Support\Traits\Macroable; use Illuminate\Support\Facades\Config; +use Illuminate\Support\Traits\Macroable; +use InvalidArgumentException; +use Money\Currency; +use Money\Money; class Cart { @@ -116,36 +116,36 @@ class Cart { /* Allow adding a CartItem by raw parameters */ if (is_int($id) || is_string($id)) { - if (! is_null($nameOrQty) && ! is_string($nameOrQty)) { + if (!is_null($nameOrQty) && !is_string($nameOrQty)) { throw new InvalidArgumentException('$nameOrQty must be of type string (name) or null when adding with raw parameters'); } - - if (! is_null($qtyOrOptions) && ! is_int($qtyOrOptions)) { + + if (!is_null($qtyOrOptions) && !is_int($qtyOrOptions)) { throw new InvalidArgumentException('$nameOrQty must be of type int (quantity) or null when adding with raw parameters'); } - + return $this->addCartItem(CartItem::fromAttributes($id, $nameOrQty, $price, $qtyOrOptions ?: 1, $weight ?: 0, $options ?: new CartItemOptions([]))); } /* Also allow passing a Buyable instance, get data from the instance rather than parameters */ - else if ($id instanceof Buyable) { - if (! is_null($qtyOrOptions) && ! is_int($nameOrQty)) { + elseif ($id instanceof Buyable) { + if (!is_null($qtyOrOptions) && !is_int($nameOrQty)) { throw new InvalidArgumentException('$nameOrQty must be of type int (quantity) when adding a Buyable instance'); } - - if (! is_null($qtyOrOptions) && ! $qtyOrOptions instanceof CartItemOptions) { + + if (!is_null($qtyOrOptions) && !$qtyOrOptions instanceof CartItemOptions) { throw new InvalidArgumentException('$qtyOrOptions must be of type CartItemOptions (options) or null when adding a Buyable instance'); } - + $cartItem = CartItem::fromBuyable($id, $nameOrQty ?: 1, $qtyOrOptions ?: new CartItemOptions([])); if ($id instanceof Model) { $cartItem->associate($id); } - + return $this->addCartItem($cartItem); } /* Also allow passing multiple definitions at the same time, simply call same method and collec return value */ - else if (is_array($id)) { + elseif (is_array($id)) { /* Check if this iterable contains instances */ if (is_array(head($id)) || head($id) instanceof Buyable) { return array_map(function (Buyable|iterable $item) { @@ -155,7 +155,7 @@ class Cart /* Treat the array itself as an instance */ else { $cartItem = CartItem::fromArray($id); - + return $this->addCartItem($cartItem); } } @@ -178,8 +178,8 @@ class Cart public function addCartItem(CartItem $item, bool $keepDiscount = false, bool $keepTax = false, bool $dispatchEvent = true): CartItem { $item->setInstance($this->currentInstance()); - - if (! $keepDiscount) { + + if (!$keepDiscount) { $item->setDiscount($this->discount); } @@ -363,7 +363,7 @@ class Cart if ($calculated instanceof Money) { return $calculated; } else { - throw new \TypeError("Calculated price is not an instance of Money"); + throw new \TypeError('Calculated price is not an instance of Money'); } } @@ -381,7 +381,7 @@ class Cart if ($calculated instanceof Money) { return $calculated; } else { - throw new \TypeError("Calculated discount is not an instance of Money"); + throw new \TypeError('Calculated discount is not an instance of Money'); } } @@ -397,10 +397,10 @@ class Cart if ($calculated instanceof Money) { return $calculated; } else { - throw new \TypeError("Calculated subtotal is not an instance of Money"); + throw new \TypeError('Calculated subtotal is not an instance of Money'); } } - + /** * Get the total tax of the items in the cart. */ @@ -427,7 +427,7 @@ class Cart if ($calculated instanceof Money) { return $calculated; } else { - throw new \TypeError("Calculated total is not an instance of Money"); + throw new \TypeError('Calculated total is not an instance of Money'); } } @@ -443,7 +443,7 @@ class Cart if (is_int($calculated)) { return $calculated; } else { - throw new \TypeError("Calculated weight was not an integer"); + throw new \TypeError('Calculated weight was not an integer'); } } diff --git a/src/CartItem.php b/src/CartItem.php index 3559480..6bd14d8 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -8,9 +8,9 @@ use Illuminate\Contracts\Support\Jsonable; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; use Illuminate\Support\Facades\Config; -use Money\Money; -use Money\Formatter\DecimalMoneyFormatter; use Money\Currencies\ISOCurrencies; +use Money\Formatter\DecimalMoneyFormatter; +use Money\Money; class CartItem implements Arrayable, Jsonable { @@ -118,7 +118,7 @@ class CartItem implements Arrayable, Jsonable * * @param mixed $model */ - public function associate(string|Model $model) : self + public function associate(string|Model $model): self { $this->associatedModel = is_string($model) ? $model : get_class($model); @@ -128,7 +128,7 @@ class CartItem implements Arrayable, Jsonable /** * Set the tax rate. */ - public function setTaxRate(float $taxRate) : self + public function setTaxRate(float $taxRate): self { $this->taxRate = $taxRate; @@ -138,7 +138,7 @@ class CartItem implements Arrayable, Jsonable /** * Set the discount rate. */ - public function setDiscount(float|Money $discount) : self + public function setDiscount(float|Money $discount): self { $this->discount = $discount; @@ -148,7 +148,7 @@ class CartItem implements Arrayable, Jsonable /** * Set cart instance. */ - public function setInstance(?string $instance) : self + public function setInstance(?string $instance): self { $this->instance = $instance; @@ -165,7 +165,7 @@ class CartItem implements Arrayable, Jsonable } /** - * This will is the price of the CartItem considering the set quantity. If you need the single + * This will is the price of the CartItem considering the set quantity. If you need the single * price just set the parameter to true. */ public function price(): Money @@ -197,7 +197,7 @@ class CartItem implements Arrayable, Jsonable } /** - * This is the tax, based on the subtotal (all previous calculations) and set tax rate + * This is the tax, based on the subtotal (all previous calculations) and set tax rate. */ public function tax(): Money { @@ -223,18 +223,20 @@ class CartItem implements Arrayable, Jsonable /** * Create a new instance from a Buyable. */ - public static function fromBuyable(Buyable $item, int $qty = 1, ?CartItemOptions $options = null) : self + public static function fromBuyable(Buyable $item, int $qty = 1, ?CartItemOptions $options = null): self { $options = $options ?: new CartItemOptions([]); + return new self($item->getBuyableIdentifier($options), $item->getBuyableDescription($options), $item->getBuyablePrice($options), $qty, $item->getBuyableWeight($options), $options); } /** * Create a new instance from the given array. */ - public static function fromArray(array $attributes) : self + public static function fromArray(array $attributes): self { $options = new CartItemOptions(Arr::get($attributes, 'options', [])); + return new self($attributes['id'], $attributes['name'], $attributes['price'], $attributes['qty'], $attributes['weight'], $options); } @@ -243,9 +245,10 @@ class CartItem implements Arrayable, Jsonable * * @param int|string $id */ - public static function fromAttributes(int|string $id, string $name, Money $price, int $qty = 1, int $weight = 0, ?CartItemOptions $options = null) : self + public static function fromAttributes(int|string $id, string $name, Money $price, int $qty = 1, int $weight = 0, ?CartItemOptions $options = null): self { $options = $options ?: new CartItemOptions([]); + return new self($id, $name, $price, $qty, $weight, $options); } @@ -269,7 +272,7 @@ class CartItem implements Arrayable, Jsonable 'discount' => self::formatMoney($this->discount()), 'subtotal' => self::formatMoney($this->subtotal()), 'tax' => self::formatMoney($this->tax()), - 'total' => self::formatMoney($this->total()), + 'total' => self::formatMoney($this->total()), ]; } @@ -284,22 +287,22 @@ class CartItem implements Arrayable, Jsonable { return json_encode($this->toArray(), $options); } - + /** * Generate a unique id for the cart item. */ - private static function formatMoney(Money $money) : string + private static function formatMoney(Money $money): string { return (new DecimalMoneyFormatter(new ISOCurrencies()))->format($money); } - + /** * Generate a unique id for the cart item. */ - protected function generateRowId(string $id, array $options) : string + protected function generateRowId(string $id, array $options): string { ksort($options); - return md5($id . serialize($options)); + return md5($id.serialize($options)); } } diff --git a/src/Config/cart.php b/src/Config/cart.php index f3f2bc2..eb072dd 100644 --- a/src/Config/cart.php +++ b/src/Config/cart.php @@ -3,7 +3,7 @@ use Money\Money; return [ - + /* |-------------------------------------------------------------------------- | Rounding strategy diff --git a/src/ShoppingcartServiceProvider.php b/src/ShoppingcartServiceProvider.php index 7a92bd3..ecda57d 100644 --- a/src/ShoppingcartServiceProvider.php +++ b/src/ShoppingcartServiceProvider.php @@ -2,8 +2,6 @@ namespace Gloudemans\Shoppingcart; -use Illuminate\Auth\Events\Logout; -use Illuminate\Session\SessionManager; use Illuminate\Support\ServiceProvider; class ShoppingcartServiceProvider extends ServiceProvider diff --git a/tests/CartItemTest.php b/tests/CartItemTest.php index 88487d2..885d1d3 100644 --- a/tests/CartItemTest.php +++ b/tests/CartItemTest.php @@ -5,9 +5,9 @@ namespace Gloudemans\Tests\Shoppingcart; use Gloudemans\Shoppingcart\CartItem; use Gloudemans\Shoppingcart\CartItemOptions; use Gloudemans\Shoppingcart\ShoppingcartServiceProvider; -use Orchestra\Testbench\TestCase; -use Money\Money; use Money\Currency; +use Money\Money; +use Orchestra\Testbench\TestCase; class CartItemTest extends TestCase { diff --git a/tests/CartTest.php b/tests/CartTest.php index 2e75695..baa8f10 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -3,8 +3,6 @@ namespace Gloudemans\Tests\Shoppingcart; use Carbon\Carbon; -use Money\Money; -use Money\Currency; use Gloudemans\Shoppingcart\Cart; use Gloudemans\Shoppingcart\CartItem; use Gloudemans\Shoppingcart\CartItemOptions; @@ -15,6 +13,8 @@ use Gloudemans\Tests\Shoppingcart\Fixtures\Identifiable; use Gloudemans\Tests\Shoppingcart\Fixtures\ProductModel; use Illuminate\Support\Collection; use Illuminate\Support\Facades\Event; +use Money\Currency; +use Money\Money; use Orchestra\Testbench\TestCase; class CartTest extends TestCase @@ -31,7 +31,7 @@ class CartTest extends TestCase protected function getPackageProviders($app) { return [ - ShoppingcartServiceProvider::class + ShoppingcartServiceProvider::class, ]; } @@ -525,7 +525,7 @@ class CartTest extends TestCase ])); $cartItem = $cart->get('027c91341fd5cf4d2579b49c4b6a90da'); - + $this->assertEquals(new Money(1000, $cartItem->price->getCurrency()), $cartItem->price()); $this->assertEquals(new Money(1000, $cartItem->price->getCurrency()), $cartItem->subtotal()); $this->assertEquals(new Money(210, $cartItem->price->getCurrency()), $cartItem->tax()); @@ -547,7 +547,7 @@ class CartTest extends TestCase 'options' => [], 'discount' => '0.00', 'weight' => 0, - + ], '370d08585360f5c568b18d1f2e4ca1df' => [ 'rowId' => '370d08585360f5c568b18d1f2e4ca1df', @@ -1009,7 +1009,7 @@ class CartTest extends TestCase $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->price); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->discount()); $this->assertEquals(new Money(1000, new Currency('USD')), $cartItem->subtotal()); - $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->tax()); + $this->assertEquals(new Money(190, new Currency('USD')), $cartItem->tax()); $this->assertEquals(new Money(1190, new Currency('USD')), $cartItem->total()); } diff --git a/tests/Fixtures/BuyableProduct.php b/tests/Fixtures/BuyableProduct.php index 32dc098..9265aab 100644 --- a/tests/Fixtures/BuyableProduct.php +++ b/tests/Fixtures/BuyableProduct.php @@ -2,11 +2,11 @@ namespace Gloudemans\Tests\Shoppingcart\Fixtures; -use Gloudemans\Shoppingcart\Contracts\Buyable; use Gloudemans\Shoppingcart\CartItemOptions; +use Gloudemans\Shoppingcart\Contracts\Buyable; use Illuminate\Database\Eloquent\Model; -use Money\Money; use Money\Currency; +use Money\Money; class BuyableProduct extends Model implements Buyable { @@ -48,7 +48,7 @@ class BuyableProduct extends Model implements Buyable * * @return string */ - public function getBuyableDescription(CartItemOptions $options) : ?string + public function getBuyableDescription(CartItemOptions $options): ?string { return $this->name; } diff --git a/tests/Fixtures/ProductModel.php b/tests/Fixtures/ProductModel.php index 20c57f0..5e89dd2 100644 --- a/tests/Fixtures/ProductModel.php +++ b/tests/Fixtures/ProductModel.php @@ -8,7 +8,7 @@ class ProductModel extends Model { public $someValue = 'Some value'; - public function find($id) : self + public function find($id): self { return $this; }