-
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed the problem with mapping sentry events. Additionally, limited t…
…he number of frames in the exception.
- Loading branch information
1 parent
2f0b317
commit c1bcd40
Showing
9 changed files
with
324 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\App\Sentry; | ||
|
||
use Sentry\Event; | ||
use Sentry\EventId; | ||
use Sentry\Serializer\PayloadSerializerInterface; | ||
use Sentry\Transport\Result; | ||
use Sentry\Transport\ResultStatus; | ||
use Sentry\Transport\TransportInterface; | ||
|
||
final class FakeTransport implements TransportInterface | ||
{ | ||
/** @var array<non-empty-string, non-empty-string> */ | ||
private array $events = []; | ||
|
||
public function __construct( | ||
private readonly PayloadSerializerInterface $payloadSerializer, | ||
) { | ||
} | ||
|
||
public function send(Event $event): Result | ||
{ | ||
$this->events[(string)$event->getId()] = $this->payloadSerializer->serialize($event); | ||
|
||
return new Result(ResultStatus::success(), $event); | ||
} | ||
|
||
public function close(?int $timeout = null): Result | ||
{ | ||
return new Result(ResultStatus::success()); | ||
} | ||
|
||
public function findEvent(EventId $id): string | ||
{ | ||
return $this->events[(string)$id] ?? throw new \InvalidArgumentException('Event not found'); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
tests/Feature/Interfaces/Http/Sentry/SentryControllerTestCase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Feature\Interfaces\Http\Sentry; | ||
|
||
use Sentry\Client; | ||
use Sentry\Options; | ||
use Sentry\Serializer\PayloadSerializer; | ||
use Sentry\Severity; | ||
use Sentry\State\Scope; | ||
use Tests\App\Sentry\FakeTransport; | ||
use Tests\Feature\Interfaces\Http\ControllerTestCase; | ||
|
||
abstract class SentryControllerTestCase extends ControllerTestCase | ||
{ | ||
public function getClient(): Client | ||
{ | ||
$options = new Options(); | ||
return new Client($options, new FakeTransport(new PayloadSerializer($options))); | ||
} | ||
|
||
public function makeEventPayload(string $message, ?Severity $level = null): string | ||
{ | ||
$client = $this->getClient(); | ||
|
||
$level = $level ?? Severity::info(); | ||
|
||
$scope = new Scope(); | ||
|
||
$eventId = $client->captureMessage(message: $message, level: $level, scope: $scope); | ||
|
||
return $client->getTransport()->findEvent($eventId); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
tests/Feature/Interfaces/Http/Sentry/SentryEventActionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Feature\Interfaces\Http\Sentry; | ||
|
||
use App\Application\Broadcasting\Channel\EventsChannel; | ||
use Modules\Projects\Domain\Project; | ||
use Modules\Projects\Domain\ValueObject\Key; | ||
use Nyholm\Psr7\Stream; | ||
use Tests\App\Http\ResponseAssertions; | ||
|
||
final class SentryEventActionTest extends SentryControllerTestCase | ||
{ | ||
private Project $project; | ||
|
||
protected function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->project = $this->createProject('default'); | ||
} | ||
|
||
public function testSendWithoutGzip(): void | ||
{ | ||
$payload = $this->makeEventPayload('Hello world'); | ||
$this->makeRequest(payload: $payload, project: $this->project->getKey())->assertOk(); | ||
|
||
$this->broadcastig->assertPushed(new EventsChannel($this->project->getKey()), function (array $data) { | ||
$this->assertSame('event.received', $data['event']); | ||
$this->assertSame('sentry', $data['data']['type']); | ||
$this->assertSame('default', $data['data']['project']); | ||
|
||
$this->assertSame('production', $data['data']['payload']['environment']); | ||
$this->assertSame('Hello world', $data['data']['payload']['message']); | ||
$this->assertSame('info', $data['data']['payload']['level']); | ||
|
||
$this->assertNotEmpty($data['data']['uuid']); | ||
$this->assertNotEmpty($data['data']['timestamp']); | ||
|
||
return true; | ||
}); | ||
} | ||
|
||
private function makeRequest( | ||
string $payload, | ||
string $secret = 'secret', | ||
string|Key $project = 'default', | ||
): ResponseAssertions { | ||
return $this->http | ||
->postJson( | ||
uri: '/api/' . $project . '/envelope/', | ||
data: Stream::create($payload), | ||
headers: [ | ||
'X-Buggregator-Event' => 'sentry', | ||
'Content-Type' => 'application/x-sentry-envelope', | ||
'X-Sentry-Auth' => 'Sentry sentry_version=7, sentry_client=sentry.php/4.0.1, sentry_key=' . $secret, | ||
], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters