|
4 | 4 |
|
5 | 5 | namespace Sentry\SentryBundle\Tests\Twig;
|
6 | 6 |
|
7 |
| -use PHPUnit\Framework\MockObject\MockObject; |
8 | 7 | use PHPUnit\Framework\TestCase;
|
| 8 | +use Sentry\ClientInterface; |
| 9 | +use Sentry\Options; |
9 | 10 | use Sentry\SentryBundle\Twig\SentryExtension;
|
10 |
| -use Sentry\State\HubInterface; |
11 |
| -use Sentry\Tracing\DynamicSamplingContext; |
12 |
| -use Sentry\Tracing\Span; |
| 11 | +use Sentry\SentrySdk; |
| 12 | +use Sentry\State\Hub; |
| 13 | +use Sentry\State\Scope; |
| 14 | +use Sentry\Tracing\PropagationContext; |
13 | 15 | use Sentry\Tracing\SpanId;
|
14 | 16 | use Sentry\Tracing\TraceId;
|
15 | 17 | use Sentry\Tracing\Transaction;
|
|
20 | 22 |
|
21 | 23 | final class SentryExtensionTest extends TestCase
|
22 | 24 | {
|
23 |
| - /** |
24 |
| - * @var MockObject&HubInterface |
25 |
| - */ |
26 |
| - private $hub; |
27 |
| - |
28 | 25 | public static function setUpBeforeClass(): void
|
29 | 26 | {
|
30 | 27 | if (!self::isTwigBundlePackageInstalled()) {
|
31 | 28 | self::markTestSkipped('This test requires the "symfony/twig-bundle" Composer package to be installed.');
|
32 | 29 | }
|
33 | 30 | }
|
34 | 31 |
|
35 |
| - protected function setUp(): void |
| 32 | + public function testTraceMetaFunctionWithNoActiveSpan(): void |
36 | 33 | {
|
37 |
| - $this->hub = $this->createMock(HubInterface::class); |
38 |
| - } |
| 34 | + $environment = new Environment(new ArrayLoader(['foo.twig' => '{{ sentry_trace_meta() }}'])); |
| 35 | + $environment->addExtension(new SentryExtension()); |
39 | 36 |
|
40 |
| - /** |
41 |
| - * @dataProvider traceMetaFunctionDataProvider |
42 |
| - */ |
43 |
| - public function testTraceMetaFunction(?Span $span, string $expectedTemplate): void |
44 |
| - { |
45 |
| - $this->hub->expects($this->once()) |
46 |
| - ->method('getSpan') |
47 |
| - ->willReturn($span); |
| 37 | + $propagationContext = PropagationContext::fromDefaults(); |
| 38 | + $propagationContext->setTraceId(new TraceId('566e3688a61d4bc888951642d6f14a19')); |
| 39 | + $propagationContext->setSpanId(new SpanId('566e3688a61d4bc8')); |
48 | 40 |
|
49 |
| - $environment = new Environment(new ArrayLoader(['foo.twig' => '{{ sentry_trace_meta() }}'])); |
50 |
| - $environment->addExtension(new SentryExtension($this->hub)); |
| 41 | + $hub = new Hub(null, new Scope($propagationContext)); |
| 42 | + |
| 43 | + SentrySdk::setCurrentHub($hub); |
51 | 44 |
|
52 |
| - $this->assertSame($expectedTemplate, $environment->render('foo.twig')); |
| 45 | + $this->assertSame('<meta name="sentry-trace" content="566e3688a61d4bc888951642d6f14a19-566e3688a61d4bc8" />', $environment->render('foo.twig')); |
53 | 46 | }
|
54 | 47 |
|
55 |
| - /** |
56 |
| - * @return \Generator<mixed> |
57 |
| - */ |
58 |
| - public function traceMetaFunctionDataProvider(): \Generator |
| 48 | + public function testTraceMetaFunctionWithActiveSpan(): void |
59 | 49 | {
|
60 |
| - yield [ |
61 |
| - null, |
62 |
| - '<meta name="sentry-trace" content="" />', |
63 |
| - ]; |
| 50 | + $environment = new Environment(new ArrayLoader(['foo.twig' => '{{ sentry_trace_meta() }}'])); |
| 51 | + $environment->addExtension(new SentryExtension()); |
64 | 52 |
|
65 | 53 | $transaction = new Transaction(new TransactionContext());
|
66 | 54 | $transaction->setTraceId(new TraceId('a3c01c41d7b94b90aee23edac90f4319'));
|
67 | 55 | $transaction->setSpanId(new SpanId('e69c2aef0ec34f2a'));
|
68 | 56 |
|
69 |
| - yield [ |
70 |
| - $transaction, |
71 |
| - '<meta name="sentry-trace" content="a3c01c41d7b94b90aee23edac90f4319-e69c2aef0ec34f2a" />', |
72 |
| - ]; |
| 57 | + $client = $this->createMock(ClientInterface::class); |
| 58 | + $client->expects($this->atLeastOnce()) |
| 59 | + ->method('getOptions') |
| 60 | + ->willReturn(new Options([ |
| 61 | + 'traces_sample_rate' => 1.0, |
| 62 | + 'release' => '1.0.0', |
| 63 | + 'environment' => 'development', |
| 64 | + ])); |
| 65 | + |
| 66 | + $hub = new Hub($client); |
| 67 | + $hub->setSpan($transaction); |
| 68 | + |
| 69 | + SentrySdk::setCurrentHub($hub); |
| 70 | + |
| 71 | + $this->assertSame('<meta name="sentry-trace" content="a3c01c41d7b94b90aee23edac90f4319-e69c2aef0ec34f2a" />', $environment->render('foo.twig')); |
73 | 72 | }
|
74 | 73 |
|
75 |
| - /** |
76 |
| - * @dataProvider baggageMetaFunctionDataProvider |
77 |
| - */ |
78 |
| - public function testBaggageMetaFunction(?Span $span, string $expectedTemplate): void |
| 74 | + public function testBaggageMetaFunctionWithNoActiveSpan(): void |
79 | 75 | {
|
80 |
| - $this->hub->expects($this->once()) |
81 |
| - ->method('getSpan') |
82 |
| - ->willReturn($span); |
83 |
| - |
84 | 76 | $environment = new Environment(new ArrayLoader(['foo.twig' => '{{ sentry_baggage_meta() }}']));
|
85 |
| - $environment->addExtension(new SentryExtension($this->hub)); |
| 77 | + $environment->addExtension(new SentryExtension()); |
| 78 | + |
| 79 | + $propagationContext = PropagationContext::fromDefaults(); |
| 80 | + $propagationContext->setTraceId(new TraceId('566e3688a61d4bc888951642d6f14a19')); |
86 | 81 |
|
87 |
| - $this->assertSame($expectedTemplate, $environment->render('foo.twig')); |
| 82 | + $client = $this->createMock(ClientInterface::class); |
| 83 | + $client->expects($this->atLeastOnce()) |
| 84 | + ->method('getOptions') |
| 85 | + ->willReturn(new Options([ |
| 86 | + 'traces_sample_rate' => 1.0, |
| 87 | + 'release' => '1.0.0', |
| 88 | + 'environment' => 'development', |
| 89 | + ])); |
| 90 | + |
| 91 | + $hub = new Hub($client, new Scope($propagationContext)); |
| 92 | + |
| 93 | + SentrySdk::setCurrentHub($hub); |
| 94 | + |
| 95 | + $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')); |
88 | 96 | }
|
89 | 97 |
|
90 |
| - /** |
91 |
| - * @return \Generator<mixed> |
92 |
| - */ |
93 |
| - public function baggageMetaFunctionDataProvider(): \Generator |
| 98 | + public function testBaggageMetaFunctionWithActiveSpan(): void |
94 | 99 | {
|
95 |
| - yield [ |
96 |
| - null, |
97 |
| - '<meta name="baggage" content="" />', |
98 |
| - ]; |
99 |
| - |
100 |
| - $samplingContext = DynamicSamplingContext::fromHeader('sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-public_key=public,sentry-sample_rate=1'); |
101 |
| - $transactionContext = new TransactionContext(); |
102 |
| - $transactionContext->getMetadata()->setDynamicSamplingContext($samplingContext); |
103 |
| - $transaction = new Transaction($transactionContext); |
104 |
| - |
105 |
| - yield [ |
106 |
| - $transaction, |
107 |
| - '<meta name="baggage" content="sentry-trace_id=566e3688a61d4bc888951642d6f14a19,sentry-public_key=public,sentry-sample_rate=1" />', |
108 |
| - ]; |
| 100 | + $environment = new Environment(new ArrayLoader(['foo.twig' => '{{ sentry_baggage_meta() }}'])); |
| 101 | + $environment->addExtension(new SentryExtension()); |
| 102 | + |
| 103 | + $transaction = new Transaction(new TransactionContext()); |
| 104 | + $transaction->setTraceId(new TraceId('a3c01c41d7b94b90aee23edac90f4319')); |
| 105 | + |
| 106 | + $client = $this->createMock(ClientInterface::class); |
| 107 | + $client->expects($this->atLeastOnce()) |
| 108 | + ->method('getOptions') |
| 109 | + ->willReturn(new Options([ |
| 110 | + 'traces_sample_rate' => 1.0, |
| 111 | + 'release' => '1.0.0', |
| 112 | + 'environment' => 'development', |
| 113 | + ])); |
| 114 | + |
| 115 | + $hub = new Hub($client); |
| 116 | + $hub->setSpan($transaction); |
| 117 | + |
| 118 | + SentrySdk::setCurrentHub($hub); |
| 119 | + |
| 120 | + $this->assertSame('<meta name="baggage" content="sentry-trace_id=a3c01c41d7b94b90aee23edac90f4319,sentry-sample_rate=1,sentry-release=1.0.0,sentry-environment=development" />', $environment->render('foo.twig')); |
109 | 121 | }
|
110 | 122 |
|
111 | 123 | private static function isTwigBundlePackageInstalled(): bool
|
|
0 commit comments