Skip to content

Commit 2891197

Browse files
authored
Fix tests (#920)
1 parent b4dc2ab commit 2891197

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"php": "^7.2||^8.0",
1616
"guzzlehttp/psr7": "^2.1.1",
1717
"jean85/pretty-package-versions": "^1.5||^2.0",
18-
"sentry/sentry": "^4.10.0",
18+
"sentry/sentry": "^4.11.0",
1919
"symfony/cache-contracts": "^1.1||^2.4||^3.0",
2020
"symfony/config": "^4.4.20||^5.0.11||^6.0||^7.0",
2121
"symfony/console": "^4.4.20||^5.0.11||^6.0||^7.0",

tests/EventListener/TracingConsoleListenerTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public function testHandleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHub
4444
$this->hub->expects($this->once())
4545
->method('startTransaction')
4646
->with($this->callback(function (TransactionContext $context) use ($expectedTransactionContext): bool {
47+
// This value is random when the metadata is constructed, thus we set it to a fixed expected value since we don't care for the value here
48+
$context->getMetadata()->setSampleRand(0.1337);
49+
4750
$this->assertEquals($expectedTransactionContext, $context);
4851

4952
return true;
@@ -73,6 +76,7 @@ public function handleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHubData
7376
$transactionContext->setName('<unnamed command>');
7477
$transactionContext->setOrigin('auto.console');
7578
$transactionContext->setSource(TransactionSource::task());
79+
$transactionContext->getMetadata()->setSampleRand(0.1337);
7680

7781
yield [
7882
new Command(),
@@ -84,6 +88,7 @@ public function handleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHubData
8488
$transactionContext->setName('app:command');
8589
$transactionContext->setOrigin('auto.console');
8690
$transactionContext->setSource(TransactionSource::task());
91+
$transactionContext->getMetadata()->setSampleRand(0.1337);
8792

8893
yield [
8994
new Command('app:command'),

tests/EventListener/TracingRequestListenerTest.php

+17
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public function testHandleKernelRequestEvent(Options $options, Request $request,
6565
$this->hub->expects($this->once())
6666
->method('startTransaction')
6767
->with($this->callback(function (TransactionContext $context) use ($expectedTransactionContext): bool {
68+
// This value is random when the metadata is constructed, thus we set it to a fixed expected value since we don't care for the value here
69+
$context->getMetadata()->setSampleRand(0.1337);
70+
6871
$this->assertEquals($expectedTransactionContext, $context);
6972

7073
return true;
@@ -108,6 +111,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
108111
'net.host.name' => 'www.example.com',
109112
]);
110113
$transactionContext->getMetadata()->setDynamicSamplingContext($samplingContext);
114+
$transactionContext->getMetadata()->setSampleRand(0.1337);
111115

112116
yield 'request.headers.sentry-trace EXISTS' => [
113117
new Options(),
@@ -146,6 +150,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
146150
'net.host.name' => 'www.example.com',
147151
]);
148152
$transactionContext->getMetadata()->setDynamicSamplingContext($samplingContext);
153+
$transactionContext->getMetadata()->setSampleRand(0.1337);
149154

150155
yield 'request.headers.traceparent EXISTS' => [
151156
new Options(),
@@ -184,6 +189,8 @@ public function handleKernelRequestEventDataProvider(): \Generator
184189
'net.host.name' => 'www.example.com',
185190
]);
186191
$transactionContext->getMetadata()->setDynamicSamplingContext($samplingContext);
192+
$transactionContext->getMetadata()->setParentSamplingRate(1.0);
193+
$transactionContext->getMetadata()->setSampleRand(0.1337);
187194

188195
yield 'request.headers.sentry-trace and headers.baggage EXISTS' => [
189196
new Options(),
@@ -216,6 +223,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
216223
'route' => '<unknown>',
217224
'net.host.name' => 'www.example.com',
218225
]);
226+
$transactionContext->getMetadata()->setSampleRand(0.1337);
219227

220228
$request = Request::create('http://www.example.com');
221229
$request->server->remove('REQUEST_TIME_FLOAT');
@@ -240,6 +248,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
240248
'route' => '<unknown>',
241249
'net.host.ip' => '127.0.0.1',
242250
]);
251+
$transactionContext->getMetadata()->setSampleRand(0.1337);
243252

244253
yield 'request.server.HOST IS IPV4' => [
245254
new Options(),
@@ -272,6 +281,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
272281
'route' => 'app_homepage',
273282
'net.host.name' => 'www.example.com',
274283
]);
284+
$transactionContext->getMetadata()->setSampleRand(0.1337);
275285

276286
yield 'request.attributes.route IS STRING' => [
277287
new Options(),
@@ -297,6 +307,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
297307
'route' => '/path',
298308
'net.host.name' => 'www.example.com',
299309
]);
310+
$transactionContext->getMetadata()->setSampleRand(0.1337);
300311

301312
yield 'request.attributes.route IS INSTANCEOF Symfony\Component\Routing\Route' => [
302313
new Options(),
@@ -322,6 +333,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
322333
'route' => 'App\\Controller::indexAction',
323334
'net.host.name' => 'www.example.com',
324335
]);
336+
$transactionContext->getMetadata()->setSampleRand(0.1337);
325337

326338
yield 'request.attributes._controller IS STRING' => [
327339
new Options(),
@@ -347,6 +359,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
347359
'route' => 'App\\Controller::indexAction',
348360
'net.host.name' => 'www.example.com',
349361
]);
362+
$transactionContext->getMetadata()->setSampleRand(0.1337);
350363

351364
yield 'request.attributes._controller IS CALLABLE (1)' => [
352365
new Options(),
@@ -372,6 +385,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
372385
'route' => 'class@anonymous::indexAction',
373386
'net.host.name' => 'www.example.com',
374387
]);
388+
$transactionContext->getMetadata()->setSampleRand(0.1337);
375389

