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

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