feat: add SessionDriver

This commit is contained in:
Joel Male
2026-05-27 20:50:47 +10:00
parent 11e23a946e
commit c9f48d7e88
2 changed files with 157 additions and 0 deletions
+84
View File
@@ -0,0 +1,84 @@
<?php
declare(strict_types=1);
namespace Wearepixel\Cart\Drivers;
use Wearepixel\Cart\Drivers\Contracts\CartDriver;
class SessionDriver implements CartDriver
{
private string $itemsKey;
private string $conditionsKey;
public function __construct(
private mixed $session,
private string $sessionKey,
) {
$this->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;
}
}
+73
View File
@@ -0,0 +1,73 @@
<?php
declare(strict_types=1);
use Wearepixel\Cart\Drivers\SessionDriver;
use Wearepixel\Cart\Tests\Helpers\SessionMock;
describe('SessionDriver', function () {
beforeEach(function () {
$this->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();
});
});