From 83aa9a1431f7398306710f40f8252cbc5760fb04 Mon Sep 17 00:00:00 2001 From: Joel Male Date: Wed, 27 May 2026 20:59:45 +1000 Subject: [PATCH] feat: add MultiDriver --- src/Drivers/MultiDriver.php | 84 ++++++++++++++++++++++++++ tests/Unit/Drivers/MultiDriverTest.php | 80 ++++++++++++++++++++++++ 2 files changed, 164 insertions(+) create mode 100644 src/Drivers/MultiDriver.php create mode 100644 tests/Unit/Drivers/MultiDriverTest.php diff --git a/src/Drivers/MultiDriver.php b/src/Drivers/MultiDriver.php new file mode 100644 index 0000000..4278006 --- /dev/null +++ b/src/Drivers/MultiDriver.php @@ -0,0 +1,84 @@ +drivers[0]; + } + + public function getSessionKey(): string + { + return $this->primary()->getSessionKey(); + } + + public function setSessionKey(string $sessionKey): void + { + foreach ($this->drivers as $driver) { + $driver->setSessionKey($sessionKey); + } + } + + public function getDriverName(): string + { + return 'multi'; + } + + public function getItems(): array + { + return $this->primary()->getItems(); + } + + public function putItems(array $items): void + { + foreach ($this->drivers as $driver) { + $driver->putItems($items); + } + } + + public function getConditions(): array + { + return $this->primary()->getConditions(); + } + + public function putConditions(array $conditions): void + { + foreach ($this->drivers as $driver) { + $driver->putConditions($conditions); + } + } + + public function clearItems(): void + { + foreach ($this->drivers as $driver) { + $driver->clearItems(); + } + } + + public function flush(): void + { + foreach ($this->drivers as $driver) { + $driver->flush(); + } + } + + public function getSessionModel(): mixed + { + return $this->primary()->getSessionModel(); + } +} diff --git a/tests/Unit/Drivers/MultiDriverTest.php b/tests/Unit/Drivers/MultiDriverTest.php new file mode 100644 index 0000000..0a16a9a --- /dev/null +++ b/tests/Unit/Drivers/MultiDriverTest.php @@ -0,0 +1,80 @@ +primary = new NullDriver('test-session'); + $this->secondary = new NullDriver('test-session'); + $this->driver = new MultiDriver([$this->primary, $this->secondary]); + }); + + test('returns session key from primary driver', function () { + expect($this->driver->getSessionKey())->toBe('test-session'); + }); + + test('returns multi as driver name', function () { + expect($this->driver->getDriverName())->toBe('multi'); + }); + + test('reads items from primary driver', function () { + $this->primary->putItems([['id' => 1]]); + $this->secondary->putItems([['id' => 2]]); + + expect($this->driver->getItems())->toBe([['id' => 1]]); + }); + + test('writes items to all drivers', function () { + $this->driver->putItems([['id' => 1]]); + + expect($this->primary->getItems())->toBe([['id' => 1]]); + expect($this->secondary->getItems())->toBe([['id' => 1]]); + }); + + test('reads conditions from primary driver', function () { + $this->primary->putConditions([['name' => 'GST']]); + $this->secondary->putConditions([['name' => 'Other']]); + + expect($this->driver->getConditions())->toBe([['name' => 'GST']]); + }); + + test('writes conditions to all drivers', function () { + $this->driver->putConditions([['name' => 'GST']]); + + expect($this->primary->getConditions())->toBe([['name' => 'GST']]); + expect($this->secondary->getConditions())->toBe([['name' => 'GST']]); + }); + + test('clearItems clears items on all drivers', function () { + $this->driver->putItems([['id' => 1]]); + $this->driver->clearItems(); + + expect($this->primary->getItems())->toBe([]); + expect($this->secondary->getItems())->toBe([]); + }); + + test('flush flushes all drivers', function () { + $this->driver->putItems([['id' => 1]]); + $this->driver->putConditions([['name' => 'GST']]); + $this->driver->flush(); + + expect($this->primary->getItems())->toBe([]); + expect($this->primary->getConditions())->toBe([]); + expect($this->secondary->getItems())->toBe([]); + expect($this->secondary->getConditions())->toBe([]); + }); + + test('setSessionKey updates all drivers', function () { + $this->driver->setSessionKey('new-key'); + + expect($this->primary->getSessionKey())->toBe('new-key'); + expect($this->secondary->getSessionKey())->toBe('new-key'); + }); + + test('throws when constructed with no drivers', function () { + expect(fn() => new MultiDriver([]))->toThrow(InvalidArgumentException::class); + }); +});