Correcting mocking of `Throwable` implementations, which is impossible in bare PHPUnit

parent 4758f426
...@@ -4,20 +4,41 @@ namespace Doctrine\Tests\DBAL; ...@@ -4,20 +4,41 @@ namespace Doctrine\Tests\DBAL;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception\DriverException; use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Driver\DriverException as InnerDriverException;
use Doctrine\Tests\DbalTestCase;
use Doctrine\DBAL\Driver;
class DBALExceptionTest extends \Doctrine\Tests\DbalTestCase class DBALExceptionTest extends DbalTestCase
{ {
public function testDriverExceptionDuringQueryAcceptsBinaryData() public function testDriverExceptionDuringQueryAcceptsBinaryData()
{ {
$driver = $this->createMock('\Doctrine\DBAL\Driver'); /* @var $driver Driver */
$driver = $this->createMock(Driver::class);
$e = DBALException::driverExceptionDuringQuery($driver, new \Exception, '', array('ABC', chr(128))); $e = DBALException::driverExceptionDuringQuery($driver, new \Exception, '', array('ABC', chr(128)));
$this->assertContains('with params ["ABC", "\x80"]', $e->getMessage()); $this->assertContains('with params ["ABC", "\x80"]', $e->getMessage());
} }
public function testAvoidOverWrappingOnDriverException() public function testAvoidOverWrappingOnDriverException()
{ {
$driver = $this->createMock('\Doctrine\DBAL\Driver'); /* @var $driver Driver */
$ex = new DriverException('', $this->createMock('\Doctrine\DBAL\Driver\DriverException')); $driver = $this->createMock(Driver::class);
$inner = new class extends \Exception implements InnerDriverException
{
/**
* {@inheritDoc}
*/
public function getErrorCode()
{
}
/**
* {@inheritDoc}
*/
public function getSQLState()
{
}
};
$ex = new DriverException('', $inner);
$e = DBALException::driverExceptionDuringQuery($driver, $ex, ''); $e = DBALException::driverExceptionDuringQuery($driver, $ex, '');
$this->assertSame($ex, $e); $this->assertSame($ex, $e);
} }
...@@ -27,11 +48,11 @@ class DBALExceptionTest extends \Doctrine\Tests\DbalTestCase ...@@ -27,11 +48,11 @@ class DBALExceptionTest extends \Doctrine\Tests\DbalTestCase
$url = 'mysql://localhost'; $url = 'mysql://localhost';
$exception = DBALException::driverRequired($url); $exception = DBALException::driverRequired($url);
$this->assertInstanceOf('Doctrine\DBAL\DBALException', $exception); $this->assertInstanceOf(DBALException::class, $exception);
$this->assertSame( $this->assertSame(
sprintf( sprintf(
"The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " . "The options 'driver' or 'driverClass' are mandatory if a connection URL without scheme " .
"is given to DriverManager::getConnection(). Given URL: %s", 'is given to DriverManager::getConnection(). Given URL: %s',
$url $url
), ),
$exception->getMessage() $exception->getMessage()
......
...@@ -3,9 +3,11 @@ ...@@ -3,9 +3,11 @@
namespace Doctrine\Tests\DBAL\Driver; namespace Doctrine\Tests\DBAL\Driver;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver\DriverException;
use Doctrine\DBAL\Driver\ExceptionConverterDriver; use Doctrine\DBAL\Driver\ExceptionConverterDriver;
use Doctrine\DBAL\VersionAwarePlatformDriver; use Doctrine\DBAL\VersionAwarePlatformDriver;
use Doctrine\Tests\DbalTestCase; use Doctrine\Tests\DbalTestCase;
use Throwable;
abstract class AbstractDriverTest extends DbalTestCase abstract class AbstractDriverTest extends DbalTestCase
{ {
...@@ -59,19 +61,29 @@ abstract class AbstractDriverTest extends DbalTestCase ...@@ -59,19 +61,29 @@ abstract class AbstractDriverTest extends DbalTestCase
); );
} }
$driverException = $this->createMock('Doctrine\DBAL\Driver\DriverException'); $driverException = new class extends \Exception implements DriverException
{
$driverException->expects($this->any()) public function __construct()
->method('getErrorCode') {
->will($this->returnValue('foo')); parent::__construct('baz');
}
$driverException->expects($this->any()) /**
->method('getSQLState') * {@inheritDoc}
->will($this->returnValue('bar')); */
public function getErrorCode()
{
return 'foo';
}
$driverException->expects($this->any()) /**
->method('getMessage') * {@inheritDoc}
->will($this->returnValue('baz')); */
public function getSQLState()
{
return 'bar';
}
};
$data[] = array($driverException, self::EXCEPTION_DRIVER); $data[] = array($driverException, self::EXCEPTION_DRIVER);
...@@ -209,19 +221,44 @@ abstract class AbstractDriverTest extends DbalTestCase ...@@ -209,19 +221,44 @@ abstract class AbstractDriverTest extends DbalTestCase
foreach ($this->getExceptionConversionData() as $convertedExceptionClassName => $errors) { foreach ($this->getExceptionConversionData() as $convertedExceptionClassName => $errors) {
foreach ($errors as $error) { foreach ($errors as $error) {
$driverException = $this->createMock('Doctrine\DBAL\Driver\DriverException'); $driverException = new class ($error[0], $error[1], $error[2])
extends \Exception
$driverException->expects($this->any()) implements DriverException
->method('getErrorCode') {
->will($this->returnValue($error[0])); /**
* @var mixed
$driverException->expects($this->any()) */
->method('getSQLState') private $errorCode;
->will($this->returnValue($error[1]));
/**
$driverException->expects($this->any()) * @var mixed
->method('getMessage') */
->will($this->returnValue($error[2])); private $sqlState;
public function __construct($errorCode, $sqlState, $message)
{
parent::__construct($message);
$this->errorCode = $errorCode;
$this->sqlState = $sqlState;
}
/**
* {@inheritDoc}
*/
public function getErrorCode()
{
return $this->errorCode;
}
/**
* {@inheritDoc}
*/
public function getSQLState()
{
return $this->sqlState;
}
};
$data[] = array($driverException, $convertedExceptionClassName); $data[] = array($driverException, $convertedExceptionClassName);
} }
......
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