From 6b4c1e07be450481ecfc63f91f4c39379c460e89 Mon Sep 17 00:00:00 2001 From: David Badura Date: Thu, 2 Dec 2021 12:15:12 +0100 Subject: [PATCH 1/2] rename DefaultApplyMethod to NonStrictApplyMethod & cleanup tests --- ...plyMethod.php => NonStrictApplyMethod.php} | 2 +- src/Console/InvalidArgumentException.php | 1 + .../BasicImplementation/Aggregate/Profile.php | 4 +- .../Pipeline/Aggregate/Profile.php | 4 +- .../AggregateRootWithNonStrictApplyTest.php | 64 +++++++++++++++++++ .../Console/InvalidArgumentExceptionTest.php | 20 ++++++ tests/Unit/Fixture/Profile.php | 29 +++++---- .../Fixture/ProfileWithNonStrictApply.php | 58 +++++++++++++++++ tests/Unit/Fixture/ProfileWithSnapshot.php | 4 +- 9 files changed, 166 insertions(+), 20 deletions(-) rename src/Aggregate/{DefaultApplyMethod.php => NonStrictApplyMethod.php} (95%) create mode 100644 tests/Unit/Aggregate/AggregateRootWithNonStrictApplyTest.php create mode 100644 tests/Unit/Console/InvalidArgumentExceptionTest.php create mode 100644 tests/Unit/Fixture/ProfileWithNonStrictApply.php diff --git a/src/Aggregate/DefaultApplyMethod.php b/src/Aggregate/NonStrictApplyMethod.php similarity index 95% rename from src/Aggregate/DefaultApplyMethod.php rename to src/Aggregate/NonStrictApplyMethod.php index f8057f11..489de0ed 100644 --- a/src/Aggregate/DefaultApplyMethod.php +++ b/src/Aggregate/NonStrictApplyMethod.php @@ -12,7 +12,7 @@ /** * @psalm-require-extends AggregateRoot */ -trait DefaultApplyMethod +trait NonStrictApplyMethod { protected function apply(AggregateChanged $event): void { diff --git a/src/Console/InvalidArgumentException.php b/src/Console/InvalidArgumentException.php index 6a7284c7..33b5562a 100644 --- a/src/Console/InvalidArgumentException.php +++ b/src/Console/InvalidArgumentException.php @@ -26,6 +26,7 @@ public function __construct($value, string $need) gettype($value) ) ); + $this->value = $value; } diff --git a/tests/Integration/BasicImplementation/Aggregate/Profile.php b/tests/Integration/BasicImplementation/Aggregate/Profile.php index f4dde4c1..2a55c006 100644 --- a/tests/Integration/BasicImplementation/Aggregate/Profile.php +++ b/tests/Integration/BasicImplementation/Aggregate/Profile.php @@ -4,13 +4,13 @@ namespace Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Aggregate; -use Patchlevel\EventSourcing\Aggregate\DefaultApplyMethod; +use Patchlevel\EventSourcing\Aggregate\NonStrictApplyMethod; use Patchlevel\EventSourcing\Aggregate\SnapshotableAggregateRoot; use Patchlevel\EventSourcing\Tests\Integration\BasicImplementation\Events\ProfileCreated; final class Profile extends SnapshotableAggregateRoot { - use DefaultApplyMethod; + use NonStrictApplyMethod; private string $id; diff --git a/tests/Integration/Pipeline/Aggregate/Profile.php b/tests/Integration/Pipeline/Aggregate/Profile.php index 34765be2..06ef0270 100644 --- a/tests/Integration/Pipeline/Aggregate/Profile.php +++ b/tests/Integration/Pipeline/Aggregate/Profile.php @@ -5,7 +5,7 @@ namespace Patchlevel\EventSourcing\Tests\Integration\Pipeline\Aggregate; use Patchlevel\EventSourcing\Aggregate\AggregateRoot; -use Patchlevel\EventSourcing\Aggregate\DefaultApplyMethod; +use Patchlevel\EventSourcing\Aggregate\NonStrictApplyMethod; use Patchlevel\EventSourcing\Tests\Integration\Pipeline\Events\NewVisited; use Patchlevel\EventSourcing\Tests\Integration\Pipeline\Events\OldVisited; use Patchlevel\EventSourcing\Tests\Integration\Pipeline\Events\PrivacyAdded; @@ -13,7 +13,7 @@ final class Profile extends AggregateRoot { - use DefaultApplyMethod; + use NonStrictApplyMethod; private string $id; private bool $privacy; diff --git a/tests/Unit/Aggregate/AggregateRootWithNonStrictApplyTest.php b/tests/Unit/Aggregate/AggregateRootWithNonStrictApplyTest.php new file mode 100644 index 00000000..97a4ba43 --- /dev/null +++ b/tests/Unit/Aggregate/AggregateRootWithNonStrictApplyTest.php @@ -0,0 +1,64 @@ +aggregateRootId()); + self::assertEquals(1, $profile->playhead()); + self::assertEquals($id, $profile->id()); + self::assertEquals($email, $profile->email()); + + $events = $profile->releaseEvents(); + + self::assertCount(1, $events); + $event = $events[0]; + self::assertEquals(1, $event->playhead()); + } + + public function testEventWithoutApplyMethod(): void + { + $profileId = ProfileId::fromString('1'); + $email = Email::fromString('david.badura@patchlevel.de'); + + $messageId = MessageId::fromString('2'); + + $profile = ProfileWithNonStrictApply::createProfile($profileId, $email); + + $events = $profile->releaseEvents(); + + self::assertCount(1, $events); + self::assertEquals(1, $profile->playhead()); + $event = $events[0]; + self::assertEquals(1, $event->playhead()); + + $profile->publishMessage( + Message::create( + $messageId, + 'foo' + ) + ); + + $events = $profile->releaseEvents(); + + self::assertCount(1, $events); + $event = $events[0]; + self::assertEquals(2, $event->playhead()); + } +} diff --git a/tests/Unit/Console/InvalidArgumentExceptionTest.php b/tests/Unit/Console/InvalidArgumentExceptionTest.php new file mode 100644 index 00000000..f718d97d --- /dev/null +++ b/tests/Unit/Console/InvalidArgumentExceptionTest.php @@ -0,0 +1,20 @@ +getMessage()); + self::assertEquals($expectedValue, $exception->value()); + } +} diff --git a/tests/Unit/Fixture/Profile.php b/tests/Unit/Fixture/Profile.php index 0952346f..0b4cfa76 100644 --- a/tests/Unit/Fixture/Profile.php +++ b/tests/Unit/Fixture/Profile.php @@ -4,13 +4,11 @@ namespace Patchlevel\EventSourcing\Tests\Unit\Fixture; +use Patchlevel\EventSourcing\Aggregate\AggregateChanged; use Patchlevel\EventSourcing\Aggregate\AggregateRoot; -use Patchlevel\EventSourcing\Aggregate\DefaultApplyMethod; final class Profile extends AggregateRoot { - use DefaultApplyMethod; - private ProfileId $id; private Email $email; /** @var array */ @@ -55,20 +53,25 @@ public function visitProfile(ProfileId $profileId): void $this->record(ProfileVisited::raise($this->id, $profileId)); } - protected function applyProfileCreated(ProfileCreated $event): void + public function aggregateRootId(): string { - $this->id = $event->profileId(); - $this->email = $event->email(); - $this->messages = []; + return $this->id->toString(); } - protected function applyMessagePublished(MessagePublished $event): void + protected function apply(AggregateChanged $event): void { - $this->messages[] = $event->message(); - } + if ($event instanceof ProfileCreated) { + $this->id = $event->profileId(); + $this->email = $event->email(); + $this->messages = []; - public function aggregateRootId(): string - { - return $this->id->toString(); + return; + } + + if ($event instanceof MessagePublished) { + $this->messages[] = $event->message(); + + return; + } } } diff --git a/tests/Unit/Fixture/ProfileWithNonStrictApply.php b/tests/Unit/Fixture/ProfileWithNonStrictApply.php new file mode 100644 index 00000000..7a5a70f1 --- /dev/null +++ b/tests/Unit/Fixture/ProfileWithNonStrictApply.php @@ -0,0 +1,58 @@ +id; + } + + public function email(): Email + { + return $this->email; + } + + public static function createProfile(ProfileId $id, Email $email): self + { + $self = new self(); + $self->record(ProfileCreated::raise($id, $email)); + + return $self; + } + + public function publishMessage(Message $message): void + { + $this->record(MessagePublished::raise( + $this->id, + $message, + )); + } + + public function visitProfile(ProfileId $profileId): void + { + $this->record(ProfileVisited::raise($this->id, $profileId)); + } + + protected function applyProfileCreated(ProfileCreated $event): void + { + $this->id = $event->profileId(); + $this->email = $event->email(); + } + + public function aggregateRootId(): string + { + return $this->id->toString(); + } +} diff --git a/tests/Unit/Fixture/ProfileWithSnapshot.php b/tests/Unit/Fixture/ProfileWithSnapshot.php index 3017a29d..5f86e6d8 100644 --- a/tests/Unit/Fixture/ProfileWithSnapshot.php +++ b/tests/Unit/Fixture/ProfileWithSnapshot.php @@ -4,12 +4,12 @@ namespace Patchlevel\EventSourcing\Tests\Unit\Fixture; -use Patchlevel\EventSourcing\Aggregate\DefaultApplyMethod; +use Patchlevel\EventSourcing\Aggregate\NonStrictApplyMethod; use Patchlevel\EventSourcing\Aggregate\SnapshotableAggregateRoot; final class ProfileWithSnapshot extends SnapshotableAggregateRoot { - use DefaultApplyMethod; + use NonStrictApplyMethod; private ProfileId $id; private Email $email; From c0143e86ee8744501d861c485d7cd7a40a5fb029 Mon Sep 17 00:00:00 2001 From: David Badura Date: Thu, 2 Dec 2021 13:00:27 +0100 Subject: [PATCH 2/2] update docs --- README.md | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 995f2848..a7ae3282 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ namespace App\Domain\Profile; use App\Domain\Profile\Event\MessagePublished; use App\Domain\Profile\Event\ProfileCreated; +use Patchlevel\EventSourcing\Aggregate\AggregateChanged; use Patchlevel\EventSourcing\Aggregate\AggregateRoot; final class Profile extends AggregateRoot @@ -59,17 +60,22 @@ final class Profile extends AggregateRoot $message, )); } - - protected function applyProfileCreated(ProfileCreated $event): void - { - $this->id = $event->profileId(); - $this->email = $event->email(); - $this->messages = []; - } - - protected function applyMessagePublished(MessagePublished $event): void + + protected function apply(AggregateChanged $event): void { - $this->messages[] = $event->message(); + if ($event instanceof ProfileCreated) { + $this->id = $event->profileId(); + $this->email = $event->email(); + $this->messages = []; + + return; + } + + if ($event instanceof MessagePublished) { + $this->messages[] = $event->message(); + + return; + } } public function aggregateRootId(): string