From 597da1781cea0778b14851e700bb2e9eb4bbf1d1 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sat, 4 Jul 2020 16:14:55 -0700 Subject: [PATCH] Replace DefaultExceptionConverter with driver-specific implementations --- .../ExceptionConverter.php} | 5 ++-- src/Driver/API/SQLSrv/ExceptionConverter.php | 17 ++++++++++++ src/Driver/AbstractDB2Driver.php | 10 +++---- src/Driver/AbstractSQLServerDriver.php | 10 +++---- tests/Connection/ExceptionHandlingTest.php | 26 +++++++++++++------ tests/Driver/AbstractDB2DriverTest.php | 8 +++--- tests/Driver/AbstractSQLServerDriverTest.php | 8 +++--- 7 files changed, 56 insertions(+), 28 deletions(-) rename src/Driver/API/{DefaultExceptionConverter.php => IBMDB2/ExceptionConverter.php} (58%) create mode 100644 src/Driver/API/SQLSrv/ExceptionConverter.php diff --git a/src/Driver/API/DefaultExceptionConverter.php b/src/Driver/API/IBMDB2/ExceptionConverter.php similarity index 58% rename from src/Driver/API/DefaultExceptionConverter.php rename to src/Driver/API/IBMDB2/ExceptionConverter.php index fa28b4fe473..2f7035326e4 100644 --- a/src/Driver/API/DefaultExceptionConverter.php +++ b/src/Driver/API/IBMDB2/ExceptionConverter.php @@ -2,12 +2,13 @@ declare(strict_types=1); -namespace Doctrine\DBAL\Driver\API; +namespace Doctrine\DBAL\Driver\API\IBMDB2; +use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface; use Doctrine\DBAL\Driver\Exception; use Doctrine\DBAL\Exception\DriverException; -final class DefaultExceptionConverter implements ExceptionConverter +final class ExceptionConverter implements ExceptionConverterInterface { public function convert(string $message, Exception $exception): DriverException { diff --git a/src/Driver/API/SQLSrv/ExceptionConverter.php b/src/Driver/API/SQLSrv/ExceptionConverter.php new file mode 100644 index 00000000000..37b1c7cbcec --- /dev/null +++ b/src/Driver/API/SQLSrv/ExceptionConverter.php @@ -0,0 +1,17 @@ +exceptionConverter = $this->createMock(ExceptionConverter::class); + $this->connection = new Connection([], $this->createConfiguredMock(Driver::class, [ - 'getExceptionConverter' => new DefaultExceptionConverter(), + 'getExceptionConverter' => $this->exceptionConverter, ])); } public function testDriverExceptionDuringQueryAcceptsBinaryData(): void { - $e = $this->connection->convertExceptionDuringQuery( + $this->exceptionConverter->expects(self::once()) + ->method('convert') + ->with(self::stringContains('with params ["ABC", "\x80"]')); + + $this->connection->convertExceptionDuringQuery( $this->createMock(DriverException::class), '', ['ABC', chr(128)] ); - - self::assertStringContainsString('with params ["ABC", "\x80"]', $e->getMessage()); } public function testDriverExceptionDuringQueryAcceptsResource(): void { - $e = $this->connection->convertExceptionDuringQuery( + $this->exceptionConverter->expects(self::once()) + ->method('convert') + ->with(self::stringContains('Resource')); + + $this->connection->convertExceptionDuringQuery( $this->createMock(DriverException::class), 'INSERT INTO file (`content`) VALUES (?)', [ 1 => fopen(__FILE__, 'r'), ] ); - - self::assertStringContainsString('Resource', $e->getMessage()); } } diff --git a/tests/Driver/AbstractDB2DriverTest.php b/tests/Driver/AbstractDB2DriverTest.php index 52b614c38ac..6439a54f6ee 100644 --- a/tests/Driver/AbstractDB2DriverTest.php +++ b/tests/Driver/AbstractDB2DriverTest.php @@ -5,8 +5,8 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver\AbstractDB2Driver; -use Doctrine\DBAL\Driver\API\DefaultExceptionConverter; -use Doctrine\DBAL\Driver\API\ExceptionConverter; +use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface; +use Doctrine\DBAL\Driver\API\IBMDB2\ExceptionConverter; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\DB2Platform; use Doctrine\DBAL\Schema\AbstractSchemaManager; @@ -29,8 +29,8 @@ protected function createSchemaManager(Connection $connection): AbstractSchemaMa return new DB2SchemaManager($connection); } - protected function createExceptionConverter(): ExceptionConverter + protected function createExceptionConverter(): ExceptionConverterInterface { - return new DefaultExceptionConverter(); + return new ExceptionConverter(); } } diff --git a/tests/Driver/AbstractSQLServerDriverTest.php b/tests/Driver/AbstractSQLServerDriverTest.php index 4ef2a2c1bd5..cb94e9d1555 100644 --- a/tests/Driver/AbstractSQLServerDriverTest.php +++ b/tests/Driver/AbstractSQLServerDriverTest.php @@ -4,8 +4,8 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Driver\AbstractSQLServerDriver\Exception\PortWithoutHost; -use Doctrine\DBAL\Driver\API\DefaultExceptionConverter; -use Doctrine\DBAL\Driver\API\ExceptionConverter; +use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface; +use Doctrine\DBAL\Driver\API\SQLSrv\ExceptionConverter; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\SQLServer2012Platform; use Doctrine\DBAL\Schema\AbstractSchemaManager; @@ -23,9 +23,9 @@ protected function createSchemaManager(Connection $connection): AbstractSchemaMa return new SQLServerSchemaManager($connection); } - protected function createExceptionConverter(): ExceptionConverter + protected function createExceptionConverter(): ExceptionConverterInterface { - return new DefaultExceptionConverter(); + return new ExceptionConverter(); } /**