From d51b2bb900ef25a25970ce8e351886996a9315e4 Mon Sep 17 00:00:00 2001 From: Daniel Badura Date: Sat, 8 Mar 2025 14:46:08 +0100 Subject: [PATCH 1/2] Handle throwing exceptions when creating the aggregate better. Now, it won't assert the events anymore if an exception is thrown. --- src/Test/AggregateRootTestCase.php | 2 ++ tests/Unit/Fixture/CreateProfileException.php | 14 +++++++++++ tests/Unit/Fixture/Profile.php | 7 ++++++ tests/Unit/Test/AggregateRootTestCaseTest.php | 25 ++++++++++++++++--- 4 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 tests/Unit/Fixture/CreateProfileException.php diff --git a/src/Test/AggregateRootTestCase.php b/src/Test/AggregateRootTestCase.php index a818f87..8c8d365 100644 --- a/src/Test/AggregateRootTestCase.php +++ b/src/Test/AggregateRootTestCase.php @@ -118,6 +118,8 @@ final public function assert(): self } } catch (Throwable $throwable) { $this->handleException($throwable); + + return $this; } if (!$aggregate instanceof AggregateRoot) { diff --git a/tests/Unit/Fixture/CreateProfileException.php b/tests/Unit/Fixture/CreateProfileException.php new file mode 100644 index 0000000..8113d2d --- /dev/null +++ b/tests/Unit/Fixture/CreateProfileException.php @@ -0,0 +1,14 @@ +throwException(); + } + #[Handle] public function visitProfile(VisitProfile $visitProfile, string|null $token = null): void { diff --git a/tests/Unit/Test/AggregateRootTestCaseTest.php b/tests/Unit/Test/AggregateRootTestCaseTest.php index 64af241..595a2f8 100644 --- a/tests/Unit/Test/AggregateRootTestCaseTest.php +++ b/tests/Unit/Test/AggregateRootTestCaseTest.php @@ -11,6 +11,7 @@ use Patchlevel\EventSourcing\PhpUnit\Test\NoAggregateCreated; use Patchlevel\EventSourcing\PhpUnit\Test\NoWhenProvided; use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\CreateProfile; +use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\CreateProfileException; use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\Email; use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\Profile; use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\ProfileCreated; @@ -40,7 +41,7 @@ public function testException(): void ->expectsException(ProfileError::class); $test->assert(); - self::assertSame(2, $test::getCount()); + self::assertSame(1, $test::getCount()); } public function testExceptionMessage(): void @@ -60,7 +61,7 @@ public function testExceptionMessage(): void ->expectsExceptionMessage('throwing so that you can catch it!'); $test->assert(); - self::assertSame(2, $test::getCount()); + self::assertSame(1, $test::getCount()); } public function testExceptionAndMessage(): void @@ -81,7 +82,7 @@ public function testExceptionAndMessage(): void ->expectsExceptionMessage('throwing so that you can catch it!'); $test->assert(); - self::assertSame(3, $test::getCount()); + self::assertSame(2, $test::getCount()); } public function testExceptionUncatched(): void @@ -100,6 +101,24 @@ public function testExceptionUncatched(): void ); $this->expectException(ProfileError::class); + $test->assert(); + self::assertSame(1, $test::getCount()); + } + + public function testExceptionWhenCreating(): void + { + $test = $this->getTester(); + + $test + ->when( + new CreateProfileException( + ProfileId::fromString('1'), + Email::fromString('hq@patchlevel.de'), + ), + ) + ->expectsException(ProfileError::class) + ->expectsExceptionMessage('throwing so that you can catch it!'); + $test->assert(); self::assertSame(2, $test::getCount()); } From ffda9114cb056c0eed898bdc99dd267f44ec42f6 Mon Sep 17 00:00:00 2001 From: Daniel Badura Date: Sat, 8 Mar 2025 14:49:28 +0100 Subject: [PATCH 2/2] Rename command --- ...reateProfileException.php => CreateProfileWithFailure.php} | 2 +- tests/Unit/Fixture/Profile.php | 2 +- tests/Unit/Test/AggregateRootTestCaseTest.php | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) rename tests/Unit/Fixture/{CreateProfileException.php => CreateProfileWithFailure.php} (81%) diff --git a/tests/Unit/Fixture/CreateProfileException.php b/tests/Unit/Fixture/CreateProfileWithFailure.php similarity index 81% rename from tests/Unit/Fixture/CreateProfileException.php rename to tests/Unit/Fixture/CreateProfileWithFailure.php index 8113d2d..fd66dff 100644 --- a/tests/Unit/Fixture/CreateProfileException.php +++ b/tests/Unit/Fixture/CreateProfileWithFailure.php @@ -4,7 +4,7 @@ namespace Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture; -final readonly class CreateProfileException +final readonly class CreateProfileWithFailure { public function __construct( public ProfileId $id, diff --git a/tests/Unit/Fixture/Profile.php b/tests/Unit/Fixture/Profile.php index 4b0d819..e6df059 100644 --- a/tests/Unit/Fixture/Profile.php +++ b/tests/Unit/Fixture/Profile.php @@ -38,7 +38,7 @@ public static function createProfile(CreateProfile $createProfile): self } #[Handle] - public static function createProfileException(CreateProfileException $createProfile): void + public static function createProfileException(CreateProfileWithFailure $createProfile): void { $self = new self(); $self->throwException(); diff --git a/tests/Unit/Test/AggregateRootTestCaseTest.php b/tests/Unit/Test/AggregateRootTestCaseTest.php index 595a2f8..eea79b1 100644 --- a/tests/Unit/Test/AggregateRootTestCaseTest.php +++ b/tests/Unit/Test/AggregateRootTestCaseTest.php @@ -11,7 +11,7 @@ use Patchlevel\EventSourcing\PhpUnit\Test\NoAggregateCreated; use Patchlevel\EventSourcing\PhpUnit\Test\NoWhenProvided; use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\CreateProfile; -use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\CreateProfileException; +use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\CreateProfileWithFailure; use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\Email; use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\Profile; use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\ProfileCreated; @@ -111,7 +111,7 @@ public function testExceptionWhenCreating(): void $test ->when( - new CreateProfileException( + new CreateProfileWithFailure( ProfileId::fromString('1'), Email::fromString('hq@patchlevel.de'), ),