mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 13:51:44 +00:00
fix: prevent save() on array after clear() with database driver
This commit is contained in:
+2
-4
@@ -159,10 +159,8 @@ public function clear()
|
||||
|
||||
$deleted = $this->session->delete();
|
||||
|
||||
$this->session = [
|
||||
$this->itemsKey => [],
|
||||
$this->conditionsKey => [],
|
||||
];
|
||||
$this->session[$this->itemsKey] = [];
|
||||
$this->session[$this->conditionsKey] = [];
|
||||
|
||||
return $deleted;
|
||||
}
|
||||
|
||||
@@ -123,4 +123,23 @@
|
||||
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');
|
||||
});
|
||||
|
||||
test('can clear cart conditions after clearing the cart', function () {
|
||||
$condition = new CartCondition([
|
||||
'name' => '10% Off',
|
||||
'type' => 'coupon',
|
||||
'target' => 'subtotal',
|
||||
'value' => '-10%',
|
||||
]);
|
||||
|
||||
$this->cart->add(1, 'Sample Item', 100.99, 2, []);
|
||||
$this->cart->condition($condition);
|
||||
|
||||
$this->cart->clear();
|
||||
|
||||
// This was throwing "Call to a member function save() on array"
|
||||
// because clear() replaced $this->session with a plain array
|
||||
expect(fn () => $this->cart->clearCartConditions())->not->toThrow(Error::class);
|
||||
expect($this->cart->getConditions())->toBeEmpty();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user