|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sentry\SentryBundle\Tests\EventListener\Tracing; |
| 6 | + |
| 7 | +use PHPUnit\Framework\MockObject\MockObject; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use Sentry\SentryBundle\EventListener\Tracing\DbalListener; |
| 10 | +use Sentry\State\HubInterface; |
| 11 | +use Sentry\Tracing\Transaction; |
| 12 | +use Sentry\Tracing\TransactionContext; |
| 13 | + |
| 14 | +final class DbalListenerTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @var MockObject&HubInterface |
| 18 | + */ |
| 19 | + private $hub; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var DbalListener |
| 23 | + */ |
| 24 | + private $listener; |
| 25 | + |
| 26 | + protected function setUp(): void |
| 27 | + { |
| 28 | + $this->hub = $this->createMock(HubInterface::class); |
| 29 | + $this->listener = new DbalListener($this->hub); |
| 30 | + } |
| 31 | + |
| 32 | + public function testThatDbalStartQueryIgnoresTracingWhenTransactionIsNotStarted(): void |
| 33 | + { |
| 34 | + $this->hub->expects($this->once()) |
| 35 | + ->method('getTransaction') |
| 36 | + ->willReturn(null); |
| 37 | + |
| 38 | + $this->listener->startQuery(''); |
| 39 | + } |
| 40 | + |
| 41 | + public function testThatDbalStopQueryIgnoresTracingWhenChildSpanWasNotStarted(): void |
| 42 | + { |
| 43 | + $this->hub->expects($this->once()) |
| 44 | + ->method('getTransaction') |
| 45 | + ->willReturn(null); |
| 46 | + |
| 47 | + $this->listener->startQuery(''); |
| 48 | + $this->listener->stopQuery(); |
| 49 | + } |
| 50 | + |
| 51 | + public function testThatDbalStartQueryAttachesAChildSpanWhenTransactionStarted(): void |
| 52 | + { |
| 53 | + $transaction = new Transaction(new TransactionContext()); |
| 54 | + $transaction->initSpanRecorder(); |
| 55 | + |
| 56 | + $this->hub->expects($this->once()) |
| 57 | + ->method('getTransaction') |
| 58 | + ->willReturn($transaction); |
| 59 | + |
| 60 | + $this->listener->startQuery(''); |
| 61 | + |
| 62 | + $spans = $transaction->getSpanRecorder()->getSpans(); |
| 63 | + |
| 64 | + $this->assertCount(2, $spans); |
| 65 | + $this->assertNull($spans['1']->getEndTimestamp()); |
| 66 | + } |
| 67 | + |
| 68 | + public function testThatDbalStopQueryFinishesTheChildSpanWhenChildSpanStarted(): void |
| 69 | + { |
| 70 | + $transaction = new Transaction(new TransactionContext()); |
| 71 | + $transaction->initSpanRecorder(); |
| 72 | + |
| 73 | + $this->hub->expects($this->once()) |
| 74 | + ->method('getTransaction') |
| 75 | + ->willReturn($transaction); |
| 76 | + |
| 77 | + $this->listener->startQuery(''); |
| 78 | + $this->listener->stopQuery(); |
| 79 | + |
| 80 | + $spans = $transaction->getSpanRecorder()->getSpans(); |
| 81 | + |
| 82 | + $this->assertCount(2, $spans); |
| 83 | + $this->assertNotNull($spans['1']->getEndTimestamp()); |
| 84 | + |
| 85 | + } |
| 86 | +} |
0 commit comments