diff --git a/src/Dibi/Drivers/PdoDriver.php b/src/Dibi/Drivers/PdoDriver.php index 109829732..82991dd82 100644 --- a/src/Dibi/Drivers/PdoDriver.php +++ b/src/Dibi/Drivers/PdoDriver.php @@ -69,6 +69,10 @@ public function __construct(array $config) } } + if ($this->connection->getAttribute(PDO::ATTR_ERRMODE) !== PDO::ERRMODE_SILENT) { + throw new Dibi\DriverException('PDO connection in exception or warning error mode is not supported.'); + } + $this->driverName = $this->connection->getAttribute(PDO::ATTR_DRIVER_NAME); $this->serverVersion = (string) ($config['version'] ?? @$this->connection->getAttribute(PDO::ATTR_SERVER_VERSION)); // @ - may be not supported } diff --git a/tests/dibi/PdoDriver.providedConnection.phpt b/tests/dibi/PdoDriver.providedConnection.phpt new file mode 100644 index 000000000..6f2ba007e --- /dev/null +++ b/tests/dibi/PdoDriver.providedConnection.phpt @@ -0,0 +1,41 @@ +setAttribute(PDO::ATTR_ERRMODE, $errorMode); + } + new Dibi\Drivers\PdoDriver(['resource' => $pdo]); +} + + +// PDO error mode: exception +Assert::exception(function () { + buildPdoDriver(PDO::ERRMODE_EXCEPTION); +}, Dibi\DriverException::class, 'PDO connection in exception or warning error mode is not supported.'); + + +// PDO error mode: warning +Assert::exception(function () { + buildPdoDriver(PDO::ERRMODE_WARNING); +}, Dibi\DriverException::class, 'PDO connection in exception or warning error mode is not supported.'); + + +// PDO error mode: explicitly set silent +test(function () { + buildPdoDriver(PDO::ERRMODE_SILENT); +}); + + +// PDO error mode: implicitly set silent +test(function () { + buildPdoDriver(null); +});