From a52852d9fa09368c97639ed98ba3f3865b1dd3c9 Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Mon, 12 Jun 2023 12:29:36 +0300 Subject: [PATCH] Add existence StateInterface check --- src/Client.php | 6 +++--- tests/Sentry/ClientTest.php | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Client.php b/src/Client.php index 7d985e6..65bae44 100644 --- a/src/Client.php +++ b/src/Client.php @@ -23,11 +23,11 @@ public function __construct( public function send(\Throwable $exception): ?EventId { - $state = $this->container->get(StateInterface::class); - $scope = new Scope(); - if ($state !== null) { + if ($this->container->has(StateInterface::class)) { + $state = $this->container->get(StateInterface::class); + $scope->setTags($state->getTags()); $scope->setExtras($state->getVariables()); diff --git a/tests/Sentry/ClientTest.php b/tests/Sentry/ClientTest.php index 60aed61..69cebef 100644 --- a/tests/Sentry/ClientTest.php +++ b/tests/Sentry/ClientTest.php @@ -36,4 +36,25 @@ public function testSend(): void $this->assertSame($eventId, $mainClient->send($errorException)); } + + public function testSendWithoutState(): void + { + $mainClient = new Client( + $client = m::mock(ClientInterface::class), + new Container() + ); + + $errorException = new \ErrorException('Test exception'); + + $client->shouldReceive('captureException') + ->once() + ->withArgs(function (\Throwable $exception) use ($errorException) { + return $errorException === $exception; + }) + ->andReturn( + $eventId = new EventId('c8c46e00bf53942206fd2ad9546daac2') + ); + + $this->assertSame($eventId, $mainClient->send($errorException)); + } } \ No newline at end of file