Skip to content

Commit 6aa45ad

Browse files
committed
Added tests to test the DbalListener and make sure dbal is installed
1 parent b2e1c71 commit 6aa45ad

File tree

4 files changed

+97
-1
lines changed

4 files changed

+97
-1
lines changed

.github/workflows/static-analysis.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ jobs:
2323
- name: Install dependencies
2424
run: composer update --no-progress --no-interaction --prefer-dist
2525

26+
- name: Install suggested packages
27+
run: composer suggests sentry/sentry-symfony --list | xargs -i composer require {}
28+
2629
- name: Run script
2730
run: composer phpcs
2831

@@ -39,6 +42,9 @@ jobs:
3942
- name: Install dependencies
4043
run: composer update --no-progress --no-interaction --prefer-dist
4144

45+
- name: Install suggested packages
46+
run: composer suggests sentry/sentry-symfony --list | xargs -i composer require {}
47+
4248
- name: Run script
4349
run: composer phpstan
4450

@@ -55,5 +61,8 @@ jobs:
5561
- name: Install dependencies
5662
run: composer update --no-progress --no-interaction --prefer-dist
5763

64+
- name: Install suggested packages
65+
run: composer suggests sentry/sentry-symfony --list | xargs -i composer require {}
66+
5867
- name: Run script
5968
run: composer psalm

.github/workflows/tests.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ jobs:
5858
- run: composer remove --dev symfony/messenger --no-update
5959
if: matrix.symfony_constraint == '3.4.*' || matrix.composer_option == '--prefer-lowest'
6060
- run: composer update --no-progress --ansi ${{ matrix.composer_option }}
61+
- run: composer suggests sentry/sentry-symfony --list | xargs -i composer require {}
6162
- run: composer require sentry/sentry dev-develop
6263
if: matrix.sentry_constraint == 'dev-develop'
6364
- run: vendor/bin/phpunit --coverage-clover=coverage.xml

src/EventListener/Tracing/DbalListener.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Sentry\State\HubInterface;
99
use Sentry\Tracing\Span;
1010
use Sentry\Tracing\SpanContext;
11+
use Sentry\Tracing\Transaction;
1112

1213
/**
1314
* Getting the logger, tied into dbal seems extremely hard. Cheating the system a bit by putting it in between the
@@ -40,7 +41,7 @@ public function startQuery($sql, ?array $params = null, ?array $types = null)
4041
{
4142
$transaction = $this->hub->getTransaction();
4243

43-
if (!$transaction) {
44+
if (!$transaction instanceof Transaction) {
4445
return;
4546
}
4647

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}

0 commit comments

Comments
 (0)