Skip to content

Commit 4d845d2

Browse files
Updating Event Store dependency, fixing bugs (#3)
1 parent 22b66fb commit 4d845d2

File tree

7 files changed

+62
-26
lines changed

7 files changed

+62
-26
lines changed

.idea/eventsourcing.iml

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212
],
1313
"require": {
1414
"php": "^8.2",
15-
"phauthentic/event-store": "dev-master",
15+
"phauthentic/event-store": "^1.0@beta",
1616
"phauthentic/snapshot-store": "dev-master"
1717
},
1818
"require-dev": {
1919
"ext-pdo": "*",
2020
"infection/infection": "^0.29.6",
2121
"phpmd/phpmd": "^2.15",
2222
"phpro/grumphp-shim": "^2.5",
23-
"phpstan/phpstan": "^1.10",
23+
"phpstan/phpstan": "^1.11",
2424
"phpunit/phpunit": "^9.6",
2525
"psr/container": "^1.0||^2.0",
2626
"psr/log": "^2.0||^3.0",

composer.lock

Lines changed: 13 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpstan.neon

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
parameters:
22
level: 8
3+
ignoreErrors:
4+
- identifier: missingType.generics
35
paths:
46
- src
5-
checkGenericClassInNonGenericObjectType: false
7+
parallel:
8+
maximumNumberOfProcesses: 4

src/Repository/AggregateExtractor/ReflectionBasedExtractor.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Phauthentic\EventSourcing\Repository\AggregateExtractor;
66

77
use DateTimeImmutable;
8+
use Phauthentic\EventSourcing\DomainEvent\AggregateIdentityProvidingEventInterface;
89
use Phauthentic\EventSourcing\Repository\AggregateData;
910
use Phauthentic\EventSourcing\Repository\AggregateDataInterface;
1011
use Phauthentic\EventSourcing\Repository\AggregateExtractor\Exception\ExtractorException;
@@ -61,6 +62,7 @@ protected function assertNotEmpty(mixed $value, string $name): void
6162
}
6263

6364
/**
65+
* @param ReflectionClass<object> $reflectionClass
6466
* @throws \Phauthentic\EventSourcing\Repository\AggregateExtractor\Exception\ExtractorException
6567
*/
6668
protected function assertAggregateProperties(ReflectionClass $reflectionClass): void
@@ -71,7 +73,7 @@ protected function assertAggregateProperties(ReflectionClass $reflectionClass):
7173
}
7274

7375
/**
74-
* @param ReflectionClass $reflectionClass
76+
* @param ReflectionClass<object> $reflectionClass
7577
* @param string $propertyName
7678
* @param object $object
7779
* @return mixed

src/Repository/EventSourcedRepository.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Phauthentic\EventStore\EventFactoryInterface;
1313
use Phauthentic\EventStore\EventInterface;
1414
use Phauthentic\EventStore\EventStoreInterface;
15+
use Phauthentic\EventStore\ReplyFromPositionQuery;
1516
use Phauthentic\SnapshotStore\SnapshotFactoryInterface;
1617
use Phauthentic\SnapshotStore\SnapshotInterface;
1718
use Phauthentic\SnapshotStore\Store\SnapshotStoreInterface;
@@ -26,7 +27,7 @@
2627
* - An aggregate extractor that extracts all relevant data from the aggregate object.
2728
* - A snapshot store that can take serialized snapshots of aggregate objects.
2829
* - An event publisher that is a wrapper to connect the aggregate with whatever
29-
* event system you are using. Just wrap with a class implementing the EventPublisherInterface
30+
* event system you are using. Just wrap with a class implementing the EventPublisherInterface.
3031
*/
3132
readonly class EventSourcedRepository implements EventSourcedRepositoryInterface
3233
{
@@ -122,7 +123,10 @@ public function restore(string $aggregateId, string $aggregateType): object
122123
}
123124
}
124125

125-
$events = $this->eventStore->replyFromPosition($aggregateId, $position);
126+
$events = $this->eventStore->replyFromPosition(new ReplyFromPositionQuery(
127+
aggregateId: $aggregateId,
128+
position: $position + 1
129+
));
126130

