|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sentry\SentryBundle\Tests\EventListener; |
| 6 | + |
| 7 | +use Generator; |
| 8 | +use PHPUnit\Framework\MockObject\MockObject; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Sentry\SentryBundle\EventListener\TracingConsoleListener; |
| 11 | +use Sentry\State\HubInterface; |
| 12 | +use Sentry\Tracing\Span; |
| 13 | +use Sentry\Tracing\Transaction; |
| 14 | +use Sentry\Tracing\TransactionContext; |
| 15 | +use Symfony\Component\Console\Command\Command; |
| 16 | +use Symfony\Component\Console\Event\ConsoleCommandEvent; |
| 17 | +use Symfony\Component\Console\Event\ConsoleTerminateEvent; |
| 18 | +use Symfony\Component\Console\Input\InputInterface; |
| 19 | +use Symfony\Component\Console\Output\OutputInterface; |
| 20 | + |
| 21 | +final class TracingConsoleListenerTest extends TestCase |
| 22 | +{ |
| 23 | + /** |
| 24 | + * @var MockObject&HubInterface |
| 25 | + */ |
| 26 | + private $hub; |
| 27 | + |
| 28 | + /** |
| 29 | + * @var TracingConsoleListener |
| 30 | + */ |
| 31 | + private $listener; |
| 32 | + |
| 33 | + protected function setUp(): void |
| 34 | + { |
| 35 | + $this->hub = $this->createMock(HubInterface::class); |
| 36 | + $this->listener = new TracingConsoleListener($this->hub); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * @dataProvider handleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHubDataProvider |
| 41 | + */ |
| 42 | + public function testHandleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHub(?Command $command, TransactionContext $expectedTransactionContext): void |
| 43 | + { |
| 44 | + $transaction = new Transaction(new TransactionContext()); |
| 45 | + |
| 46 | + $this->hub->expects($this->once()) |
| 47 | + ->method('getSpan') |
| 48 | + ->willReturn(null); |
| 49 | + |
| 50 | + $this->hub->expects($this->once()) |
| 51 | + ->method('startTransaction') |
| 52 | + ->with($this->callback(function (TransactionContext $context) use ($expectedTransactionContext): bool { |
| 53 | + $this->assertEquals($expectedTransactionContext, $context); |
| 54 | + |
| 55 | + return true; |
| 56 | + })) |
| 57 | + ->willReturn($transaction); |
| 58 | + |
| 59 | + $this->hub->expects($this->once()) |
| 60 | + ->method('setSpan') |
| 61 | + ->with($transaction) |
| 62 | + ->willReturnSelf(); |
| 63 | + |
| 64 | + $this->listener->handleConsoleCommandEvent(new ConsoleCommandEvent( |
| 65 | + $command, |
| 66 | + $this->createMock(InputInterface::class), |
| 67 | + $this->createMock(OutputInterface::class) |
| 68 | + )); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @return Generator<mixed> |
| 73 | + */ |
| 74 | + public function handleConsoleCommandEventStartsTransactionIfNoSpanIsSetOnHubDataProvider(): Generator |
| 75 | + { |
| 76 | + $transactionContext = new TransactionContext(); |
| 77 | + $transactionContext->setOp('console.command'); |
| 78 | + $transactionContext->setName('<unnamed command>'); |
| 79 | + |
| 80 | + yield [ |
| 81 | + null, |
| 82 | + $transactionContext, |
| 83 | + ]; |
| 84 | + |
| 85 | + $transactionContext = new TransactionContext(); |
| 86 | + $transactionContext->setOp('console.command'); |
| 87 | + $transactionContext->setName('<unnamed command>'); |
| 88 | + |
| 89 | + yield [ |
| 90 | + new Command(), |
| 91 | + $transactionContext, |
| 92 | + ]; |
| 93 | + |
| 94 | + $transactionContext = new TransactionContext(); |
| 95 | + $transactionContext->setOp('console.command'); |
| 96 | + $transactionContext->setName('app:command'); |
| 97 | + |
| 98 | + yield [ |
| 99 | + new Command('app:command'), |
| 100 | + $transactionContext, |
| 101 | + ]; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * @dataProvider handleConsoleCommandEventStartsChildSpanIfSpanIsSetOnHubDataProvider |
| 106 | + */ |
| 107 | + public function testHandleConsoleCommandEventStartsChildSpanIfSpanIsSetOnHub(?Command $command, string $expectedDescription): void |
| 108 | + { |
| 109 | + $span = new Span(); |
| 110 | + |
| 111 | + $this->hub->expects($this->once()) |
| 112 | + ->method('getSpan') |
| 113 | + ->willReturn($span); |
| 114 | + |
| 115 | + $this->hub->expects($this->once()) |
| 116 | + ->method('setSpan') |
| 117 | + ->with($this->callback(function (Span $spanArg) use ($span, $expectedDescription): bool { |
| 118 | + $this->assertSame('console.command', $spanArg->getOp()); |
| 119 | + $this->assertSame($expectedDescription, $spanArg->getDescription()); |
| 120 | + $this->assertSame($span->getSpanId(), $spanArg->getParentSpanId()); |
| 121 | + |
| 122 | + return true; |
| 123 | + })) |
| 124 | + ->willReturnSelf(); |
| 125 | + |
| 126 | + $this->listener->handleConsoleCommandEvent(new ConsoleCommandEvent( |
| 127 | + $command, |
| 128 | + $this->createMock(InputInterface::class), |
| 129 | + $this->createMock(OutputInterface::class) |
| 130 | + )); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * @return Generator<mixed> |
| 135 | + */ |
| 136 | + public function handleConsoleCommandEventStartsChildSpanIfSpanIsSetOnHubDataProvider(): Generator |
| 137 | + { |
| 138 | + yield [ |
| 139 | + null, |
| 140 | + '<unnamed command>', |
| 141 | + ]; |
| 142 | + |
| 143 | + yield [ |
| 144 | + new Command(), |
| 145 | + '<unnamed command>', |
| 146 | + ]; |
| 147 | + |
| 148 | + yield [ |
| 149 | + new Command('app:command'), |
| 150 | + 'app:command', |
| 151 | + ]; |
| 152 | + } |
| 153 | + |
| 154 | + public function testHandleConsoleTerminateEvent(): void |
| 155 | + { |
| 156 | + $span = new Span(); |
| 157 | + |
| 158 | + $this->hub->expects($this->once()) |
| 159 | + ->method('getSpan') |
| 160 | + ->willReturn($span); |
| 161 | + |
| 162 | + $this->listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent( |
| 163 | + new Command(), |
| 164 | + $this->createMock(InputInterface::class), |
| 165 | + $this->createMock(OutputInterface::class), |
| 166 | + 0 |
| 167 | + )); |
| 168 | + |
| 169 | + $this->assertNotNull($span->getEndTimestamp()); |
| 170 | + } |
| 171 | + |
| 172 | + public function testHandleConsoleTerminateEventDoesNothingIfNoSpanIsSetOnHub(): void |
| 173 | + { |
| 174 | + $this->hub->expects($this->once()) |
| 175 | + ->method('getSpan') |
| 176 | + ->willReturn(null); |
| 177 | + |
| 178 | + $this->listener->handleConsoleTerminateEvent(new ConsoleTerminateEvent( |
| 179 | + new Command(), |
| 180 | + $this->createMock(InputInterface::class), |
| 181 | + $this->createMock(OutputInterface::class), |
| 182 | + 0 |
| 183 | + )); |
| 184 | + } |
| 185 | +} |
0 commit comments