-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce TracingDriverForV32 for DBAL
>= 3.2
(#723)
- Loading branch information
Showing
11 changed files
with
247 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sentry\SentryBundle\Tracing\Doctrine\DBAL; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Driver; | ||
use Doctrine\DBAL\Driver\API\ExceptionConverter; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Schema\AbstractSchemaManager; | ||
|
||
/** | ||
* This is a simple implementation of the {@see Driver} interface that decorates | ||
* an existing driver to support distributed tracing capabilities. This implementation | ||
* is compatible with all versions of Doctrine DBAL >= 3.2. | ||
* | ||
* @internal | ||
* | ||
* @phpstan-import-type Params from \Doctrine\DBAL\DriverManager as ConnectionParams | ||
*/ | ||
final class TracingDriverForV32 implements Driver | ||
{ | ||
/** | ||
* @var TracingDriverConnectionFactoryInterface The connection factory | ||
*/ | ||
private $connectionFactory; | ||
|
||
/** | ||
* @var Driver The instance of the decorated driver | ||
*/ | ||
private $decoratedDriver; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param TracingDriverConnectionFactoryInterface $connectionFactory The connection factory | ||
* @param Driver $decoratedDriver The instance of the driver to decorate | ||
*/ | ||
public function __construct(TracingDriverConnectionFactoryInterface $connectionFactory, Driver $decoratedDriver) | ||
{ | ||
$this->connectionFactory = $connectionFactory; | ||
$this->decoratedDriver = $decoratedDriver; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @phpstan-param ConnectionParams $params | ||
*/ | ||
public function connect(array $params): TracingDriverConnectionInterface | ||
{ | ||
return $this->connectionFactory->create( | ||
$this->decoratedDriver->connect($params), | ||
$this->decoratedDriver->getDatabasePlatform(), | ||
$params | ||
); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDatabasePlatform(): AbstractPlatform | ||
{ | ||
return $this->decoratedDriver->getDatabasePlatform(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @phpstan-template T of AbstractPlatform | ||
* | ||
* @phpstan-param T $platform | ||
* | ||
* @phpstan-return AbstractSchemaManager<T> | ||
*/ | ||
public function getSchemaManager(Connection $conn, AbstractPlatform $platform): AbstractSchemaManager | ||
{ | ||
return $this->decoratedDriver->getSchemaManager($conn, $platform); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getExceptionConverter(): ExceptionConverter | ||
{ | ||
return $this->decoratedDriver->getExceptionConverter(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
tests/Tracing/Doctrine/DBAL/TracingDriverForV32Test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sentry\SentryBundle\Tests\Tracing\Doctrine\DBAL; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Driver\API\ExceptionConverter; | ||
use Doctrine\DBAL\Driver as DriverInterface; | ||
use Doctrine\DBAL\Driver\Connection as DriverConnectionInterface; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Schema\AbstractSchemaManager; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Sentry\SentryBundle\Tests\DoctrineTestCase; | ||
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverConnectionFactoryInterface; | ||
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverConnectionInterface; | ||
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverForV32; | ||
|
||
final class TracingDriverForV32Test extends DoctrineTestCase | ||
{ | ||
/** | ||
* @var MockObject&TracingDriverConnectionFactoryInterface | ||
*/ | ||
private $connectionFactory; | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
if (!self::isDoctrineDBALVersion32Installed()) { | ||
self::markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be >= 3.2.'); | ||
} | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->connectionFactory = $this->createMock(TracingDriverConnectionFactoryInterface::class); | ||
} | ||
|
||
public function testConnect(): void | ||
{ | ||
$params = ['host' => 'localhost']; | ||
$databasePlatform = $this->createMock(AbstractPlatform::class); | ||
$driverConnection = $this->createMock(DriverConnectionInterface::class); | ||
$tracingDriverConnection = $this->createMock(TracingDriverConnectionInterface::class); | ||
$decoratedDriver = $this->createMock(DriverInterface::class); | ||
|
||
$decoratedDriver->expects($this->once()) | ||
->method('connect') | ||
->with($params) | ||
->willReturn($driverConnection); | ||
|
||
$decoratedDriver->expects($this->once()) | ||
->method('getDatabasePlatform') | ||
->willReturn($databasePlatform); | ||
|
||
$this->connectionFactory->expects($this->once()) | ||
->method('create') | ||
->with($driverConnection, $databasePlatform, $params) | ||
->willReturn($tracingDriverConnection); | ||
|
||
$driver = new TracingDriverForV32($this->connectionFactory, $decoratedDriver); | ||
|
||
$this->assertSame($tracingDriverConnection, $driver->connect($params)); | ||
} | ||
|
||
public function testGetDatabasePlatform(): void | ||
{ | ||
$databasePlatform = $this->createMock(AbstractPlatform::class); | ||
|
||
$decoratedDriver = $this->createMock(DriverInterface::class); | ||
$decoratedDriver->expects($this->once()) | ||
->method('getDatabasePlatform') | ||
->willReturn($databasePlatform); | ||
|
||
$driver = new TracingDriverForV32($this->connectionFactory, $decoratedDriver); | ||
|
||
$this->assertSame($databasePlatform, $driver->getDatabasePlatform()); | ||
} | ||
|
||
/** | ||
* @group legacy | ||
*/ | ||
public function testGetSchemaManager(): void | ||
{ | ||
$connection = $this->createMock(Connection::class); | ||
$databasePlatform = $this->createMock(AbstractPlatform::class); | ||
$schemaManager = $this->createMock(AbstractSchemaManager::class); | ||
|
||
$decoratedDriver = $this->createMock(DriverInterface::class); | ||
$decoratedDriver->expects($this->once()) | ||
->method('getSchemaManager') | ||
->with($connection, $databasePlatform) | ||
->willReturn($schemaManager); | ||
|
||
$driver = new TracingDriverForV32($this->connectionFactory, $decoratedDriver); | ||
|
||
$this->assertSame($schemaManager, $driver->getSchemaManager($connection, $databasePlatform)); | ||
} | ||
|
||
public function testGetExceptionConverter(): void | ||
{ | ||
$exceptionConverter = $this->createMock(ExceptionConverter::class); | ||
|
||
$decoratedDriver = $this->createMock(DriverInterface::class); | ||
$decoratedDriver->expects($this->once()) | ||
->method('getExceptionConverter') | ||
->willReturn($exceptionConverter); | ||
|
||
$driver = new TracingDriverForV32($this->connectionFactory, $decoratedDriver); | ||
|
||
$this->assertSame($exceptionConverter, $driver->getExceptionConverter()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters