Skip to content

Commit

Permalink
Merge pull request #16: support container scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk authored Aug 21, 2024
2 parents 3f25e4f + 5f1d126 commit 57c447d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 19 deletions.
8 changes: 5 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
],
"require": {
"php": ">=8.1",
"spiral/boot": "^3.0",
"spiral/snapshots": "^3.0",
"spiral/boot": "^3.13",
"spiral/core": "^3.13",
"spiral/snapshots": "^3.13",
"sentry/sentry": "^4.0",
"psr/http-factory": "^1.0.1",
"psr/http-message": "^1.0.1 || ^2.0",
Expand All @@ -45,7 +46,8 @@
"require-dev": {
"vimeo/psalm": "^5.25",
"psr/log": "^3.0",
"spiral/testing": "^2.6"
"spiral/testing": "^2.6",
"buggregator/trap": "^1.10"
},
"autoload": {
"psr-4": {
Expand Down
16 changes: 2 additions & 14 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,11 @@
</MixedArgument>
</file>
<file src="src/Client.php">
<MixedArgument>
<code><![CDATA[$event]]></code>
<code><![CDATA[$state->getTags()]]></code>
<code><![CDATA[$state->getVariables()]]></code>
</MixedArgument>
<MixedArgumentTypeCoercion>
<code><![CDATA[$event->getContext()]]></code>
<code><![CDATA[$state->getTags()]]></code>
<code><![CDATA[$state->getVariables()]]></code>
</MixedArgumentTypeCoercion>
<MixedAssignment>
<code><![CDATA[$event]]></code>
<code><![CDATA[$state]]></code>
</MixedAssignment>
<MixedMethodCall>
<code><![CDATA[getLogEvents]]></code>
<code><![CDATA[getTags]]></code>
<code><![CDATA[getVariables]]></code>
</MixedMethodCall>
</file>
<file src="src/Config/SentryConfig.php">
<MixedInferredReturnType>
Expand Down
8 changes: 6 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@
use Sentry\EventId;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Spiral\Core\Attribute\Proxy;
use Spiral\Debug\StateInterface;
use Spiral\Logger\Event\LogEvent;

final class Client
{
public function __construct(
private readonly HubInterface $hub,
private readonly ContainerInterface $container,
#[Proxy] private readonly ContainerInterface $container,
) {
}

public function send(\Throwable $exception): ?EventId
{
if ($this->container->has(StateInterface::class)) {
try {
/** @var StateInterface $state */
$state = $this->container->get(StateInterface::class);

$this->hub->configureScope(function (Scope $scope) use ($state): void {
Expand All @@ -34,6 +36,8 @@ public function send(\Throwable $exception): ?EventId
$scope->addBreadcrumb($this->makeBreadcrumb($event));
}
});
} catch (\Throwable) {
// Do nothing
}

return $this->hub->captureException($exception);
Expand Down
37 changes: 37 additions & 0 deletions tests/Sentry/SentrySnapshotterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Mockery as m;
use Sentry\State\HubInterface;
use Spiral\Core\Container;
use Spiral\Core\Scope;
use Spiral\Debug\State;
use Spiral\Debug\StateInterface;
use Spiral\Sentry\Client;
Expand All @@ -30,4 +31,40 @@ public function testRegister(): void

$this->assertInstanceOf(SnapshotInterface::class, $sentry->register(new \Error('hello world')));
}

public function testScopedState(): void
{
// DTOs
$scope = new \Sentry\State\Scope();
$scopedState = new State();
$scopedState->setTag('foo', 'bar');
$rootState = new State();
$rootState->setTag('root', 'baz');
// Mock Hub
$hub = $this->createMock(HubInterface::class);
$hub->expects($this->once())
->method('configureScope')
->with($this->callback(function (callable $modifier) use ($scope) {
$modifier($scope);
return true;
}));
$hub->expects($this->once())->method('captureException');
// Create container
$container = new Container();
$container->bindSingleton(StateInterface::class, $rootState);
$container->bindSingleton(HubInterface::class, $hub);

$container->runScope(
new Scope(name: 'test', bindings: [
StateInterface::class => $scopedState,
]),
function (SentrySnapshotter $snapshotter) {
$snapshotter->register(new \Error('hello world'));
}
);

$tags = (fn() => $this->tags)->call($scope);
self::assertNotSame(['root' => 'baz'], $tags, 'Root state should not be used');
self::assertSame(['foo' => 'bar'], $tags);
}
}

0 comments on commit 57c447d

Please sign in to comment.