Skip to content

Instantly share code, notes, and snippets.

@michelv
Created September 29, 2022 21:01
Show Gist options
  • Save michelv/8b378fced30307b43f33d7bc066bb168 to your computer and use it in GitHub Desktop.
Save michelv/8b378fced30307b43f33d7bc066bb168 to your computer and use it in GitHub Desktop.
one way to simulate a DBAL DriverException in your PHPunit tests
<?php
declare(strict_types=1);
use Doctrine\DBAL\Driver\Exception as TheDriverException;
use Doctrine\DBAL\Exception\DriverException;
use PHPUnit\Framework\TestCase;
final class MyTest extends TestCase
{
// ...
private function getDriverExceptionWithCode(int $code): DriverException
{
$theDriverException = new class($code) extends \Exception implements TheDriverException {
public function __construct(int $code)
{
parent::__construct('oh no, you broke it :(', $code);
}
public function getSQLState(): ?string
{
return null;
}
};
return new DriverException($theDriverException, null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment