mirror of
https://github.com/wearepixel/laravel-cart.git
synced 2026-11-14 22:01:48 +00:00
feat: add NullDriver
This commit is contained in:
@@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Wearepixel\Cart\Drivers;
|
||||||
|
|
||||||
|
use Wearepixel\Cart\Drivers\Contracts\CartDriver;
|
||||||
|
|
||||||
|
class NullDriver implements CartDriver
|
||||||
|
{
|
||||||
|
private array $items = [];
|
||||||
|
|
||||||
|
private array $conditions = [];
|
||||||
|
|
||||||
|
public function __construct(private string $sessionKey) {}
|
||||||
|
|
||||||
|
public function getSessionKey(): string
|
||||||
|
{
|
||||||
|
return $this->sessionKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setSessionKey(string $sessionKey): void
|
||||||
|
{
|
||||||
|
$this->sessionKey = $sessionKey;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDriverName(): string
|
||||||
|
{
|
||||||
|
return 'null';
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getItems(): array
|
||||||
|
{
|
||||||
|
return $this->items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function putItems(array $items): void
|
||||||
|
{
|
||||||
|
$this->items = $items;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getConditions(): array
|
||||||
|
{
|
||||||
|
return $this->conditions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function putConditions(array $conditions): void
|
||||||
|
{
|
||||||
|
$this->conditions = $conditions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function clearItems(): void
|
||||||
|
{
|
||||||
|
$this->items = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function flush(): void
|
||||||
|
{
|
||||||
|
$this->items = [];
|
||||||
|
$this->conditions = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getSessionModel(): mixed
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Wearepixel\Cart\Drivers\NullDriver;
|
||||||
|
|
||||||
|
describe('NullDriver', function () {
|
||||||
|
beforeEach(function () {
|
||||||
|
$this->driver = new NullDriver('test-session');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('returns its session key', function () {
|
||||||
|
expect($this->driver->getSessionKey())->toBe('test-session');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('returns null as driver name', function () {
|
||||||
|
expect($this->driver->getDriverName())->toBe('null');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('starts with empty items', function () {
|
||||||
|
expect($this->driver->getItems())->toBe([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('stores and retrieves items', function () {
|
||||||
|
$this->driver->putItems([['id' => 1, 'name' => 'Widget']]);
|
||||||
|
expect($this->driver->getItems())->toBe([['id' => 1, 'name' => 'Widget']]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('starts with empty conditions', function () {
|
||||||
|
expect($this->driver->getConditions())->toBe([]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('stores and retrieves conditions', function () {
|
||||||
|
$this->driver->putConditions([['name' => 'GST', 'type' => 'tax', 'value' => '10%']]);
|
||||||
|
expect($this->driver->getConditions())->toBe([['name' => 'GST', 'type' => 'tax', 'value' => '10%']]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('clearItems empties items but keeps conditions', 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 empties items and conditions', 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 updates the key', function () {
|
||||||
|
$this->driver->setSessionKey('new-key');
|
||||||
|
expect($this->driver->getSessionKey())->toBe('new-key');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getSessionModel returns null', function () {
|
||||||
|
expect($this->driver->getSessionModel())->toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user