Skip to content

Commit

Permalink
Correcting mocking of Throwable implementations, which is impossibl…
Browse files Browse the repository at this point in the history
…e in bare PHPUnit
  • Loading branch information
Ocramius committed Jul 22, 2017
1 parent 4758f42 commit 047d0ec
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 30 deletions.
33 changes: 27 additions & 6 deletions tests/Doctrine/Tests/DBAL/DBALExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,41 @@

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);
}
Expand All @@ -27,11 +48,11 @@ public function testDriverRequiredWithUrl()
$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()
Expand Down
85 changes: 61 additions & 24 deletions tests/Doctrine/Tests/DBAL/Driver/AbstractDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -59,19 +61,29 @@ public function testConvertsException()
);
}

$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);

Expand Down Expand Up @@ -209,19 +221,44 @@ private function getExceptionConversions()

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);
}
Expand Down

0 comments on commit 047d0ec

Please sign in to comment.