/
TwigTracingExtensionTest.php
133 lines (104 loc) · 3.55 KB
/
TwigTracingExtensionTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
declare(strict_types=1);
namespace Sentry\SentryBundle\Tests\Tracing\Twig;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sentry\SentryBundle\Tracing\Twig\TwigTracingExtension;
use Sentry\State\HubInterface;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Twig\Profiler\Profile;
final class TwigTracingExtensionTest extends TestCase
{
/**
* @var MockObject&HubInterface
*/
private $hub;
/**
* @var TwigTracingExtension
*/
private $listener;
public static function setUpBeforeClass(): void
{
if (!self::isTwigBundlePackageInstalled()) {
self::markTestSkipped('This test requires the "symfony/twig-bundle" Composer package to be installed.');
}
}
protected function setUp(): void
{
$this->hub = $this->createMock(HubInterface::class);
$this->listener = new TwigTracingExtension($this->hub);
}
/**
* @dataProvider enterDataProvider
*/
public function testEnter(Profile $profile, string $spanDescription): void
{
$transaction = new Transaction(new TransactionContext());
$transaction->initSpanRecorder();
$this->hub->expects($this->once())
->method('getTransaction')
->willReturn($transaction);
$this->listener->enter($profile);
$this->assertNotNull($transaction->getSpanRecorder());
$spans = $transaction->getSpanRecorder()->getSpans();
$this->assertCount(2, $spans);
$this->assertNull($spans[1]->getEndTimestamp());
$this->assertSame('view.render', $spans[1]->getOp());
$this->assertSame($spanDescription, $spans[1]->getDescription());
}
/**
* @return \Generator<mixed>
*/
public function enterDataProvider(): \Generator
{
yield [
new Profile('main.twig', Profile::ROOT),
'main',
];
yield [
new Profile('main.twig', Profile::TEMPLATE),
'main.twig',
];
yield [
new Profile('main.twig', Profile::BLOCK, 'content'),
'main.twig::block(content)',
];
yield [
new Profile('main.twig', Profile::MACRO, 'input'),
'main.twig::macro(input)',
];
}
public function testEnterDoesNothingIfNoSpanIsSetOnHub(): void
{
$this->hub->expects($this->once())
->method('getTransaction')
->willReturn(null);
$this->listener->enter(new Profile('main', Profile::TEMPLATE));
}
public function testLeave(): void
{
$transaction = new Transaction(new TransactionContext());
$transaction->initSpanRecorder();
$this->hub->expects($this->once())
->method('getTransaction')
->willReturn($transaction);
$profile = new Profile('main', Profile::TEMPLATE);
$this->listener->enter($profile);
$this->listener->leave($profile);
$this->assertNotNull($transaction->getSpanRecorder());
$spans = $transaction->getSpanRecorder()->getSpans();
$this->assertCount(2, $spans);
$this->assertNotNull($spans[1]->getEndTimestamp());
}
public function testLeaveDoesNothingIfSpanDoesNotExistsForProfile(): void
{
$this->expectNotToPerformAssertions();
$this->listener->leave(new Profile('main', Profile::TEMPLATE));
}
private static function isTwigBundlePackageInstalled(): bool
{
return class_exists(TwigBundle::class);
}
}