Replace DefaultExceptionConverter with driver-specific implementations

parent 17e6eaed
...@@ -2,12 +2,13 @@ ...@@ -2,12 +2,13 @@
declare(strict_types=1); 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\Driver\Exception;
use Doctrine\DBAL\Exception\DriverException; use Doctrine\DBAL\Exception\DriverException;
final class DefaultExceptionConverter implements ExceptionConverter final class ExceptionConverter implements ExceptionConverterInterface
{ {
public function convert(string $message, Exception $exception): DriverException public function convert(string $message, Exception $exception): DriverException
{ {
......
<?php
declare(strict_types=1);
namespace Doctrine\DBAL\Driver\API\SQLSrv;
use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface;
use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Exception\DriverException;
final class ExceptionConverter implements ExceptionConverterInterface
{
public function convert(string $message, Exception $exception): DriverException
{
return new DriverException($message, $exception);
}
}
...@@ -4,13 +4,13 @@ namespace Doctrine\DBAL\Driver; ...@@ -4,13 +4,13 @@ namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\API\DefaultExceptionConverter; use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface;
use Doctrine\DBAL\Driver\API\ExceptionConverter; use Doctrine\DBAL\Driver\API\IBMDB2\ExceptionConverter;
use Doctrine\DBAL\Platforms\DB2Platform; use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\DBAL\Schema\DB2SchemaManager; use Doctrine\DBAL\Schema\DB2SchemaManager;
/** /**
* Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for IBM DB2 based drivers. * Abstract base implementation of the {@link Driver} interface for IBM DB2 based drivers.
*/ */
abstract class AbstractDB2Driver implements Driver abstract class AbstractDB2Driver implements Driver
{ {
...@@ -30,8 +30,8 @@ abstract class AbstractDB2Driver implements Driver ...@@ -30,8 +30,8 @@ abstract class AbstractDB2Driver implements Driver
return new DB2SchemaManager($conn); return new DB2SchemaManager($conn);
} }
public function getExceptionConverter(): ExceptionConverter public function getExceptionConverter(): ExceptionConverterInterface
{ {
return new DefaultExceptionConverter(); return new ExceptionConverter();
} }
} }
...@@ -4,13 +4,13 @@ namespace Doctrine\DBAL\Driver; ...@@ -4,13 +4,13 @@ namespace Doctrine\DBAL\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\API\DefaultExceptionConverter; use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface;
use Doctrine\DBAL\Driver\API\ExceptionConverter; use Doctrine\DBAL\Driver\API\SQLSrv\ExceptionConverter;
use Doctrine\DBAL\Platforms\SQLServer2012Platform; use Doctrine\DBAL\Platforms\SQLServer2012Platform;
use Doctrine\DBAL\Schema\SQLServerSchemaManager; use Doctrine\DBAL\Schema\SQLServerSchemaManager;
/** /**
* Abstract base implementation of the {@link Doctrine\DBAL\Driver} interface for Microsoft SQL Server based drivers. * Abstract base implementation of the {@link Driver} interface for Microsoft SQL Server based drivers.
*/ */
abstract class AbstractSQLServerDriver implements Driver abstract class AbstractSQLServerDriver implements Driver
{ {
...@@ -30,8 +30,8 @@ abstract class AbstractSQLServerDriver implements Driver ...@@ -30,8 +30,8 @@ abstract class AbstractSQLServerDriver implements Driver
return new SQLServerSchemaManager($conn); return new SQLServerSchemaManager($conn);
} }
public function getExceptionConverter(): ExceptionConverter public function getExceptionConverter(): ExceptionConverterInterface
{ {
return new DefaultExceptionConverter(); return new ExceptionConverter();
} }
} }
...@@ -4,8 +4,9 @@ namespace Doctrine\Tests\DBAL\Connection; ...@@ -4,8 +4,9 @@ namespace Doctrine\Tests\DBAL\Connection;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\API\DefaultExceptionConverter; use Doctrine\DBAL\Driver\API\ExceptionConverter;
use Doctrine\DBAL\Driver\Exception as DriverException; use Doctrine\DBAL\Driver\Exception as DriverException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use function chr; use function chr;
...@@ -16,34 +17,43 @@ final class ExceptionHandlingTest extends TestCase ...@@ -16,34 +17,43 @@ final class ExceptionHandlingTest extends TestCase
/** @var Connection */ /** @var Connection */
private $connection; private $connection;
/** @var ExceptionConverter&MockObject */
private $exceptionConverter;
protected function setUp(): void protected function setUp(): void
{ {
$this->exceptionConverter = $this->createMock(ExceptionConverter::class);
$this->connection = new Connection([], $this->createConfiguredMock(Driver::class, [ $this->connection = new Connection([], $this->createConfiguredMock(Driver::class, [
'getExceptionConverter' => new DefaultExceptionConverter(), 'getExceptionConverter' => $this->exceptionConverter,
])); ]));
} }
public function testDriverExceptionDuringQueryAcceptsBinaryData(): void 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), $this->createMock(DriverException::class),
'', '',
['ABC', chr(128)] ['ABC', chr(128)]
); );
self::assertStringContainsString('with params ["ABC", "\x80"]', $e->getMessage());
} }
public function testDriverExceptionDuringQueryAcceptsResource(): void 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), $this->createMock(DriverException::class),
'INSERT INTO file (`content`) VALUES (?)', 'INSERT INTO file (`content`) VALUES (?)',
[ [
1 => fopen(__FILE__, 'r'), 1 => fopen(__FILE__, 'r'),
] ]
); );
self::assertStringContainsString('Resource', $e->getMessage());
} }
} }
...@@ -5,8 +5,8 @@ namespace Doctrine\DBAL\Tests\Driver; ...@@ -5,8 +5,8 @@ namespace Doctrine\DBAL\Tests\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\AbstractDB2Driver; use Doctrine\DBAL\Driver\AbstractDB2Driver;
use Doctrine\DBAL\Driver\API\DefaultExceptionConverter; use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface;
use Doctrine\DBAL\Driver\API\ExceptionConverter; use Doctrine\DBAL\Driver\API\IBMDB2\ExceptionConverter;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\DB2Platform; use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\AbstractSchemaManager;
...@@ -29,8 +29,8 @@ class AbstractDB2DriverTest extends AbstractDriverTest ...@@ -29,8 +29,8 @@ class AbstractDB2DriverTest extends AbstractDriverTest
return new DB2SchemaManager($connection); return new DB2SchemaManager($connection);
} }
protected function createExceptionConverter(): ExceptionConverter protected function createExceptionConverter(): ExceptionConverterInterface
{ {
return new DefaultExceptionConverter(); return new ExceptionConverter();
} }
} }
...@@ -4,8 +4,8 @@ namespace Doctrine\DBAL\Tests\Driver; ...@@ -4,8 +4,8 @@ namespace Doctrine\DBAL\Tests\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\AbstractSQLServerDriver\Exception\PortWithoutHost; use Doctrine\DBAL\Driver\AbstractSQLServerDriver\Exception\PortWithoutHost;
use Doctrine\DBAL\Driver\API\DefaultExceptionConverter; use Doctrine\DBAL\Driver\API\ExceptionConverter as ExceptionConverterInterface;
use Doctrine\DBAL\Driver\API\ExceptionConverter; use Doctrine\DBAL\Driver\API\SQLSrv\ExceptionConverter;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SQLServer2012Platform; use Doctrine\DBAL\Platforms\SQLServer2012Platform;
use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\AbstractSchemaManager;
...@@ -23,9 +23,9 @@ abstract class AbstractSQLServerDriverTest extends AbstractDriverTest ...@@ -23,9 +23,9 @@ abstract class AbstractSQLServerDriverTest extends AbstractDriverTest
return new SQLServerSchemaManager($connection); return new SQLServerSchemaManager($connection);
} }
protected function createExceptionConverter(): ExceptionConverter protected function createExceptionConverter(): ExceptionConverterInterface
{ {
return new DefaultExceptionConverter(); return new ExceptionConverter();
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment