diff --git a/src/CartItem.php b/src/CartItem.php index 99d6e24..162615d 100644 --- a/src/CartItem.php +++ b/src/CartItem.php @@ -15,18 +15,10 @@ class CartItem protected $itemHash; - public $id; - public $tax; - - public $name; - public $code; - public $locale; - public $taxable; public $lineItem; - public $discount = 0; + public $taxable; public $subItems = []; public $couponInfo = []; - public $internationalFormat; /** * CartItem constructor. diff --git a/src/Contracts/LaraCartContract.php b/src/Contracts/LaraCartContract.php index 1762667..577421e 100644 --- a/src/Contracts/LaraCartContract.php +++ b/src/Contracts/LaraCartContract.php @@ -3,6 +3,9 @@ namespace LukePOLO\LaraCart\Contracts; use LukePOLO\LaraCart\CartItem; +use LukePOLO\LaraCart\LaraCart; +use LukePOLO\LaraCart\Exceptions\InvalidPrice; +use LukePOLO\LaraCart\Exceptions\InvalidQuantity; /** * Interface LaraCartContract @@ -16,10 +19,11 @@ interface LaraCartContract * * @param string $instance * - * @return mixed + * @return LaraCart */ public function setInstance($instance = 'default'); + /** * Gets the instance in the session * @@ -29,23 +33,6 @@ public function setInstance($instance = 'default'); */ public function get($instance = 'default'); - /** - * Updates cart session - */ - public function update(); - - /** - * - * Formats the number into a money format based on the locale and international formats - * - * @param $number - * @param $locale - * @param $internationalFormat - * @param $format - * - * @return string - */ - public function formatMoney($number, $locale = null, $internationalFormat = null, $format = true); /** * Gets an an attribute from the cart @@ -73,79 +60,100 @@ public function getAttributes(); public function setAttribute($attribute, $value); /** - * Removes an attribute from the cart - * - * @param $attribute - */ - public function removeAttribute($attribute); - - /** - * Finds a cartItem based on the itemHash - * - * @param $itemHash - * - * @return CartItem | null + * Updates cart session */ - public function getItem($itemHash); + public function update(); /** - * Gets all the items within the cart + * Removes an attribute from the cart * - * @return array + * @param $attribute */ - public function getItems(); + public function removeAttribute($attribute); /** * Creates a CartItem and then adds it to cart * - * @param $itemID + * @param string|int $itemID * @param null $name * @param int $qty * @param string $price * @param array $options - * @param bool|false $lineItem + * @param bool|true $taxable * * @return CartItem */ - public function add($itemID, $name = null, $qty = 1, $price = '0.00', $options = [], $lineItem = false); + public function addLine($itemID, $name = null, $qty = 1, $price = '0.00', $options = [], $taxable = true); /** * Creates a CartItem and then adds it to cart * - * @param string|int $itemID + * @param $itemID * @param null $name * @param int $qty * @param string $price * @param array $options + * @param bool|false $taxable + * @param bool|false $lineItem * - * @return string CartItem + * @return CartItem */ - public function addLine($itemID, $name = null, $qty = 1, $price = '0.00', $options = []); + public function add( + $itemID, + $name = null, + $qty = 1, + $price = '0.00', + $options = [], + $taxable = true, + $lineItem = false + ); /** * Adds the cartItem into the cart session * - * @param $cartItem + * @param CartItem $cartItem * * @return CartItem */ public function addItem(CartItem $cartItem); /** - * Removes a CartItem based on the itemHash + * Finds a cartItem based on the itemHash * * @param $itemHash + * + * @return CartItem | null */ - public function removeItem($itemHash); + public function getItem($itemHash); /** - * Get the count based on qty, or number of unique items + * Gets all the items within the cart * - * @param bool $withItemQty + * @return array + */ + public function getItems(); + + + /** + * Updates an items attributes * - * @return int + * @param $itemHash + * @param $key + * @param $value + * + * @return CartItem + * + * @throws InvalidPrice + * @throws InvalidQuantity */ - public function count($withItemQty = true); + public function updateItem($itemHash, $key, $value); + + /** + * Removes a CartItem based on the itemHash + * + * @param $itemHash + */ + public function removeItem($itemHash); /** * Empties the carts items @@ -195,13 +203,6 @@ public function removeCoupon($code); */ public function getFee($name); - /** - * Getes all the fees on the cart object - * - * @return mixed - */ - public function getFees(); - /** * Allows to charge for additional fees that may or may not be taxable * ex - service fee , delivery fee, tips @@ -221,49 +222,79 @@ public function addFee($name, $amount, $taxable = false, Array $options = []); public function removeFee($name); /** - * Gets all the fee totals + * Gets the total tax for the cart * * @param bool|true $format * * @return string */ - public function feeTotals($format = true); + public function taxTotal($format = true); /** - * Gets the total amount discounted + * Gets the total of the cart with or without tax * - * @param bool|true $format + * @param boolean $format + * @param boolean $withDiscount * - * @return int|string + * @return string */ - public function totalDiscount($format = true); + public function total($format = true, $withDiscount = true); /** - * Gets the total tax for the cart + * Gets the subtotal of the cart with or without tax * - * @param bool|true $format + * @param bool|false $tax + * @param boolean $format + * @param boolean $withDiscount * * @return string */ - public function taxTotal($format = true); + public function subTotal($tax = false, $format = true, $withDiscount = true); /** - * Gets the subtotal of the cart with or without tax + * Get the count based on qty, or number of unique items * - * @param bool|false $tax - * @param bool|true $format - * @param bool|true $withDiscount + * @param bool $withItemQty + * + * @return int + */ + public function count($withItemQty = true); + + /** + * + * Formats the number into a money format based on the locale and international formats + * + * @param $number + * @param $locale + * @param $internationalFormat + * @param $format * * @return string */ - public function subTotal($tax = false, $format = true, $withDiscount = true); + public function formatMoney($number, $locale = null, $internationalFormat = null, $format = true); /** - * Gets the total of the cart with or without tax + * Gets all the fee totals + * + * @param boolean $format * - * @param bool|true $format - * @param bool|true $withDiscount * @return string */ - public function total($format = true, $withDiscount = true); + public function feeTotals($format = true); + + /** + * Getes all the fees on the cart object + * + * @return mixed + */ + public function getFees(); + + /** + * Gets the total amount discounted + * + * @param bool|true $format + * + * @return int|string + */ + public function totalDiscount($format = true); } diff --git a/src/Traits/CartOptionsMagicMethodsTrait.php b/src/Traits/CartOptionsMagicMethodsTrait.php index 1193a06..c3fc055 100644 --- a/src/Traits/CartOptionsMagicMethodsTrait.php +++ b/src/Traits/CartOptionsMagicMethodsTrait.php @@ -42,10 +42,6 @@ public function __get($option) */ public function __set($option, $value) { - if(is_callable(array($this, 'generateHash'))) { - $this->generateHash(); - } - switch ($option) { case LaraCart::QTY: if (!is_numeric($value) || $value < 0) { @@ -59,6 +55,10 @@ public function __set($option, $value) break; } array_set($this->options, $option, $value); + + if(is_callable(array($this, 'generateHash'))) { + $this->generateHash(); + } } /** diff --git a/tests/ItemsTest.php b/tests/ItemsTest.php index 220b341..53b1881 100644 --- a/tests/ItemsTest.php +++ b/tests/ItemsTest.php @@ -189,4 +189,23 @@ public function testSetQty() $this->assertEquals('The quantity must be a valid number', $e->getMessage()); } } + + /** + * Tests the different taxes on items + */ + public function testDifferentTaxes() { + + $item = $this->addItem(); + + $prevHash = $item->getHash(); + + $item->tax = .05; + + $this->assertNotEquals($prevHash, $item->getHash()); + + $item = $this->addItem(); + $item->tax = .3; + + $this->assertEquals('2.35', $this->laracart->total(false)); + } } diff --git a/tests/LaraCartTest.php b/tests/LaraCartTest.php index 574144a..aff818a 100644 --- a/tests/LaraCartTest.php +++ b/tests/LaraCartTest.php @@ -36,6 +36,9 @@ public function testFormatMoney() $this->assertEquals('$25.54', $this->laracart->formatMoney('25.544')); } + /** + * Test getting the attributes from the cart + */ public function testGetAttributes() { $this->laracart->setAttribute('test1', 1); @@ -47,6 +50,9 @@ public function testGetAttributes() $this->assertEquals(2, $attributes['test2']); } + /** + * Test removing attributes from the cart + */ public function testRemoveAttribute() { $this->laracart->setAttribute('test1', 1); @@ -124,4 +130,19 @@ public function testDestroyOtherCart() $this->assertEquals(1, $cart->count()); } + + /** + * Tests if generating a new hash when we change an option + */ + public function testGeneratingHashes() { + + $item = $this->addItem(); + + $prevHash = $item->getHash(); + + $item->name = 'NEW NAME'; + + $this->assertNotEquals($prevHash, $item->getHash()); + } + } diff --git a/tests/SubItemsTest.php b/tests/SubItemsTest.php index e66204c..1bad16d 100644 --- a/tests/SubItemsTest.php +++ b/tests/SubItemsTest.php @@ -44,11 +44,14 @@ public function testSubItemTotal() $this->assertEquals('2.68', $item->subItemsTotal(true, false)); } + /** + * Test the sub items totals + */ public function testSubItemItemsTotal() { $item = $this->addItem(); - $subItem = $item->addSubItem([ + $item->addSubItem([ 'size' => 'XXL', 'items' => [ new \LukePOLO\LaraCart\CartItem('10', 'sub item item', 1, 15)