Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace DefaultExceptionConverter with driver-specific implementations #4149

Merged
merged 1 commit into from
Jul 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
17 changes: 17 additions & 0 deletions src/Driver/API/SQLSrv/ExceptionConverter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?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);
}
}
10 changes: 5 additions & 5 deletions src/Driver/AbstractDB2Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
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\DB2Platform;
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
{
Expand All @@ -30,8 +30,8 @@ public function getSchemaManager(Connection $conn)
return new DB2SchemaManager($conn);
}

public function getExceptionConverter(): ExceptionConverter
public function getExceptionConverter(): ExceptionConverterInterface
{
return new DefaultExceptionConverter();
return new ExceptionConverter();
}
}
10 changes: 5 additions & 5 deletions src/Driver/AbstractSQLServerDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
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\SQLServer2012Platform;
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
{
Expand All @@ -30,8 +30,8 @@ public function getSchemaManager(Connection $conn)
return new SQLServerSchemaManager($conn);
}

public function getExceptionConverter(): ExceptionConverter
public function getExceptionConverter(): ExceptionConverterInterface
{
return new DefaultExceptionConverter();
return new ExceptionConverter();
}
}
26 changes: 18 additions & 8 deletions tests/Connection/ExceptionHandlingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\API\DefaultExceptionConverter;
use Doctrine\DBAL\Driver\API\ExceptionConverter;
use Doctrine\DBAL\Driver\Exception as DriverException;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

use function chr;
Expand All @@ -16,34 +17,43 @@ final class ExceptionHandlingTest extends TestCase
/** @var Connection */
private $connection;

/** @var ExceptionConverter&MockObject */
private $exceptionConverter;

protected function setUp(): void
{
$this->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());
}
}
8 changes: 4 additions & 4 deletions tests/Driver/AbstractDB2DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}
8 changes: 4 additions & 4 deletions tests/Driver/AbstractSQLServerDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}

/**
Expand Down