Skip to content

Commit

Permalink
Introduce TracingDriverForV3Point2
Browse files Browse the repository at this point in the history
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
ruudk committed Jun 6, 2023
1 parent a39c362 commit df90a7a
Show file tree
Hide file tree
Showing 6 changed files with 264 additions and 5 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
- 6.*
dependencies:
- highest
doctrine-dbal:
- highest
exclude:
- php: '7.2'
symfony-version: 6.*
Expand All @@ -47,6 +49,12 @@ jobs:
- php: '8.0'
symfony-version: 6.*
dependencies: lowest
- php: '8.1'
symfony-version: 6.*
doctrine-dbal: '^2.13'
- php: '8.1'
symfony-version: 6.*
doctrine-dbal: '<3.2'

steps:
- name: Checkout
Expand All @@ -68,6 +76,10 @@ jobs:
run: composer require --dev phpunit/phpunit ^9.3.9 --no-update
if: matrix.php == '8.0' && matrix.dependencies == 'lowest'

- name: Update Doctrine DBAL
run: composer require --dev doctrine/dbal "${{ matrix.doctrine-dbal }}" --no-update
if: matrix.doctrine-dbal != 'highest'

- name: Install dependencies
uses: ramsey/composer-install@v1
with:
Expand Down
90 changes: 90 additions & 0 deletions src/Tracing/Doctrine/DBAL/TracingDriverForV3Point2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?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;
use Doctrine\DBAL\VersionAwarePlatformDriver;

/**
* 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|VersionAwarePlatformDriver 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();
}
}
9 changes: 8 additions & 1 deletion src/aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sentry\SentryBundle;

use Composer\InstalledVersions;
use Doctrine\DBAL\Result;
use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapter;
use Sentry\SentryBundle\Tracing\Cache\TraceableCacheAdapterForV2;
Expand All @@ -15,6 +16,7 @@
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverForV2;
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverForV3;
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingStatement;
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverForV3Point2;
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingStatementForV2;
use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingStatementForV3;
use Sentry\SentryBundle\Tracing\HttpClient\TraceableHttpClient;
Expand Down Expand Up @@ -53,7 +55,12 @@ class_alias(TraceableTagAwareCacheAdapterForV2::class, TraceableTagAwareCacheAda
if (!class_exists(TracingStatement::class)) {
if (class_exists(Result::class)) {
class_alias(TracingStatementForV3::class, TracingStatement::class);
class_alias(TracingDriverForV3::class, TracingDriver::class);

if (class_exists(InstalledVersions::class) && version_compare(InstalledVersions::getVersion('doctrine/dbal') ?? '0', '3.2', '>=')) {
class_alias(TracingDriverForV3Point2::class, 'Sentry\\SentryBundle\\Tracing\\Doctrine\\DBAL\\TracingDriver');
} else {
class_alias(TracingDriverForV3::class, TracingDriver::class);
}
} elseif (interface_exists(Result::class)) {
class_alias(TracingStatementForV2::class, TracingStatement::class);
class_alias(TracingDriverForV2::class, TracingDriver::class);
Expand Down
15 changes: 11 additions & 4 deletions tests/DoctrineTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Sentry\SentryBundle\Tests;

use Composer\InstalledVersions;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\ResultStatement;
Expand All @@ -13,23 +14,29 @@ abstract class DoctrineTestCase extends TestCase
{
protected static function isDoctrineDBALInstalled(): bool
{
return interface_exists(Driver::class);
return InstalledVersions::isInstalled('doctrine/dbal');
}

protected static function isDoctrineDBALVersion2Installed(): bool
{
return self::isDoctrineDBALInstalled()
&& interface_exists(ResultStatement::class);
&& version_compare(InstalledVersions::getVersion('doctrine/dbal') ?? '0', '3.0', '<');
}

protected static function isDoctrineDBALVersion3Installed(): bool
{
return self::isDoctrineDBALInstalled()
&& !self::isDoctrineDBALVersion2Installed();
&& version_compare(InstalledVersions::getVersion('doctrine/dbal') ?? '0', '3.0', '>=');
}

protected static function isDoctrineDBALVersion3Point2Installed(): bool
{
return self::isDoctrineDBALInstalled()
&& version_compare(InstalledVersions::getVersion('doctrine/dbal') ?? '0', '3.2', '>=');
}

protected static function isDoctrineBundlePackageInstalled(): bool
{
return class_exists(DoctrineBundle::class);
return InstalledVersions::isInstalled('doctrine/doctrine-bundle');
}
}
139 changes: 139 additions & 0 deletions tests/Tracing/Doctrine/DBAL/TracingDriverForV3Point2Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
<?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());
}

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());
}

public function testCreateDatabasePlatformForVersion(): void
{
$databasePlatform = $this->createMock(AbstractPlatform::class);

$decoratedDriver = $this->createMock(VersionAwarePlatformDriverInterface::class);
$decoratedDriver->expects($this->once())
->method('createDatabasePlatformForVersion')
->with('5.7')
->willReturn($databasePlatform);

$driver = new TracingDriverForV3Point2($this->connectionFactory, $decoratedDriver);

$this->assertSame($databasePlatform, $driver->createDatabasePlatformForVersion('5.7'));
}

public function testCreateDatabasePlatformForVersionWhenDriverDoesNotImplementInterface(): 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->createDatabasePlatformForVersion('5.7'));
}
}
4 changes: 4 additions & 0 deletions tests/Tracing/Doctrine/DBAL/TracingDriverForV3Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public static function setUpBeforeClass(): void
if (!self::isDoctrineDBALVersion3Installed()) {
self::markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be >= 3.0.');
}

if (self::isDoctrineDBALVersion3Point2Installed()) {
self::markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be >= 3.0 <= 3.0.');
}
}

protected function setUp(): void
Expand Down

0 comments on commit df90a7a

Please sign in to comment.