diff --git a/src/Drivers/SessionDriver.php b/src/Drivers/SessionDriver.php new file mode 100644 index 0000000..8af5687 --- /dev/null +++ b/src/Drivers/SessionDriver.php @@ -0,0 +1,84 @@ +itemsKey = $sessionKey . '_cart_items'; + $this->conditionsKey = $sessionKey . '_cart_conditions'; + } + + public function getSessionKey(): string + { + return $this->sessionKey; + } + + public function setSessionKey(string $sessionKey): void + { + $items = $this->session->get($this->itemsKey); + $conditions = $this->session->get($this->conditionsKey); + + $this->session->forget($this->itemsKey); + $this->session->forget($this->conditionsKey); + + $this->sessionKey = $sessionKey; + $this->itemsKey = $sessionKey . '_cart_items'; + $this->conditionsKey = $sessionKey . '_cart_conditions'; + + $this->session->put($this->itemsKey, $items); + $this->session->put($this->conditionsKey, $conditions); + } + + public function getDriverName(): string + { + return 'session'; + } + + public function getItems(): array + { + return $this->session->get($this->itemsKey) ?? []; + } + + public function putItems(array $items): void + { + $this->session->put($this->itemsKey, $items); + } + + public function getConditions(): array + { + return $this->session->get($this->conditionsKey) ?? []; + } + + public function putConditions(array $conditions): void + { + $this->session->put($this->conditionsKey, $conditions); + } + + public function clearItems(): void + { + $this->session->forget($this->itemsKey); + } + + public function flush(): void + { + $this->session->forget($this->itemsKey); + $this->session->forget($this->conditionsKey); + } + + public function getSessionModel(): mixed + { + return null; + } +} diff --git a/tests/Unit/Drivers/SessionDriverTest.php b/tests/Unit/Drivers/SessionDriverTest.php new file mode 100644 index 0000000..c6c3f15 --- /dev/null +++ b/tests/Unit/Drivers/SessionDriverTest.php @@ -0,0 +1,73 @@ +session = new SessionMock; + $this->driver = new SessionDriver($this->session, 'test-session'); + }); + + test('returns its session key', function () { + expect($this->driver->getSessionKey())->toBe('test-session'); + }); + + test('returns session as driver name', function () { + expect($this->driver->getDriverName())->toBe('session'); + }); + + test('starts with empty items', function () { + expect($this->driver->getItems())->toBe([]); + }); + + test('stores and retrieves items', function () { + $items = [['id' => 1, 'name' => 'Widget', 'price' => 10.0]]; + $this->driver->putItems($items); + expect($this->driver->getItems())->toBe($items); + }); + + test('starts with empty conditions', function () { + expect($this->driver->getConditions())->toBe([]); + }); + + test('stores and retrieves conditions', function () { + $conditions = [['name' => 'GST', 'type' => 'tax', 'value' => '10%']]; + $this->driver->putConditions($conditions); + expect($this->driver->getConditions())->toBe($conditions); + }); + + test('clearItems removes items from session', function () { + $this->driver->putItems([['id' => 1]]); + $this->driver->putConditions([['name' => 'GST']]); + $this->driver->clearItems(); + + expect($this->driver->getItems())->toBe([]); + expect($this->driver->getConditions())->toBe([['name' => 'GST']]); + }); + + test('flush removes items and conditions from session', function () { + $this->driver->putItems([['id' => 1]]); + $this->driver->putConditions([['name' => 'GST']]); + $this->driver->flush(); + + expect($this->driver->getItems())->toBe([]); + expect($this->driver->getConditions())->toBe([]); + }); + + test('setSessionKey migrates data to new key', function () { + $this->driver->putItems([['id' => 1]]); + $this->driver->putConditions([['name' => 'GST']]); + $this->driver->setSessionKey('new-key'); + + expect($this->driver->getSessionKey())->toBe('new-key'); + expect($this->driver->getItems())->toBe([['id' => 1]]); + expect($this->driver->getConditions())->toBe([['name' => 'GST']]); + }); + + test('getSessionModel returns null', function () { + expect($this->driver->getSessionModel())->toBeNull(); + }); +});