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/CreateProfileWithFailure.php b/tests/Unit/Fixture/CreateProfileWithFailure.php new file mode 100644 index 0000000..fd66dff --- /dev/null +++ b/tests/Unit/Fixture/CreateProfileWithFailure.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..eea79b1 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\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; @@ -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 CreateProfileWithFailure( + 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()); }