mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 22:01:48 +00:00
fix: guard null writes in SessionDriver::setSessionKey
This commit is contained in:
@@ -37,8 +37,13 @@ public function setSessionKey(string $sessionKey): void
|
|||||||
$this->itemsKey = $sessionKey . '_cart_items';
|
$this->itemsKey = $sessionKey . '_cart_items';
|
||||||
$this->conditionsKey = $sessionKey . '_cart_conditions';
|
$this->conditionsKey = $sessionKey . '_cart_conditions';
|
||||||
|
|
||||||
$this->session->put($this->itemsKey, $items);
|
if ($items !== null) {
|
||||||
$this->session->put($this->conditionsKey, $conditions);
|
$this->session->put($this->itemsKey, $items);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($conditions !== null) {
|
||||||
|
$this->session->put($this->conditionsKey, $conditions);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDriverName(): string
|
public function getDriverName(): string
|
||||||
|
|||||||
@@ -70,4 +70,15 @@
|
|||||||
test('getSessionModel returns null', function () {
|
test('getSessionModel returns null', function () {
|
||||||
expect($this->driver->getSessionModel())->toBeNull();
|
expect($this->driver->getSessionModel())->toBeNull();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('setSessionKey on empty cart does not write null to session', function () {
|
||||||
|
$this->driver->setSessionKey('new-key');
|
||||||
|
|
||||||
|
// After key change, empty cart should still return empty arrays, not null
|
||||||
|
expect($this->driver->getItems())->toBe([]);
|
||||||
|
expect($this->driver->getConditions())->toBe([]);
|
||||||
|
// And the session should not have null values written
|
||||||
|
expect($this->session->get('new-key_cart_items'))->toBeNull();
|
||||||
|
expect($this->session->get('new-key_cart_conditions'))->toBeNull();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user