From 4838498249fbb9d1582279304793c4674fd746a1 Mon Sep 17 00:00:00 2001 From: David Badura Date: Tue, 2 Jan 2024 18:17:00 +0100 Subject: [PATCH] clean-up aggregate id implementation --- docs/pages/aggregate.md | 48 +++++++++---------- docs/pages/events.md | 14 +++--- docs/pages/getting_started.md | 23 ++++----- docs/pages/normalizer.md | 41 +++------------- docs/pages/repository.md | 10 ++-- docs/pages/snapshots.md | 11 +++-- src/Aggregate/BasicAggregateRootId.php | 10 ---- src/Aggregate/CustomId.php | 10 ++++ ...eIdBehaviour.php => CustomIdBehaviour.php} | 2 +- ...dBehaviour.php => RamseyUuidBehaviour.php} | 9 +++- src/Aggregate/Uuid.php | 10 ++++ src/Aggregate/UuidAggregateRootId.php | 10 ---- .../AttributeAggregateRootMetadataFactory.php | 2 +- .../Normalizer/BasicAggregateIdNormalizer.php | 17 ------- ...egateIdNormalizer.php => IdNormalizer.php} | 4 +- .../Normalizer/UuidAggregateIdNormalizer.php | 17 ------- .../BasicImplementation/Aggregate/Profile.php | 6 +-- .../Events/ProfileCreated.php | 4 +- .../BasicImplementation/Events/Reborn.php | 4 +- .../Normalizer/ProfileIdNormalizer.php | 18 ------- .../BasicImplementation/ProfileId.php | 4 +- tests/Benchmark/SimpleSetupBench.php | 10 ++-- tests/Benchmark/SnapshotsBench.php | 2 +- tests/Benchmark/SplitStreamBench.php | 4 +- tests/Benchmark/SyncProjectionistBench.php | 2 +- tests/Benchmark/blackfire.php | 2 +- .../Aggregate/BankAccount.php | 2 +- .../BasicImplementation/Aggregate/Profile.php | 2 +- .../Integration/Outbox/Aggregate/Profile.php | 2 +- .../Pipeline/Aggregate/Profile.php | 2 +- .../Projectionist/Aggregate/Profile.php | 2 +- tests/Integration/Store/Profile.php | 2 +- tests/Unit/Fixture/Profile.php | 2 +- tests/Unit/Fixture/ProfileInvalid.php | 2 +- .../ProfileWithBrokenApplyBothUsage.php | 2 +- .../ProfileWithBrokenApplyIntersection.php | 2 +- .../ProfileWithBrokenApplyMultipleApply.php | 2 +- .../Fixture/ProfileWithBrokenApplyNoType.php | 2 +- tests/Unit/Fixture/ProfileWithEmptyApply.php | 2 +- tests/Unit/Fixture/ProfileWithSnapshot.php | 2 +- ...ormalizerTest.php => IdNormalizerTest.php} | 26 +++++----- 41 files changed, 138 insertions(+), 210 deletions(-) delete mode 100644 src/Aggregate/BasicAggregateRootId.php create mode 100644 src/Aggregate/CustomId.php rename src/Aggregate/{ValueAggregateIdBehaviour.php => CustomIdBehaviour.php} (91%) rename src/Aggregate/{RamseyAggregateIdBehaviour.php => RamseyUuidBehaviour.php} (75%) create mode 100644 src/Aggregate/Uuid.php delete mode 100644 src/Aggregate/UuidAggregateRootId.php delete mode 100644 src/Serializer/Normalizer/BasicAggregateIdNormalizer.php rename src/Serializer/Normalizer/{AggregateIdNormalizer.php => IdNormalizer.php} (87%) delete mode 100644 src/Serializer/Normalizer/UuidAggregateIdNormalizer.php delete mode 100644 tests/Benchmark/BasicImplementation/Normalizer/ProfileIdNormalizer.php rename tests/Unit/Serializer/Normalizer/{AggregateIdNormalizerTest.php => IdNormalizerTest.php} (54%) diff --git a/docs/pages/aggregate.md b/docs/pages/aggregate.md index 7b899862f..cc4ae5ff9 100644 --- a/docs/pages/aggregate.md +++ b/docs/pages/aggregate.md @@ -19,12 +19,12 @@ An aggregate must fulfill a few points so that we can use it in event-sourcing: We can implement this ourselves, or use the `BasicAggregateRoot` implementation that already brings everything with it. This basic implementation uses attributes to configure the aggregate and to specify how it should handle events. -We are building a minimal aggregate class here which only has an ID and mark this with the `AggregateId` attribute. +We are building a minimal aggregate class here which only has an ID and mark this with the `Id` attribute. To make it easy to register with a name, we also add the `Aggregate` attribute. This is what it looks like: ```php use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; -use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId; +use Patchlevel\EventSourcing\Aggregate\Uuid; use Patchlevel\EventSourcing\Attribute\Aggregate; use Patchlevel\EventSourcing\Attribute\Id; @@ -32,9 +32,9 @@ use Patchlevel\EventSourcing\Attribute\Id; final class Profile extends BasicAggregateRoot { #[Id] - private UuidAggregateRootId $id; + private Uuid $id; - public static function register(UuidAggregateRootId $id): self + public static function register(Uuid $id): self { $self = new self(); @@ -93,20 +93,20 @@ final class CreateProfileHandler In order that an aggregate is actually saved, at least one event must exist in the DB. For our aggregate we create the Event `ProfileRegistered` with an ID and a name. Since the ID is a complex data type and cannot be easily serialized, we need to define a normalizer for the ID. -We do this with the `UuidAggregateIdNormalizer` attribute. +We do this with the `IdNormalizer` attribute. We also give the event a unique name using the `Event` attribute. ```php -use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId; +use Patchlevel\EventSourcing\Aggregate\Uuid; use Patchlevel\EventSourcing\Attribute\Event; -use Patchlevel\EventSourcing\Serializer\Normalizer\UuidAggregateIdNormalizer; +use Patchlevel\EventSourcing\Serializer\Normalizer\IdNormalizer; #[Event('profile.registered')] final class ProfileRegistered { public function __construct( - #[UuidAggregateIdNormalizer] - public readonly UuidAggregateRootId $profileId, + #[IdNormalizer(Uuid::class)] + public readonly Uuid $profileId, public readonly string $name ) {} } @@ -120,7 +120,7 @@ After we have defined the event, we have to adapt the profile aggregate: ```php use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; -use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId; +use Patchlevel\EventSourcing\Aggregate\Uuid; use Patchlevel\EventSourcing\Attribute\Aggregate; use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; @@ -129,7 +129,7 @@ use Patchlevel\EventSourcing\Attribute\Apply; final class Profile extends BasicAggregateRoot { #[Id] - private UuidAggregateRootId $id; + private Uuid $id; private string $name; public function name(): string @@ -137,7 +137,7 @@ final class Profile extends BasicAggregateRoot return $this->name; } - public static function register(UuidAggregateRootId $id, string $name): self + public static function register(Uuid $id, string $name): self { $self = new self(); $self->recordThat(new ProfileRegistered($id, $name)); @@ -197,7 +197,7 @@ This method then creates the event `NameChanged` and records it: ```php use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; -use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId; +use Patchlevel\EventSourcing\Aggregate\Uuid; use Patchlevel\EventSourcing\Attribute\Aggregate; use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; @@ -206,7 +206,7 @@ use Patchlevel\EventSourcing\Attribute\Apply; final class Profile extends BasicAggregateRoot { #[Id] - private UuidAggregateRootId $id; + private Uuid $id; private string $name; public function name(): string @@ -214,7 +214,7 @@ final class Profile extends BasicAggregateRoot return $this->name; } - public static function register(UuidAggregateRootId $id, string $name): static + public static function register(Uuid $id, string $name): static { $self = new static(); $self->recordThat(new ProfileRegistered($id, $name)); @@ -444,7 +444,7 @@ We can now use the value object `Name` in our aggregate: ```php use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; -use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId; +use Patchlevel\EventSourcing\Aggregate\Uuid; use Patchlevel\EventSourcing\Attribute\Aggregate; use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; @@ -453,10 +453,10 @@ use Patchlevel\EventSourcing\Attribute\Apply; final class Profile extends BasicAggregateRoot { #[Id] - private UuidAggregateRootId $id; + private Uuid $id; private Name $name; - public static function register(UuidAggregateRootId $id, Name $name): static + public static function register(Uuid $id, Name $name): static { $self = new static(); $self->recordThat(new ProfileRegistered($id, $name)); @@ -563,7 +563,7 @@ But you can pass this information by yourself. ```php use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; -use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId; +use Patchlevel\EventSourcing\Aggregate\Uuid; use Patchlevel\EventSourcing\Attribute\Aggregate; use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; @@ -572,11 +572,11 @@ use Patchlevel\EventSourcing\Attribute\Apply; final class Profile extends BasicAggregateRoot { #[Id] - private UuidAggregateRootId $id; + private Uuid $id; private Name $name; private DateTimeImmutable $registeredAt; - public static function register(UuidAggregateRootId $id, string $name, DateTimeImmutable $registeredAt): static + public static function register(Uuid $id, string $name, DateTimeImmutable $registeredAt): static { $self = new static(); $self->recordThat(new ProfileRegistered($id, $name, $registeredAt)); @@ -592,7 +592,7 @@ But if you still want to make sure that the time is "now" and not in the past or ```php use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; -use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId; +use Patchlevel\EventSourcing\Aggregate\Uuid; use Patchlevel\EventSourcing\Attribute\Aggregate; use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; @@ -602,11 +602,11 @@ use Patchlevel\EventSourcing\Clock\Clock; final class Profile extends BasicAggregateRoot { #[Id] - private UuidAggregateRootId $id; + private Uuid $id; private Name $name; private DateTimeImmutable $registeredAt; - public static function register(UuidAggregateRootId $id, string $name, Clock $clock): static + public static function register(Uuid $id, string $name, Clock $clock): static { $self = new static(); $self->recordThat(new ProfileRegistered($id, $name, $clock->now())); diff --git a/docs/pages/events.md b/docs/pages/events.md index fc611d24e..5a3f8690e 100644 --- a/docs/pages/events.md +++ b/docs/pages/events.md @@ -67,16 +67,18 @@ so that the library knows how to write this data to the database and load it aga ```php use Patchlevel\EventSourcing\Attribute\Event; use Patchlevel\Hydrator\Normalizer\DateTimeImmutableNormalizer; -use Patchlevel\Hydrator\Normalizer\UuidAggregateIdNormalizer; +use Patchlevel\Hydrator\Normalizer\IdNormalizer; -#[Event('profile.name_changed')] -final class NameChanged +#[Event('profile.created')] +final class ProfileCreated { public function __construct( - #[UuidAggregateIdNormalizer] - public readonly UuidAggregateRootId $name, + #[IdNormalizer(Uuid::class)] + public readonly Uuid $id, + #[NameNormalizer] + public readonly Name $name, #[DateTimeImmutableNormalizer] - public readonly DateTimeImmutable $changedAt + public readonly DateTimeImmutable $createdAt ) {} } ``` diff --git a/docs/pages/getting_started.md b/docs/pages/getting_started.md index f72311d15..07ed91b37 100644 --- a/docs/pages/getting_started.md +++ b/docs/pages/getting_started.md @@ -10,15 +10,15 @@ First we define the events that happen in our system. A hotel can be created with a `name` and a `id`: ```php -use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId; -use Patchlevel\EventSourcing\Serializer\Normalizer\UuidAggregateIdNormalizer; +use Patchlevel\EventSourcing\Aggregate\Uuid; +use Patchlevel\EventSourcing\Serializer\Normalizer\IdNormalizer; #[Event('hotel.created')] final class HotelCreated { public function __construct( - #[UuidAggregateIdNormalizer] - public readonly UuidAggregateRootId $hotelId, + #[IdNormalizer(Uuid::class)] + public readonly Uuid $hotelId, public readonly string $hotelName ) { } @@ -64,7 +64,7 @@ These events are thrown here and the state of the hotel is also changed. ```php use Patchlevel\EventSourcing\Aggregate\AggregateChanged; use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; -use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId; +use Patchlevel\EventSourcing\Aggregate\Uuid; use Patchlevel\EventSourcing\Attribute\Aggregate; use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; @@ -73,7 +73,7 @@ use Patchlevel\EventSourcing\Attribute\Apply; final class Hotel extends BasicAggregateRoot { #[Id] - private UuidAggregateRootId $id; + private Uuid $id; private string $name; /** @@ -91,7 +91,7 @@ final class Hotel extends BasicAggregateRoot return $this->guests; } - public static function create(UuidAggregateRootId $id, string $hotelName): static + public static function create(Uuid $id, string $hotelName): static { $self = new static(); $self->recordThat(new HotelCreated($id, $hotelName)); @@ -366,16 +366,16 @@ $projectionist->boot(); We are now ready to use the Event Sourcing System. We can load, change and save aggregates. ```php -use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId; +use Patchlevel\EventSourcing\Aggregate\Uuid; -$hotel1 = Hotel::create(UuidAggregateRootId::generate(), 'HOTEL'); +$hotel1 = Hotel::create(Uuid::v7(), 'HOTEL'); $hotel1->checkIn('David'); $hotel1->checkIn('Daniel'); $hotel1->checkOut('David'); $hotelRepository->save($hotel1); -$hotel2 = $hotelRepository->load(UuidAggregateRootId::fromString('d0d0d0d0-d0d0-d0d0-d0d0-d0d0d0d0d0d0')); +$hotel2 = $hotelRepository->load(Uuid::fromString('d0d0d0d0-d0d0-d0d0-d0d0-d0d0d0d0d0d0')); $hotel2->checkIn('David'); $hotelRepository->save($hotel2); @@ -384,7 +384,8 @@ $hotels = $hotelProjection->getHotels(); !!! note - An aggregateId can be an **uuid**, you can find more about this [here](uuid.md). + You can also use other forms of IDs such as uuid version 6 or a custom format. + You can find more about this [here](aggregate_id.md). ## Result diff --git a/docs/pages/normalizer.md b/docs/pages/normalizer.md index b67abfc64..2cd8b5e0c 100644 --- a/docs/pages/normalizer.md +++ b/docs/pages/normalizer.md @@ -200,45 +200,18 @@ final class DTO { } ``` -### UuidAggregateId +### Id -To normalize a `UuidAggregateRootId` one can use the `UuidAggregateIdNormalizer`. +If you have your own AggregateRootId, you can use the `IdNormalizer`. +the `IdNormalizer` needs the FQCN of the AggregateRootId as a parameter. ```php -use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId; -use Patchlevel\Hydrator\Normalizer\UuidAggregateIdNormalizer; +use Patchlevel\EventSourcing\Aggregate\Uuid; +use Patchlevel\Hydrator\Normalizer\IdNormalizer; final class DTO { - #[UuidAggregateIdNormalizer] - public UuidAggregateRootId $id; -} -``` - -### BasicAggregateIdNormalizer - -To normalize a `ValueAggregateRootId` one can use the `BasicAggregateIdNormalizer`. - -```php -use Patchlevel\EventSourcing\Aggregate\ValueAggregateRootId; -use Patchlevel\Hydrator\Normalizer\BasicAggregateIdNormalizer; - -final class DTO { - #[BasicAggregateIdNormalizer] - public ValueAggregateRootId $id; -} -``` - -### Own AggregateId - -If you have your own AggregateId, you can use the `AggregateIdNormalizer` base normalizer. -the `AggregateIdNormalizer` needs the FQCN of the AggregateId as a parameter. - -```php -use Patchlevel\Hydrator\Normalizer\AggregateIdNormalizer; - -final class DTO { - #[AggregateIdNormalizer(Id::class)] - public Id $id; + #[IdNormalizer(Uuid::class)] + public Uuid $id; } ``` diff --git a/docs/pages/repository.md b/docs/pages/repository.md index 00dbfee28..dd3adf485 100644 --- a/docs/pages/repository.md +++ b/docs/pages/repository.md @@ -103,9 +103,9 @@ After the events have been written, the new events are dispatched on the [event bus](./event_bus.md). ```php -use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId; +use Patchlevel\EventSourcing\Aggregate\Uuid; -$id = UuidAggregateRootId::generate(); +$id = Uuid::v7(); $profile = Profile::create($id, 'david.badura@patchlevel.de'); $repository->save($profile); @@ -126,9 +126,9 @@ An `aggregate` can be loaded using the `load` method. All events for the aggregate are loaded from the database and the current state is rebuilt. ```php -use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId; +use Patchlevel\EventSourcing\Aggregate\Uuid; -$id = UuidAggregateRootId::fromString('229286ff-6f95-4df6-bc72-0a239fe7b284'); +$id = Uuid::fromString('229286ff-6f95-4df6-bc72-0a239fe7b284'); $profile = $repository->load($id); ``` @@ -147,7 +147,7 @@ You can also check whether an `aggregate` with a certain id exists. It is checked whether any event with this id exists in the database. ```php -$id = UuidAggregateRootId::fromString('229286ff-6f95-4df6-bc72-0a239fe7b284'); +$id = Uuid::fromString('229286ff-6f95-4df6-bc72-0a239fe7b284'); if($repository->has($id)) { // ... diff --git a/docs/pages/snapshots.md b/docs/pages/snapshots.md index e2958c77c..fc33c026e 100644 --- a/docs/pages/snapshots.md +++ b/docs/pages/snapshots.md @@ -72,18 +72,19 @@ You can define normalizers to bring the properties into the correct format. ```php use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; -use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId; +use Patchlevel\EventSourcing\Aggregate\Uuid; use Patchlevel\EventSourcing\Attribute\Aggregate; use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Snapshot; +use Patchlevel\EventSourcing\Serializer\Normalizer\IdNormalizer; #[Aggregate('profile')] #[Snapshot('default')] final class Profile extends BasicAggregateRoot { #[Id] - #[UuidAggregateRootIdNormalizer] - public UuidAggregateRootId $id; + #[IdNormalizer] + public Uuid $id; public string $name, #[Normalize(new DateTimeImmutableNormalizer())] public DateTimeImmutable $createdAt; @@ -221,9 +222,9 @@ $snapshotStore->save($aggregate); You can also load an aggregate from the snapshot store: ```php -use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId; +use Patchlevel\EventSourcing\Aggregate\Uuid; -$id = UuidAggregateRootId::fromString('229286ff-6f95-4df6-bc72-0a239fe7b284'); +$id = Uuid::fromString('229286ff-6f95-4df6-bc72-0a239fe7b284'); $aggregate = $snapshotStore->load(Profile::class, $id); ``` diff --git a/src/Aggregate/BasicAggregateRootId.php b/src/Aggregate/BasicAggregateRootId.php deleted file mode 100644 index d68a33ac0..000000000 --- a/src/Aggregate/BasicAggregateRootId.php +++ /dev/null @@ -1,10 +0,0 @@ -id->toString(); } - public static function generate(): self + public static function v6(): self + { + return new self(Uuid::uuid6()); + } + + public static function v7(): self { return new self(Uuid::uuid7()); } diff --git a/src/Aggregate/Uuid.php b/src/Aggregate/Uuid.php new file mode 100644 index 000000000..41144d4ac --- /dev/null +++ b/src/Aggregate/Uuid.php @@ -0,0 +1,10 @@ + */ diff --git a/src/Serializer/Normalizer/UuidAggregateIdNormalizer.php b/src/Serializer/Normalizer/UuidAggregateIdNormalizer.php deleted file mode 100644 index 7dd0402cf..000000000 --- a/src/Serializer/Normalizer/UuidAggregateIdNormalizer.php +++ /dev/null @@ -1,17 +0,0 @@ -create(); - $this->id = ProfileId::generate(); + $this->id = ProfileId::v7(); $profile = Profile::create($this->id, 'Peter'); @@ -83,14 +83,14 @@ public function benchLoad10000Events(): void #[Bench\Revs(20)] public function benchSave1Event(): void { - $profile = Profile::create(ProfileId::generate(), 'Peter'); + $profile = Profile::create(ProfileId::v7(), 'Peter'); $this->repository->save($profile); } #[Bench\Revs(20)] public function benchSave10000Events(): void { - $profile = Profile::create(ProfileId::generate(), 'Peter'); + $profile = Profile::create(ProfileId::v7(), 'Peter'); for ($i = 1; $i < 10_000; $i++) { $profile->changeName('Peter'); @@ -103,7 +103,7 @@ public function benchSave10000Events(): void public function benchSave10000Aggregates(): void { for ($i = 1; $i < 10_000; $i++) { - $profile = Profile::create(ProfileId::generate(), 'Peter'); + $profile = Profile::create(ProfileId::v7(), 'Peter'); $this->repository->save($profile); } } @@ -113,7 +113,7 @@ public function benchSave10000AggregatesTransaction(): void { $this->store->transactional(function (): void { for ($i = 1; $i < 10_000; $i++) { - $profile = Profile::create(ProfileId::generate(), 'Peter'); + $profile = Profile::create(ProfileId::v7(), 'Peter'); $this->repository->save($profile); } }); diff --git a/tests/Benchmark/SnapshotsBench.php b/tests/Benchmark/SnapshotsBench.php index f4dbd5e13..411989cec 100644 --- a/tests/Benchmark/SnapshotsBench.php +++ b/tests/Benchmark/SnapshotsBench.php @@ -73,7 +73,7 @@ public function setUp(): void $schemaDirector->create(); - $this->id = ProfileId::generate(); + $this->id = ProfileId::v7(); $profile = Profile::create($this->id, 'Peter'); for ($i = 0; $i < 10_000; $i++) { diff --git a/tests/Benchmark/SplitStreamBench.php b/tests/Benchmark/SplitStreamBench.php index 46aa0c0f3..db3413f68 100644 --- a/tests/Benchmark/SplitStreamBench.php +++ b/tests/Benchmark/SplitStreamBench.php @@ -76,7 +76,7 @@ public function setUp(): void $schemaDirector->create(); - $this->id = ProfileId::generate(); + $this->id = ProfileId::v7(); } public function provideData(): void @@ -106,7 +106,7 @@ public function benchLoad10000Events(): void #[Bench\Revs(20)] public function benchSave10000Events(): void { - $profile = Profile::create(ProfileId::generate(), 'Peter'); + $profile = Profile::create(ProfileId::v7(), 'Peter'); for ($i = 0; $i < 10_000; $i++) { $profile->changeName(sprintf('Peter %d', $i)); diff --git a/tests/Benchmark/SyncProjectionistBench.php b/tests/Benchmark/SyncProjectionistBench.php index 9aef5d853..667f36b24 100644 --- a/tests/Benchmark/SyncProjectionistBench.php +++ b/tests/Benchmark/SyncProjectionistBench.php @@ -102,7 +102,7 @@ public function setUp(): void $schemaDirector->create(); $projectionist->boot(); - $this->id = ProfileId::generate(); + $this->id = ProfileId::v7(); $this->profile = Profile::create($this->id, 'Peter'); $this->repository->save($this->profile); } diff --git a/tests/Benchmark/blackfire.php b/tests/Benchmark/blackfire.php index a2181fe23..fea73a725 100644 --- a/tests/Benchmark/blackfire.php +++ b/tests/Benchmark/blackfire.php @@ -46,7 +46,7 @@ $store->transactional(static function () use ($repository): void { for ($i = 0; $i < 10_000; $i++) { - $id = ProfileId::generate(); + $id = ProfileId::v7(); $profile = Profile::create($id, 'Peter'); for ($j = 0; $j < 10; $j++) { diff --git a/tests/Integration/BankAccountSplitStream/Aggregate/BankAccount.php b/tests/Integration/BankAccountSplitStream/Aggregate/BankAccount.php index 914d6b746..61f4dd5b1 100644 --- a/tests/Integration/BankAccountSplitStream/Aggregate/BankAccount.php +++ b/tests/Integration/BankAccountSplitStream/Aggregate/BankAccount.php @@ -6,8 +6,8 @@ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; use Patchlevel\EventSourcing\Attribute\Aggregate; -use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; +use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\AccountId; use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Events\BalanceAdded; use Patchlevel\EventSourcing\Tests\Integration\BankAccountSplitStream\Events\BankAccountCreated; diff --git a/tests/Integration/BasicImplementation/Aggregate/Profile.php b/tests/Integration/BasicImplementation/Aggregate/Profile.php index 0b36dd2f9..34b90b38c 100644 --- a/tests/Integration/BasicImplementation/Aggregate/Profile.php +++ b/tests/Integration/BasicImplementation/Aggregate/Profile.php @@ -6,8 +6,8 @@ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; use Patchlevel\EventSourcing\Attribute\Aggregate; -use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; +use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Snapshot; use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Events\ProfileCreated; use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Normalizer\ProfileIdNormalizer; diff --git a/tests/Integration/Outbox/Aggregate/Profile.php b/tests/Integration/Outbox/Aggregate/Profile.php index 1af677b51..8ed0ab53c 100644 --- a/tests/Integration/Outbox/Aggregate/Profile.php +++ b/tests/Integration/Outbox/Aggregate/Profile.php @@ -6,8 +6,8 @@ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; use Patchlevel\EventSourcing\Attribute\Aggregate; -use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; +use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Snapshot; use Patchlevel\EventSourcing\Tests\Integration\Outbox\Events\ProfileCreated; use Patchlevel\EventSourcing\Tests\Integration\Outbox\Normalizer\ProfileIdNormalizer; diff --git a/tests/Integration/Pipeline/Aggregate/Profile.php b/tests/Integration/Pipeline/Aggregate/Profile.php index 27697d4d1..7bade7fb2 100644 --- a/tests/Integration/Pipeline/Aggregate/Profile.php +++ b/tests/Integration/Pipeline/Aggregate/Profile.php @@ -6,8 +6,8 @@ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; use Patchlevel\EventSourcing\Attribute\Aggregate; -use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; +use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Tests\Integration\Pipeline\Events\NewVisited; use Patchlevel\EventSourcing\Tests\Integration\Pipeline\Events\OldVisited; use Patchlevel\EventSourcing\Tests\Integration\Pipeline\Events\PrivacyAdded; diff --git a/tests/Integration/Projectionist/Aggregate/Profile.php b/tests/Integration/Projectionist/Aggregate/Profile.php index 975087719..95cc98b20 100644 --- a/tests/Integration/Projectionist/Aggregate/Profile.php +++ b/tests/Integration/Projectionist/Aggregate/Profile.php @@ -6,8 +6,8 @@ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; use Patchlevel\EventSourcing\Attribute\Aggregate; -use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; +use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Tests\Integration\Projectionist\Events\ProfileCreated; use Patchlevel\EventSourcing\Tests\Integration\Projectionist\Normalizer\ProfileIdNormalizer; use Patchlevel\EventSourcing\Tests\Integration\Projectionist\ProfileId; diff --git a/tests/Integration/Store/Profile.php b/tests/Integration/Store/Profile.php index bf81a15d2..f78412d06 100644 --- a/tests/Integration/Store/Profile.php +++ b/tests/Integration/Store/Profile.php @@ -6,8 +6,8 @@ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; use Patchlevel\EventSourcing\Attribute\Aggregate; -use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; +use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Tests\Integration\Projectionist\Events\ProfileCreated; use Patchlevel\EventSourcing\Tests\Integration\Projectionist\Normalizer\ProfileIdNormalizer; use Patchlevel\EventSourcing\Tests\Integration\Projectionist\ProfileId; diff --git a/tests/Unit/Fixture/Profile.php b/tests/Unit/Fixture/Profile.php index 17303f378..3e1257b1d 100644 --- a/tests/Unit/Fixture/Profile.php +++ b/tests/Unit/Fixture/Profile.php @@ -6,8 +6,8 @@ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; use Patchlevel\EventSourcing\Attribute\Aggregate; -use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; +use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\SuppressMissingApply; #[Aggregate('profile')] diff --git a/tests/Unit/Fixture/ProfileInvalid.php b/tests/Unit/Fixture/ProfileInvalid.php index 938e12b73..7f31579c3 100644 --- a/tests/Unit/Fixture/ProfileInvalid.php +++ b/tests/Unit/Fixture/ProfileInvalid.php @@ -6,8 +6,8 @@ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; use Patchlevel\EventSourcing\Attribute\Aggregate; -use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; +use Patchlevel\EventSourcing\Attribute\Id; #[Aggregate(ProfileInvalid::class)] final class ProfileInvalid extends BasicAggregateRoot diff --git a/tests/Unit/Fixture/ProfileWithBrokenApplyBothUsage.php b/tests/Unit/Fixture/ProfileWithBrokenApplyBothUsage.php index 3ea79a9f4..5e4c26919 100644 --- a/tests/Unit/Fixture/ProfileWithBrokenApplyBothUsage.php +++ b/tests/Unit/Fixture/ProfileWithBrokenApplyBothUsage.php @@ -6,8 +6,8 @@ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; use Patchlevel\EventSourcing\Attribute\Aggregate; -use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; +use Patchlevel\EventSourcing\Attribute\Id; #[Aggregate(ProfileWithBrokenApplyBothUsage::class)] final class ProfileWithBrokenApplyBothUsage extends BasicAggregateRoot diff --git a/tests/Unit/Fixture/ProfileWithBrokenApplyIntersection.php b/tests/Unit/Fixture/ProfileWithBrokenApplyIntersection.php index 505652164..e09b1433e 100644 --- a/tests/Unit/Fixture/ProfileWithBrokenApplyIntersection.php +++ b/tests/Unit/Fixture/ProfileWithBrokenApplyIntersection.php @@ -6,8 +6,8 @@ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; use Patchlevel\EventSourcing\Attribute\Aggregate; -use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; +use Patchlevel\EventSourcing\Attribute\Id; #[Aggregate(ProfileWithBrokenApplyIntersection::class)] final class ProfileWithBrokenApplyIntersection extends BasicAggregateRoot diff --git a/tests/Unit/Fixture/ProfileWithBrokenApplyMultipleApply.php b/tests/Unit/Fixture/ProfileWithBrokenApplyMultipleApply.php index 01eeaa40c..cd9e66ee9 100644 --- a/tests/Unit/Fixture/ProfileWithBrokenApplyMultipleApply.php +++ b/tests/Unit/Fixture/ProfileWithBrokenApplyMultipleApply.php @@ -6,8 +6,8 @@ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; use Patchlevel\EventSourcing\Attribute\Aggregate; -use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; +use Patchlevel\EventSourcing\Attribute\Id; #[Aggregate(ProfileWithBrokenApplyMultipleApply::class)] final class ProfileWithBrokenApplyMultipleApply extends BasicAggregateRoot diff --git a/tests/Unit/Fixture/ProfileWithBrokenApplyNoType.php b/tests/Unit/Fixture/ProfileWithBrokenApplyNoType.php index 8a914634f..0352a72ff 100644 --- a/tests/Unit/Fixture/ProfileWithBrokenApplyNoType.php +++ b/tests/Unit/Fixture/ProfileWithBrokenApplyNoType.php @@ -6,8 +6,8 @@ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; use Patchlevel\EventSourcing\Attribute\Aggregate; -use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; +use Patchlevel\EventSourcing\Attribute\Id; #[Aggregate(ProfileWithBrokenApplyNoType::class)] final class ProfileWithBrokenApplyNoType extends BasicAggregateRoot diff --git a/tests/Unit/Fixture/ProfileWithEmptyApply.php b/tests/Unit/Fixture/ProfileWithEmptyApply.php index 43c11a281..06b343108 100644 --- a/tests/Unit/Fixture/ProfileWithEmptyApply.php +++ b/tests/Unit/Fixture/ProfileWithEmptyApply.php @@ -6,8 +6,8 @@ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; use Patchlevel\EventSourcing\Attribute\Aggregate; -use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; +use Patchlevel\EventSourcing\Attribute\Id; #[Aggregate(ProfileWithEmptyApply::class)] final class ProfileWithEmptyApply extends BasicAggregateRoot diff --git a/tests/Unit/Fixture/ProfileWithSnapshot.php b/tests/Unit/Fixture/ProfileWithSnapshot.php index e2e328013..3c0f65e5f 100644 --- a/tests/Unit/Fixture/ProfileWithSnapshot.php +++ b/tests/Unit/Fixture/ProfileWithSnapshot.php @@ -6,8 +6,8 @@ use Patchlevel\EventSourcing\Aggregate\BasicAggregateRoot; use Patchlevel\EventSourcing\Attribute\Aggregate; -use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Apply; +use Patchlevel\EventSourcing\Attribute\Id; use Patchlevel\EventSourcing\Attribute\Snapshot; use Patchlevel\EventSourcing\Attribute\SuppressMissingApply; use Patchlevel\Hydrator\Normalizer\ArrayNormalizer; diff --git a/tests/Unit/Serializer/Normalizer/AggregateIdNormalizerTest.php b/tests/Unit/Serializer/Normalizer/IdNormalizerTest.php similarity index 54% rename from tests/Unit/Serializer/Normalizer/AggregateIdNormalizerTest.php rename to tests/Unit/Serializer/Normalizer/IdNormalizerTest.php index 40d2165df..8a24d8f07 100644 --- a/tests/Unit/Serializer/Normalizer/AggregateIdNormalizerTest.php +++ b/tests/Unit/Serializer/Normalizer/IdNormalizerTest.php @@ -5,37 +5,37 @@ namespace Patchlevel\EventSourcing\Tests\Unit\Serializer\Normalizer; use Attribute; -use Patchlevel\EventSourcing\Aggregate\BasicAggregateRootId; -use Patchlevel\EventSourcing\Aggregate\UuidAggregateRootId; -use Patchlevel\EventSourcing\Serializer\Normalizer\AggregateIdNormalizer; +use Patchlevel\EventSourcing\Aggregate\CustomId; +use Patchlevel\EventSourcing\Aggregate\Uuid; +use Patchlevel\EventSourcing\Serializer\Normalizer\IdNormalizer; use Patchlevel\Hydrator\Normalizer\InvalidArgument; use PHPUnit\Framework\TestCase; use Prophecy\PhpUnit\ProphecyTrait; use Ramsey\Uuid\Exception\InvalidUuidStringException; #[Attribute(Attribute::TARGET_PROPERTY)] -final class AggregateIdNormalizerTest extends TestCase +final class IdNormalizerTest extends TestCase { use ProphecyTrait; public function testNormalizeWithNull(): void { - $normalizer = new AggregateIdNormalizer(BasicAggregateRootId::class); + $normalizer = new IdNormalizer(CustomId::class); $this->assertEquals(null, $normalizer->normalize(null)); } public function testDenormalizeWithNull(): void { - $normalizer = new AggregateIdNormalizer(BasicAggregateRootId::class); + $normalizer = new IdNormalizer(CustomId::class); $this->assertEquals(null, $normalizer->denormalize(null)); } public function testNormalizeWithInvalidArgument(): void { $this->expectException(InvalidArgument::class); - $this->expectExceptionMessage('type "Patchlevel\EventSourcing\Aggregate\BasicAggregateRootId" was expected but "string" was passed.'); + $this->expectExceptionMessage('type "Patchlevel\EventSourcing\Aggregate\CustomId" was expected but "string" was passed.'); - $normalizer = new AggregateIdNormalizer(BasicAggregateRootId::class); + $normalizer = new IdNormalizer(CustomId::class); $normalizer->normalize('foo'); } @@ -43,19 +43,19 @@ public function testDenormalizeWithInvalidArgument(): void { $this->expectException(InvalidUuidStringException::class); - $normalizer = new AggregateIdNormalizer(UuidAggregateRootId::class); + $normalizer = new IdNormalizer(Uuid::class); $normalizer->denormalize('foo'); } public function testNormalizeWithValue(): void { - $normalizer = new AggregateIdNormalizer(BasicAggregateRootId::class); - $this->assertEquals('foo', $normalizer->normalize(new BasicAggregateRootId('foo'))); + $normalizer = new IdNormalizer(CustomId::class); + $this->assertEquals('foo', $normalizer->normalize(new CustomId('foo'))); } public function testDenormalizeWithValue(): void { - $normalizer = new AggregateIdNormalizer(BasicAggregateRootId::class); - $this->assertEquals(new BasicAggregateRootId('foo'), $normalizer->denormalize('foo')); + $normalizer = new IdNormalizer(CustomId::class); + $this->assertEquals(new CustomId('foo'), $normalizer->denormalize('foo')); } }