Skip to content

Commit aa39a68

Browse files
committed
Switch from Hub::getScope to configureScope
1 parent 57333d9 commit aa39a68

File tree

4 files changed

+32
-13
lines changed

4 files changed

+32
-13
lines changed

src/EventListener/ConsoleListener.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Sentry\State\Hub;
66
use Sentry\State\HubInterface;
7+
use Sentry\State\Scope;
78
use Symfony\Component\Console\Event\ConsoleCommandEvent;
89

910
final class ConsoleListener
@@ -33,7 +34,8 @@ public function onConsoleCommand(ConsoleCommandEvent $event): void
3334
$command = $event->getCommand();
3435

3536
Hub::getCurrent()
36-
->getScope()
37-
->setTag('command', $command ? $command->getName() : 'N/A');
37+
->configureScope(function (Scope $scope) use ($command): void {
38+
$scope->setTag('command', $command ? $command->getName() : 'N/A');
39+
});
3840
}
3941
}

src/EventListener/RequestListener.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Sentry\State\Hub;
66
use Sentry\State\HubInterface;
7+
use Sentry\State\Scope;
78
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
89
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
910
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
@@ -71,8 +72,9 @@ public function onKernelRequest(GetResponseEvent $event): void
7172
$userData['ip_address'] = $event->getRequest()->getClientIp();
7273

7374
Hub::getCurrent()
74-
->getScope()
75-
->setUser($userData);
75+
->configureScope(function (Scope $scope) use ($userData): void {
76+
$scope->setUser($userData);
77+
});
7678
}
7779

7880
public function onKernelController(FilterControllerEvent $event): void
@@ -84,8 +86,9 @@ public function onKernelController(FilterControllerEvent $event): void
8486
$matchedRoute = $event->getRequest()->attributes->get('_route');
8587

8688
Hub::getCurrent()
87-
->getScope()
88-
->setTag('route', $matchedRoute);
89+
->configureScope(function (Scope $scope) use ($matchedRoute): void {
90+
$scope->setTag('route', $matchedRoute);
91+
});
8992
}
9093

9194
/**

test/EventListener/ConsoleListenerTest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Sentry\SentryBundle\Test\EventListener;
44

5+
use PHPUnit\Framework\Assert;
6+
use Prophecy\Argument;
57
use Sentry\SentryBundle\EventListener\ConsoleListener;
68
use Sentry\State\Hub;
79
use Sentry\State\HubInterface;
@@ -18,11 +20,16 @@ protected function setUp()
1820
{
1921
parent::setUp();
2022

21-
$this->currentScope = new Scope();
23+
$this->currentScope = $scope = new Scope();
2224
$this->currentHub = $this->prophesize(HubInterface::class);
23-
$this->currentHub->getScope()
25+
$this->currentHub->configureScope(Argument::type('callable'))
2426
->shouldBeCalled()
25-
->willReturn($this->currentScope);
27+
->will(function ($arguments) use ($scope): void {
28+
$callable = $arguments[0];
29+
Assert::assertIsCallable($callable);
30+
31+
$callable($scope);
32+
});
2633

2734
Hub::setCurrent($this->currentHub->reveal());
2835
}

test/EventListener/RequestListenerTest.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Sentry\SentryBundle\Test\EventListener;
44

5+
use PHPUnit\Framework\Assert;
56
use PHPUnit\Framework\TestCase;
7+
use Prophecy\Argument;
68
use Sentry\SentryBundle\EventListener\RequestListener;
79
use Sentry\State\Hub;
810
use Sentry\State\HubInterface;
@@ -25,11 +27,16 @@ protected function setUp()
2527
{
2628
parent::setUp();
2729

28-
$this->currentScope = new Scope();
30+
$this->currentScope = $scope = new Scope();
2931
$this->currentHub = $this->prophesize(HubInterface::class);
30-
$this->currentHub->getScope()
32+
$this->currentHub->configureScope(Argument::type('callable'))
3133
->shouldBeCalled()
32-
->willReturn($this->currentScope);
34+
->will(function ($arguments) use ($scope): void {
35+
$callable = $arguments[0];
36+
Assert::assertIsCallable($callable);
37+
38+
$callable($scope);
39+
});
3340

3441
Hub::setCurrent($this->currentHub->reveal());
3542
}
@@ -289,7 +296,7 @@ public function testOnKernelControllerAddsRouteTag(): void
289296

290297
public function testOnKernelRequestUserDataAndTagsAreNotSetInSubRequest(): void
291298
{
292-
$this->currentHub->getScope()
299+
$this->currentHub->configureScope(Argument::type('callable'))
293300
->shouldNotBeCalled();
294301

295302
$tokenStorage = $this->prophesize(TokenStorageInterface::class);

0 commit comments

Comments
 (0)