From b4068934c30000b8a6889000e72402369bbb7871 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Mon, 17 Sep 2018 13:01:09 +0200 Subject: [PATCH] PdoDriver: check for misconfigured PDO connections resource (#294) --- src/Dibi/Drivers/PdoDriver.php | 4 ++ tests/dibi/PdoDriver.providedConnection.phpt | 41 ++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 tests/dibi/PdoDriver.providedConnection.phpt 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); +});