Skip to content

Commit daad82a

Browse files
authored
Merge pull request #15 from patchlevel/if-exception-expected-and-not-thrown-fail
If exception is expected and not thrown, fail the test
2 parents 7848314 + fd74097 commit daad82a

2 files changed

Lines changed: 108 additions & 0 deletions

File tree

src/Test/AggregateRootTestCase.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ final public function assert(): self
127127
return $this;
128128
}
129129

130+
$this->expectedExceptionWasNotThrown();
131+
130132
if (!$aggregate instanceof AggregateRoot) {
131133
throw new NoAggregateCreated();
132134
}
@@ -187,4 +189,24 @@ private function handleException(Throwable $throwable): void
187189
throw $throwable;
188190
}
189191
}
192+
193+
private function expectedExceptionWasNotThrown(): void
194+
{
195+
if ($this->expectedException !== null) {
196+
self::assertThat(null, new ExceptionConstraint($this->expectedException));
197+
}
198+
199+
if ($this->expectedExceptionMessage === null) {
200+
return;
201+
}
202+
203+
self::assertThat(
204+
null,
205+
new ExceptionMessageIsOrContains($this->expectedExceptionMessage),
206+
sprintf(
207+
'Failed asserting that exception with message "%s" is thrown',
208+
$this->expectedExceptionMessage,
209+
),
210+
);
211+
}
190212
}

tests/Unit/Test/AggregateRootTestCaseTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use PHPUnit\Framework\Attributes\CoversClass;
2424
use PHPUnit\Framework\Attributes\DataProvider;
2525
use PHPUnit\Framework\TestCase;
26+
use Throwable;
2627

2728
#[CoversClass(AggregateRootTestCase::class)]
2829
final class AggregateRootTestCaseTest extends TestCase
@@ -86,6 +87,91 @@ public function testExceptionAndMessage(): void
8687
self::assertSame(2, $test::getCount());
8788
}
8889

90+
public function testExceptionNotThrown(): void
91+
{
92+
$test = $this->getTester();
93+
94+
$test
95+
->given(
96+
new ProfileCreated(
97+
ProfileId::fromString('1'),
98+
Email::fromString('hq@patchlevel.de'),
99+
),
100+
)
101+
->when(
102+
static fn (Profile $profile) => $profile->visitProfile(new VisitProfile(ProfileId::fromString('2'))),
103+
)
104+
->expectsException(ProfileError::class);
105+
106+
$exception = null;
107+
108+
try {
109+
$test->assert();
110+
} catch (Throwable $e) {
111+
$exception = $e;
112+
}
113+
114+
self::assertNotNull($exception);
115+
self::assertSame('Failed asserting that exception of type "Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\ProfileError" is thrown.', $exception->getMessage());
116+
}
117+
118+
public function testExceptionNotThrownWithMessageSpecified(): void
119+
{
120+
$test = $this->getTester();
121+
122+
$test
123+
->given(
124+
new ProfileCreated(
125+
ProfileId::fromString('1'),
126+
Email::fromString('hq@patchlevel.de'),
127+
),
128+
)
129+
->when(
130+
static fn (Profile $profile) => $profile->visitProfile(new VisitProfile(ProfileId::fromString('2'))),
131+
)
132+
->expectsExceptionMessage('throwing so that you can catch it!');
133+
134+
$exception = null;
135+
136+
try {
137+
$test->assert();
138+
} catch (Throwable $e) {
139+
$exception = $e;
140+
}
141+
142+
self::assertNotNull($exception);
143+
self::assertSame("Failed asserting that exception with message \"throwing so that you can catch it!\" is thrown\nFailed asserting that exception message '' contains 'throwing so that you can catch it!'.", $exception->getMessage());
144+
}
145+
146+
public function testExceptionNotThrownAndMessageSpecified(): void
147+
{
148+
$test = $this->getTester();
149+
150+
$test
151+
->given(
152+
new ProfileCreated(
153+
ProfileId::fromString('1'),
154+
Email::fromString('hq@patchlevel.de'),
155+
),
156+
)
157+
->when(
158+
static fn (Profile $profile) => $profile->visitProfile(new VisitProfile(ProfileId::fromString('2'))),
159+
)
160+
->expectsException(ProfileError::class)
161+
->expectsExceptionMessage('throwing so that you can catch it!');
162+
163+
$exception = null;
164+
165+
try {
166+
$test->assert();
167+
} catch (Throwable $e) {
168+
$exception = $e;
169+
}
170+
171+
self::assertNotNull($exception);
172+
self::assertSame('Failed asserting that exception of type "Patchlevel\EventSourcing\PhpUnit\Tests\Unit\Fixture\ProfileError" is thrown.', $exception->getMessage());
173+
}
174+
89175
public function testExceptionUncatched(): void
90176
{
91177
$test = $this->getTester();

0 commit comments

Comments
 (0)