-
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.
This driver does not implement the deprecated `VersionAwarePlatformDriver`. It's automatically picked when `doctrine/dbal` version `3.2.0` or higher is installed. Fixes #579
- Loading branch information
Showing
9 changed files
with
256 additions
and
7 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
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 TracingDriverForV3Point2 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
113 changes: 113 additions & 0 deletions
113
tests/Tracing/Doctrine/DBAL/TracingDriverForV3Point2Test.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,113 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sentry\SentryBundle\Tests\Tracing\Doctrine\DBAL; | ||
|
||
use Doctrine\DBAL\Connection; | ||
use Doctrine\DBAL\Driver as DriverInterface; | ||
use Doctrine\DBAL\Driver\API\ExceptionConverter; | ||
use Doctrine\DBAL\Driver\Connection as DriverConnectionInterface; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Schema\AbstractSchemaManager; | ||
use Doctrine\DBAL\VersionAwarePlatformDriver as VersionAwarePlatformDriverInterface; | ||
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\TracingDriverForV3Point2; | ||
|
||
final class TracingDriverForV3Point2Test extends DoctrineTestCase | ||
{ | ||
/** | ||
* @var MockObject&TracingDriverConnectionFactoryInterface | ||
*/ | ||
private $connectionFactory; | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
if (!self::isDoctrineDBALVersion3Point2Installed()) { | ||
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 TracingDriverForV3Point2($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 TracingDriverForV3Point2($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 TracingDriverForV3Point2($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 TracingDriverForV3Point2($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