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
{
/** @var Driver $driver */
$driver = $this->createMock(Driver::class);
$inner = new class extends Exception implements InnerDriverException
{
/**
* {@inheritDoc}
*/
public function getErrorCode()
{
}
/**
* {@inheritDoc}
*/
public function getSQLState()
{
}
};
/** @var InnerDriverException $inner */
$inner = $this->createMock(InnerDriverException::class);
$ex = new DriverException('', $inner);
$e = DBALException::driverExceptionDuringQuery($driver, $ex, '');
self::assertSame($ex, $e);
......
......@@ -30,6 +30,7 @@ use Doctrine\DBAL\VersionAwarePlatformDriver;
use Doctrine\Tests\DbalTestCase;
use Exception;
use ReflectionProperty;
use function array_merge;
use function get_class;
use function sprintf;
......@@ -67,29 +68,31 @@ abstract class AbstractDriverTest extends DbalTestCase
$this->driver = $this->createDriver();
}
public function testConvertsException()
/**
* @dataProvider exceptionConversionProvider
*/
public function testConvertsException($errorCode, $sqlState, $message, string $expectedClass) : void
{
if (! $this->driver instanceof ExceptionConverterDriver) {
$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)) {
$this->fail(
sprintf(
'No test data found for test %s. You have to return test data from %s.',
static::class . '::' . __FUNCTION__,
static::class . '::getExceptionConversionData'
)
);
}
/** @var mixed */
private $sqlState;
$driverException = new class extends Exception implements DriverExceptionInterface
{
public function __construct()
public function __construct($errorCode, $sqlState, $message)
{
parent::__construct('baz');
parent::__construct($message);
$this->errorCode = $errorCode;
$this->sqlState = $sqlState;
}
/**
......@@ -97,7 +100,7 @@ abstract class AbstractDriverTest extends DbalTestCase
*/
public function getErrorCode()
{
return 'foo';
return $this->errorCode;
}
/**
......@@ -105,26 +108,19 @@ abstract class AbstractDriverTest extends DbalTestCase
*/
public function getSQLState()
{
return 'bar';
return $this->sqlState;
}
};
$data[] = [$driverException, self::EXCEPTION_DRIVER];
$message = 'DBAL exception message';
foreach ($data as $item) {
/** @var $driverException \Doctrine\DBAL\Driver\DriverException */
[$driverException, $convertedExceptionClassName] = $item;
$convertedException = $this->driver->convertException($message, $driverException);
$dbalMessage = 'DBAL exception message';
$dbalException = $this->driver->convertException($dbalMessage, $driverException);
self::assertSame($convertedExceptionClassName, get_class($convertedException));
self::assertInstanceOf($expectedClass, $dbalException);
self::assertSame($driverException->getErrorCode(), $convertedException->getErrorCode());
self::assertSame($driverException->getSQLState(), $convertedException->getSQLState());
self::assertSame($message, $convertedException->getMessage());
}
self::assertSame($driverException->getErrorCode(), $dbalException->getErrorCode());
self::assertSame($driverException->getSQLState(), $dbalException->getSQLState());
self::assertSame($driverException, $dbalException->getPrevious());
self::assertSame($dbalMessage, $dbalException->getMessage());
}
public function testCreatesDatabasePlatformForVersion()
......@@ -246,56 +242,25 @@ abstract class AbstractDriverTest extends DbalTestCase
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;
}
/**
* {@inheritDoc}
*/
public function getErrorCode()
{
return $this->errorCode;
yield ['foo', 'bar', 'baz', self::EXCEPTION_DRIVER];
}
/**
* {@inheritDoc}
* @return array<string,mixed[][]>
*/
public function getSQLState()
protected static function getExceptionConversionData() : array
{
return $this->sqlState;
}
};
$data[] = [$driverException, $convertedExceptionClassName];
}
}
return $data;
return [];
}
}
......@@ -83,7 +83,10 @@ class AbstractMySQLDriverTest extends AbstractDriverTest
];
}
protected function getExceptionConversionData()
/**
* {@inheritDoc}
*/
protected static function getExceptionConversionData() : array
{
return [
self::EXCEPTION_CONNECTION => [
......
......@@ -60,7 +60,10 @@ class AbstractOracleDriverTest extends AbstractDriverTest
return new OracleSchemaManager($connection);
}
protected function getExceptionConversionData()
/**
* {@inheritDoc}
*/
protected static function getExceptionConversionData() : array
{
return [
self::EXCEPTION_CONNECTION => [
......
......@@ -77,7 +77,10 @@ class AbstractPostgreSQLDriverTest extends AbstractDriverTest
];
}
protected function getExceptionConversionData()
/**
* {@inheritDoc}
*/
protected static function getExceptionConversionData() : array
{
return [
self::EXCEPTION_CONNECTION => [
......
......@@ -62,7 +62,10 @@ class AbstractSQLAnywhereDriverTest extends AbstractDriverTest
];
}
protected function getExceptionConversionData()
/**
* {@inheritDoc}
*/
protected static function getExceptionConversionData() : array
{
return [
self::EXCEPTION_CONNECTION => [
......
......@@ -42,7 +42,10 @@ class AbstractSQLiteDriverTest extends AbstractDriverTest
return new SqliteSchemaManager($connection);
}
protected function getExceptionConversionData()
/**
* {@inheritDoc}
*/
protected static function getExceptionConversionData() : array
{
return [
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