fix: guard null writes in SessionDriver::setSessionKey

This commit is contained in:
Joel Male
2026-05-27 20:53:19 +10:00
parent c9f48d7e88
commit 8927ef08a0
2 changed files with 18 additions and 2 deletions
+7 -2
View File
@@ -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
+11
View File
@@ -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();
});
}); });