Lines
0.00%
0 / 8
Functions and Methods
0.00%
0 / 1
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| AssertsReason | 0.00% | 0 / 8 | 0.00% | 0 / 1 | 12 | 0.00% | 0 / 1 | |||
| assertThrowsReason | 0.00% | 0 / 8 | 0.00% | 0 / 1 | 12 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Testing\Exception; | |
| 6 | ||
| 7 | use Closure; | |
| 8 | use PHPUnit\Framework\TestCase; | |
| 9 | use Stewart\Contracts\Exception\ExceptionReason; | |
| 10 | use Stewart\Contracts\Exception\StewartException; | |
| 11 | ||
| 12 | /** @phpstan-require-extends TestCase */ | |
| 13 | trait AssertsReason | |
| 14 | { | |
| 15 | /** | |
| 16 | * @param Closure(): mixed $action | |
| 17 | * @return StewartException<ExceptionReason> | |
| 18 | */ | |
| 19 | protected function assertThrowsReason(ExceptionReason $reason, Closure $action): StewartException | |
| 20 | { | |
| 21 | $exceptionClass = preg_replace('/Error$/', 'Exception', $reason::class); | |
| 22 | \assert(\is_string($exceptionClass) && is_subclass_of($exceptionClass, StewartException::class)); | |
| 23 | ||
| 24 | try { | |
| 25 | $action(); | |
| 26 | } catch (StewartException $e) { | |
| 27 | self::assertInstanceOf($exceptionClass, $e); | |
| 28 | self::assertSame($reason, $e->reason, $e->getMessage()); | |
| 29 | ||
| 30 | return $e; | |
| 31 | } | |
| 32 | ||
| 33 | self::fail(\sprintf('Expected %s to be thrown with reason %s.', $exceptionClass, $reason->name)); | |
| 34 | } | |
| 35 | } |