Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating Event Store dependency, fixing bugs #3

Merged
merged 2 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .idea/eventsourcing.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
],
"require": {
"php": "^8.2",
"phauthentic/event-store": "dev-master",
"phauthentic/event-store": "^1.0@beta",
"phauthentic/snapshot-store": "dev-master"
},
"require-dev": {
"ext-pdo": "*",
"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",
Expand Down
22 changes: 13 additions & 9 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
parameters:
level: 8
ignoreErrors:
- identifier: missingType.generics
paths:
- src
checkGenericClassInNonGenericObjectType: false
parallel:
maximumNumberOfProcesses: 4
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,6 +62,7 @@ protected function assertNotEmpty(mixed $value, string $name): void
}

/**
* @param ReflectionClass<object> $reflectionClass
* @throws \Phauthentic\EventSourcing\Repository\AggregateExtractor\Exception\ExtractorException
*/
protected function assertAggregateProperties(ReflectionClass $reflectionClass): void
Expand All @@ -71,7 +73,7 @@ protected function assertAggregateProperties(ReflectionClass $reflectionClass):
}

/**
* @param ReflectionClass $reflectionClass
* @param ReflectionClass<object> $reflectionClass
* @param string $propertyName
* @param object $object
* @return mixed
Expand Down
8 changes: 6 additions & 2 deletions src/Repository/EventSourcedRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -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);
}
Expand Down
41 changes: 34 additions & 7 deletions tests/Repository/EventSourcedRepositoryIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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);

Expand All @@ -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',
Expand All @@ -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);
Expand All @@ -172,6 +177,11 @@ public function testEventSourcedRepositoryIntegration(): void
expectedLineItemCount: 2
);

return $invoice;
}

private function flagInvoiceAsPaidAndAssertState(Invoice $invoice): Invoice
{
// Act: Flag as Paid
$invoice->flagAsPaid();

Expand All @@ -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);
Expand All @@ -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);
}
}
Loading