feat: add NullDriver

This commit is contained in:
Joel Male
2026-05-27 20:47:14 +10:00
parent c9f3d76366
commit b3c4c53e63
2 changed files with 129 additions and 0 deletions
+67
View File
@@ -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;
}
}
+62
View File
@@ -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();
});
});