Skip to content

Commit 06084f2

Browse files
authored
Merge pull request #8 from patchlevel/fix-throwing-when-creating
Handle throwing exceptions when creating the aggregate better.
2 parents 0eac3c7 + ffda911 commit 06084f2

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

src/Test/AggregateRootTestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ final public function assert(): self
118118
}
119119
} catch (Throwable $throwable) {
120120
$this->handleException($throwable);
121+
122+
return $this;
121123
}
122124

123125
if (!$aggregate instanceof AggregateRoot) {
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture;
6+
7+
final readonly class CreateProfileWithFailure
8+
{
9+
public function __construct(
10+
public ProfileId $id,
11+
public Email $email,
12+
) {
13+
}
14+
}

tests/Unit/Fixture/Profile.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ public static function createProfile(CreateProfile $createProfile): self
3737
return $self;
3838
}
3939

40+
#[Handle]
41+
public static function createProfileException(CreateProfileWithFailure $createProfile): void
42+
{
43+
$self = new self();
44+
$self->throwException();
45+
}
46+
4047
#[Handle]
4148
public function visitProfile(VisitProfile $visitProfile, string|null $token = null): void
4249
{

tests/Unit/Test/AggregateRootTestCaseTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Patchlevel\EventSourcing\PhpUnit\Test\NoAggregateCreated;
1212
use Patchlevel\EventSourcing\PhpUnit\Test\NoWhenProvided;
1313
use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\CreateProfile;
14+
use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\CreateProfileWithFailure;
1415
use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\Email;
1516
use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\Profile;
1617
use Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\ProfileCreated;
@@ -40,7 +41,7 @@ public function testException(): void
4041
->expectsException(ProfileError::class);
4142

4243
$test->assert();
43-
self::assertSame(2, $test::getCount());
44+
self::assertSame(1, $test::getCount());
4445
}
4546

4647
public function testExceptionMessage(): void
@@ -60,7 +61,7 @@ public function testExceptionMessage(): void
6061
->expectsExceptionMessage('throwing so that you can catch it!');
6162

6263
$test->assert();
63-
self::assertSame(2, $test::getCount());
64+
self::assertSame(1, $test::getCount());
6465
}
6566

6667
public function testExceptionAndMessage(): void
@@ -81,7 +82,7 @@ public function testExceptionAndMessage(): void
8182
->expectsExceptionMessage('throwing so that you can catch it!');
8283

8384
$test->assert();
84-
self::assertSame(3, $test::getCount());
85+
self::assertSame(2, $test::getCount());
8586
}
8687

8788
public function testExceptionUncatched(): void
@@ -100,6 +101,24 @@ public function testExceptionUncatched(): void
100101
);
101102

102103
$this->expectException(ProfileError::class);
104+
$test->assert();
105+
self::assertSame(1, $test::getCount());
106+
}
107+
108+
public function testExceptionWhenCreating(): void
109+
{
110+
$test = $this->getTester();
111+
112+
$test
113+
->when(
114+
new CreateProfileWithFailure(
115+
ProfileId::fromString('1'),
116+
Email::fromString('hq@patchlevel.de'),
117+
),
118+
)
119+
->expectsException(ProfileError::class)
120+
->expectsExceptionMessage('throwing so that you can catch it!');
121+
103122
$test->assert();
104123
self::assertSame(2, $test::getCount());
105124
}

0 commit comments

Comments
 (0)