diff --git a/CHANGELOG.md b/CHANGELOG.md index 434c8e11..afb9a67a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Make the list of commands for which distributed tracing is active configurable (#515) - Introduce `TracingDriverConnection::getWrappedConnection()` (#536) - Add the `logger` config option to ease setting a PSR-3 logger to debug the SDK (#538) +- Bump requirement for DBAL tracing to `^2.13|^3`; simplify the DBAL tracing feature (#527) ## 4.1.4 (2021-06-18) diff --git a/composer.json b/composer.json index 11f0b314..92b8e9d8 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,7 @@ "symfony/security-core": "^3.4.44||^4.4.20||^5.0.11" }, "require-dev": { - "doctrine/dbal": "^2.10||^3.0", + "doctrine/dbal": "^2.13||^3.0", "doctrine/doctrine-bundle": "^1.12||^2.0", "friendsofphp/php-cs-fixer": "^2.18", "jangregor/phpstan-prophecy": "^0.8", diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index d6b95d77..ed5e529f 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -56,7 +56,12 @@ parameters: path: src/Tracing/Doctrine/DBAL/TracingDriver.php - - message: "#^Parameter \\#2 \\$query of class Doctrine\\\\DBAL\\\\Exception\\\\DriverException constructor expects Doctrine\\\\DBAL\\\\Query\\|null, Doctrine\\\\DBAL\\\\Driver\\\\Exception given\\.$#" + message: "#^Parameter \\#2 \\$query of class Doctrine\\\\DBAL\\\\Exception\\\\DriverException constructor expects Doctrine\\\\DBAL\\\\Query\\|null, Doctrine\\\\DBAL\\\\Driver\\\\DriverException given\\.$#" + count: 1 + path: src/Tracing/Doctrine/DBAL/TracingDriver.php + + - + message: "#^Parameter \\$exception of method Sentry\\\\SentryBundle\\\\Tracing\\\\Doctrine\\\\DBAL\\\\TracingDriver\\:\\:convertException\\(\\) has invalid typehint type Doctrine\\\\DBAL\\\\Driver\\\\DriverException\\.$#" count: 1 path: src/Tracing/Doctrine/DBAL/TracingDriver.php @@ -115,6 +120,11 @@ parameters: count: 1 path: src/aliases.php + - + message: "#^Class Symfony\\\\Bundle\\\\FrameworkBundle\\\\Client not found\\.$#" + count: 1 + path: tests/End2End/TracingEnd2EndTest.php + - message: "#^Call to method getException\\(\\) on an unknown class Symfony\\\\Component\\\\HttpKernel\\\\Event\\\\GetResponseForExceptionEvent\\.$#" count: 1 @@ -235,13 +245,23 @@ parameters: count: 1 path: tests/Tracing/Cache/AbstractTraceableCacheAdapterTest.php + - + message: "#^Class Doctrine\\\\DBAL\\\\Driver\\\\DriverException not found\\.$#" + count: 3 + path: tests/Tracing/Doctrine/DBAL/TracingDriverTest.php + - message: "#^Parameter \\#1 \\$driverException of class Doctrine\\\\DBAL\\\\Exception\\\\DriverException constructor expects Doctrine\\\\DBAL\\\\Driver\\\\Exception, string given\\.$#" count: 1 path: tests/Tracing/Doctrine/DBAL/TracingDriverTest.php - - message: "#^Parameter \\#2 \\$query of class Doctrine\\\\DBAL\\\\Exception\\\\DriverException constructor expects Doctrine\\\\DBAL\\\\Query\\|null, Doctrine\\\\DBAL\\\\Driver\\\\Exception&PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#" + message: "#^Parameter \\#1 \\$originalClassName of method PHPUnit\\\\Framework\\\\TestCase\\:\\:createMock\\(\\) expects class\\-string\\, string given\\.$#" + count: 2 + path: tests/Tracing/Doctrine/DBAL/TracingDriverTest.php + + - + message: "#^Parameter \\#2 \\$query of class Doctrine\\\\DBAL\\\\Exception\\\\DriverException constructor expects Doctrine\\\\DBAL\\\\Query\\|null, Doctrine\\\\DBAL\\\\Driver\\\\DriverException&PHPUnit\\\\Framework\\\\MockObject\\\\MockObject given\\.$#" count: 1 path: tests/Tracing/Doctrine/DBAL/TracingDriverTest.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index f72cae2a..f7d837b6 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,5 +1,5 @@ - + ConsoleListener @@ -8,9 +8,4 @@ public function __construct(HubInterface $hub, bool $captureErrors = true) - - - getItems - - diff --git a/src/DependencyInjection/Compiler/DbalTracingPass.php b/src/DependencyInjection/Compiler/DbalTracingPass.php index 9ce8c26a..28d9b86c 100644 --- a/src/DependencyInjection/Compiler/DbalTracingPass.php +++ b/src/DependencyInjection/Compiler/DbalTracingPass.php @@ -5,6 +5,7 @@ namespace Sentry\SentryBundle\DependencyInjection\Compiler; use Doctrine\DBAL\Driver\ResultStatement; +use Doctrine\DBAL\Result; use Sentry\SentryBundle\Tracing\Doctrine\DBAL\ConnectionConfigurator; use Sentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverMiddleware; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; @@ -39,6 +40,8 @@ public function process(ContainerBuilder $container): void return; } + $this->assertRequiredDbalVersion(); + /** @var string[] $connectionsToTrace */ $connectionsToTrace = $container->getParameter('sentry.tracing.dbal.connections'); @@ -92,4 +95,19 @@ private function getSetMiddlewaresMethodCallArguments(Definition $definition): a return []; } + + private function assertRequiredDbalVersion(): void + { + if (interface_exists(Result::class)) { + // DBAL ^2.13 + return; + } + + if (class_exists(Result::class)) { + // DBAL ^3 + return; + } + + throw new \LogicException('Tracing support cannot be enabled as the Doctrine DBAL 2.13+ package is not installed. Try running "composer require doctrine/dbal:^2.13".'); + } } diff --git a/src/Tracing/Doctrine/DBAL/Compatibility/DriverInterface.php b/src/Tracing/Doctrine/DBAL/Compatibility/DriverInterface.php deleted file mode 100644 index 8d320f5e..00000000 --- a/src/Tracing/Doctrine/DBAL/Compatibility/DriverInterface.php +++ /dev/null @@ -1,12 +0,0 @@ -= 2.10. + * DBAL >= 2.13. * * @internal */ @@ -36,8 +36,6 @@ final class TracingDriver implements DriverInterface, VersionAwarePlatformDriver private $decoratedDriver; /** - * Constructor. - * * @param HubInterface $hub The current hub * @param DriverInterface $decoratedDriver The instance of the driver to decorate */ diff --git a/src/Tracing/Doctrine/DBAL/TracingDriverMiddleware.php b/src/Tracing/Doctrine/DBAL/TracingDriverMiddleware.php index 1a77e9d6..95f6afc5 100644 --- a/src/Tracing/Doctrine/DBAL/TracingDriverMiddleware.php +++ b/src/Tracing/Doctrine/DBAL/TracingDriverMiddleware.php @@ -4,15 +4,15 @@ namespace Sentry\SentryBundle\Tracing\Doctrine\DBAL; -use Doctrine\DBAL\Driver as DriverInterface; -use Doctrine\DBAL\Driver\Middleware as MiddlewareInterface; +use Doctrine\DBAL\Driver; +use Doctrine\DBAL\Driver\Middleware; use Sentry\State\HubInterface; /** - * This middleware wraps a {@see DriverInterface} instance into one that + * This middleware wraps a {@see Driver} instance into one that * supports the distributed tracing feature of Sentry. */ -final class TracingDriverMiddleware implements MiddlewareInterface +final class TracingDriverMiddleware implements Middleware { /** * @var HubInterface The current hub @@ -32,7 +32,7 @@ public function __construct(HubInterface $hub) /** * {@inheritdoc} */ - public function wrap(DriverInterface $driver): DriverInterface + public function wrap(Driver $driver): Driver { return new TracingDriver($this->hub, $driver); } diff --git a/src/aliases.php b/src/aliases.php index ecd968d5..fb37f890 100644 --- a/src/aliases.php +++ b/src/aliases.php @@ -4,20 +4,14 @@ namespace Sentry\SentryBundle; -use Doctrine\DBAL\Driver as DoctrineDriverInterface; -use Doctrine\DBAL\Driver\DriverException as LegacyDriverExceptionInterface; -use Doctrine\DBAL\Driver\Exception as DriverExceptionInterface; use Doctrine\DBAL\Driver\ExceptionConverterDriver as LegacyExceptionConverterDriverInterface; use Doctrine\DBAL\Driver\Middleware as DoctrineMiddlewareInterface; -use Doctrine\DBAL\Driver\Result; -use Doctrine\DBAL\Driver\Statement; use Sentry\SentryBundle\EventListener\ErrorListenerExceptionEvent; use Sentry\SentryBundle\EventListener\RequestListenerControllerEvent; use Sentry\SentryBundle\EventListener\RequestListenerRequestEvent; use Sentry\SentryBundle\EventListener\RequestListenerResponseEvent; use Sentry\SentryBundle\EventListener\RequestListenerTerminateEvent; use Sentry\SentryBundle\EventListener\SubRequestListenerRequestEvent; -use Sentry\SentryBundle\Tracing\Doctrine\DBAL\Compatibility\DriverInterface; use Sentry\SentryBundle\Tracing\Doctrine\DBAL\Compatibility\ExceptionConverterDriverInterface; use Sentry\SentryBundle\Tracing\Doctrine\DBAL\Compatibility\MiddlewareInterface; use Symfony\Component\HttpKernel\Event\ControllerEvent; @@ -94,26 +88,11 @@ class_alias(GetResponseEvent::class, SubRequestListenerRequestEvent::class); } } -if (interface_exists(Statement::class) && !interface_exists(Result::class)) { - /** @psalm-suppress UndefinedClass */ - class_alias(Statement::class, Result::class); -} - -if (interface_exists(DriverExceptionInterface::class) && !interface_exists(LegacyDriverExceptionInterface::class)) { - /** @psalm-suppress UndefinedClass */ - class_alias(DriverExceptionInterface::class, LegacyDriverExceptionInterface::class); -} - if (!interface_exists(DoctrineMiddlewareInterface::class)) { /** @psalm-suppress UndefinedClass */ class_alias(MiddlewareInterface::class, DoctrineMiddlewareInterface::class); } -if (!interface_exists(DoctrineDriverInterface::class)) { - /** @psalm-suppress UndefinedClass */ - class_alias(DriverInterface::class, DoctrineDriverInterface::class); -} - if (!interface_exists(LegacyExceptionConverterDriverInterface::class)) { /** @psalm-suppress UndefinedClass */ class_alias(ExceptionConverterDriverInterface::class, LegacyExceptionConverterDriverInterface::class); diff --git a/tests/DependencyInjection/Compiler/DbalTracingPassTest.php b/tests/DependencyInjection/Compiler/DbalTracingPassTest.php index 8205ddd2..93a6b5d7 100644 --- a/tests/DependencyInjection/Compiler/DbalTracingPassTest.php +++ b/tests/DependencyInjection/Compiler/DbalTracingPassTest.php @@ -83,8 +83,8 @@ public function testProcessWithDoctrineDBALVersionAtLeast30(): void public function testProcessWithDoctrineDBALVersionLowerThan30(): void { - if (self::isDoctrineDBALVersion3Installed()) { - $this->markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be < 3.0.'); + if (!self::isDoctrineDBALVersion2Installed()) { + $this->markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be ^2.13.'); } $connection1 = (new Definition(Connection::class))->setPublic(true); @@ -103,11 +103,30 @@ public function testProcessWithDoctrineDBALVersionLowerThan30(): void $this->assertNull($connection2->getConfigurator()); } + public function testProcessWithDoctrineDBALMissing(): void + { + if (self::isDoctrineDBALInstalled()) { + $this->markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be missing.'); + } + + $container = $this->createContainerBuilder(); + $container->setParameter('sentry.tracing.dbal.connections', ['foo', 'baz']); + + $this->expectException(\LogicException::class); + $this->expectExceptionMessage('Tracing support cannot be enabled as the Doctrine DBAL 2.13+ package is not installed. Try running "composer require doctrine/dbal:^2.13".'); + + $container->compile(); + } + /** * @dataProvider processDoesNothingIfConditionsForEnablingTracingAreMissingDataProvider */ public function testProcessDoesNothingIfConditionsForEnablingTracingAreMissing(ContainerBuilder $container): void { + if (!self::isDoctrineDBALInstalled()) { + $this->markTestSkipped('This test requires the "doctrine/dbal" Composer package.'); + } + $connectionConfigDefinition = new Definition(); $connectionConfigDefinition->setClass(Configuration::class); $connectionConfigDefinition->setPublic(true); @@ -141,6 +160,10 @@ public function processDoesNothingIfConditionsForEnablingTracingAreMissingDataPr public function testContainerCompilationFailsIfConnectionDoesntExist(): void { + if (!self::isDoctrineDBALInstalled()) { + $this->markTestSkipped('This test requires the "doctrine/dbal" Composer package.'); + } + $container = $this->createContainerBuilder(); $container->setParameter('sentry.tracing.dbal.connections', ['missing']); diff --git a/tests/DoctrineTestCase.php b/tests/DoctrineTestCase.php index f5d32981..6d4c7626 100644 --- a/tests/DoctrineTestCase.php +++ b/tests/DoctrineTestCase.php @@ -5,14 +5,27 @@ namespace Sentry\SentryBundle\Tests; use Doctrine\Bundle\DoctrineBundle\DoctrineBundle; +use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver\ResultStatement; use PHPUnit\Framework\TestCase; abstract class DoctrineTestCase extends TestCase { + protected static function isDoctrineDBALInstalled(): bool + { + return interface_exists(Driver::class); + } + + protected static function isDoctrineDBALVersion2Installed(): bool + { + return self::isDoctrineDBALInstalled() + && interface_exists(ResultStatement::class); + } + protected static function isDoctrineDBALVersion3Installed(): bool { - return !interface_exists(ResultStatement::class); + return self::isDoctrineDBALInstalled() + && !self::isDoctrineDBALVersion2Installed(); } protected static function isDoctrineBundlePackageInstalled(): bool diff --git a/tests/End2End/TracingEnd2EndTest.php b/tests/End2End/TracingEnd2EndTest.php index 392f7ff2..7943a22e 100644 --- a/tests/End2End/TracingEnd2EndTest.php +++ b/tests/End2End/TracingEnd2EndTest.php @@ -12,7 +12,6 @@ use Symfony\Component\HttpFoundation\Response; if (!class_exists(KernelBrowser::class)) { - /** @phpstan-ignore-next-line */ class_alias(Client::class, KernelBrowser::class); } diff --git a/tests/Tracing/Doctrine/DBAL/TracingDriverMiddlewareTest.php b/tests/Tracing/Doctrine/DBAL/TracingDriverMiddlewareTest.php index 69dd5683..e0c8d541 100644 --- a/tests/Tracing/Doctrine/DBAL/TracingDriverMiddlewareTest.php +++ b/tests/Tracing/Doctrine/DBAL/TracingDriverMiddlewareTest.php @@ -25,7 +25,7 @@ final class TracingDriverMiddlewareTest extends DoctrineTestCase public static function setUpBeforeClass(): void { - if (!self::isDoctrineBundlePackageInstalled()) { + if (!self::isDoctrineDBALVersion3Installed()) { self::markTestSkipped(); } } diff --git a/tests/Tracing/Doctrine/DBAL/TracingDriverTest.php b/tests/Tracing/Doctrine/DBAL/TracingDriverTest.php index 6fa50e9e..c43590c1 100644 --- a/tests/Tracing/Doctrine/DBAL/TracingDriverTest.php +++ b/tests/Tracing/Doctrine/DBAL/TracingDriverTest.php @@ -10,6 +10,7 @@ use Doctrine\DBAL\Driver\Connection as DriverConnectionInterface; use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface; use Doctrine\DBAL\Driver\ExceptionConverterDriver as ExceptionConverterDriverInterface; +use Doctrine\DBAL\Exception; use Doctrine\DBAL\Exception\DriverException as DBALDriverException; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Schema\AbstractSchemaManager; @@ -217,8 +218,8 @@ public function testCreateDatabasePlatformForVersionWhenDriverDoesNotImplementIn public function testConvertException(): void { - if (self::isDoctrineDBALVersion3Installed()) { - $this->markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be <= 3.0.'); + if (!self::isDoctrineDBALVersion2Installed()) { + $this->markTestSkipped('This test requires the version of the "doctrine/dbal" Composer package to be ^2.13.'); } $exception = $this->createMock(DriverExceptionInterface::class); @@ -249,18 +250,24 @@ public function testConvertExceptionThrowsIfDoctrineDBALVersionIsAtLeast30(): vo } } -if (interface_exists(VersionAwarePlatformDriverInterface::class)) { - interface StubVersionAwarePlatformDriverInterface extends DriverInterface, VersionAwarePlatformDriverInterface - { +if (interface_exists(DriverInterface::class)) { + if (interface_exists(VersionAwarePlatformDriverInterface::class)) { + interface StubVersionAwarePlatformDriverInterface extends DriverInterface, VersionAwarePlatformDriverInterface + { + } } -} -if (interface_exists(ExceptionConverterDriverInterface::class)) { - interface StubExceptionConverterDriverInterface extends ExceptionConverterDriverInterface, DriverInterface - { + if (interface_exists(ExceptionConverterDriverInterface::class)) { + interface StubExceptionConverterDriverInterface extends ExceptionConverterDriverInterface, DriverInterface + { + } + } else { + interface StubExceptionConverterDriverInterface extends DriverInterface + { + } } -} else { - interface StubExceptionConverterDriverInterface extends DriverInterface - { + + if (!interface_exists(DriverExceptionInterface::class)) { + class_alias(Exception::class, DriverExceptionInterface::class); } }