Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Test/AggregateRootTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ final public function assert(): self
}
} catch (Throwable $throwable) {
$this->handleException($throwable);

return $this;
}

if (!$aggregate instanceof AggregateRoot) {
Expand Down
14 changes: 14 additions & 0 deletions tests/Unit/Fixture/CreateProfileWithFailure.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

declare(strict_types=1);

namespace Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture;

final readonly class CreateProfileWithFailure
{
public function __construct(
public ProfileId $id,
public Email $email,
) {
}
}
7 changes: 7 additions & 0 deletions tests/Unit/Fixture/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ public static function createProfile(CreateProfile $createProfile): self
return $self;
}

#[Handle]
public static function createProfileException(CreateProfileWithFailure $createProfile): void
{
$self = new self();
$self->throwException();
}

#[Handle]
public function visitProfile(VisitProfile $visitProfile, string|null $token = null): void
{
Expand Down
25 changes: 22 additions & 3 deletions tests/Unit/Test/AggregateRootTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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());
}
Expand Down