Skip to content

Commit b255a6c

Browse files
committed
feat(scopes): update tests to not use Hub
1 parent 539bb96 commit b255a6c

18 files changed

Lines changed: 85 additions & 109 deletions

tests/FunctionsTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ public function testInit(): void
5858
{
5959
init(['default_integrations' => false]);
6060

61-
$this->assertNotNull(SentrySdk::getCurrentHub()->getClient());
62-
$this->assertSame(SentrySdk::getCurrentHub()->getClient(), SentrySdk::getClient());
61+
$client = SentrySdk::getClient();
62+
63+
$this->assertNotInstanceOf(NoOpClient::class, $client);
64+
$this->assertSame($client, SentrySdk::getGlobalScope()->getClient());
6365
}
6466

6567
public function testInitPreservesGlobalScope(): void
@@ -70,7 +72,7 @@ public function testInitPreservesGlobalScope(): void
7072
init(['default_integrations' => false]);
7173

7274
$this->assertSame($globalScope, SentrySdk::getGlobalScope());
73-
$this->assertSame(SentrySdk::getCurrentHub()->getClient(), $globalScope->getClient());
75+
$this->assertSame(SentrySdk::getClient(), $globalScope->getClient());
7476

7577
$event = $globalScope->applyToEvent(Event::createEvent());
7678

@@ -655,7 +657,7 @@ public function testWithContextAlwaysEndsContextWithOptionalTimeout(): void
655657
->with(13)
656658
->willReturn(new Result(ResultStatus::success()));
657659

658-
SentrySdk::init()->bindClient($client);
660+
SentrySdk::init($client);
659661

