Skip to content
Draft
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
36 changes: 35 additions & 1 deletion src/Test/AggregateRootTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use Closure;
use Patchlevel\EventSourcing\Aggregate\AggregateRoot;
use Patchlevel\EventSourcing\CommandBus\HandlerFinder;
use PHPUnit\Framework\AssertionFailedError;
use PHPUnit\Framework\Attributes\After;
use PHPUnit\Framework\Attributes\Before;
use PHPUnit\Framework\Constraint\Exception as ExceptionConstraint;
use PHPUnit\Framework\Constraint\ExceptionMessageIsOrContains;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
use Throwable;
use function sprintf;

abstract class AggregateRootTestCase extends TestCase
{
Expand All @@ -26,7 +28,7 @@

/** @var array<object> */
private array $expectedEvents = [];
/** @var class-string<Throwable>|null */
/** @var class-string<Throwable>|null */
private string|null $expectedException = null;
private string|null $expectedExceptionMessage = null;

Expand Down Expand Up @@ -122,6 +124,8 @@
return $this;
}

$this->expectedExceptionWasNotThrown();

if (!$aggregate instanceof AggregateRoot) {
throw new NoAggregateCreated();
}
Expand Down Expand Up @@ -172,4 +176,34 @@
throw $throwable;
}
}

private function expectedExceptionWasNotThrown(): void
{
if ($this->expectedException !== null) {
$this->numberOfAssertionsPerformed++;

Check failure on line 183 in src/Test/AggregateRootTestCase.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.4, ubuntu-latest)

Cannot use ++ on mixed.

Check failure on line 183 in src/Test/AggregateRootTestCase.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.4, ubuntu-latest)

Access to private property $numberOfAssertionsPerformed of parent class PHPUnit\Framework\TestCase.

Check failure on line 183 in src/Test/AggregateRootTestCase.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.4, ubuntu-latest)

Access to private property $numberOfAssertionsPerformed of parent class PHPUnit\Framework\TestCase.
throw new AssertionFailedError(
sprintf(
'Failed asserting that exception "%s" is thrown',
$this->expectedException,
),
);
self::assertThat(null, new ExceptionConstraint($this->expectedException));

Check failure on line 190 in src/Test/AggregateRootTestCase.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.4, ubuntu-latest)

Unreachable statement - code above always terminates.
} elseif ($this->expectedExceptionMessage !== null) {
$this->numberOfAssertionsPerformed++;

Check failure on line 192 in src/Test/AggregateRootTestCase.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.4, ubuntu-latest)

Cannot use ++ on mixed.

Check failure on line 192 in src/Test/AggregateRootTestCase.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.4, ubuntu-latest)

Access to private property $numberOfAssertionsPerformed of parent class PHPUnit\Framework\TestCase.

Check failure on line 192 in src/Test/AggregateRootTestCase.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.4, ubuntu-latest)

Access to private property $numberOfAssertionsPerformed of parent class PHPUnit\Framework\TestCase.
throw new AssertionFailedError(
sprintf(
'Failed asserting that exception with message "%s" is thrown',
$this->expectedExceptionMessage,
),
);
self::assertThat(

Check failure on line 199 in src/Test/AggregateRootTestCase.php

View workflow job for this annotation

GitHub Actions / Static Analysis by PHPStan (locked, 8.4, ubuntu-latest)

Unreachable statement - code above always terminates.
null,
new ExceptionMessageIsOrContains($this->expectedExceptionMessage),
sprintf(
'Failed asserting that exception with message "%s" is thrown',
$this->expectedExceptionMessage,
),
);
}
}
}
65 changes: 65 additions & 0 deletions tests/Unit/Test/AggregateRootTestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,71 @@ public function testExceptionAndMessage(): void
self::assertSame(2, $test::getCount());
}

public function testExceptionNotThrown(): void
{
$test = $this->getTester();

$test
->given(
new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('hq@patchlevel.de'),
),
)
->when(
static fn (Profile $profile) => $profile->visitProfile(new VisitProfile(ProfileId::fromString('2'))),
)
->expectsException(ProfileError::class);

try {
$test->assert();
} catch (\Throwable $e) {

}
self::assertSame(1, $test::getCount());
}

public function testExceptionNotThrownWithMessageSpecified(): void
{
$test = $this->getTester();

$test
->given(
new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('hq@patchlevel.de'),
),
)
->when(
static fn (Profile $profile) => $profile->visitProfile(new VisitProfile(ProfileId::fromString('2'))),
)
->expectsExceptionMessage('throwing so that you can catch it!');

$test->assert();
self::assertSame(1, $test::getCount());
}

public function testExceptionNotThrownAndMessageSpecified(): void
{
$test = $this->getTester();

$test
->given(
new ProfileCreated(
ProfileId::fromString('1'),
Email::fromString('hq@patchlevel.de'),
),
)
->when(
static fn (Profile $profile) => $profile->visitProfile(new VisitProfile(ProfileId::fromString('2'))),
)
->expectsException(ProfileError::class)
->expectsExceptionMessage('throwing so that you can catch it!');

$test->assert();
self::assertSame(2, $test::getCount());
}

public function testExceptionUncatched(): void
{
$test = $this->getTester();
Expand Down
Loading