@@ -26,10 +26,21 @@ final class ProfileTest extends AggregateRootTestCase
2626 {
2727 $this
2828 ->when(static fn () => Profile::createProfile(new CreateProfile(ProfileId::fromString('1'), Email::fromString('hq@patchlevel.de'))))
29- ->then(new ProfileCreated(ProfileId::fromString('1'), Email::fromString('hq@patchlevel.de')));
29+ ->then(
30+ new ProfileCreated(ProfileId::fromString('1'), Email::fromString('hq@patchlevel.de')),
31+ static function (Profile $profile): void {
32+ self::assertSame('1', $profile->id()->toString());
33+ self::assertSame('hq@patchlevel.de', $profile->email()->toString());
34+ self::assertSame(0, $profile->visited());
35+ },
36+ );
3037 }
3138}
3239```
40+ In addition to expected events, you can pass ` Closure ` s to ` then ` .
41+ The closure receives the aggregate instance after ` when ` , so you can run PHPUnit assertions on aggregate state.
42+ This support is available since ` patchlevel/event-sourcing-phpunit ` ` 1.5 ` .
43+
3344You can also prepare the aggregate with events to set it to a specific state and then test whether it behaves as
3445expected.
3546
@@ -50,7 +61,14 @@ final class ProfileTest extends AggregateRootTestCase
5061 ),
5162 )
5263 ->when(static fn (Profile $profile) => $profile->visitProfile(ProfileId::fromString('2')))
53- ->then(new ProfileVisited(ProfileId::fromString('2')));
64+ ->then(
65+ new ProfileVisited(ProfileId::fromString('2')),
66+ static function (Profile $profile): void {
67+ self::assertSame('1', $profile->id()->toString());
68+ self::assertSame('hq@patchlevel.de', $profile->email()->toString());
69+ self::assertSame(1, $profile->visited());
70+ },
71+ );
5472 }
5573}
5674```
@@ -70,7 +88,10 @@ final class ProfileTest extends AggregateRootTestCase
7088 {
7189 $this
7290 ->when(new CreateProfile(ProfileId::fromString('1'), Email::fromString('hq@patchlevel.de')))
73- ->then(new ProfileCreated(ProfileId::fromString('1'), Email::fromString('hq@patchlevel.de')));
91+ ->then(
92+ new ProfileCreated(ProfileId::fromString('1'), Email::fromString('hq@patchlevel.de')),
93+ static fn (Profile $profile) => self::assertSame('hq@patchlevel.de', $profile->email()->toString()),
94+ );
7495 }
7596}
7697```
@@ -99,6 +120,7 @@ final class ProfileTest extends AggregateRootTestCase
99120 )
100121 ->then(
101122 new ProfileVisited(ProfileId::fromString('2'), 'Extra Parameter / Dependency'),
123+ static fn (Profile $profile) => self::assertSame(1, $profile->visited()),
102124 );
103125 }
104126}
@@ -176,11 +198,14 @@ final class ProfileTest extends AggregateRootTestCase
176198 new ChangeEmail(ProfileId::fromString('1'), Email::fromString('new-hq@patchlevel.de')),
177199 $clock,
178200 )
179- ->then(new EmailChanged(
180- ProfileId::fromString('1'),
181- Email::fromString('new-hq@patchlevel.de'),
182- new DateTimeImmutable('2021-01-01 00:00:10'),
183- ));
201+ ->then(
202+ new EmailChanged(
203+ ProfileId::fromString('1'),
204+ Email::fromString('new-hq@patchlevel.de'),
205+ new DateTimeImmutable('2021-01-01 00:00:10'),
206+ ),
207+ static fn (Profile $profile) => self::assertSame('new-hq@patchlevel.de', $profile->email()->toString()),
208+ );
184209 }
185210}
186211```
@@ -220,4 +245,4 @@ final class ProfileTest extends TestCase
220245
221246 The `IncrementalRamseyUuidFactory` is only for testing purposes
222247 and supports only the version 7 what is used by the library.
223-
248+
0 commit comments