|
23 | 23 | use PHPUnit\Framework\Attributes\CoversClass; |
24 | 24 | use PHPUnit\Framework\Attributes\DataProvider; |
25 | 25 | use PHPUnit\Framework\TestCase; |
| 26 | +use Throwable; |
26 | 27 |
|
27 | 28 | #[CoversClass(AggregateRootTestCase::class)] |
28 | 29 | final class AggregateRootTestCaseTest extends TestCase |
@@ -86,6 +87,91 @@ public function testExceptionAndMessage(): void |
86 | 87 | self::assertSame(2, $test::getCount()); |
87 | 88 | } |
88 | 89 |
|
| 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 | + |
89 | 175 | public function testExceptionUncatched(): void |
90 | 176 | { |
91 | 177 | $test = $this->getTester(); |
|
0 commit comments