Skip to content

Commit

Permalink
Fix Sentry test
Browse files Browse the repository at this point in the history
  • Loading branch information
Baldinof committed Apr 29, 2024
1 parent e3de097 commit e839660
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/Integration/Sentry/SentryListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Baldinof\RoadRunnerBundle\Event\WorkerExceptionEvent;
use Baldinof\RoadRunnerBundle\Event\WorkerStopEvent;
use GuzzleHttp\Promise\PromiseInterface;
use Sentry\State\HubInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

Expand All @@ -18,7 +19,7 @@ public function __construct(private HubInterface $hub)
public function onWorkerStop(WorkerStopEvent $event): void
{
$result = $this->hub->getClient()?->flush();
if ($result instanceof \GuzzleHttp\Promise\PromiseInterface) {
if (class_exists(PromiseInterface::class) && $result instanceof PromiseInterface) {
$result->wait(false);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Integration/Sentry/SentryMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Baldinof\RoadRunnerBundle\Integration\Sentry;

use Baldinof\RoadRunnerBundle\Http\MiddlewareInterface;
use GuzzleHttp\Promise\PromiseInterface;
use Sentry\State\HubInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand All @@ -26,7 +27,7 @@ public function process(Request $request, HttpKernelInterface $next): \Iterator
yield $next->handle($request);
} finally {
$result = $this->hub->getClient()?->flush();
if ($result instanceof \GuzzleHttp\Promise\PromiseInterface) {
if (class_exists(PromiseInterface::class) && $result instanceof PromiseInterface) {
$result->wait(false);
}

Expand Down
30 changes: 27 additions & 3 deletions tests/Integration/Sentry/SentryMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
use GuzzleHttp\Promise\PromiseInterface;
use PHPUnit\Framework\TestCase;
use Sentry\Breadcrumb;
use Sentry\Client;
use Sentry\ClientBuilder;
use Sentry\Event;
use Sentry\Options;
use Sentry\SentrySdk;
use Sentry\State\Hub;
use Sentry\Transport\Result;
use Sentry\Transport\ResultStatus;
use Sentry\Transport\TransportFactoryInterface;
use Sentry\Transport\TransportInterface;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -49,9 +52,13 @@ public function initHub(array $options): void

$opts = new Options(array_merge($options, ['default_integrations' => true]));

$client = (new ClientBuilder($opts))
->setTransportFactory($this->getTransportFactoryMock())
->getClient();
$builder = new ClientBuilder($opts);
if (version_compare(Client::SDK_VERSION, '4.0.0', '<')) {
$builder->setTransportFactory($this->getTransportFactoryMock());
} else {
$builder->setTransport($this->getV4TransportMock());
}
$client = $builder->getClient();

$hub = new Hub($client);

Expand Down Expand Up @@ -115,4 +122,21 @@ public function close(?int $timeout = null): PromiseInterface
}
};
}

private function getV4TransportMock(): TransportInterface
{
return new class() implements TransportInterface {
public function send(Event $event): Result
{
SentryMiddlewareTest::$collectedEvents->push($event);

return new Result(ResultStatus::success(), $event);
}

public function close(?int $timeout = null): Result
{
return new Result(ResultStatus::success());
}
};
}
}

0 comments on commit e839660

Please sign in to comment.