From a93e9bd7457201322b00392c4bb96eb289855cb2 Mon Sep 17 00:00:00 2001 From: Benoit Galati Date: Sat, 3 Feb 2024 13:30:48 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Support=20Sentry=20V4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/ci.yml | 1 + composer.json | 2 +- tests/SentryHandlerTest.php | 140 ++++++++++++++++++++++++------------ 3 files changed, 98 insertions(+), 45 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index fc030b2..9cb1ae7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -24,6 +24,7 @@ jobs: - php: 8.1 # should use monolog v3 - php: 8.2 # should use monolog v3 - php: 8.3 # should use monolog v3 + # TODO matrix should support Both versions of Sentry SDK steps: - uses: actions/checkout@v2 diff --git a/composer.json b/composer.json index 06d6576..4c5a542 100644 --- a/composer.json +++ b/composer.json @@ -37,7 +37,7 @@ "require": { "php": "^7.4 || ^8.0", "monolog/monolog": "^1.6 || ^2.0 || ^3.0", - "sentry/sentry": "^3.1" + "sentry/sentry": "^3.1 || ^4.0" }, "require-dev": { "coduo/php-matcher": "^6.0.8", diff --git a/tests/SentryHandlerTest.php b/tests/SentryHandlerTest.php index 27e6854..ffbcee0 100644 --- a/tests/SentryHandlerTest.php +++ b/tests/SentryHandlerTest.php @@ -14,6 +14,7 @@ use Monolog\LogRecord; use PHPUnit\Framework\TestCase; use Sentry\Breadcrumb; +use Sentry\Client; use Sentry\ClientBuilder; use Sentry\Event; use Sentry\Integration\EnvironmentIntegration; @@ -24,6 +25,8 @@ use Sentry\Severity; use Sentry\State\HubInterface; use Sentry\State\Scope; +use Sentry\Transport\Result; +use Sentry\Transport\ResultStatus; use Sentry\Transport\TransportFactoryInterface; use Sentry\Transport\TransportInterface; @@ -47,7 +50,12 @@ protected function setUp(): void ], ] ); - $clientBuilder->setTransportFactory(new FakeTransportFactory($this->transport)); + + if (defined(Client::class.'::SDK_VERSION') && version_compare(Client::SDK_VERSION, '4.0.0') >= 0) { + $clientBuilder->setTransport($this->transport); + } else { + $clientBuilder->setTransportFactory(new FakeTransportFactory($this->transport)); + } $client = $clientBuilder->getClient(); @@ -492,7 +500,7 @@ private function assertCapturedEvent(Severity $severity, string $message, array if ($breadcrumbs) { $this->assertMatchesPattern( - json_encode($breadcrumbs), + json_encode($breadcrumbs, JSON_THROW_ON_ERROR), json_encode( array_map( static function (Breadcrumb $breadcrumb) { @@ -570,62 +578,106 @@ public function resetSpy(): void } } -class SpyTransport implements TransportInterface -{ - /** - * @var Event|null - */ - public $spiedEvent; +if (defined(Client::class.'::SDK_VERSION') && version_compare(Client::SDK_VERSION, '4.0.0') >= 0) { + class SpyTransport implements TransportInterface + { + /** + * @var Event|null + */ + public $spiedEvent; - /** - * @var bool - */ - public $isFlushed = false; + /** + * @var bool + */ + public $isFlushed = false; - public function send(Event $event): PromiseInterface - { - $this->spiedEvent = $event; + public function send(Event $event): Result + { + $this->spiedEvent = $event; - return new FulfilledPromise(new Response(ResponseStatus::skipped(), $event)); - } + return new Result(ResultStatus::skipped(), $event); + } - public function resetSpy(): void - { - $this->spiedEvent = null; - $this->isFlushed = false; - } + public function resetSpy(): void + { + $this->spiedEvent = null; + $this->isFlushed = false; + } - public function getSpiedEvent(): Event - { - if (null === $this->spiedEvent) { - throw new \RuntimeException('No spied scope'); + public function getSpiedEvent(): Event + { + if (null === $this->spiedEvent) { + throw new \RuntimeException('No spied scope'); + } + + return $this->spiedEvent; } - return $this->spiedEvent; - } + public function close(?int $timeout = null): Result + { + $this->isFlushed = true; - public function close(?int $timeout = null): PromiseInterface + return new Result(ResultStatus::success()); + } + } +} else { + class SpyTransport implements TransportInterface { - $this->isFlushed = true; + /** + * @var Event|null + */ + public $spiedEvent; - return new FulfilledPromise(true); - } -} + /** + * @var bool + */ + public $isFlushed = false; -class FakeTransportFactory implements TransportFactoryInterface -{ - /** - * @var SpyTransport - */ - private $transport; + public function send(Event $event): PromiseInterface + { + $this->spiedEvent = $event; - public function __construct(SpyTransport $transport) - { - $this->transport = $transport; + return new FulfilledPromise(new Response(ResponseStatus::skipped(), $event)); + } + + public function resetSpy(): void + { + $this->spiedEvent = null; + $this->isFlushed = false; + } + + public function getSpiedEvent(): Event + { + if (null === $this->spiedEvent) { + throw new \RuntimeException('No spied scope'); + } + + return $this->spiedEvent; + } + + public function close(?int $timeout = null): PromiseInterface + { + $this->isFlushed = true; + + return new FulfilledPromise(true); + } } - public function create(Options $options): TransportInterface + class FakeTransportFactory implements TransportFactoryInterface { - return $this->transport; + /** + * @var SpyTransport + */ + private $transport; + + public function __construct(SpyTransport $transport) + { + $this->transport = $transport; + } + + public function create(Options $options): TransportInterface + { + return $this->transport; + } } }