Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Support Sentry V4 #35

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@
"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",
"friendsofphp/php-cs-fixer": "^3.16",
"friendsofphp/php-cs-fixer": "3.51.0",
"jangregor/phpstan-prophecy": "1.0.0",
"phpstan/phpstan": "1.10.14",
"phpstan/phpstan-phpunit": "1.3.11",
"phpstan/phpstan": "1.10.59",
"phpstan/phpstan-phpunit": "1.3.16",
"phpunit/phpunit": "^9.6.7",
"sentry/sdk": "^3.1",
"symfony/phpunit-bridge": "^6.2.10",
"symfony/var-dumper": ">4.3"
}
Expand Down
12 changes: 2 additions & 10 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ parameters:
inferPrivatePropertyTypeFromConstructor: true
tipsOfTheDay: false
checkMissingIterableValueType: false
treatPhpDocTypesAsCertain: false

level: 8
paths: [ src, tests ]
paths: [ src ]

ignoreErrors:
- "#^Cannot assign offset 'formatted' to Monolog\\\\LogRecord\\.$#"
Expand All @@ -20,12 +21,3 @@ parameters:

- message: "#^Parameter \\#1 \\$record of method Monolog\\\\Formatter\\\\LineFormatter\\:\\:format\\(\\) expects Monolog\\\\LogRecord, array\\<string, mixed\\>\\|Monolog\\\\LogRecord given\\.$#"
path: src/SentryHandler.php

- message: "#expects Monolog\\\\LogRecord, array\\<string, mixed\\>\\|Monolog\\\\LogRecord given\\.$#"
path: tests

- message: "#expects array\\<Monolog\\\\LogRecord\\>, array\\<array\\<string, mixed\\>\\|Monolog\\\\LogRecord\\> given\\.$#"
path: tests

- message: "#^PHPDoc tag @param for parameter \\$level with type array\\<int, Monolog\\\\Level\\>\\|int is not subtype of native type int\\|null\\.$#"
path: tests
111 changes: 14 additions & 97 deletions tests/SentryHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,25 @@

namespace BGalati\MonologSentryHandler\Tests;

use BGalati\MonologSentryHandler\SentryHandler;
use Coduo\PHPMatcher\PHPUnit\PHPMatcherAssertions;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Promise\PromiseInterface;
use Monolog\Formatter\LineFormatter;
use Monolog\Level;
use Monolog\Logger;
use Monolog\LogRecord;
use PHPUnit\Framework\TestCase;
use Sentry\Breadcrumb;
use Sentry\Client;
use Sentry\ClientBuilder;
use Sentry\Event;
use Sentry\Integration\EnvironmentIntegration;
use Sentry\Options;
use Sentry\Response;
use Sentry\ResponseStatus;
use Sentry\SentrySdk;
use Sentry\Severity;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Sentry\Transport\TransportFactoryInterface;
use Sentry\Transport\TransportInterface;

if (defined(Client::class.'::SDK_VERSION') && version_compare(Client::SDK_VERSION, '4.0.0') >= 0) {
require __DIR__.'/SentryStubV4.php';
} else {
require __DIR__.'/SentryStubV3.php';
}

final class SentryHandlerTest extends TestCase
{
Expand All @@ -47,7 +44,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();

Expand Down Expand Up @@ -492,7 +494,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) {
Expand Down Expand Up @@ -544,88 +546,3 @@ private function createSentryHandler(?int $level = null): SpySentryHandler
return $handler;
}
}

class SpySentryHandler extends SentryHandler
{
/**
* @var bool
*/
public $afterWriteCalled = false;

protected function processScope(Scope $scope, $record, Event $sentryEvent): void
{
$scope->setExtra('processScope', 'called');
}

protected function afterWrite(): void
{
$this->afterWriteCalled = true;

parent::afterWrite();
}

public function resetSpy(): void
{
$this->afterWriteCalled = false;
}
}

class SpyTransport implements TransportInterface
{
/**
* @var Event|null
*/
public $spiedEvent;

/**
* @var bool
*/
public $isFlushed = false;

public function send(Event $event): PromiseInterface
{
$this->spiedEvent = $event;

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);
}
}

class FakeTransportFactory implements TransportFactoryInterface
{
/**
* @var SpyTransport
*/
private $transport;

public function __construct(SpyTransport $transport)
{
$this->transport = $transport;
}

public function create(Options $options): TransportInterface
{
return $this->transport;
}
}
74 changes: 74 additions & 0 deletions tests/SentryStubV3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace BGalati\MonologSentryHandler\Tests;

use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Promise\PromiseInterface;
use Sentry\Event;
use Sentry\Options;
use Sentry\Response;
use Sentry\ResponseStatus;
use Sentry\Transport\TransportFactoryInterface;
use Sentry\Transport\TransportInterface;

class SpyTransport implements TransportInterface
{
/**
* @var Event|null
*/
public $spiedEvent;

/**
* @var bool
*/
public $isFlushed = false;

public function send(Event $event): PromiseInterface
{
$this->spiedEvent = $event;

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);
}
}

class FakeTransportFactory implements TransportFactoryInterface
{
/**
* @var SpyTransport
*/
private $transport;

public function __construct(SpyTransport $transport)
{
$this->transport = $transport;
}

public function create(Options $options): TransportInterface
{
return $this->transport;
}
}
52 changes: 52 additions & 0 deletions tests/SentryStubV4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace BGalati\MonologSentryHandler\Tests;

use Sentry\Event;
use Sentry\Transport\Result;
use Sentry\Transport\ResultStatus;
use Sentry\Transport\TransportInterface;

class SpyTransport implements TransportInterface
{
/**
* @var Event|null
*/
public $spiedEvent;

/**
* @var bool
*/
public $isFlushed = false;

public function send(Event $event): Result
{
$this->spiedEvent = $event;

return new Result(ResultStatus::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): Result
{
$this->isFlushed = true;

return new Result(ResultStatus::success());
}
}
34 changes: 34 additions & 0 deletions tests/SpySentryHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace BGalati\MonologSentryHandler\Tests;

use BGalati\MonologSentryHandler\SentryHandler;
use Sentry\Event;
use Sentry\State\Scope;

class SpySentryHandler extends SentryHandler
{
/**
* @var bool
*/
public $afterWriteCalled = false;

protected function processScope(Scope $scope, $record, Event $sentryEvent): void
{
$scope->setExtra('processScope', 'called');
}

protected function afterWrite(): void
{
$this->afterWriteCalled = true;

parent::afterWrite();
}

public function resetSpy(): void
{
$this->afterWriteCalled = false;
}
}
Loading