/
TracingConsoleListenerTest.php
199 lines (167 loc) · 6.06 KB
/
TracingConsoleListenerTest.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
declare(strict_types=1);
namespace Sentry\SentryBundle\Tests\EventListener;
use Generator;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Sentry\SentryBundle\EventListener\TracingConsoleListener;
use Sentry\State\HubInterface;
use Sentry\Tracing\Span;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Sentry\Tracing\TransactionSource;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
final class TracingConsoleListenerTest extends TestCase
{
/**
* @var MockObject&HubInterface
*/
private $hub;
protected function setUp(): void
{
$this->hub = $this->createMock(HubInterface::class);
}
/**
* @dataProvider handleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHubDataProvider
*/
public function testHandleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHub(Command $command, TransactionContext $expectedTransactionContext): void
{
$transaction = new Transaction(new TransactionContext());
$this->hub->expects($this->once())
->method('getSpan')
->willReturn(null);
$this->hub->expects($this->once())
->method('startTransaction')
->with($this->callback(function (TransactionContext $context) use ($expectedTransactionContext): bool {
$this->assertEquals($expectedTransactionContext, $context);
return true;
}))
->willReturn($transaction);
$this->hub->expects($this->once())
->method('setSpan')
->with($transaction)
->willReturnSelf();
$listener = new TracingConsoleListener($this->hub);
$listener->handleConsoleCommandEvent(new ConsoleCommandEvent(
$command,
new ArrayInput([]),
new NullOutput()
));
}
/**
* @return Generator<mixed>
*/
public function handleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHubDataProvider(): Generator
{
$transactionContext = new TransactionContext();
$transactionContext->setOp('console.command');
$transactionContext->setName('<unnamed command>');
$transactionContext->setSource(TransactionSource::task());
yield [
new Command(),
$transactionContext,
];
$transactionContext = new TransactionContext();
$transactionContext->setOp('console.command');
$transactionContext->setName('app:command');
$transactionContext->setSource(TransactionSource::task());
yield [
new Command('app:command'),
$transactionContext,
];
}
/**
* @dataProvider handleConsoleCommandEventStartsChildSpanIfSpanIsSetOnHubDataProvider
*/
public function testHandleConsoleCommandEventStartsChildSpanIfSpanIsSetOnHub(Command $command, string $expectedDescription): void
{
$span = new Span();
$this->hub->expects($this->once())
->method('getSpan')
->willReturn($span);
$this->hub->expects($this->once())
->method('setSpan')
->with($this->callback(function (Span $spanArg) use ($span, $expectedDescription): bool {
$this->assertSame('console.command', $spanArg->getOp());
$this->assertSame($expectedDescription, $spanArg->getDescription());
$this->assertSame($span->getSpanId(), $spanArg->getParentSpanId());
return true;
}))
->willReturnSelf();
$listener = new TracingConsoleListener($this->hub);
$listener->handleConsoleCommandEvent(new ConsoleCommandEvent(
$command,
new ArrayInput([]),
new NullOutput()
));
}
/**
* @return Generator<mixed>
*/
public function handleConsoleCommandEventStartsChildSpanIfSpanIsSetOnHubDataProvider(): Generator
{
yield [
new Command(),
'<unnamed command>',
];
yield [
new Command('app:command'),
'app:command',
];
}
public function testHandleConsoleCommandEvent(): void
{
$this->hub->expects($this->never())
->method('getSpan');
$listener = new TracingConsoleListener($this->hub, ['foo:bar']);
$listener->handleConsoleCommandEvent(new ConsoleCommandEvent(
new Command('foo:bar'),
new ArrayInput([]),
new NullOutput()
));
}
public function testHandleConsoleTerminateEvent(): void
{
$span = new Span();
$this->hub->expects($this->once())
->method('getSpan')
->willReturn($span);
$listener = new TracingConsoleListener($this->hub);
$listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent(
new Command(),
new ArrayInput([]),
new NullOutput(),
0
));
$this->assertNotNull($span->getEndTimestamp());
}
public function testHandleConsoleTerminateEventDoesNothingIfNoSpanIsSetOnHub(): void
{
$this->hub->expects($this->once())
->method('getSpan')
->willReturn(null);
$listener = new TracingConsoleListener($this->hub);
$listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent(
new Command(),
new ArrayInput([]),
new NullOutput(),
0
));
}
public function testHandleConsoleTerminateEventDoesNothingIfCommandIsExcluded(): void
{
$this->hub->expects($this->never())
->method('getSpan');
$listener = new TracingConsoleListener($this->hub, ['foo:bar']);
$listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent(
new Command('foo:bar'),
new ArrayInput([]),
new NullOutput(),
0
));
}
}