|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Tempest\Integration\Core\Exceptions; |
| 6 | + |
| 7 | +use Exception; |
| 8 | +use PHPUnit\Framework\Attributes\Test; |
| 9 | +use Tempest\Core\Exceptions\LoggingExceptionReporter; |
| 10 | +use Tempest\Core\ProvidesContext; |
| 11 | +use Tempest\Log\Logger; |
| 12 | +use Tests\Tempest\Integration\FrameworkIntegrationTestCase; |
| 13 | + |
| 14 | +final class LoggingExceptionReporterTest extends FrameworkIntegrationTestCase |
| 15 | +{ |
| 16 | + #[Test] |
| 17 | + public function logs_exception_with_message(): void |
| 18 | + { |
| 19 | + $logger = new TestLogger(); |
| 20 | + $reporter = new LoggingExceptionReporter($logger); |
| 21 | + $reporter->report(new Exception('Something went wrong')); |
| 22 | + |
| 23 | + $this->assertCount(2, $logger->logs); |
| 24 | + $this->assertSame('error', $logger->logs[0]['level']); |
| 25 | + $this->assertSame('Something went wrong', $logger->logs[0]['message']); |
| 26 | + } |
| 27 | + |
| 28 | + #[Test] |
| 29 | + public function logs_exception_with_fallback_message(): void |
| 30 | + { |
| 31 | + $logger = new TestLogger(); |
| 32 | + $reporter = new LoggingExceptionReporter($logger); |
| 33 | + $reporter->report(new Exception('')); |
| 34 | + |
| 35 | + $this->assertCount(2, $logger->logs); |
| 36 | + $this->assertSame('error', $logger->logs[0]['level']); |
| 37 | + $this->assertSame('(no message)', $logger->logs[0]['message']); |
| 38 | + } |
| 39 | + |
| 40 | + #[Test] |
| 41 | + public function logs_exception_trace_separately(): void |
| 42 | + { |
| 43 | + $logger = new TestLogger(); |
| 44 | + $reporter = new LoggingExceptionReporter($logger); |
| 45 | + $reporter->report(new Exception('Test')); |
| 46 | + |
| 47 | + $this->assertCount(2, $logger->logs); |
| 48 | + $this->assertSame('error', $logger->logs[1]['level']); |
| 49 | + $this->assertIsString($logger->logs[1]['message']); |
| 50 | + $this->assertStringStartsWith('#0 ', $logger->logs[1]['message']); |
| 51 | + } |
| 52 | + |
| 53 | + #[Test] |
| 54 | + public function logs_exception_with_context_when_exception_provides_context(): void |
| 55 | + { |
| 56 | + $logger = new TestLogger(); |
| 57 | + $reporter = new LoggingExceptionReporter($logger); |
| 58 | + $reporter->report(new ExceptionWithContext('Test')); |
| 59 | + |
| 60 | + $this->assertCount(2, $logger->logs); |
| 61 | + $this->assertSame(['foo' => 'bar', 'baz' => 'qux'], $logger->logs[0]['context']); |
| 62 | + } |
| 63 | + |
| 64 | + #[Test] |
| 65 | + public function logs_exception_with_empty_context_when_exception_does_not_provide_context(): void |
| 66 | + { |
| 67 | + $logger = new TestLogger(); |
| 68 | + $reporter = new LoggingExceptionReporter($logger); |
| 69 | + $reporter->report(new Exception('Test')); |
| 70 | + |
| 71 | + $this->assertCount(2, $logger->logs); |
| 72 | + $this->assertEmpty($logger->logs[0]['context']); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +final class TestLogger implements Logger |
| 77 | +{ |
| 78 | + public array $logs = []; |
| 79 | + |
| 80 | + public function emergency(string|\Stringable $message, array $context = []): void |
| 81 | + { |
| 82 | + $this->logs[] = ['level' => 'emergency', 'message' => $message, 'context' => $context]; |
| 83 | + } |
| 84 | + |
| 85 | + public function alert(string|\Stringable $message, array $context = []): void |
| 86 | + { |
| 87 | + $this->logs[] = ['level' => 'alert', 'message' => $message, 'context' => $context]; |
| 88 | + } |
| 89 | + |
| 90 | + public function critical(string|\Stringable $message, array $context = []): void |
| 91 | + { |
| 92 | + $this->logs[] = ['level' => 'critical', 'message' => $message, 'context' => $context]; |
| 93 | + } |
| 94 | + |
| 95 | + public function error(string|\Stringable $message, array $context = []): void |
| 96 | + { |
| 97 | + $this->logs[] = ['level' => 'error', 'message' => $message, 'context' => $context]; |
| 98 | + } |
| 99 | + |
| 100 | + public function warning(string|\Stringable $message, array $context = []): void |
| 101 | + { |
| 102 | + $this->logs[] = ['level' => 'warning', 'message' => $message, 'context' => $context]; |
| 103 | + } |
| 104 | + |
| 105 | + public function notice(string|\Stringable $message, array $context = []): void |
| 106 | + { |
| 107 | + $this->logs[] = ['level' => 'notice', 'message' => $message, 'context' => $context]; |
| 108 | + } |
| 109 | + |
| 110 | + public function info(string|\Stringable $message, array $context = []): void |
| 111 | + { |
| 112 | + $this->logs[] = ['level' => 'info', 'message' => $message, 'context' => $context]; |
| 113 | + } |
| 114 | + |
| 115 | + public function debug(string|\Stringable $message, array $context = []): void |
| 116 | + { |
| 117 | + $this->logs[] = ['level' => 'debug', 'message' => $message, 'context' => $context]; |
| 118 | + } |
| 119 | + |
| 120 | + public function log(mixed $level, string|\Stringable $message, array $context = []): void |
| 121 | + { |
| 122 | + $this->logs[] = ['level' => $level, 'message' => $message, 'context' => $context]; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +final class ExceptionWithContext extends Exception implements ProvidesContext |
| 127 | +{ |
| 128 | + public function context(): iterable |
| 129 | + { |
| 130 | + return ['foo' => 'bar', 'baz' => 'qux']; |
| 131 | + } |
| 132 | +} |
0 commit comments