-
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 new exceptions for repository & store
- Loading branch information
1 parent
81b322a
commit 4fec8c5
Showing
12 changed files
with
216 additions
and
9 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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Repository; | ||
|
||
use Patchlevel\EventSourcing\Aggregate\AggregateRoot; | ||
use Patchlevel\EventSourcing\Aggregate\AggregateRootId; | ||
|
||
use function sprintf; | ||
|
||
final class AggregateAlreadyExists extends RepositoryException | ||
{ | ||
/** @param class-string<AggregateRoot> $aggregate */ | ||
public function __construct(string $aggregate, AggregateRootId $id) | ||
{ | ||
parent::__construct( | ||
sprintf( | ||
'aggregate %s with id %s already exists', | ||
$aggregate, | ||
$id->toString(), | ||
), | ||
); | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Repository; | ||
|
||
use Patchlevel\EventSourcing\Aggregate\AggregateRoot; | ||
use Patchlevel\EventSourcing\Aggregate\AggregateRootId; | ||
|
||
use function sprintf; | ||
|
||
final class AggregateOutdated extends RepositoryException | ||
{ | ||
/** @param class-string<AggregateRoot> $aggregate */ | ||
public function __construct(string $aggregate, AggregateRootId $id) | ||
{ | ||
parent::__construct( | ||
sprintf( | ||
'Aggregate %s with id %s is outdated. There are new events in the store. Please reload the aggregate.', | ||
$aggregate, | ||
$id->toString(), | ||
), | ||
); | ||
} | ||
} |
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
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
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,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Patchlevel\EventSourcing\Store; | ||
|
||
use Throwable; | ||
|
||
final class UniqueConstraintViolation extends StoreException | ||
{ | ||
public function __construct(Throwable|null $previous = null) | ||
{ | ||
parent::__construct('unique constraint violation', 0, $previous); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -9,8 +9,10 @@ | |
use Patchlevel\EventSourcing\EventBus\EventBus; | ||
use Patchlevel\EventSourcing\EventBus\Message; | ||
use Patchlevel\EventSourcing\Metadata\Event\AttributeEventMetadataFactory; | ||
use Patchlevel\EventSourcing\Repository\AggregateAlreadyExists; | ||
use Patchlevel\EventSourcing\Repository\AggregateDetached; | ||
use Patchlevel\EventSourcing\Repository\AggregateNotFound; | ||
use Patchlevel\EventSourcing\Repository\AggregateOutdated; | ||
use Patchlevel\EventSourcing\Repository\AggregateUnknown; | ||
use Patchlevel\EventSourcing\Repository\DefaultRepository; | ||
use Patchlevel\EventSourcing\Repository\WrongAggregate; | ||
|
@@ -20,6 +22,7 @@ | |
use Patchlevel\EventSourcing\Store\ArrayStream; | ||
use Patchlevel\EventSourcing\Store\Criteria; | ||
use Patchlevel\EventSourcing\Store\Store; | ||
use Patchlevel\EventSourcing\Store\UniqueConstraintViolation; | ||
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Email; | ||
use Patchlevel\EventSourcing\Tests\Unit\Fixture\Profile; | ||
use Patchlevel\EventSourcing\Tests\Unit\Fixture\ProfileCreated; | ||
|
@@ -404,6 +407,81 @@ public function testUnknownException(): void | |
$repository->save($aggregate); | ||
} | ||
|
||
public function testDuplicate(): void | ||
{ | ||
$this->expectException(AggregateAlreadyExists::class); | ||
|
||
$store = $this->prophesize(Store::class); | ||
$store->save( | ||
Argument::type(Message::class), | ||
)->willThrow(new UniqueConstraintViolation()); | ||
|
||
$store->transactional(Argument::any())->will( | ||
/** @param array{0: callable} $args */ | ||
static fn (array $args): mixed => $args[0]() | ||
); | ||
|
||
$eventBus = $this->prophesize(EventBus::class); | ||
$eventBus->dispatch(Argument::type('object'))->shouldNotBeCalled(); | ||
|
||
$repository = new DefaultRepository( | ||
$store->reveal(), | ||
$eventBus->reveal(), | ||
Profile::metadata(), | ||
); | ||
|
||
$aggregate = Profile::createProfile( | ||
ProfileId::fromString('1'), | ||
Email::fromString('[email protected]'), | ||
); | ||
|
||
$repository->save($aggregate); | ||
} | ||
|
||
public function testOutdated(): void | ||
{ | ||
$this->expectException(AggregateOutdated::class); | ||
|
||
$store = $this->prophesize(Store::class); | ||
|
||
$store->save( | ||
Argument::that(static function (Message $message) { | ||
return $message->playhead() === 1; | ||
}), | ||
)->shouldBeCalled(); | ||
|
||
$store->save( | ||
Argument::that(static function (Message $message) { | ||
return $message->playhead() === 2; | ||
}), | ||
)->willThrow(new UniqueConstraintViolation()); | ||
|
||
$store->transactional(Argument::any())->will( | ||
/** @param array{0: callable} $args */ | ||
static fn (array $args): mixed => $args[0]() | ||
); | ||
|
||
$eventBus = $this->prophesize(EventBus::class); | ||
$eventBus->dispatch(Argument::type('object'))->shouldBeCalled(); | ||
|
||
$repository = new DefaultRepository( | ||
$store->reveal(), | ||
$eventBus->reveal(), | ||
Profile::metadata(), | ||
); | ||
|
||
$aggregate = Profile::createProfile( | ||
ProfileId::fromString('1'), | ||
Email::fromString('[email protected]'), | ||
); | ||
|
||
$repository->save($aggregate); | ||
|
||
$aggregate->visitProfile(ProfileId::fromString('2')); | ||
|
||
$repository->save($aggregate); | ||
} | ||
|
||
public function testSaveAggregateWithSplitStream(): void | ||
{ | ||
$store = $this->prophesize(Store::class); | ||
|