|
7 | 7 | use Monolog\Logger; |
8 | 8 | use PHPUnit\Framework\TestCase; |
9 | 9 | use Sentry\ClientBuilder; |
| 10 | +use Sentry\Event; |
10 | 11 | use Sentry\Logs\Log; |
11 | 12 | use Sentry\Logs\LogLevel; |
12 | 13 | use Sentry\Logs\Logs; |
13 | 14 | use Sentry\Monolog\LogsHandler; |
14 | 15 | use Sentry\SentrySdk; |
15 | 16 | use Sentry\State\Hub; |
| 17 | +use Sentry\Transport\Result; |
| 18 | +use Sentry\Transport\ResultStatus; |
| 19 | +use Sentry\Transport\TransportInterface; |
16 | 20 |
|
17 | 21 | final class LogsHandlerTest extends TestCase |
18 | 22 | { |
@@ -82,6 +86,51 @@ public function testLogLevels($record, int $countLogs): void |
82 | 86 | $this->assertCount($countLogs, $logs); |
83 | 87 | } |
84 | 88 |
|
| 89 | + public function testLogsHandlerDestructor() |
| 90 | + { |
| 91 | + $transport = new class implements TransportInterface { |
| 92 | + private $events = []; |
| 93 | + |
| 94 | + public function send(Event $event): Result |
| 95 | + { |
| 96 | + $this->events[] = $event; |
| 97 | + |
| 98 | + return new Result(ResultStatus::success()); |
| 99 | + } |
| 100 | + |
| 101 | + public function close(?int $timeout = null): Result |
| 102 | + { |
| 103 | + return new Result(ResultStatus::success()); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @return Event[] |
| 108 | + */ |
| 109 | + public function getEvents(): array |
| 110 | + { |
| 111 | + return $this->events; |
| 112 | + } |
| 113 | + }; |
| 114 | + $client = ClientBuilder::create([ |
| 115 | + 'enable_logs' => true, |
| 116 | + ])->setTransport($transport) |
| 117 | + ->getClient(); |
| 118 | + |
| 119 | + $hub = new Hub($client); |
| 120 | + SentrySdk::setCurrentHub($hub); |
| 121 | + |
| 122 | + $this->handleLogAndDrop(); |
| 123 | + |
| 124 | + $this->assertCount(1, $transport->getEvents()); |
| 125 | + $this->assertSame('I was dropped :(', $transport->getEvents()[0]->getLogs()[0]->getBody()); |
| 126 | + } |
| 127 | + |
| 128 | + private function handleLogAndDrop(): void |
| 129 | + { |
| 130 | + $handler = new LogsHandler(); |
| 131 | + $handler->handle(RecordFactory::create('I was dropped :(', Logger::INFO, 'chanel.foo', [], [])); |
| 132 | + } |
| 133 | + |
85 | 134 | public static function handleDataProvider(): iterable |
86 | 135 | { |
87 | 136 | yield [ |
|
0 commit comments