mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 13:51:44 +00:00
feat: add DatabaseDriver
Implements CartDriver for Eloquent-backed persistence using firstOrNew pattern. Includes 9 unit tests covering CRUD, flush, clearItems, and model retrieval.
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Wearepixel\Cart\Drivers;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Wearepixel\Cart\Drivers\Contracts\CartDriver;
|
||||
|
||||
class DatabaseDriver implements CartDriver
|
||||
{
|
||||
private Model $model;
|
||||
|
||||
public function __construct(
|
||||
private string $modelClass,
|
||||
private string $idColumn,
|
||||
private string $itemsColumn,
|
||||
private string $conditionsColumn,
|
||||
private string $sessionKey,
|
||||
) {
|
||||
$this->model = (new $modelClass)->firstOrNew([$idColumn => $sessionKey]);
|
||||
$this->model[$itemsColumn] ??= [];
|
||||
$this->model[$conditionsColumn] ??= [];
|
||||
}
|
||||
|
||||
public function getSessionKey(): string
|
||||
{
|
||||
return $this->sessionKey;
|
||||
}
|
||||
|
||||
public function setSessionKey(string $sessionKey): void
|
||||
{
|
||||
$this->sessionKey = $sessionKey;
|
||||
$this->model[$this->idColumn] = $sessionKey;
|
||||
$this->model->save();
|
||||
}
|
||||
|
||||
public function getDriverName(): string
|
||||
{
|
||||
return 'database';
|
||||
}
|
||||
|
||||
public function getItems(): array
|
||||
{
|
||||
return $this->model[$this->itemsColumn] ?? [];
|
||||
}
|
||||
|
||||
public function putItems(array $items): void
|
||||
{
|
||||
$this->model[$this->itemsColumn] = $items;
|
||||
$this->model->save();
|
||||
}
|
||||
|
||||
public function getConditions(): array
|
||||
{
|
||||
return $this->model[$this->conditionsColumn] ?? [];
|
||||
}
|
||||
|
||||
public function putConditions(array $conditions): void
|
||||
{
|
||||
$this->model[$this->conditionsColumn] = $conditions;
|
||||
$this->model->save();
|
||||
}
|
||||
|
||||
public function clearItems(): void
|
||||
{
|
||||
$this->model[$this->itemsColumn] = [];
|
||||
$this->model->save();
|
||||
}
|
||||
|
||||
public function flush(): void
|
||||
{
|
||||
$this->model->delete();
|
||||
$this->model[$this->itemsColumn] = [];
|
||||
$this->model[$this->conditionsColumn] = [];
|
||||
}
|
||||
|
||||
public function getSessionModel(): mixed
|
||||
{
|
||||
return $this->model;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Wearepixel\Cart\Drivers\DatabaseDriver;
|
||||
use Wearepixel\Cart\Tests\Helpers\MockCartModel;
|
||||
|
||||
beforeEach(function () {
|
||||
$this->driver = new DatabaseDriver(
|
||||
MockCartModel::class,
|
||||
'session_id',
|
||||
'items',
|
||||
'conditions',
|
||||
'test-session',
|
||||
);
|
||||
});
|
||||
|
||||
describe('DatabaseDriver', function () {
|
||||
test('returns its session key', function () {
|
||||
expect($this->driver->getSessionKey())->toBe('test-session');
|
||||
});
|
||||
|
||||
test('returns database as driver name', function () {
|
||||
expect($this->driver->getDriverName())->toBe('database');
|
||||
});
|
||||
|
||||
test('starts with empty items', function () {
|
||||
expect($this->driver->getItems())->toBe([]);
|
||||
});
|
||||
|
||||
test('stores and retrieves items', function () {
|
||||
$items = [['id' => 1, 'name' => 'Widget']];
|
||||
$this->driver->putItems($items);
|
||||
|
||||
$fresh = new DatabaseDriver(MockCartModel::class, 'session_id', 'items', 'conditions', 'test-session');
|
||||
expect($fresh->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);
|
||||
|
||||
$fresh = new DatabaseDriver(MockCartModel::class, 'session_id', 'items', 'conditions', 'test-session');
|
||||
expect($fresh->getConditions())->toBe($conditions);
|
||||
});
|
||||
|
||||
test('flush deletes the database record', function () {
|
||||
$this->driver->putItems([['id' => 1]]);
|
||||
$this->driver->flush();
|
||||
|
||||
expect(MockCartModel::count())->toBe(0);
|
||||
});
|
||||
|
||||
test('clearItems empties items but keeps conditions', function () {
|
||||
$this->driver->putItems([['id' => 1]]);
|
||||
$this->driver->putConditions([['name' => 'GST']]);
|
||||
$this->driver->clearItems();
|
||||
|
||||
$fresh = new DatabaseDriver(MockCartModel::class, 'session_id', 'items', 'conditions', 'test-session');
|
||||
expect($fresh->getItems())->toBe([]);
|
||||
expect($fresh->getConditions())->toBe([['name' => 'GST']]);
|
||||
});
|
||||
|
||||
test('getSessionModel returns the Eloquent model', function () {
|
||||
expect($this->driver->getSessionModel())->toBeInstanceOf(MockCartModel::class);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user