-
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.
add adapter repository for snapshot store
- Loading branch information
1 parent
9c96e5a
commit 3c6a340
Showing
4 changed files
with
85 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Snapshot; | ||
|
||
use Patchlevel\EventSourcing\Snapshot\Adapter\SnapshotAdapter; | ||
|
||
interface AdapterRepository | ||
{ | ||
public function get(string $name): SnapshotAdapter; | ||
} |
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,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Snapshot; | ||
|
||
use Patchlevel\EventSourcing\Snapshot\Adapter\SnapshotAdapter; | ||
|
||
use function array_key_exists; | ||
|
||
final class ArrayAdapterRepository implements AdapterRepository | ||
{ | ||
/** @param array<string, SnapshotAdapter> $snapshotAdapters */ | ||
public function __construct( | ||
private readonly array $snapshotAdapters, | ||
) { | ||
} | ||
|
||
public function get(string $name): SnapshotAdapter | ||
{ | ||
if (!array_key_exists($name, $this->snapshotAdapters)) { | ||
throw new AdapterNotFound($name); | ||
} | ||
|
||
return $this->snapshotAdapters[$name]; | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Tests\Unit\Snapshot; | ||
|
||
use Patchlevel\EventSourcing\Snapshot\Adapter\SnapshotAdapter; | ||
use Patchlevel\EventSourcing\Snapshot\AdapterNotFound; | ||
use Patchlevel\EventSourcing\Snapshot\ArrayAdapterRepository; | ||
use PHPUnit\Framework\TestCase; | ||
use Prophecy\PhpUnit\ProphecyTrait; | ||
|
||
/** @covers \Patchlevel\EventSourcing\Snapshot\ArrayAdapterRepository */ | ||
final class ArrayAdapterRepositoryTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
public function testGetAdapter(): void | ||
{ | ||
$adapter = $this->prophesize(SnapshotAdapter::class); | ||
$repository = new ArrayAdapterRepository(['memory' => $adapter->reveal()]); | ||
|
||
self::assertSame($adapter->reveal(), $repository->get('memory')); | ||
} | ||
|
||
public function testAdapterNotFound(): void | ||
{ | ||
$this->expectException(AdapterNotFound::class); | ||
|
||
$repository = new ArrayAdapterRepository([]); | ||
$repository->get('memory'); | ||
} | ||
} |