Skip to content

Commit b3cc806

Browse files
authored
Merge pull request #630 from patchlevel/fix-child-aggregates-with-snapshots
fix child aggregates with snapshots
2 parents 4fc8a18 + 88b1e84 commit b3cc806

7 files changed

Lines changed: 42 additions & 18 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"php": "~8.1.0 || ~8.2.0 || ~8.3.0",
2323
"doctrine/dbal": "^4.0.0",
2424
"doctrine/migrations": "^3.3.2",
25-
"patchlevel/hydrator": "^1.4.1",
25+
"patchlevel/hydrator": "^1.5.0",
2626
"patchlevel/worker": "^1.2.0",
2727
"psr/cache": "^2.0.0|^3.0.0",
2828
"psr/clock": "^1.0",

composer.lock

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

src/Aggregate/AggregateRootAttributeBehaviour.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Patchlevel\EventSourcing\Aggregate;
66

77
use Patchlevel\Hydrator\Attribute\Ignore;
8+
use Patchlevel\Hydrator\Attribute\PostHydrate;
89
use ReflectionProperty;
910

1011
use function array_key_exists;
@@ -43,8 +44,6 @@ protected function apply(object $event): void
4344
return;
4445
}
4546

46-
$this->recorder ??= $this->recordThat(...);
47-
4847
$parts = explode('.', $method);
4948

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

59+
$this->passRecorderToChildAggregates();
60+
}
61+
62+
#[PostHydrate]
63+
private function passRecorderToChildAggregates(): void
64+
{
65+
$metadata = static::metadata();
66+
$this->recorder ??= $this->recordThat(...);
67+
6068
foreach ($metadata->childAggregates as $property) {
6169
if (!isset($this->{$property})) {
6270
continue;

src/Aggregate/BasicChildAggregate.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
namespace Patchlevel\EventSourcing\Aggregate;
66

7+
use Patchlevel\Hydrator\Normalizer\ObjectNormalizer;
8+
79
/** @experimental */
10+
#[ObjectNormalizer]
811
abstract class BasicChildAggregate implements ChildAggregate
912
{
1013
use ChildAggregateBehaviour;

src/Aggregate/ChildAggregateBehaviour.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44

55
namespace Patchlevel\EventSourcing\Aggregate;
66

7+
use Patchlevel\Hydrator\Attribute\Ignore;
8+
79
/** @experimental */
810
trait ChildAggregateBehaviour
911
{
1012
/** @var callable(object $event): void */
13+
#[Ignore]
1114
private $recorder;
1215

1316
protected function recordThat(object $event): void

tests/Integration/ChildAggregate/ChildAggregateIntegrationTest.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,21 @@ public function testSnapshot(): void
146146
self::assertSame('John', $result['name']);
147147

148148
$repository = $manager->get(Profile::class);
149+
150+
// create snapshot
151+
$repository->load($profileId);
152+
153+
// load from snapshot
154+
$profile = $repository->load($profileId);
155+
156+
$profile->changeName('Snow');
157+
$repository->save($profile);
158+
149159
$profile = $repository->load($profileId);
150160

151161
self::assertInstanceOf(Profile::class, $profile);
152162
self::assertEquals($profileId, $profile->aggregateRootId());
153-
self::assertSame(1, $profile->playhead());
154-
self::assertSame('John', $profile->name());
163+
self::assertSame(2, $profile->playhead());
164+
self::assertSame('Snow', $profile->name());
155165
}
156166
}

tests/Integration/ChildAggregate/Profile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use Patchlevel\EventSourcing\Tests\Integration\ChildAggregate\Events\ProfileCreated;
1414

1515
#[Aggregate('profile')]
16-
#[Snapshot('default', 100)]
16+
#[Snapshot('default', 1)]
1717
final class Profile extends BasicAggregateRoot
1818
{
1919
#[Id]

0 commit comments

Comments
 (0)