376390
yield 'request.attributes._controller IS CALLABLE (2)' => [
377391
new Options(),
@@ -397,6 +411,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
397411
'route' => '<unknown>',
398412
'net.host.name' => 'www.example.com',
399413
]);
414+
$transactionContext->getMetadata()->setSampleRand(0.1337);
400415

401416
yield 'request.attributes._controller IS ARRAY and NOT VALID CALLABLE' => [
402417
new Options(),
@@ -423,6 +438,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
423438
'net.host.name' => 'www.example.com',
424439
'net.peer.ip' => '127.0.0.1',
425440
]);
441+
$transactionContext->getMetadata()->setSampleRand(0.1337);
426442

427443
yield 'request.server.REMOTE_ADDR EXISTS and client.options.send_default_pii = TRUE' => [
428444
new Options(['send_default_pii' => true]),
@@ -446,6 +462,7 @@ public function handleKernelRequestEventDataProvider(): \Generator
446462
'route' => '<unknown>',
447463
'net.host.name' => '',
448464
]);
465+
$transactionContext->getMetadata()->setSampleRand(0.1337);
449466

450467
yield 'request.server.SERVER_PROTOCOL NOT EXISTS' => [
451468
new Options(),

tests/Tracing/HttpClient/TraceableHttpClientTest.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,9 @@ public function testRequest(): void
9292
$this->assertSame(200, $response->getStatusCode());
9393
$this->assertSame('GET', $response->getInfo('http_method'));
9494
$this->assertSame('https://username:[email protected]/test-page?foo=bar#baz', $response->getInfo('url'));
95-
$this->assertSame(['sentry-trace: ' . $spans[1]->toTraceparent()], $mockResponse->getRequestOptions()['normalized_headers']['sentry-trace']);
96-
$this->assertSame(['traceparent: ' . $spans[1]->toW3CTraceparent()], $mockResponse->getRequestOptions()['normalized_headers']['traceparent']);
97-
$this->assertSame(['baggage: ' . $transaction->toBaggage()], $mockResponse->getRequestOptions()['normalized_headers']['baggage']);
95+
$this->assertSame([\sprintf('sentry-trace: %s', $spans[1]->toTraceparent())], $mockResponse->getRequestOptions()['normalized_headers']['sentry-trace']);
96+
$this->assertSame([\sprintf('traceparent: %s', $spans[1]->toW3CTraceparent())], $mockResponse->getRequestOptions()['normalized_headers']['traceparent']);
97+
$this->assertSame([\sprintf('baggage: %s', $transaction->toBaggage())], $mockResponse->getRequestOptions()['normalized_headers']['baggage']);
9898
$this->assertNotNull($transaction->getSpanRecorder());
9999

100100
$spans = $transaction->getSpanRecorder()->getSpans();
@@ -199,9 +199,9 @@ public function testRequestDoesContainsTracingHeadersWithoutTransaction(): void
199199
$this->assertSame(200, $response->getStatusCode());
200200
$this->assertSame('POST', $response->getInfo('http_method'));
201201
$this->assertSame('https://www.example.com/test-page', $response->getInfo('url'));
202-
$this->assertSame(['sentry-trace: 566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8'], $mockResponse->getRequestOptions()['normalized_headers']['sentry-trace']);
203-
$this->assertSame(['traceparent: 00-566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8-00'], $mockResponse->getRequestOptions()['normalized_headers']['traceparent']);
204-
$this->assertSame(['baggage: sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-public_key=public,sentry-release=1.0.0,sentry-environment=test'], $mockResponse->getRequestOptions()['normalized_headers']['baggage']);
202+
$this->assertSame([\sprintf('sentry-trace: %s', $propagationContext->toTraceparent())], $mockResponse->getRequestOptions()['normalized_headers']['sentry-trace']);
203+
$this->assertSame([\sprintf('traceparent: %s', $propagationContext->toW3CTraceparent())], $mockResponse->getRequestOptions()['normalized_headers']['traceparent']);
204+
$this->assertSame([\sprintf('baggage: %s', $propagationContext->toBaggage())], $mockResponse->getRequestOptions()['normalized_headers']['baggage']);
205205
}
206206

207207
public function testRequestSetsUnknownErrorAsSpanStatusIfResponseStatusCodeIsUnavailable(): void

tests/Twig/SentryExtensionTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public function testBaggageMetaFunctionWithNoActiveSpan(): void
138138

139139
SentrySdk::setCurrentHub($hub);
140140

141-
$this->assertSame('<meta name="baggage" content="sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-sample_rate=1,sentry-release=1.0.0,sentry-environment=development" />', $environment->render('foo.twig'));
141+
$this->assertSame(\sprintf('<meta name="baggage" content="%s" />', $propagationContext->toBaggage()), $environment->render('foo.twig'));
142142
}
143143

144144
public function testBaggageMetaFunctionWithActiveSpan(): void
@@ -164,7 +164,7 @@ public function testBaggageMetaFunctionWithActiveSpan(): void
164164

165165
$hub->setSpan($transaction);
166166

167-
$this->assertSame('<meta name="baggage" content="sentry-trace_id=a3c01c41d7b94b90aee23edac90f4319,sentry-transaction=%3Cunlabeled%20transaction%3E,sentry-release=1.0.0,sentry-environment=development" />', $environment->render('foo.twig'));
167+
$this->assertSame(\sprintf('<meta name="baggage" content="%s" />', $transaction->toBaggage()), $environment->render('foo.twig'));
168168
}
169169

170170
private static function isTwigBundlePackageInstalled(): bool

0 commit comments

Comments
 (0)