feat: finish tests & docs

This commit is contained in:
Joel Male
2025-02-25 16:54:03 +10:00
parent 7adf5ecd1f
commit 4cf078277d
5 changed files with 75 additions and 39 deletions
+10
View File
@@ -300,10 +300,20 @@ #### Check if cart is empty: **Cart::isEmpty()**
#### Clearing the Cart: **Cart::clear()**
This clears all items and conditions from the cart.
```php
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 are very useful for adding things like discounts, taxes, shipping, etc.
+15 -12
View File
@@ -268,18 +268,26 @@ public function remove($id)
return true;
}
/**
* clear cart
*
* @return bool
*/
public function clear()
{
if ($this->fireEvent('Clearing') === 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');
@@ -654,12 +662,7 @@ public function getContent(): CartCollection
});
}
/**
* check if cart is empty
*
* @return bool
*/
public function isEmpty()
public function isEmpty($test = null)
{
return $this->getContent()->isEmpty();
}
+31 -24
View File
@@ -32,10 +32,8 @@ public function __construct($session, $sessionKey, $config)
$this->itemsKey = $config['storage']['database']['items'];
$this->conditionsKey = $config['storage']['database']['conditions'];
// find or create the session in the database
$this->session = $session->firstOrNew([$this->sessionId => $this->sessionKey]);
// initialize the items and conditions
$this->session[$this->itemsKey] = $this->session[$this->itemsKey] ?? [];
$this->session[$this->conditionsKey] = $this->session[$this->conditionsKey] ?? [];
}
@@ -56,22 +54,22 @@ public function has($key)
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()
{
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)
{
if (! $this->customStorage) {
if ($this->driver == 'session') {
return $this->session->put($this->itemsKey, $value);
}
// convert any conditions to arrays
$value = $value->toArray();
$value = is_array($value) ? $value : $value->toArray();
foreach ($value as $key => $item) {
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->save();
@@ -104,7 +91,7 @@ public function putItems($value)
public function putConditions($value)
{
if (! $this->customStorage) {
if ($this->driver == 'session') {
return $this->session->put($this->conditionsKey, $value);
}
@@ -120,16 +107,36 @@ public function putConditions($value)
return $this->session;
}
public function clear()
public function clearItems()
{
if ($this->customStorage) {
return $this->session->delete();
if ($this->driver == 'session') {
$this->session->forget($this->itemsKey);
return true;
}
$this->session->forget($this->itemsKey);
$this->session->forget($this->conditionsKey);
$this->session[$this->itemsKey] = [];
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()
+3 -3
View File
@@ -1065,7 +1065,7 @@
expect($this->cart->getTotal())->toEqual(90, 'Cart should have total of 90');
$this->cart->clear();
$this->cart->clearItems();
$this->cart->add([
'id' => 1,
@@ -1099,7 +1099,7 @@
expect($this->cart->getTotal())->toEqual(90, 'Cart should have total of 90');
$this->cart->clear();
$this->cart->clearItems();
$this->cart->add([
'id' => 1,
@@ -1201,7 +1201,7 @@
expect($this->cart->getConditions(active: true))->toHaveCount(0, 'Cart should have 0 conditions');
$this->cart->clear();
$this->cart->clearItems();
$this->cart->add([
'id' => 1,
+16
View File
@@ -107,4 +107,20 @@
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');
});
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');
});
});