|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Tempest\Core; |
| 4 | + |
| 5 | +use Closure; |
| 6 | +use PHPUnit\Framework\Assert; |
| 7 | + |
| 8 | +final readonly class ExceptionTester |
| 9 | +{ |
| 10 | + public function __construct( |
| 11 | + private ExceptionReporter $reporter, |
| 12 | + ) {} |
| 13 | + |
| 14 | + /** |
| 15 | + * Prevents the reporter from reporting exceptions. |
| 16 | + */ |
| 17 | + public function preventReporting(bool $prevent = true): self |
| 18 | + { |
| 19 | + $this->reporter->enabled = ! $prevent; |
| 20 | + |
| 21 | + return $this; |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Asserts that the given `$exception` has been reported. |
| 26 | + * |
| 27 | + * @param null|Closure $callback A callback accepting the exception instance. The assertion fails if the callback returns `false`. |
| 28 | + * @param null|int $count If specified, the assertion fails if the exception has been reported a different amount of times. |
| 29 | + */ |
| 30 | + public function assertReported(string|object $exception, ?Closure $callback = null, ?int $count = null): self |
| 31 | + { |
| 32 | + Assert::assertNotNull( |
| 33 | + actual: $reports = $this->findReports($exception), |
| 34 | + message: 'The exception was not reported.', |
| 35 | + ); |
| 36 | + |
| 37 | + if ($count !== null) { |
| 38 | + Assert::assertCount($count, $reports, sprintf('Expected %s report(s), got %s.', $count, count($reports))); |
| 39 | + } |
| 40 | + |
| 41 | + if ($callback !== null) { |
| 42 | + foreach ($reports as $dispatch) { |
| 43 | + Assert::assertNotFalse($callback($dispatch), 'The callback failed.'); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return $this; |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Asserts that the given `$exception` was not reported. |
| 52 | + */ |
| 53 | + public function assertNotReported(string|object $exception): self |
| 54 | + { |
| 55 | + Assert::assertEmpty( |
| 56 | + actual: $this->findReports($exception), |
| 57 | + message: 'The exception was reported.', |
| 58 | + ); |
| 59 | + |
| 60 | + return $this; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Asserts that no exceptions were reported. |
| 65 | + */ |
| 66 | + public function assertNothingReported(): self |
| 67 | + { |
| 68 | + Assert::assertEmpty( |
| 69 | + actual: $this->reporter->reported, |
| 70 | + message: sprintf('There are unexpected reported exceptions: [%s]', implode(', ', $this->reporter->reported)), |
| 71 | + ); |
| 72 | + |
| 73 | + return $this; |
| 74 | + } |
| 75 | + |
| 76 | + private function findReports(string|object $exception): array |
| 77 | + { |
| 78 | + return array_filter($this->reporter->reported, function (string|object $reported) use ($exception) { |
| 79 | + if ($reported === $exception) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + |
| 83 | + if (class_exists($exception) && is_a($reported, $exception, allow_string: true)) { |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + return false; |
| 88 | + }); |
| 89 | + } |
| 90 | +} |
0 commit comments