Skip to content

Commit

Permalink
Merge pull request #630 from patchlevel/fix-child-aggregates-with-sna…
Browse files Browse the repository at this point in the history
…pshots

fix child aggregates with snapshots
  • Loading branch information
DanielBadura authored Sep 18, 2024
2 parents 4fc8a18 + 88b1e84 commit b3cc806
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 18 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
"doctrine/dbal": "^4.0.0",
"doctrine/migrations": "^3.3.2",
"patchlevel/hydrator": "^1.4.1",
"patchlevel/hydrator": "^1.5.0",
"patchlevel/worker": "^1.2.0",
"psr/cache": "^2.0.0|^3.0.0",
"psr/clock": "^1.0",
Expand Down
24 changes: 12 additions & 12 deletions composer.lock

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

12 changes: 10 additions & 2 deletions src/Aggregate/AggregateRootAttributeBehaviour.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Patchlevel\EventSourcing\Aggregate;

use Patchlevel\Hydrator\Attribute\Ignore;
use Patchlevel\Hydrator\Attribute\PostHydrate;
use ReflectionProperty;

use function array_key_exists;
Expand Down Expand Up @@ -43,8 +44,6 @@ protected function apply(object $event): void
return;
}

$this->recorder ??= $this->recordThat(...);

$parts = explode('.', $method);

if (count($parts) === 2) {
Expand All @@ -57,6 +56,15 @@ protected function apply(object $event): void
$this->$method($event);
}

$this->passRecorderToChildAggregates();
}

#[PostHydrate]
private function passRecorderToChildAggregates(): void
{
$metadata = static::metadata();
$this->recorder ??= $this->recordThat(...);

Check warning on line 66 in src/Aggregate/AggregateRootAttributeBehaviour.php

View workflow job for this annotation

GitHub Actions / Mutation tests (locked, 8.3, ubuntu-latest)

Escaped Mutant for Mutator "AssignCoalesce": --- Original +++ New @@ @@ private function passRecorderToChildAggregates() : void { $metadata = static::metadata(); - $this->recorder ??= $this->recordThat(...); + $this->recorder = $this->recordThat(...); foreach ($metadata->childAggregates as $property) { if (!isset($this->{$property})) { continue;

foreach ($metadata->childAggregates as $property) {
if (!isset($this->{$property})) {
continue;
Expand Down
3 changes: 3 additions & 0 deletions src/Aggregate/BasicChildAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace Patchlevel\EventSourcing\Aggregate;

use Patchlevel\Hydrator\Normalizer\ObjectNormalizer;

/** @experimental */
#[ObjectNormalizer]
abstract class BasicChildAggregate implements ChildAggregate
{
use ChildAggregateBehaviour;
Expand Down
3 changes: 3 additions & 0 deletions src/Aggregate/ChildAggregateBehaviour.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

namespace Patchlevel\EventSourcing\Aggregate;

use Patchlevel\Hydrator\Attribute\Ignore;

/** @experimental */
trait ChildAggregateBehaviour
{
/** @var callable(object $event): void */
#[Ignore]
private $recorder;

protected function recordThat(object $event): void
Expand Down
14 changes: 12 additions & 2 deletions tests/Integration/ChildAggregate/ChildAggregateIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,21 @@ public function testSnapshot(): void
self::assertSame('John', $result['name']);

$repository = $manager->get(Profile::class);

// create snapshot
$repository->load($profileId);

// load from snapshot
$profile = $repository->load($profileId);

$profile->changeName('Snow');
$repository->save($profile);

$profile = $repository->load($profileId);

self::assertInstanceOf(Profile::class, $profile);
self::assertEquals($profileId, $profile->aggregateRootId());
self::assertSame(1, $profile->playhead());
self::assertSame('John', $profile->name());
self::assertSame(2, $profile->playhead());
self::assertSame('Snow', $profile->name());
}
}
2 changes: 1 addition & 1 deletion tests/Integration/ChildAggregate/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Patchlevel\EventSourcing\Tests\Integration\ChildAggregate\Events\ProfileCreated;

#[Aggregate('profile')]
#[Snapshot('default', 100)]
#[Snapshot('default', 1)]
final class Profile extends BasicAggregateRoot
{
#[Id]
Expand Down

0 comments on commit b3cc806

Please sign in to comment.