From 28ef0e4723d26472119a63110b1bd31cacf91c7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Sun, 14 Jul 2024 00:12:16 +0200 Subject: [PATCH 1/2] Updating Event Store dependency, fixing bugs --- composer.json | 2 +- composer.lock | 21 ++++++---- phpstan.neon | 5 ++- .../ReflectionBasedExtractor.php | 4 +- src/Repository/EventSourcedRepository.php | 8 +++- .../EventSourcedRepositoryIntegrationTest.php | 41 +++++++++++++++---- 6 files changed, 60 insertions(+), 21 deletions(-) diff --git a/composer.json b/composer.json index 201acb8..428e60c 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ ], "require": { "php": "^8.2", - "phauthentic/event-store": "dev-master", + "phauthentic/event-store": "^1.0@beta", "phauthentic/snapshot-store": "dev-master" }, "require-dev": { diff --git a/composer.lock b/composer.lock index f811bfb..9d53127 100644 --- a/composer.lock +++ b/composer.lock @@ -4,29 +4,33 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "cb6cfb3c9e7069894d5ca18b3c999124", + "content-hash": "21deb4c4bc1d8f702e1c8e23b4e4c2d9", "packages": [ { "name": "phauthentic/event-store", - "version": "dev-master", + "version": "1.0.0-beta1", "source": { "type": "git", "url": "https://github.com/Phauthentic/event-store.git", - "reference": "c44ff53420afe6387aa73c78c4f33ae4d4eae3c8" + "reference": "ac563a0ede64ad39d49171bb514e2c2955de68bf" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Phauthentic/event-store/zipball/c44ff53420afe6387aa73c78c4f33ae4d4eae3c8", - "reference": "c44ff53420afe6387aa73c78c4f33ae4d4eae3c8", + "url": "https://api.github.com/repos/Phauthentic/event-store/zipball/ac563a0ede64ad39d49171bb514e2c2955de68bf", + "reference": "ac563a0ede64ad39d49171bb514e2c2955de68bf", "shasum": "" }, + "require": { + "php": "^8.2" + }, "require-dev": { + "ext-pdo": "*", + "infection/infection": "^0.29.6", "phpstan/phpstan": "^1.10", "phpunit/phpunit": "^10.5", "ramsey/uuid": "^4.7", "squizlabs/php_codesniffer": "^3.9" }, - "default-branch": true, "type": "library", "autoload": { "psr-4": { @@ -43,9 +47,9 @@ ], "support": { "issues": "https://github.com/Phauthentic/event-store/issues", - "source": "https://github.com/Phauthentic/event-store/tree/master" + "source": "https://github.com/Phauthentic/event-store/tree/1.0.0-beta1" }, - "time": "2024-07-12T20:20:45+00:00" + "time": "2024-07-13T20:01:29+00:00" }, { "name": "phauthentic/snapshot-store", @@ -5076,7 +5080,6 @@ "aliases": [], "minimum-stability": "dev", "stability-flags": { - "phauthentic/event-store": 20, "phauthentic/snapshot-store": 20 }, "prefer-stable": true, diff --git a/phpstan.neon b/phpstan.neon index c3df0b7..abdad39 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,5 +1,8 @@ parameters: level: 8 + ignoreErrors: + - identifier: missingType.generics paths: - src - checkGenericClassInNonGenericObjectType: false + parallel: + maximumNumberOfProcesses: 4 diff --git a/src/Repository/AggregateExtractor/ReflectionBasedExtractor.php b/src/Repository/AggregateExtractor/ReflectionBasedExtractor.php index f98cecf..31b53f1 100644 --- a/src/Repository/AggregateExtractor/ReflectionBasedExtractor.php +++ b/src/Repository/AggregateExtractor/ReflectionBasedExtractor.php @@ -5,6 +5,7 @@ namespace Phauthentic\EventSourcing\Repository\AggregateExtractor; use DateTimeImmutable; +use Phauthentic\EventSourcing\DomainEvent\AggregateIdentityProvidingEventInterface; use Phauthentic\EventSourcing\Repository\AggregateData; use Phauthentic\EventSourcing\Repository\AggregateDataInterface; use Phauthentic\EventSourcing\Repository\AggregateExtractor\Exception\ExtractorException; @@ -61,6 +62,7 @@ protected function assertNotEmpty(mixed $value, string $name): void } /** + * @param ReflectionClass $reflectionClass * @throws \Phauthentic\EventSourcing\Repository\AggregateExtractor\Exception\ExtractorException */ protected function assertAggregateProperties(ReflectionClass $reflectionClass): void @@ -71,7 +73,7 @@ protected function assertAggregateProperties(ReflectionClass $reflectionClass): } /** - * @param ReflectionClass $reflectionClass + * @param ReflectionClass $reflectionClass * @param string $propertyName * @param object $object * @return mixed diff --git a/src/Repository/EventSourcedRepository.php b/src/Repository/EventSourcedRepository.php index 133fe2d..1cfe3bc 100644 --- a/src/Repository/EventSourcedRepository.php +++ b/src/Repository/EventSourcedRepository.php @@ -12,6 +12,7 @@ use Phauthentic\EventStore\EventFactoryInterface; use Phauthentic\EventStore\EventInterface; use Phauthentic\EventStore\EventStoreInterface; +use Phauthentic\EventStore\ReplyFromPositionQuery; use Phauthentic\SnapshotStore\SnapshotFactoryInterface; use Phauthentic\SnapshotStore\SnapshotInterface; use Phauthentic\SnapshotStore\Store\SnapshotStoreInterface; @@ -26,7 +27,7 @@ * - An aggregate extractor that extracts all relevant data from the aggregate object. * - A snapshot store that can take serialized snapshots of aggregate objects. * - An event publisher that is a wrapper to connect the aggregate with whatever - * event system you are using. Just wrap with a class implementing the EventPublisherInterface + * event system you are using. Just wrap with a class implementing the EventPublisherInterface. */ readonly class EventSourcedRepository implements EventSourcedRepositoryInterface { @@ -122,7 +123,10 @@ public function restore(string $aggregateId, string $aggregateType): object } } - $events = $this->eventStore->replyFromPosition($aggregateId, $position); + $events = $this->eventStore->replyFromPosition(new ReplyFromPositionQuery( + aggregateId: $aggregateId, + position: $position + 1 + )); return $this->aggregateFactory->reconstituteFromEvents($aggregate, $events); } diff --git a/tests/Repository/EventSourcedRepositoryIntegrationTest.php b/tests/Repository/EventSourcedRepositoryIntegrationTest.php index fd3c66a..e0ea751 100644 --- a/tests/Repository/EventSourcedRepositoryIntegrationTest.php +++ b/tests/Repository/EventSourcedRepositoryIntegrationTest.php @@ -115,15 +115,11 @@ private function assertAggregateState( $this->assertSame($expectedDomainEventCount, $invoice->getDomainEventCount()); $this->assertSame($expectedAggregateVersion, $invoice->getAggregateVersion()); $this->assertSame($expectedLineItemCount, $invoice->lineItemCount()); - - //var_dump($this->snapshotStore); } - public function testEventSourcedRepositoryIntegration(): void + private function persistAndRestoreTheAggregateFirstTime(Invoice $invoice): Invoice { - $invoice = $this->createInvoice(); - - // Act: Restore teh aggregate + // Act: Restore the aggregate $this->repository->persist($invoice); $this->assertAggregateState( @@ -133,7 +129,6 @@ public function testEventSourcedRepositoryIntegration(): void expectedLineItemCount: 1 ); - //dd($this->eventStore); // Act: Restore the aggregate $invoice = $this->repository->restore($this->aggregateId, Invoice::class); @@ -145,6 +140,11 @@ public function testEventSourcedRepositoryIntegration(): void expectedLineItemCount: 1 ); + return $invoice; + } + + private function addLineItemAndAssertAggregateState(Invoice $invoice): Invoice + { // Act: Add one more line $invoice->addLineItem(LineItem::create( sku: '456', @@ -160,6 +160,11 @@ public function testEventSourcedRepositoryIntegration(): void expectedLineItemCount: 2 ); + return $invoice; + } + + private function persistAndRestoreAggregateSecondTime(Invoice $invoice): Invoice + { // Act: Persist the aggregate and restore it $this->repository->persist($invoice); $invoice = $this->repository->restore($this->aggregateId, Invoice::class); @@ -172,6 +177,11 @@ public function testEventSourcedRepositoryIntegration(): void expectedLineItemCount: 2 ); + return $invoice; + } + + private function flagInvoiceAsPaidAndAssertState(Invoice $invoice): Invoice + { // Act: Flag as Paid $invoice->flagAsPaid(); @@ -183,6 +193,11 @@ public function testEventSourcedRepositoryIntegration(): void expectedLineItemCount: 2 ); + return $invoice; + } + + private function persistAndRestoreAggregateThirdTime(Invoice $invoice): Invoice + { // Act: Persist and restore the aggregate $this->repository->persist($invoice); $invoice = $this->repository->restore($this->aggregateId, Invoice::class); @@ -193,5 +208,17 @@ public function testEventSourcedRepositoryIntegration(): void expectedAggregateVersion: 4, expectedLineItemCount: 2 ); + + return $invoice; + } + + public function testEventSourcedRepositoryIntegration(): void + { + $invoice = $this->createInvoice(); + $invoice = $this->persistAndRestoreTheAggregateFirstTime($invoice); + $invoice = $this->addLineItemAndAssertAggregateState($invoice); + $invoice = $this->persistAndRestoreAggregateSecondTime($invoice); + $invoice = $this->flagInvoiceAsPaidAndAssertState($invoice); + $this->persistAndRestoreAggregateThirdTime($invoice); } } From 816cd077d03d25025151c30c225e831599e49177 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Sun, 14 Jul 2024 00:17:25 +0200 Subject: [PATCH 2/2] Composer dependencies --- .idea/eventsourcing.iml | 4 ---- composer.json | 2 +- composer.lock | 3 ++- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/.idea/eventsourcing.iml b/.idea/eventsourcing.iml index 54890f5..a7a2bcb 100644 --- a/.idea/eventsourcing.iml +++ b/.idea/eventsourcing.iml @@ -2,10 +2,6 @@ - - - - diff --git a/composer.json b/composer.json index 428e60c..124d99a 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "infection/infection": "^0.29.6", "phpmd/phpmd": "^2.15", "phpro/grumphp-shim": "^2.5", - "phpstan/phpstan": "^1.10", + "phpstan/phpstan": "^1.11", "phpunit/phpunit": "^9.6", "psr/container": "^1.0||^2.0", "psr/log": "^2.0||^3.0", diff --git a/composer.lock b/composer.lock index 9d53127..ce6bce3 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "21deb4c4bc1d8f702e1c8e23b4e4c2d9", + "content-hash": "e6bef133f1715f47a0af436549cafcda", "packages": [ { "name": "phauthentic/event-store", @@ -5080,6 +5080,7 @@ "aliases": [], "minimum-stability": "dev", "stability-flags": { + "phauthentic/event-store": 10, "phauthentic/snapshot-store": 20 }, "prefer-stable": true,