127131
return $this->aggregateFactory->reconstituteFromEvents($aggregate, $events);
128132
}

tests/Repository/EventSourcedRepositoryIntegrationTest.php

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,11 @@ private function assertAggregateState(
115115
$this->assertSame($expectedDomainEventCount, $invoice->getDomainEventCount());
116116
$this->assertSame($expectedAggregateVersion, $invoice->getAggregateVersion());
117117
$this->assertSame($expectedLineItemCount, $invoice->lineItemCount());
118-
119-
//var_dump($this->snapshotStore);
120118
}
121119

122-
public function testEventSourcedRepositoryIntegration(): void
120+
private function persistAndRestoreTheAggregateFirstTime(Invoice $invoice): Invoice
123121
{
124-
$invoice = $this->createInvoice();
125-
126-
// Act: Restore teh aggregate
122+
// Act: Restore the aggregate
127123
$this->repository->persist($invoice);
128124

129125
$this->assertAggregateState(
@@ -133,7 +129,6 @@ public function testEventSourcedRepositoryIntegration(): void
133129
expectedLineItemCount: 1
134130
);
135131

136-
//dd($this->eventStore);
137132
// Act: Restore the aggregate
138133
$invoice = $this->repository->restore($this->aggregateId, Invoice::class);
139134

@@ -145,6 +140,11 @@ public function testEventSourcedRepositoryIntegration(): void
145140
expectedLineItemCount: 1
146141
);
147142

143+
return $invoice;
144+
}
145+
146+
private function addLineItemAndAssertAggregateState(Invoice $invoice): Invoice
147+
{
148148
// Act: Add one more line
149149
$invoice->addLineItem(LineItem::create(
150150
sku: '456',
@@ -160,6 +160,11 @@ public function testEventSourcedRepositoryIntegration(): void
160160
expectedLineItemCount: 2
161161
);
162162

163+
return $invoice;
164+
}
165+
166+
private function persistAndRestoreAggregateSecondTime(Invoice $invoice): Invoice
167+
{
163168
// Act: Persist the aggregate and restore it
164169
$this->repository->persist($invoice);
165170
$invoice = $this->repository->restore($this->aggregateId, Invoice::class);
@@ -172,6 +177,11 @@ public function testEventSourcedRepositoryIntegration(): void
172177
expectedLineItemCount: 2
173178
);
174179

180+
return $invoice;
181+
}
182+
183+
private function flagInvoiceAsPaidAndAssertState(Invoice $invoice): Invoice
184+
{
175185
// Act: Flag as Paid
176186
$invoice->flagAsPaid();
177187

@@ -183,6 +193,11 @@ public function testEventSourcedRepositoryIntegration(): void
183193
expectedLineItemCount: 2
184194
);
185195

196+
return $invoice;
197+
}
198+
199+
private function persistAndRestoreAggregateThirdTime(Invoice $invoice): Invoice
200+
{
186201
// Act: Persist and restore the aggregate
187202
$this->repository->persist($invoice);
188203
$invoice = $this->repository->restore($this->aggregateId, Invoice::class);
@@ -193,5 +208,17 @@ public function testEventSourcedRepositoryIntegration(): void
193208
expectedAggregateVersion: 4,
194209
expectedLineItemCount: 2
195210
);
211+
212+
return $invoice;
213+
}
214+
215+
public function testEventSourcedRepositoryIntegration(): void
216+
{
217+
$invoice = $this->createInvoice();
218+
$invoice = $this->persistAndRestoreTheAggregateFirstTime($invoice);
219+
$invoice = $this->addLineItemAndAssertAggregateState($invoice);
220+
$invoice = $this->persistAndRestoreAggregateSecondTime($invoice);
221+
$invoice = $this->flagInvoiceAsPaidAndAssertState($invoice);
222+
$this->persistAndRestoreAggregateThirdTime($invoice);
196223
}
197224
}

0 commit comments

Comments
 (0)