660662
try {
661663
withContext(static function (): void {

tests/Integration/EnvironmentIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testInvoke(bool $isIntegrationEnabled, ?RuntimeContext $initialR
3333
->method('getIntegration')
3434
->willReturn($isIntegrationEnabled ? $integration : null);
3535

36-
SentrySdk::getCurrentHub()->bindClient($client);
36+
SentrySdk::init($client);
3737

3838
withScope(function (Scope $scope) use ($expectedRuntimeContext, $expectedOsContext, $initialRuntimeContext, $initialOsContext): void {
3939
$event = Event::createEvent();

tests/Integration/FrameContextifierIntegrationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testInvoke(string $fixtureFilePath, int $lineNumber, int $contex
4040
->method('getOptions')
4141
->willReturn($options);
4242

43-
SentrySdk::getCurrentHub()->bindClient($client);
43+
SentrySdk::init($client);
4444

4545
$stacktrace = new Stacktrace([
4646
new Frame('[unknown]', $fixtureFilePath, $lineNumber, null, $fixtureFilePath),
@@ -133,7 +133,7 @@ public function testInvokeLogsWarningMessageIfSourceCodeExcerptCannotBeRetrieved
133133
->method('getOptions')
134134
->willReturn($options);
135135

136-
SentrySdk::getCurrentHub()->bindClient($client);
136+
SentrySdk::init($client);
137137

138138
$stacktrace = new Stacktrace([
139139
new Frame(null, '[internal]', 0),

tests/Integration/ModulesIntegrationTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testInvoke(bool $isIntegrationEnabled, bool $expectedEmptyModule
3434
->method('getIntegration')
3535
->willReturn($isIntegrationEnabled ? $integration : null);
3636

37-
SentrySdk::getCurrentHub()->bindClient($client);
37+
SentrySdk::init($client);
3838

3939
withScope(function (Scope $scope) use ($expectedEmptyModules): void {
4040
$event = $scope->applyToEvent(Event::createEvent());
@@ -86,7 +86,7 @@ public function testModuleIntegration(): void
8686
->setTransport($transport)
8787
->getClient();
8888

89-
SentrySdk::getCurrentHub()->bindClient($client);
89+
SentrySdk::init($client);
9090

9191
$client->captureEvent(Event::createEvent(), null, new Scope());
9292
}

tests/Integration/OTLPIntegrationTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
use Sentry\Integration\OTLPIntegration;
2020
use Sentry\Options;
2121
use Sentry\SentrySdk;
22-
use Sentry\State\Hub;
2322
use Sentry\State\Scope;
2423
use Sentry\Tests\Fixtures\OpenTelemetry\StubOtelHttpClient;
2524
use Sentry\Tests\Fixtures\OpenTelemetry\TestClientDiscoverer;
@@ -115,7 +114,7 @@ public function testSetupOnceRegistersExternalPropagationContext(): void
115114
->willReturn($integration);
116115

117116
try {
118-
SentrySdk::setCurrentHub(new Hub($client));
117+
SentrySdk::init($client);
119118

120119
$this->assertSame([
121120
'trace_id' => '771a43a4192642f0b136d5159a501700',
@@ -146,7 +145,7 @@ public function testExternalPropagationContextIsIgnoredWhenCurrentClientDoesNotH
146145
->willReturn(null);
147146

148147
try {
149-
SentrySdk::setCurrentHub(new Hub($client));
148+
SentrySdk::init($client);
150149

151150
$this->assertNull(Scope::getExternalPropagationContext());
152151
} finally {

tests/Integration/RequestIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testInvoke(array $options, ServerRequestInterface $request, arra
4444
->method('getOptions')
4545
->willReturn(new Options($options));
4646

47-
SentrySdk::getCurrentHub()->bindClient($client);
47+
SentrySdk::init($client);
4848

4949
withScope(function (Scope $scope) use ($event, $expectedRequestContextData, $expectedUser): void {
5050
$event = $scope->applyToEvent($event);

tests/Integration/TransactionIntegrationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function testSetupOnce(Event $event, bool $isIntegrationEnabled, ?EventHi
3535
->method('getIntegration')
3636
->willReturn($isIntegrationEnabled ? $integration : null);
3737

38-
SentrySdk::getCurrentHub()->bindClient($client);
38+
SentrySdk::init($client);
3939

4040
withScope(function (Scope $scope) use ($event, $hint, $expectedTransaction): void {
4141
$event = $scope->applyToEvent($event, $hint);

tests/Logs/LogsAggregatorTest.php

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Sentry\Logs\LogsAggregator;
1414
use Sentry\Options;
1515
use Sentry\SentrySdk;
16-
use Sentry\State\Hub;
1716
use Sentry\State\Scope;
1817
use Sentry\Tests\StubTransport;
1918
use Sentry\Tracing\PropagationContext;
@@ -38,8 +37,7 @@ public function testAttributes(array $attributes, array $expected): void
3837
'enable_logs' => true,
3938
])->getClient();
4039

41-
$hub = new Hub($client);
42-
SentrySdk::setCurrentHub($hub);
40+
SentrySdk::init($client);
4341

4442
$aggregator = new LogsAggregator();
4543

@@ -96,8 +94,7 @@ public function testMessageFormatting(string $message, array $values, string $ex
9694
'enable_logs' => true,
9795
])->getClient();
9896

99-
$hub = new Hub($client);
100-
SentrySdk::setCurrentHub($hub);
97+
SentrySdk::init($client);
10198

10299
$aggregator = new LogsAggregator();
103100

@@ -173,21 +170,19 @@ public function testAttributesAreAddedToLogMessage(): void
173170
'server_name' => 'web-server-01',
174171
])->getClient();
175172

176-
SentrySdk::setCurrentHub(new Hub($client));
173+
SentrySdk::init($client);
177174

178-
SentrySdk::getCurrentHub()->configureScope(static function (Scope $scope) {
179-
$userDataBag = new UserDataBag();
180-
$userDataBag->setId('unique_id');
181-
$userDataBag->setEmail('foo@example.com');
182-
$userDataBag->setUsername('my_user');
183-
$scope->setUser($userDataBag);
184-
});
175+
$userDataBag = new UserDataBag();
176+
$userDataBag->setId('unique_id');
177+
$userDataBag->setEmail('foo@example.com');
178+
$userDataBag->setUsername('my_user');
179+
SentrySdk::getIsolationScope()->setUser($userDataBag);
185180

186181
$spanContext = new SpanContext();
187182
$spanContext->setTraceId(new TraceId('566e3688a61d4bc888951642d6f14a19'));
188183
$spanContext->setSpanId(new SpanId('566e3688a61d4bc8'));
189184
$span = new Span($spanContext);
190-
SentrySdk::getCurrentHub()->setSpan($span);
185+
SentrySdk::getIsolationScope()->setSpan($span);
191186

192187
$aggregator = new LogsAggregator();
193188

@@ -219,7 +214,7 @@ public function testMergedScopeAttributesAreAddedToLogMessage(): void
219214
'enable_logs' => true,
220215
])->getClient();
221216

222-
SentrySdk::getGlobalScope()->setClient($client);
217+
SentrySdk::init($client);
223218
SentrySdk::getGlobalScope()->setUser(UserDataBag::createFromUserIdentifier('global-user'));
224219

225220
$spanContext = new SpanContext();
@@ -245,15 +240,13 @@ public function testUserAttributesCanBeSetManuallyWithDefaultPiiOff(): void
245240
'send_default_pii' => false,
246241
])->getClient();
247242

248-
SentrySdk::setCurrentHub(new Hub($client));
243+
SentrySdk::init($client);
249244

250-
SentrySdk::getCurrentHub()->configureScope(static function (Scope $scope) {
251-
$userDataBag = new UserDataBag();
252-
$userDataBag->setId('unique_id');
253-
$userDataBag->setEmail('foo@example.com');
254-
$userDataBag->setUsername('my_user');
255-
$scope->setUser($userDataBag);
256-
});
245+
$userDataBag = new UserDataBag();
246+
$userDataBag->setId('unique_id');
247+
$userDataBag->setEmail('foo@example.com');
248+
$userDataBag->setUsername('my_user');
249+
SentrySdk::getIsolationScope()->setUser($userDataBag);
257250

258251
$aggregator = new LogsAggregator();
259252
$aggregator->add(LogLevel::info(), 'User performed action');
@@ -278,8 +271,7 @@ public function testFlushesImmediatelyWhenThresholdIsReached(): void
278271
'log_flush_threshold' => 2,
279272
])->setTransport($transport)->getClient();
280273

281-
$hub = new Hub($client);
282-
SentrySdk::setCurrentHub($hub);
274+
SentrySdk::init($client);
283275

284276
$aggregator = new LogsAggregator();
285277

@@ -312,7 +304,7 @@ public function testFlushCapturesLogsWithProvidedClient(): void
312304
]));
313305
$fallbackClient->expects($this->never())
314306
->method('captureEvent');
315-
SentrySdk::setCurrentHub(new Hub($fallbackClient));
307+
SentrySdk::init($fallbackClient);
316308

317309
$aggregator = new LogsAggregator();
318310
$aggregator->add(LogLevel::info(), 'Test message');
@@ -340,8 +332,7 @@ public function testDoesNotFlushImmediatelyWhenThresholdIsNull(): void
340332
'log_flush_threshold' => null,
341333
])->setTransport($transport)->getClient();
342334

343-
$hub = new Hub($client);
344-
SentrySdk::setCurrentHub($hub);
335+
SentrySdk::init($client);
345336

346337
$aggregator = new LogsAggregator();
347338

@@ -362,7 +353,7 @@ public function testDoesNotUsePropagationContextSpanIdAsParentSpanIdWhenNoLocalS
362353
$propagationContext->setTraceId(new TraceId('771a43a4192642f0b136d5159a501700'));
363354
$propagationContext->setSpanId(new SpanId('1234567890abcdef'));
364355

365-
SentrySdk::setCurrentHub(new Hub($client));
356+
SentrySdk::init($client);
366357
SentrySdk::getCurrentRuntimeContext()->setIsolationScope(new Scope($propagationContext));
367358

368359
$aggregator = new LogsAggregator();
@@ -384,8 +375,7 @@ public function testUsesExternalPropagationContextWhenNoLocalSpanExists(): void
384375
'enable_logs' => true,
385376
])->getClient();
386377

387-
$hub = new Hub($client);
388-
SentrySdk::setCurrentHub($hub);
378+
SentrySdk::init($client);
389379

390380
Scope::registerExternalPropagationContext(static function (): array {
391381
return [

tests/Logs/LogsTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use Sentry\Logs\LogLevel;
1414
use Sentry\Options;
1515
use Sentry\SentrySdk;
16-
use Sentry\State\Hub;
1716
use Sentry\Transport\Result;
1817
use Sentry\Transport\ResultStatus;
1918
use Sentry\Transport\TransportInterface;
@@ -36,8 +35,7 @@ public function testLogNotSentWhenDisabled(): void
3635
$client->expects($this->never())
3736
->method('captureEvent');
3837

39-
$hub = new Hub($client);
40-
SentrySdk::setCurrentHub($hub);
38+
SentrySdk::init($client);
4139

4240
logger()->info('Some info message');
4341

@@ -186,8 +184,7 @@ private function assertEvent(callable $assert, array $options = []): ClientInter
186184

187185
$client = ClientBuilder::create($clientOptions)->setTransport($transport)->getClient();
188186

189-
$hub = new Hub($client);
190-
SentrySdk::setCurrentHub($hub);
187+
SentrySdk::init($client);
191188

192189
return $client;
193190
}

tests/Metrics/TraceMetricsTest.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
use Sentry\Metrics\Types\Metric;
1616
use Sentry\Options;
1717
use Sentry\SentrySdk;
18-
use Sentry\State\Hub;
19-
use Sentry\State\HubAdapter;
2018
use Sentry\State\Scope;
2119
use Sentry\UserDataBag;
2220

@@ -26,7 +24,7 @@ final class TraceMetricsTest extends TestCase
2624
{
2725
protected function setUp(): void
2826
{
29-
HubAdapter::getInstance()->bindClient(new Client(new Options(), StubTransport::getInstance()));
27+
SentrySdk::init(new Client(new Options(), StubTransport::getInstance()));
3028
StubTransport::$events = [];
3129
}
3230

@@ -80,7 +78,7 @@ public function testDistributionMetrics(): void
8078

8179
public function testFlushesImmediatelyWhenMetricFlushThresholdIsReached(): void
8280
{
83-
HubAdapter::getInstance()->bindClient(new Client(new Options([
81+
SentrySdk::init(new Client(new Options([
8482
'metric_flush_threshold' => 2,
8583
]), StubTransport::getInstance()));
8684

@@ -100,7 +98,7 @@ public function testFlushesImmediatelyWhenMetricFlushThresholdIsReached(): void
10098

10199
public function testDoesNotFlushImmediatelyWhenMetricFlushThresholdIsNull(): void
102100
{
103-
HubAdapter::getInstance()->bindClient(new Client(new Options([
101+
SentrySdk::init(new Client(new Options([
104102
'metric_flush_threshold' => null,
105103
]), StubTransport::getInstance()));
106104

@@ -143,7 +141,7 @@ public function testFlushCapturesMetricsWithProvidedClient(): void
143141
]));
144142
$fallbackClient->expects($this->never())
145143
->method('captureEvent');
146-
SentrySdk::setCurrentHub(new Hub($fallbackClient));
144+
SentrySdk::init($fallbackClient);
147145

148146
$aggregator = new MetricsAggregator();
149147
$aggregator->add(CounterMetric::TYPE, 'test-count', 2, ['foo' => 'bar'], null);
@@ -163,7 +161,7 @@ public function testFlushCapturesMetricsWithProvidedClient(): void
163161

164162
public function testMetricsBufferFullWhenMetricFlushThresholdIsNull(): void
165163
{
166-
HubAdapter::getInstance()->bindClient(new Client(new Options([
164+
SentrySdk::init(new Client(new Options([
167165
'metric_flush_threshold' => null,
168166
]), StubTransport::getInstance()));
169167

@@ -182,7 +180,7 @@ public function testMetricsBufferFullWhenMetricFlushThresholdIsNull(): void
182180

183181
public function testEnableMetrics(): void
184182
{
185-
HubAdapter::getInstance()->bindClient(new Client(new Options([
183+
SentrySdk::init(new Client(new Options([
186184
'enable_metrics' => false,
187185
]), StubTransport::getInstance()));
188186

@@ -194,7 +192,7 @@ public function testEnableMetrics(): void
194192

195193
public function testBeforeSendMetricAltersContent(): void
196194
{
197-
HubAdapter::getInstance()->bindClient(new Client(new Options([
195+
SentrySdk::init(new Client(new Options([
198196
'before_send_metric' => static function (Metric $metric) {
199197
$metric->setValue(99999);
200198

0 commit comments

Comments
 (0)