-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b27682d
commit bbd7f5e
Showing
5 changed files
with
186 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Store; | ||
|
||
use Closure; | ||
use Patchlevel\EventSourcing\Message\Message; | ||
use Patchlevel\EventSourcing\Store\Criteria\Criteria; | ||
use Psr\Log\LoggerInterface; | ||
|
||
final class StreamReadOnlyStore implements StreamStore | ||
{ | ||
public function __construct( | ||
private readonly StreamStore $store, | ||
private readonly LoggerInterface|null $logger = null, | ||
) { | ||
} | ||
|
||
public function load( | ||
Criteria|null $criteria = null, | ||
int|null $limit = null, | ||
int|null $offset = null, | ||
bool $backwards = false, | ||
): Stream { | ||
return $this->store->load($criteria, $limit, $offset, $backwards); | ||
} | ||
|
||
public function count(Criteria|null $criteria = null): int | ||
{ | ||
return $this->store->count($criteria); | ||
} | ||
|
||
public function save(Message ...$messages): void | ||
{ | ||
foreach ($messages as $message) { | ||
$this->logger?->info('tried to save message in read only store', ['message' => $message]); | ||
} | ||
|
||
throw new StoreIsReadOnly(); | ||
} | ||
|
||
public function transactional(Closure $function): void | ||
{ | ||
$this->store->transactional($function); | ||
} | ||
|
||
/** @return list<string> */ | ||
public function streams(): array | ||
{ | ||
return $this->store->streams(); | ||
} | ||
|
||
public function remove(Criteria|null $criteria = null): void | ||
{ | ||
throw new StoreIsReadOnly(); | ||
} | ||
|
||
public function archive(Criteria|null $criteria = null): void | ||
{ | ||
throw new StoreIsReadOnly(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Tests\Unit\Store; | ||
|
||
use Patchlevel\EventSourcing\Message\Message; | ||
use Patchlevel\EventSourcing\Store\Criteria\Criteria; | ||
use Patchlevel\EventSourcing\Store\StoreIsReadOnly; | ||
use Patchlevel\EventSourcing\Store\StreamReadOnlyStore; | ||
use Patchlevel\EventSourcing\Store\StreamStore; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
|
||
/** @covers \Patchlevel\EventSourcing\Store\ReadOnlyStore */ | ||
final class StreamReadOnlyStoreTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
public function testLoad(): void | ||
{ | ||
$criteria = new Criteria(); | ||
|
||
$parentStore = $this->prophesize(StreamStore::class); | ||
$parentStore->load($criteria, 8, 42, true)->shouldBeCalled(); | ||
|
||
$store = new StreamReadOnlyStore($parentStore->reveal()); | ||
$store->load($criteria, 8, 42, true); | ||
} | ||
|
||
public function testCount(): void | ||
{ | ||
$criteria = new Criteria(); | ||
|
||
$parentStore = $this->prophesize(StreamStore::class); | ||
$parentStore->count($criteria)->shouldBeCalled(); | ||
|
||
$store = new StreamReadOnlyStore($parentStore->reveal()); | ||
$store->count($criteria); | ||
} | ||
|
||
public function testSave(): void | ||
{ | ||
$message = new Message(new class () { | ||
}); | ||
|
||
$parentStore = $this->prophesize(StreamStore::class); | ||
$parentStore->save($message)->shouldNotBeCalled(); | ||
|
||
$store = new StreamReadOnlyStore($parentStore->reveal()); | ||
$this->expectException(StoreIsReadOnly::class); | ||
$store->save($message); | ||
} | ||
|
||
public function testTransactional(): void | ||
{ | ||
$callback = static function (): void { | ||
}; | ||
|
||
$parentStore = $this->prophesize(StreamStore::class); | ||
$parentStore->transactional($callback)->shouldBeCalled(); | ||
|
||
$store = new StreamReadOnlyStore($parentStore->reveal()); | ||
$store->transactional($callback); | ||
} | ||
|
||
public function testStreams(): void | ||
{ | ||
$parentStore = $this->prophesize(StreamStore::class); | ||
$parentStore->streams()->willReturn(['foo', 'bar'])->shouldBeCalled(); | ||
|
||
$store = new StreamReadOnlyStore($parentStore->reveal()); | ||
|
||
self::assertEquals(['foo', 'bar'], $store->streams()); | ||
} | ||
|
||
public function testRemove(): void | ||
{ | ||
$criteria = new Criteria(); | ||
|
||
$parentStore = $this->prophesize(StreamStore::class); | ||
$parentStore->remove($criteria)->shouldNotBeCalled(); | ||
|
||
$store = new StreamReadOnlyStore($parentStore->reveal()); | ||
$this->expectException(StoreIsReadOnly::class); | ||
$store->remove($criteria); | ||
} | ||
|
||
public function testArchive(): void | ||
{ | ||
$criteria = new Criteria(); | ||
|
||
$parentStore = $this->prophesize(StreamStore::class); | ||
$parentStore->archive($criteria)->shouldNotBeCalled(); | ||
|
||
$store = new StreamReadOnlyStore($parentStore->reveal()); | ||
$this->expectException(StoreIsReadOnly::class); | ||
$store->archive($criteria); | ||
} | ||
} |