mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 22:01:48 +00:00
feat: finish tests & docs
This commit is contained in:
@@ -300,10 +300,20 @@ #### Check if cart is empty: **Cart::isEmpty()**
|
|||||||
|
|
||||||
#### Clearing the Cart: **Cart::clear()**
|
#### Clearing the Cart: **Cart::clear()**
|
||||||
|
|
||||||
|
This clears all items and conditions from the cart.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
Cart::clear();
|
Cart::clear();
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Clearing the cart items only: **Cart::clearItems()**
|
||||||
|
|
||||||
|
This clears all items, but keeps the conditions (useful for when you want to keep the conditions but remove the items)
|
||||||
|
|
||||||
|
```php
|
||||||
|
Cart::clearItems();
|
||||||
|
```
|
||||||
|
|
||||||
### Conditions
|
### Conditions
|
||||||
|
|
||||||
Conditions are very useful for adding things like discounts, taxes, shipping, etc.
|
Conditions are very useful for adding things like discounts, taxes, shipping, etc.
|
||||||
|
|||||||
+15
-12
@@ -268,18 +268,26 @@ public function remove($id)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* clear cart
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function clear()
|
public function clear()
|
||||||
{
|
{
|
||||||
if ($this->fireEvent('Clearing') === false) {
|
if ($this->fireEvent('Clearing') === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->session->putItems([]);
|
$this->session->clear();
|
||||||
|
|
||||||
|
$this->fireEvent('Cleared');
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clearItems()
|
||||||
|
{
|
||||||
|
if ($this->fireEvent('Clearing') === false) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->session->clearItems();
|
||||||
|
|
||||||
$this->fireEvent('Cleared');
|
$this->fireEvent('Cleared');
|
||||||
|
|
||||||
@@ -654,12 +662,7 @@ public function getContent(): CartCollection
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function isEmpty($test = null)
|
||||||
* check if cart is empty
|
|
||||||
*
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function isEmpty()
|
|
||||||
{
|
{
|
||||||
return $this->getContent()->isEmpty();
|
return $this->getContent()->isEmpty();
|
||||||
}
|
}
|
||||||
|
|||||||
+31
-24
@@ -32,10 +32,8 @@ public function __construct($session, $sessionKey, $config)
|
|||||||
$this->itemsKey = $config['storage']['database']['items'];
|
$this->itemsKey = $config['storage']['database']['items'];
|
||||||
$this->conditionsKey = $config['storage']['database']['conditions'];
|
$this->conditionsKey = $config['storage']['database']['conditions'];
|
||||||
|
|
||||||
// find or create the session in the database
|
|
||||||
$this->session = $session->firstOrNew([$this->sessionId => $this->sessionKey]);
|
$this->session = $session->firstOrNew([$this->sessionId => $this->sessionKey]);
|
||||||
|
|
||||||
// initialize the items and conditions
|
|
||||||
$this->session[$this->itemsKey] = $this->session[$this->itemsKey] ?? [];
|
$this->session[$this->itemsKey] = $this->session[$this->itemsKey] ?? [];
|
||||||
$this->session[$this->conditionsKey] = $this->session[$this->conditionsKey] ?? [];
|
$this->session[$this->conditionsKey] = $this->session[$this->conditionsKey] ?? [];
|
||||||
}
|
}
|
||||||
@@ -56,22 +54,22 @@ public function has($key)
|
|||||||
|
|
||||||
public function getItems()
|
public function getItems()
|
||||||
{
|
{
|
||||||
return ! $this->customStorage ? $this->session->get($this->itemsKey) : $this->session[$this->itemsKey];
|
return $this->driver == 'session' ? $this->session->get($this->itemsKey) : $this->session[$this->itemsKey];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getConditions()
|
public function getConditions()
|
||||||
{
|
{
|
||||||
return ! $this->customStorage ? $this->session->get($this->conditionsKey) : $this->session[$this->conditionsKey];
|
return $this->driver == 'session' ? $this->session->get($this->conditionsKey) : $this->session[$this->conditionsKey];
|
||||||
}
|
}
|
||||||
|
|
||||||
public function putItems($value)
|
public function putItems($value)
|
||||||
{
|
{
|
||||||
if (! $this->customStorage) {
|
if ($this->driver == 'session') {
|
||||||
return $this->session->put($this->itemsKey, $value);
|
return $this->session->put($this->itemsKey, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert any conditions to arrays
|
// convert any conditions to arrays
|
||||||
$value = $value->toArray();
|
$value = is_array($value) ? $value : $value->toArray();
|
||||||
|
|
||||||
foreach ($value as $key => $item) {
|
foreach ($value as $key => $item) {
|
||||||
if ($item['conditions']) {
|
if ($item['conditions']) {
|
||||||
@@ -85,17 +83,6 @@ public function putItems($value)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if ($value['conditions']) {
|
|
||||||
// $value['conditions'] = $value['conditions']->map(function ($condition) {
|
|
||||||
// return $condition->toArray();
|
|
||||||
// });
|
|
||||||
// }
|
|
||||||
|
|
||||||
// dd($value);
|
|
||||||
// $value = $value->map(function ($item) {
|
|
||||||
// return $item->toArray();
|
|
||||||
// });
|
|
||||||
|
|
||||||
$this->session[$this->itemsKey] = $value;
|
$this->session[$this->itemsKey] = $value;
|
||||||
$this->session->save();
|
$this->session->save();
|
||||||
|
|
||||||
@@ -104,7 +91,7 @@ public function putItems($value)
|
|||||||
|
|
||||||
public function putConditions($value)
|
public function putConditions($value)
|
||||||
{
|
{
|
||||||
if (! $this->customStorage) {
|
if ($this->driver == 'session') {
|
||||||
return $this->session->put($this->conditionsKey, $value);
|
return $this->session->put($this->conditionsKey, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -120,16 +107,36 @@ public function putConditions($value)
|
|||||||
return $this->session;
|
return $this->session;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function clear()
|
public function clearItems()
|
||||||
{
|
{
|
||||||
if ($this->customStorage) {
|
if ($this->driver == 'session') {
|
||||||
return $this->session->delete();
|
$this->session->forget($this->itemsKey);
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->session->forget($this->itemsKey);
|
$this->session[$this->itemsKey] = [];
|
||||||
$this->session->forget($this->conditionsKey);
|
|
||||||
|
|
||||||
return true;
|
return $this->session->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clear()
|
||||||
|
{
|
||||||
|
if ($this->driver == 'session') {
|
||||||
|
$this->session->forget($this->itemsKey);
|
||||||
|
$this->session->forget($this->conditionsKey);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
$deleted = $this->session->delete();
|
||||||
|
|
||||||
|
$this->session = [
|
||||||
|
$this->itemsKey => [],
|
||||||
|
$this->conditionsKey => [],
|
||||||
|
];
|
||||||
|
|
||||||
|
return $deleted;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSessionModel()
|
public function getSessionModel()
|
||||||
|
|||||||
@@ -1065,7 +1065,7 @@
|
|||||||
|
|
||||||
expect($this->cart->getTotal())->toEqual(90, 'Cart should have total of 90');
|
expect($this->cart->getTotal())->toEqual(90, 'Cart should have total of 90');
|
||||||
|
|
||||||
$this->cart->clear();
|
$this->cart->clearItems();
|
||||||
|
|
||||||
$this->cart->add([
|
$this->cart->add([
|
||||||
'id' => 1,
|
'id' => 1,
|
||||||
@@ -1099,7 +1099,7 @@
|
|||||||
|
|
||||||
expect($this->cart->getTotal())->toEqual(90, 'Cart should have total of 90');
|
expect($this->cart->getTotal())->toEqual(90, 'Cart should have total of 90');
|
||||||
|
|
||||||
$this->cart->clear();
|
$this->cart->clearItems();
|
||||||
|
|
||||||
$this->cart->add([
|
$this->cart->add([
|
||||||
'id' => 1,
|
'id' => 1,
|
||||||
@@ -1201,7 +1201,7 @@
|
|||||||
|
|
||||||
expect($this->cart->getConditions(active: true))->toHaveCount(0, 'Cart should have 0 conditions');
|
expect($this->cart->getConditions(active: true))->toHaveCount(0, 'Cart should have 0 conditions');
|
||||||
|
|
||||||
$this->cart->clear();
|
$this->cart->clearItems();
|
||||||
|
|
||||||
$this->cart->add([
|
$this->cart->add([
|
||||||
'id' => 1,
|
'id' => 1,
|
||||||
|
|||||||
@@ -107,4 +107,20 @@
|
|||||||
expect($this->cart->getSubTotal())->toEqual(18.45, 'Cart should have subtotal of 18.45');
|
expect($this->cart->getSubTotal())->toEqual(18.45, 'Cart should have subtotal of 18.45');
|
||||||
expect($this->cart->getTotal())->toEqual(18.45, 'Cart should have total of 18.45');
|
expect($this->cart->getTotal())->toEqual(18.45, 'Cart should have total of 18.45');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('can clear a simple cart', function () {
|
||||||
|
$this->cart->add(1, 'Sample Item', 100.99, 2, []);
|
||||||
|
|
||||||
|
expect($this->cart->isEmpty())->toBeFalse('Cart should not be empty');
|
||||||
|
expect($this->cart->getContent()->count())->toEqual(1, 'Cart content should be 1');
|
||||||
|
expect($this->cart->getContent()->first()['id'])->toEqual(1, 'Item added has ID of 1 so first content ID should be 1');
|
||||||
|
expect($this->cart->getContent()->first()['price'])->toEqual(100.99, 'Item added has price of 100.99 so first content price should be 100.99');
|
||||||
|
expect(MockCartModel::all()->count())->toEqual(1, 'The carts database table should have 1 row');
|
||||||
|
|
||||||
|
$this->cart->clear();
|
||||||
|
|
||||||
|
expect($this->cart->isEmpty())->toBeTrue('Cart should be empty');
|
||||||
|
expect($this->cart->getContent()->count())->toEqual(0, 'Cart content should be 0');
|
||||||
|
expect(MockCartModel::all()->count())->toEqual(0, 'The carts database table should have 0 rows');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user