Reworked driver exception tests

1. Got rid of hard-coded exception mocks in favor of PHPUnit-generated ones where possible.
2. Reworked AbstractDriverTest::testConvertsException() to use a data provider to make testing all types of exceptions independent.
3. Updated PHPUnit since the reworked code relies on sebastianbergmann/phpunit#3604.
parent 4d0d2aff
This diff is collapsed.
...@@ -35,22 +35,10 @@ class DBALExceptionTest extends DbalTestCase ...@@ -35,22 +35,10 @@ class DBALExceptionTest extends DbalTestCase
{ {
/** @var Driver $driver */ /** @var Driver $driver */
$driver = $this->createMock(Driver::class); $driver = $this->createMock(Driver::class);
$inner = new class extends Exception implements InnerDriverException
{
/**
* {@inheritDoc}
*/
public function getErrorCode()
{
}
/** /** @var InnerDriverException $inner */
* {@inheritDoc} $inner = $this->createMock(InnerDriverException::class);
*/
public function getSQLState()
{
}
};
$ex = new DriverException('', $inner); $ex = new DriverException('', $inner);
$e = DBALException::driverExceptionDuringQuery($driver, $ex, ''); $e = DBALException::driverExceptionDuringQuery($driver, $ex, '');
self::assertSame($ex, $e); self::assertSame($ex, $e);
......
...@@ -30,6 +30,7 @@ use Doctrine\DBAL\VersionAwarePlatformDriver; ...@@ -30,6 +30,7 @@ use Doctrine\DBAL\VersionAwarePlatformDriver;
use Doctrine\Tests\DbalTestCase; use Doctrine\Tests\DbalTestCase;
use Exception; use Exception;
use ReflectionProperty; use ReflectionProperty;
use function array_merge;
use function get_class; use function get_class;
use function sprintf; use function sprintf;
...@@ -67,29 +68,31 @@ abstract class AbstractDriverTest extends DbalTestCase ...@@ -67,29 +68,31 @@ abstract class AbstractDriverTest extends DbalTestCase
$this->driver = $this->createDriver(); $this->driver = $this->createDriver();
} }
public function testConvertsException() /**
* @dataProvider exceptionConversionProvider
*/
public function testConvertsException($errorCode, $sqlState, $message, string $expectedClass) : void
{ {
if (! $this->driver instanceof ExceptionConverterDriver) { if (! $this->driver instanceof ExceptionConverterDriver) {
$this->markTestSkipped('This test is only intended for exception converter drivers.'); $this->markTestSkipped('This test is only intended for exception converter drivers.');
} }
$data = $this->getExceptionConversions(); $driverException = new class ($errorCode, $sqlState, $message)
extends Exception
implements DriverExceptionInterface
{
/** @var mixed */
private $errorCode;
if (empty($data)) { /** @var mixed */
$this->fail( private $sqlState;
sprintf(
'No test data found for test %s. You have to return test data from %s.',
static::class . '::' . __FUNCTION__,
static::class . '::getExceptionConversionData'
)
);
}
$driverException = new class extends Exception implements DriverExceptionInterface public function __construct($errorCode, $sqlState, $message)
{
public function __construct()
{ {
parent::__construct('baz'); parent::__construct($message);
$this->errorCode = $errorCode;
$this->sqlState = $sqlState;
} }
/** /**
...@@ -97,7 +100,7 @@ abstract class AbstractDriverTest extends DbalTestCase ...@@ -97,7 +100,7 @@ abstract class AbstractDriverTest extends DbalTestCase
*/ */
public function getErrorCode() public function getErrorCode()
{ {
return 'foo'; return $this->errorCode;
} }
/** /**
...@@ -105,26 +108,19 @@ abstract class AbstractDriverTest extends DbalTestCase ...@@ -105,26 +108,19 @@ abstract class AbstractDriverTest extends DbalTestCase
*/ */
public function getSQLState() public function getSQLState()
{ {
return 'bar'; return $this->sqlState;
} }
}; };
$data[] = [$driverException, self::EXCEPTION_DRIVER]; $dbalMessage = 'DBAL exception message';
$dbalException = $this->driver->convertException($dbalMessage, $driverException);
$message = 'DBAL exception message';
foreach ($data as $item) {
/** @var $driverException \Doctrine\DBAL\Driver\DriverException */
[$driverException, $convertedExceptionClassName] = $item;
$convertedException = $this->driver->convertException($message, $driverException);
self::assertSame($convertedExceptionClassName, get_class($convertedException)); self::assertInstanceOf($expectedClass, $dbalException);
self::assertSame($driverException->getErrorCode(), $convertedException->getErrorCode()); self::assertSame($driverException->getErrorCode(), $dbalException->getErrorCode());
self::assertSame($driverException->getSQLState(), $convertedException->getSQLState()); self::assertSame($driverException->getSQLState(), $dbalException->getSQLState());
self::assertSame($message, $convertedException->getMessage()); self::assertSame($driverException, $dbalException->getPrevious());
} self::assertSame($dbalMessage, $dbalException->getMessage());
} }
public function testCreatesDatabasePlatformForVersion() public function testCreatesDatabasePlatformForVersion()
...@@ -246,56 +242,25 @@ abstract class AbstractDriverTest extends DbalTestCase ...@@ -246,56 +242,25 @@ abstract class AbstractDriverTest extends DbalTestCase
return []; return [];
} }
protected function getExceptionConversionData() /**
* @return mixed[][]
*/
public static function exceptionConversionProvider() : iterable
{ {
return []; foreach (static::getExceptionConversionData() as $expectedClass => $items) {
foreach ($items as $item) {
yield array_merge($item, [$expectedClass]);
} }
private function getExceptionConversions()
{
$data = [];
foreach ($this->getExceptionConversionData() as $convertedExceptionClassName => $errors) {
foreach ($errors as $error) {
$driverException = new class ($error[0], $error[1], $error[2])
extends Exception
implements DriverExceptionInterface
{
/** @var mixed */
private $errorCode;
/** @var mixed */
private $sqlState;
public function __construct($errorCode, $sqlState, $message)
{
parent::__construct($message);
$this->errorCode = $errorCode;
$this->sqlState = $sqlState;
} }
/** yield ['foo', 'bar', 'baz', self::EXCEPTION_DRIVER];
* {@inheritDoc}
*/
public function getErrorCode()
{
return $this->errorCode;
} }
/** /**
* {@inheritDoc} * @return array<string,mixed[][]>
*/ */
public function getSQLState() protected static function getExceptionConversionData() : array
{ {
return $this->sqlState; return [];
}
};
$data[] = [$driverException, $convertedExceptionClassName];
}
}
return $data;
} }
} }
...@@ -83,7 +83,10 @@ class AbstractMySQLDriverTest extends AbstractDriverTest ...@@ -83,7 +83,10 @@ class AbstractMySQLDriverTest extends AbstractDriverTest
]; ];
} }
protected function getExceptionConversionData() /**
* {@inheritDoc}
*/
protected static function getExceptionConversionData() : array
{ {
return [ return [
self::EXCEPTION_CONNECTION => [ self::EXCEPTION_CONNECTION => [
......
...@@ -60,7 +60,10 @@ class AbstractOracleDriverTest extends AbstractDriverTest ...@@ -60,7 +60,10 @@ class AbstractOracleDriverTest extends AbstractDriverTest
return new OracleSchemaManager($connection); return new OracleSchemaManager($connection);
} }
protected function getExceptionConversionData() /**
* {@inheritDoc}
*/
protected static function getExceptionConversionData() : array
{ {
return [ return [
self::EXCEPTION_CONNECTION => [ self::EXCEPTION_CONNECTION => [
......
...@@ -77,7 +77,10 @@ class AbstractPostgreSQLDriverTest extends AbstractDriverTest ...@@ -77,7 +77,10 @@ class AbstractPostgreSQLDriverTest extends AbstractDriverTest
]; ];
} }
protected function getExceptionConversionData() /**
* {@inheritDoc}
*/
protected static function getExceptionConversionData() : array
{ {
return [ return [
self::EXCEPTION_CONNECTION => [ self::EXCEPTION_CONNECTION => [
......
...@@ -62,7 +62,10 @@ class AbstractSQLAnywhereDriverTest extends AbstractDriverTest ...@@ -62,7 +62,10 @@ class AbstractSQLAnywhereDriverTest extends AbstractDriverTest
]; ];
} }
protected function getExceptionConversionData() /**
* {@inheritDoc}
*/
protected static function getExceptionConversionData() : array
{ {
return [ return [
self::EXCEPTION_CONNECTION => [ self::EXCEPTION_CONNECTION => [
......
...@@ -42,7 +42,10 @@ class AbstractSQLiteDriverTest extends AbstractDriverTest ...@@ -42,7 +42,10 @@ class AbstractSQLiteDriverTest extends AbstractDriverTest
return new SqliteSchemaManager($connection); return new SqliteSchemaManager($connection);
} }
protected function getExceptionConversionData() /**
* {@inheritDoc}
*/
protected static function getExceptionConversionData() : array
{ {
return [ return [
self::EXCEPTION_CONNECTION => [ self::EXCEPTION_CONNECTION => [
......
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