Created
September 29, 2022 21:01
-
-
Save michelv/8b378fced30307b43f33d7bc066bb168 to your computer and use it in GitHub Desktop.
one way to simulate a DBAL DriverException in your PHPunit tests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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