Skip to content

Commit 1514830

Browse files
committed
Add support for accessing dbal native connection
Dbal V3 contains: ```php /** * Connection interface. * Driver connections must implement this interface. * * @method resource|object getNativeConnection() */ interface Connection ```
1 parent 517493a commit 1514830

7 files changed

+105
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Add `TracingDriverConnectionInterface::getNativeConnection()` method to get the original driver connection (#597)
6+
57
## 4.2.6 (2022-01-10)
68

79
- Add support for `symfony/cache-contracts` package version `3.x` (#588)

src/Tracing/Doctrine/DBAL/TracingDriverConnection.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,20 @@ public function rollBack(): bool
164164
});
165165
}
166166

167+
/**
168+
* {@inheritdoc}
169+
*
170+
* @return resource|object
171+
*/
172+
public function getNativeConnection()
173+
{
174+
if (!method_exists($this->decoratedConnection, 'getNativeConnection')) {
175+
throw new \BadMethodCallException(sprintf('The connection "%s" does not support accessing the native connection.', \get_class($this->decoratedConnection)));
176+
}
177+
178+
return $this->decoratedConnection->getNativeConnection();
179+
}
180+
167181
/**
168182
* {@inheritdoc}
169183
*/

src/Tracing/Doctrine/DBAL/TracingDriverConnectionInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
use Doctrine\DBAL\Driver\Connection;
88

9+
/**
10+
* @method resource|object getNativeConnection()
11+
*/
912
interface TracingDriverConnectionInterface extends Connection
1013
{
1114
public function getWrappedConnection(): Connection;

src/Tracing/Doctrine/DBAL/TracingServerInfoAwareDriverConnection.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ public function getServerVersion(): string
115115
return $wrappedConnection->getServerVersion();
116116
}
117117

118+
/**
119+
* {@inheritdoc}
120+
*
121+
* @return resource|object
122+
*/
123+
public function getNativeConnection()
124+
{
125+
if (!method_exists($this->decoratedConnection, 'getNativeConnection')) {
126+
throw new \BadMethodCallException(sprintf('The connection "%s" does not support accessing the native connection.', \get_class($this->decoratedConnection)));
127+
}
128+
129+
return $this->decoratedConnection->getNativeConnection();
130+
}
131+
118132
/**
119133
* {@inheritdoc}
120134
*/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sentry\SentryBundle\Tests\Tracing\Doctrine\DBAL\Fixture;
6+
7+
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverConnectionInterface;
8+
9+
interface NativeDriverConnectionInterface extends TracingDriverConnectionInterface
10+
{
11+
/**
12+
* @return object|resource
13+
*/
14+
public function getNativeConnection();
15+
}

tests/Tracing/Doctrine/DBAL/TracingDriverConnectionTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
use Doctrine\DBAL\ParameterType;
1111
use PHPUnit\Framework\MockObject\MockObject;
1212
use Sentry\SentryBundle\Tests\DoctrineTestCase;
13+
use Sentry\SentryBundle\Tests\Tracing\Doctrine\DBAL\Fixture\NativeDriverConnectionInterface;
1314
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverConnection;
15+
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverConnectionInterface;
1416
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingStatement;
1517
use Sentry\State\HubInterface;
1618
use Sentry\Tracing\Transaction;
@@ -396,6 +398,33 @@ public function testGetWrappedConnection(): void
396398
$this->assertSame($this->decoratedConnection, $connection->getWrappedConnection());
397399
}
398400

401+
public function testGetNativeConnection(): void
402+
{
403+
$nativeConnection = new class() {
404+
};
405+
406+
$decoratedConnection = $this->createMock(NativeDriverConnectionInterface::class);
407+
$decoratedConnection->expects($this->once())
408+
->method('getNativeConnection')
409+
->willReturn($nativeConnection);
410+
411+
$connection = new TracingDriverConnection($this->hub, $decoratedConnection, 'foo_platform', []);
412+
413+
$this->assertSame($nativeConnection, $connection->getNativeConnection());
414+
}
415+
416+
public function testGetNativeConnectionThrowsExceptionIfDecoratedConnectionDoesNotImplementMethod(): void
417+
{
418+
$decoratedConnection = $this->createMock(TracingDriverConnectionInterface::class);
419+
420+
$connection = new TracingDriverConnection($this->hub, $decoratedConnection, 'foo_platform', []);
421+
422+
$this->expectException(\BadMethodCallException::class);
423+
$this->expectExceptionMessageMatches('~The connection .+? does not support accessing the native connection~');
424+
425+
$connection->getNativeConnection();
426+
}
427+
399428
/**
400429
* @return \Generator<mixed>
401430
*/

tests/Tracing/Doctrine/DBAL/TracingServerInfoAwareDriverConnectionTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Doctrine\DBAL\ParameterType;
1111
use PHPUnit\Framework\MockObject\MockObject;
1212
use Sentry\SentryBundle\Tests\DoctrineTestCase;
13+
use Sentry\SentryBundle\Tests\Tracing\Doctrine\DBAL\Fixture\NativeDriverConnectionInterface;
1314
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverConnectionInterface;
1415
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingServerInfoAwareDriverConnection;
1516

@@ -256,4 +257,31 @@ public function testGetWrappedConnection(): void
256257

257258
$this->assertSame($wrappedConnection, $this->connection->getWrappedConnection());
258259
}
260+
261+
public function testGetNativeConnection(): void
262+
{
263+
$nativeConnection = new class() {
264+
};
265+
266+
$decoratedConnection = $this->createMock(NativeDriverConnectionInterface::class);
267+
$decoratedConnection->expects($this->once())
268+
->method('getNativeConnection')
269+
->willReturn($nativeConnection);
270+
271+
$connection = new TracingServerInfoAwareDriverConnection($decoratedConnection);
272+
273+
$this->assertSame($nativeConnection, $connection->getNativeConnection());
274+
}
275+
276+
public function testGetNativeConnectionThrowsExceptionIfDecoratedConnectionDoesNotImplementMethod(): void
277+
{
278+
$decoratedConnection = $this->createMock(TracingDriverConnectionInterface::class);
279+
280+
$connection = new TracingServerInfoAwareDriverConnection($decoratedConnection);
281+
282+
$this->expectException(\BadMethodCallException::class);
283+
$this->expectExceptionMessageMatches('~The connection .+? does not support accessing the native connection~');
284+
285+
$connection->getNativeConnection();
286+
}
259287
}

0 commit comments

Comments
 (0)