Commit 509bbbb9 authored by Thorsten Buss's avatar Thorsten Buss

* rename some functions for better usage (more laravel style)

* make item_rules customizable (preparing for own CartClass with additional custom item fields)
parent 75d7349b
......@@ -166,7 +166,7 @@ Removing an item on a cart is very easy:
Cart::remove(456);
```
Getting an item on a cart: **Cart::get()**
Getting an item on a cart: **Cart::item()**
```php
......@@ -180,13 +180,13 @@ Getting an item on a cart: **Cart::get()**
$itemId = 456;
Cart::get($itemId);
Cart::item($itemId);
// You can also get the sum of the Item multiplied by its quantity, see below:
$summedPrice = Cart::get($itemId)->getPriceSum();
$summedPrice = Cart::item($itemId)->priceSum();
```
Getting cart's contents and count: **Cart::getContent()**
Getting cart's contents and count: **Cart::items()**
```php
......@@ -196,7 +196,7 @@ Getting cart's contents and count: **Cart::getContent()**
* @return CartCollection
*/
$cartCollection = Cart::getContent();
$cartCollection = Cart::items();
// NOTE: Because cart collection extends Laravel's Collection
// You can use methods you already know about Laravel's Collection
......@@ -221,7 +221,7 @@ Check if cart is empty: **Cart::isEmpty()**
Cart::isEmpty();
```
Get cart total quantity: **Cart::getTotalQuantity()**
Get cart total quantity: **Cart::totalQuantity()**
```php
/**
......@@ -229,10 +229,10 @@ Get cart total quantity: **Cart::getTotalQuantity()**
*
* @return int
*/
$cartTotalQuantity = Cart::getTotalQuantity();
$cartTotalQuantity = Cart::totalQuantity();
```
Get cart subtotal: **Cart::getSubTotal()**
Get cart subtotal: **Cart::subTotal()**
```php
/**
......@@ -240,10 +240,10 @@ Get cart subtotal: **Cart::getSubTotal()**
*
* @return float
*/
$subTotal = Cart::getSubTotal();
$subTotal = Cart::subTotal();
```
Get cart total: **Cart::getTotal()**
Get cart total: **Cart::total()**
```php
/**
......@@ -251,7 +251,7 @@ Get cart total: **Cart::getTotal()**
*
* @return float
*/
$total = Cart::getTotal();
$total = Cart::total();
```
Clearing the Cart: **Cart::clear()**
......@@ -343,16 +343,16 @@ $condition->getValue(); // the value of the condition
$condition->getAttributes(); // the attributes of the condition, returns an empty [] if no attributes added
// You can get the conditions calculated value by providing the subtotal, see below:
$subTotal = Cart::getSubTotal();
$subTotal = Cart::subTotal();
$condition = Cart::getCondition('VAT 12.5%');
$conditionCalculatedValue = $condition->getCalculatedValue($subTotal);
```
NOTE: All cart based conditions should be applied before calling **Cart::getTotal()**
NOTE: All cart based conditions should be applied before calling **Cart::total()**
Then Finally you can call **Cart::getTotal()** to get the Cart Total with the applied conditions.
Then Finally you can call **Cart::total()** to get the Cart Total with the applied conditions.
```php
$cartTotal = Cart::getTotal(); // the total will be calculated based on the conditions you ave provided
$cartTotal = Cart::total(); // the total will be calculated based on the conditions you ave provided
```
Next is the Condition on Per-Item Bases.
......@@ -418,11 +418,11 @@ $item = array(
Cart::add($item);
```
NOTE: All cart per-item conditions should be applied before calling **Cart::getSubTotal()**
NOTE: All cart per-item conditions should be applied before calling **Cart::subTotal()**
Then Finally you can call **Cart::getSubTotal()** to get the Cart sub total with the applied conditions.
Then Finally you can call **Cart::subTotal()** to get the Cart sub total with the applied conditions.
```php
$cartSubTotal = Cart::getSubTotal(); // the subtotal will be calculated based on the conditions you have provided
$cartSubTotal = Cart::subTotal(); // the subtotal will be calculated based on the conditions you have provided
```
Add condition to exisiting Item on the cart: **Cart::addItemCondition($productId, $itemCondition)**
......@@ -523,7 +523,7 @@ public function removeConditionsByType($type)
## Items
The method **Cart::getContent()** returns a collection of items.
The method **Cart::items()** returns a collection of items.
To get the id of an item, use the property **$item->id**.
......@@ -535,40 +535,40 @@ To get the attributes of an item, use the property **$item->attributes**.
To get the price of a single item without the conditions applied, use the property **$item->price**.
To get the subtotal of an item without the conditions applied, use the method **$item->getPriceSum()**.
To get the subtotal of an item without the conditions applied, use the method **$item->priceSum()**.
```php
/**
* get the sum of price
*
* @return mixed|null
*/
public function getPriceSum()
public function priceSum()
```
To get the price of a single item without the conditions applied, use the method
**$item->getPriceWithConditions()**.
**$item->priceWithConditions()**.
```php
/**
* get the single price in which conditions are already applied
*
* @return mixed|null
*/
public function getPriceWithConditions()
public function priceWithConditions()
```
To get the subtotal of an item with the conditions applied, use the method
**$item->getPriceSumWithConditions()**
**$item->priceSumWithConditions()**
```php
/**
* get the sum of price in which conditions are already applied
*
* @return mixed|null
*/
public function getPriceSumWithConditions()
public function priceSumWithConditions()
```
......@@ -659,16 +659,16 @@ Cart::add(array(
));
// then you can:
$items = Cart::getContent();
$items = Cart::items();
foreach($items as $item)
{
$item->id; // the Id of the item
$item->name; // the name
$item->price; // the single price without conditions applied
$item->getPriceSum(); // the subtotal without conditions applied
$item->getPriceWithConditions(); // the single price with conditions applied
$item->getPriceSumWithConditions(); // the subtotal with conditions applied
$item->priceSum(); // the subtotal without conditions applied
$item->priceWithConditions(); // the single price with conditions applied
$item->priceSumWithConditions(); // the subtotal with conditions applied
$item->quantity; // the quantity
$item->attributes; // the attributes
......@@ -691,9 +691,9 @@ $items->each(function($item)
$item->id; // the Id of the item
$item->name; // the name
$item->price; // the single price without conditions applied
$item->getPriceSum(); // the subtotal without conditions applied
$item->getPriceWithConditions(); // the single price with conditions applied
$item->getPriceSumWithConditions(); // the subtotal with conditions applied
$item->priceSum(); // the subtotal without conditions applied
$item->priceWithConditions(); // the single price with conditions applied
$item->priceSumWithConditions(); // the subtotal with conditions applied
$item->quantity; // the quantity
$item->attributes; // the attributes
......@@ -716,7 +716,7 @@ $items->each(function($item)
**2.3.0
- added new Cart Method: Cart::addItemCondition($productId, $itemCondition)
- added new Cart Method: Cart::getTotalQuantity()
- added new Cart Method: Cart::totalQuantity()
**2.2.1
- bug fixes
......@@ -730,7 +730,7 @@ $items->each(function($item)
**2.1.0
- added new Cart Method: getCalculatedValue($totalOrSubTotalOrPrice)
- added new Item Method: getPriceSum()
- added new Item Method: priceSum()
**2.0.0 (breaking change)
- major changes in dealing with conditions (Please see [Conditions](#conditions) section, and read carefully)
......
......@@ -46,6 +46,13 @@ class Cart {
*/
protected $sessionKeyCartConditions;
protected $item_rules = array(
'id' => 'required',
'price' => 'required|numeric',
'quantity' => 'required|numeric|min:1',
'name' => 'required',
);
/**
* our object constructor
*
......@@ -53,14 +60,17 @@ class Cart {
* @param $events
* @param $instanceName
* @param $session_key
* @param array $custom_item_rules overwrite existing item_rules
*/
public function __construct($session, $events, $instanceName, $session_key) {
public function __construct($session, $events, $instanceName, $session_key, $custom_item_rules=[]) {
$this->events = $events;
$this->session = $session;
$this->instanceName = $instanceName;
$this->sessionKeyCartItems = $session_key . '_cart_items';
$this->sessionKeyCartConditions = $session_key . '_cart_conditions';
$this->events->fire($this->getInstanceName() . '.created', array($this));
if (!empty($custom_item_rules))
$this->item_rules = $custom_item_rules;
}
/**
......@@ -79,7 +89,7 @@ class Cart {
* @return mixed
*/
public function get($itemId) {
return $this->getContent()->get($itemId);
return $this->items()->get($itemId);
}
/**
......@@ -89,7 +99,7 @@ class Cart {
* @return bool
*/
public function has($itemId) {
return $this->getContent()->has($itemId);
return $this->items()->has($itemId);
}
/**
......@@ -104,7 +114,7 @@ class Cart {
* @return $this
* @throws InvalidItemException
*/
public function add($id, $name = null, $price = null, $quantity = null, $attributes = array(), $conditions = array()) {
public function add($id, $name = null, $price = null, $quantity = 1, $attributes = array(), $conditions = array()) {
// if the first argument is an array,
// we will need to call add again
if (is_array($id)) {
......@@ -117,8 +127,8 @@ class Cart {
$item['name'],
$item['price'],
$item['quantity'],
Helpers::issetAndHasValueOrAssignDefault($item['attributes'], array()),
Helpers::issetAndHasValueOrAssignDefault($item['conditions'], array())
@$item['attributes'] ?: array(),
@$item['conditions'] ?: array()
);
}
} else {
......@@ -127,8 +137,8 @@ class Cart {
$id['name'],
$id['price'],
$id['quantity'],
Helpers::issetAndHasValueOrAssignDefault($id['attributes'], array()),
Helpers::issetAndHasValueOrAssignDefault($id['conditions'], array())
@$id['attributes'] ?: array(),
@$id['conditions'] ?: array()
);
}
......@@ -146,7 +156,7 @@ class Cart {
));
// get the cart
$cart = $this->getContent();
$cart = $this->items();
// if the item is already in the cart we will just update it
if ($cart->has($id)) {
......@@ -173,7 +183,7 @@ class Cart {
public function update($id, $data) {
$this->events->fire($this->getInstanceName() . '.updating', array($data, $this));
$cart = $this->getContent();
$cart = $this->items();
$item = $cart->pull($id);
......@@ -251,7 +261,7 @@ class Cart {
* @param $id
*/
public function remove($id) {
$cart = $this->getContent();
$cart = $this->items();
$this->events->fire($this->getInstanceName() . '.removing', array($id, $this));
......@@ -377,7 +387,7 @@ class Cart {
* @return bool
*/
public function removeItemCondition($itemId, $conditionName) {
if (!$item = $this->getContent()->get($itemId)) {
if (!$item = $this->items()->get($itemId)) {
return false;
}
......@@ -433,7 +443,7 @@ class Cart {
* @return bool
*/
public function clearItemConditions($itemId) {
if (!$item = $this->getContent()->get($itemId)) {
if (!$item = $this->items()->get($itemId)) {
return false;
}
......@@ -463,11 +473,11 @@ class Cart {
*
* @return int
*/
public function getSubTotal() {
$cart = $this->getContent();
public function subTotal() {
$cart = $this->items();
$sum = $cart->sum(function ($item) {
return $item->getPriceSumWithConditions();
return $item->priceSumWithConditions();
});
return Helpers::intval($sum);
......@@ -478,8 +488,8 @@ class Cart {
*
* @return int
*/
public function getTotal() {
$subTotal = $this->getSubTotal();
public function total() {
$subTotal = $this->subTotal();
$newTotal = 0;
......@@ -508,8 +518,8 @@ class Cart {
*
* @return int
*/
public function getTotalQuantity() {
$items = $this->getContent();
public function totalQuantity() {
$items = $this->items();
if ($items->isEmpty()) return 0;
......@@ -525,7 +535,7 @@ class Cart {
*
* @return CartCollection
*/
public function getContent() {
public function items() {
return (new CartCollection($this->session->get($this->sessionKeyCartItems)));
}
......@@ -548,14 +558,7 @@ class Cart {
* @throws InvalidItemException
*/
protected function validate($item) {
$rules = array(
'id' => 'required',
'price' => 'required|numeric',
'quantity' => 'required|numeric|min:1',
'name' => 'required',
);
$validator = CartItemValidator::make($item, $rules);
$validator = CartItemValidator::make($item, $this->item_rules);
if ($validator->fails()) {
throw new InvalidItemException($validator->messages()->first());
......@@ -573,7 +576,7 @@ class Cart {
protected function addRow($id, $item) {
$this->events->fire($this->getInstanceName() . '.adding', array($item, $this));
$cart = $this->getContent();
$cart = $this->items();
$cart->put($id, new ItemCollection($item));
......
......@@ -21,7 +21,7 @@ class CartServiceProvider extends ServiceProvider {
$storage = $app['session'];
$events = $app['events'];
$instanceName = 'cart';
$session_key = '4yTlTDKu3oJOfzD';
$session_key = '__cart';
return new Cart(
$storage,
......
......@@ -77,17 +77,4 @@ class Helpers {
}
}
/**
* check if variable is set and has value, return a default value
*
* @param $var
* @param bool|mixed $default
* @return bool|mixed
*/
public static function issetAndHasValueOrAssignDefault(&$var, $default = false) {
if ((isset($var)) && ($var != '')) return $var;
return $default;
}
}
\ No newline at end of file
......@@ -11,12 +11,21 @@ use Illuminate\Support\Collection;
class ItemCollection extends Collection {
/**
* get the price
*
* @return mixed|null
*/
public function price() {
return $this->price;
}
/**
* get the sum of price
*
* @return mixed|null
*/
public function getPriceSum() {
public function priceSum() {
return $this->price * $this->quantity;
}
......@@ -46,7 +55,7 @@ class ItemCollection extends Collection {
*
* @return mixed|null
*/
public function getPriceWithConditions() {
public function priceWithConditions() {
$originalPrice = $this->price;
$newPrice = 0;
$processed = 0;
......@@ -76,7 +85,7 @@ class ItemCollection extends Collection {
*
* @return mixed|null
*/
public function getPriceSumWithConditions() {
return $this->getPriceWithConditions() * $this->quantity;
public function priceSumWithConditions() {
return $this->priceWithConditions() * $this->quantity;
}
}
......@@ -38,13 +38,13 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
public function test_total_without_condition() {
$this->fillCart();
$this->assertEquals(18749, $this->cart->getSubTotal(), 'Cart should have sub total of 18749');
$this->assertEquals(18749, $this->cart->subTotal(), 'Cart should have sub total of 18749');
// no changes in subtotal as the condition's target added was for total
$this->assertEquals(18749, $this->cart->getSubTotal(), 'Cart should have sub total of 18749');
$this->assertEquals(18749, $this->cart->subTotal(), 'Cart should have sub total of 18749');
// total should be the same as subtotal
$this->assertEquals(18749, $this->cart->getTotal(), 'Cart should have a total of 18749');
$this->assertEquals(18749, $this->cart->total(), 'Cart should have a total of 18749');
}
protected function fillCart() {
......@@ -78,7 +78,7 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
public function test_total_with_condition() {
$this->fillCart();
$this->assertEquals(18749, $this->cart->getSubTotal(), 'Cart should have sub total of 18749');
$this->assertEquals(18749, $this->cart->subTotal(), 'Cart should have sub total of 18749');
// add condition
$condition = new CartCondition(array(
......@@ -91,16 +91,16 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->condition($condition);
// no changes in subtotal as the condition's target added was for total
$this->assertEquals(18749, $this->cart->getSubTotal(), 'Cart should have sub total of 18749');
$this->assertEquals(18749, $this->cart->subTotal(), 'Cart should have sub total of 18749');
// total should be changed
$this->assertEquals(21093, $this->cart->getTotal(), 'Cart should have a total of 21093');
$this->assertEquals(21093, $this->cart->total(), 'Cart should have a total of 21093');
}
public function test_total_with_multiple_conditions_added_scenario_one() {
$this->fillCart();
$this->assertEquals(18749, $this->cart->getSubTotal(), 'Cart should have sub total of 18749');
$this->assertEquals(18749, $this->cart->subTotal(), 'Cart should have sub total of 18749');
// add condition
$condition1 = new CartCondition(array(
......@@ -120,16 +120,16 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->condition($condition2);
// no changes in subtotal as the condition's target added was for subtotal
$this->assertEquals(18749, $this->cart->getSubTotal(), 'Cart should have sub total of 18749');
$this->assertEquals(18749, $this->cart->subTotal(), 'Cart should have sub total of 18749');
// total should be changed
$this->assertEquals(22593, $this->cart->getTotal(), 'Cart should have a total of 22593');
$this->assertEquals(22593, $this->cart->total(), 'Cart should have a total of 22593');
}
public function test_total_with_multiple_conditions_added_scenario_two() {
$this->fillCart();
$this->assertEquals(18749, $this->cart->getSubTotal(), 'Cart should have sub total of 18749');
$this->assertEquals(18749, $this->cart->subTotal(), 'Cart should have sub total of 18749');
// add condition
$condition1 = new CartCondition(array(
......@@ -149,16 +149,16 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->condition($condition2);
// no changes in subtotal as the condition's target added was for subtotal
$this->assertEquals(18749, $this->cart->getSubTotal(), 'Cart should have sub total of 18749');
$this->assertEquals(18749, $this->cart->subTotal(), 'Cart should have sub total of 18749');
// total should be changed
$this->assertEquals(19593, $this->cart->getTotal(), 'Cart should have a total of 19593');
$this->assertEquals(19593, $this->cart->total(), 'Cart should have a total of 19593');
}
public function test_total_with_multiple_conditions_added_scenario_three() {
$this->fillCart();
$this->assertEquals(18749, $this->cart->getSubTotal(), 'Cart should have sub total of 18749');
$this->assertEquals(18749, $this->cart->subTotal(), 'Cart should have sub total of 18749');
// add condition
$condition1 = new CartCondition(array(
......@@ -178,16 +178,16 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->condition($condition2);
// no changes in subtotal as the condition's target added was for total
$this->assertEquals(18749, $this->cart->getSubTotal(), 'Cart should have sub total of 18749');
$this->assertEquals(18749, $this->cart->subTotal(), 'Cart should have sub total of 18749');
// total should be changed
$this->assertEquals(14905, $this->cart->getTotal(), 'Cart should have a total of 14905');
$this->assertEquals(14905, $this->cart->total(), 'Cart should have a total of 14905');
}
public function test_cart_multiple_conditions_can_be_added_once_by_array() {
$this->fillCart();
$this->assertEquals(18749, $this->cart->getSubTotal(), 'Cart should have sub total of 18749');
$this->assertEquals(18749, $this->cart->subTotal(), 'Cart should have sub total of 18749');
// add condition
$condition1 = new CartCondition(array(
......@@ -206,16 +206,16 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->condition([$condition1, $condition2]);
// no changes in subtotal as the condition's target added was for total
$this->assertEquals(18749, $this->cart->getSubTotal(), 'Cart should have sub total of 18749');
$this->assertEquals(18749, $this->cart->subTotal(), 'Cart should have sub total of 18749');
// total should be changed
$this->assertEquals(14905, $this->cart->getTotal(), 'Cart should have a total of 14905');
$this->assertEquals(14905, $this->cart->total(), 'Cart should have a total of 14905');
}
public function test_total_with_multiple_conditions_added_scenario_four() {
$this->fillCart();
$this->assertEquals(18749, $this->cart->getSubTotal(), 'Cart should have sub total of 18749');
$this->assertEquals(18749, $this->cart->subTotal(), 'Cart should have sub total of 18749');
// add condition
$condition1 = new CartCondition(array(
......@@ -235,10 +235,10 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->condition($condition2);
// no changes in subtotal as the condition's target added was for total
$this->assertEquals(18749, $this->cart->getSubTotal(), 'Cart should have sub total of 18749');
$this->assertEquals(18749, $this->cart->subTotal(), 'Cart should have sub total of 18749');
// total should be changed
$this->assertEquals(17905, $this->cart->getTotal(), 'Cart should have a total of 17905');
$this->assertEquals(17905, $this->cart->total(), 'Cart should have a total of 17905');
}
public function test_add_item_with_condition() {
......@@ -260,8 +260,8 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->add($item);
$this->assertEquals(95, $this->cart->get(456)->getPriceSumWithConditions());
$this->assertEquals(95, $this->cart->getSubTotal());
$this->assertEquals(95, $this->cart->get(456)->priceSumWithConditions());
$this->assertEquals(95, $this->cart->subTotal());
}
public function test_add_item_with_multiple_item_conditions_in_multiple_condition_instance() {
......@@ -295,8 +295,8 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->add($item);
$this->assertEquals(8000, $this->cart->get(456)->getPriceSumWithConditions(), 'Item subtotal with 1 item should be 80');
$this->assertEquals(8000, $this->cart->getSubTotal(), 'Cart subtotal with 1 item should be 80');
$this->assertEquals(8000, $this->cart->get(456)->priceSumWithConditions(), 'Item subtotal with 1 item should be 80');
$this->assertEquals(8000, $this->cart->subTotal(), 'Cart subtotal with 1 item should be 80');
}
public function test_add_item_with_multiple_item_conditions_with_one_condition_wrong_target() {
......@@ -342,8 +342,8 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->add($item);
$this->assertEquals(8500, $this->cart->get(456)->getPriceSumWithConditions(), 'Cart subtotal with 1 item should be 85');
$this->assertEquals(8500, $this->cart->getSubTotal(), 'Cart subtotal with 1 item should be 85');
$this->assertEquals(8500, $this->cart->get(456)->priceSumWithConditions(), 'Cart subtotal with 1 item should be 85');
$this->assertEquals(8500, $this->cart->subTotal(), 'Cart subtotal with 1 item should be 85');
}
public function test_add_item_condition() {
......@@ -403,7 +403,7 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
// Since the product price is 20 and the condition reduces it by 25,
// check that the item's price has been prevented from dropping below zero.
$this->assertEquals(0, $this->cart->get($item['id'])->getPriceSumWithConditions(), "The item's price should be prevented from going below zero.");
$this->assertEquals(0, $this->cart->get($item['id'])->priceSumWithConditions(), "The item's price should be prevented from going below zero.");
}
public function test_get_cart_condition_by_condition_name() {
......@@ -645,7 +645,7 @@ class CartConditionTest extends PHPUnit_Framework_TestCase {
$this->cart->condition([$cartCondition1, $cartCondition2]);
$subTotal = $this->cart->getSubTotal();
$subTotal = $this->cart->subTotal();
$this->assertEquals(100, $subTotal, 'Subtotal should be 100');
......
......@@ -38,9 +38,9 @@ class CartTest extends PHPUnit_Framework_TestCase {
$this->cart->add(455, 'Sample Item', 10099, 2, array());
$this->assertFalse($this->cart->isEmpty(), 'Cart should not be empty');
$this->assertEquals(1, $this->cart->getContent()->count(), 'Cart content should be 1');
$this->assertEquals(455, $this->cart->getContent()->first()['id'], 'Item added has ID of 455 so first content ID should be 455');
$this->assertEquals(10099, $this->cart->getContent()->first()['price'], 'Item added has price of 10099 so first content price should be 10099');
$this->assertEquals(1, $this->cart->items()->count(), 'Cart content should be 1');
$this->assertEquals(455, $this->cart->items()->first()['id'], 'Item added has ID of 455 so first content ID should be 455');
$this->assertEquals(10099, $this->cart->items()->first()['price'], 'Item added has price of 10099 so first content price should be 10099');
}
public function test_cart_can_add_items_as_array() {
......@@ -55,9 +55,9 @@ class CartTest extends PHPUnit_Framework_TestCase {
$this->cart->add($item);
$this->assertFalse($this->cart->isEmpty(), 'Cart should not be empty');
$this->assertEquals(1, $this->cart->getContent()->count(), 'Cart should have 1 item on it');
$this->assertEquals(456, $this->cart->getContent()->first()['id'], 'The first content must have ID of 456');
$this->assertEquals('Sample Item', $this->cart->getContent()->first()['name'], 'The first content must have name of "Sample Item"');
$this->assertEquals(1, $this->cart->items()->count(), 'Cart should have 1 item on it');
$this->assertEquals(456, $this->cart->items()->first()['id'], 'The first content must have ID of 456');
$this->assertEquals('Sample Item', $this->cart->items()->first()['name'], 'The first content must have name of "Sample Item"');
}
public function test_cart_can_add_items_with_multidimensional_array() {
......@@ -88,7 +88,7 @@ class CartTest extends PHPUnit_Framework_TestCase {
$this->cart->add($items);
$this->assertFalse($this->cart->isEmpty(), 'Cart should not be empty');
$this->assertCount(3, $this->cart->getContent()->toArray(), 'Cart should have 3 items');
$this->assertCount(3, $this->cart->items()->toArray(), 'Cart should have 3 items');
}
public function test_cart_can_add_item_without_attributes() {
......@@ -151,9 +151,9 @@ class CartTest extends PHPUnit_Framework_TestCase {
$this->cart->add($item);
$this->assertFalse($this->cart->isEmpty(), 'Cart should not be empty');
$this->assertCount(2, $this->cart->getContent()->first()['attributes'], 'Item\'s attribute should have two');
$this->assertEquals('L', $this->cart->getContent()->first()->attributes->size, 'Item should have attribute size of L');
$this->assertEquals('blue', $this->cart->getContent()->first()->attributes->color, 'Item should have attribute color of blue');
$this->assertCount(2, $this->cart->items()->first()['attributes'], 'Item\'s attribute should have two');
$this->assertEquals('L', $this->cart->items()->first()->attributes->size, 'Item should have attribute size of L');
$this->assertEquals('blue', $this->cart->items()->first()->attributes->color, 'Item should have attribute color of blue');
$this->assertTrue($this->cart->get(456)->has('attributes'), 'Item should have attributes');
$this->assertEquals('L', $this->cart->get(456)->get('attributes')->size);
}
......@@ -230,7 +230,7 @@ class CartTest extends PHPUnit_Framework_TestCase {
// add a price in a string format should be converted to int
$this->cart->add(455, 'Sample Item', '10099', 2, array());
$this->assertInternalType('int', $this->cart->getContent()->first()['price'], 'Cart price should be a int');
$this->assertInternalType('int', $this->cart->items()->first()['price'], 'Cart price should be a int');
}
public function test_it_removes_an_item_on_cart_by_item_id() {
......@@ -264,8 +264,8 @@ class CartTest extends PHPUnit_Framework_TestCase {
$this->cart->remove($removeItemId);
$this->assertCount(2, $this->cart->getContent()->toArray(), 'Cart must have 2 items left');
$this->assertFalse($this->cart->getContent()->has($removeItemId), 'Cart must have not contain the remove item anymore');
$this->assertCount(2, $this->cart->items()->toArray(), 'Cart must have 2 items left');
$this->assertFalse($this->cart->items()->has($removeItemId), 'Cart must have not contain the remove item anymore');
}
public function test_cart_sub_total() {
......@@ -295,12 +295,12 @@ class CartTest extends PHPUnit_Framework_TestCase {
$this->cart->add($items);
$this->assertEquals(18749, $this->cart->getSubTotal(), 'Cart should have sub total of 18749');
$this->assertEquals(18749, $this->cart->subTotal(), 'Cart should have sub total of 18749');
// if we remove an item, the sub total should be updated as well
$this->cart->remove(456);
$this->assertEquals(11950, $this->cart->getSubTotal(), 'Cart should have sub total of 1195');
$this->assertEquals(11950, $this->cart->subTotal(), 'Cart should have sub total of 1195');
}
public function test_sub_total_when_item_quantity_is_updated() {
...