From 303d46f1447abb8ad926216a2a091f7bdd0c1832 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Mon, 11 Jun 2018 15:04:55 +0200 Subject: [PATCH 01/11] PdoDriver: check for misconfigured PDO connections resource --- src/Dibi/Drivers/PdoDriver.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Dibi/Drivers/PdoDriver.php b/src/Dibi/Drivers/PdoDriver.php index 66321c153..7b8ad673a 100644 --- a/src/Dibi/Drivers/PdoDriver.php +++ b/src/Dibi/Drivers/PdoDriver.php @@ -71,6 +71,10 @@ public function __construct(array &$config) $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 + + if (!\in_array($this->connection->getAttribute(\PDO::ATTR_ERRMODE), [null, \PDO::ERRMODE_WARNING], true)) { + throw new Dibi\DriverException('PDO connection in other error mode then WARNING (default) is currently not supported. Consider upgrading to Dibi >=4.1.0.'); + } } From c3c0240e84f48e4752822e4db130847ba944bfc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Fri, 6 Jul 2018 17:48:00 +0200 Subject: [PATCH 02/11] PdoDriver: Only SILENT mode should be allowed as warning will not be catched --- src/Dibi/Drivers/PdoDriver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Dibi/Drivers/PdoDriver.php b/src/Dibi/Drivers/PdoDriver.php index 7b8ad673a..7be649c20 100644 --- a/src/Dibi/Drivers/PdoDriver.php +++ b/src/Dibi/Drivers/PdoDriver.php @@ -72,8 +72,8 @@ public function __construct(array &$config) $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 - if (!\in_array($this->connection->getAttribute(\PDO::ATTR_ERRMODE), [null, \PDO::ERRMODE_WARNING], true)) { - throw new Dibi\DriverException('PDO connection in other error mode then WARNING (default) is currently not supported. Consider upgrading to Dibi >=4.1.0.'); + if ($this->connection->getAttribute(\PDO::ATTR_ERRMODE) !== \PDO::ERRMODE_SILENT) { + throw new Dibi\DriverException('PDO connection in exception or warning error mode is currently not supported. Consider upgrading to Dibi >=4.1.0.'); } } From 73fef35f720b7e4ae91db4581311d827abad56ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Fri, 6 Jul 2018 17:56:06 +0200 Subject: [PATCH 03/11] conding standard --- src/Dibi/Drivers/PdoDriver.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Dibi/Drivers/PdoDriver.php b/src/Dibi/Drivers/PdoDriver.php index 7be649c20..3a53f2e30 100644 --- a/src/Dibi/Drivers/PdoDriver.php +++ b/src/Dibi/Drivers/PdoDriver.php @@ -72,7 +72,7 @@ public function __construct(array &$config) $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 - if ($this->connection->getAttribute(\PDO::ATTR_ERRMODE) !== \PDO::ERRMODE_SILENT) { + if ($this->connection->getAttribute(\PDO::ATTR_ERRMODE) !== \PDO::ERRMODE_SILENT) { throw new Dibi\DriverException('PDO connection in exception or warning error mode is currently not supported. Consider upgrading to Dibi >=4.1.0.'); } } From f25bd10c66d1a127d5d7f5826347ed16e460eb2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Thu, 19 Jul 2018 17:28:12 +0200 Subject: [PATCH 04/11] PdoDriver: added tests for misconfigured PDO connection --- tests/dibi/PdoDriver.providedConnection.phpt | 48 ++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/dibi/PdoDriver.providedConnection.phpt diff --git a/tests/dibi/PdoDriver.providedConnection.phpt b/tests/dibi/PdoDriver.providedConnection.phpt new file mode 100644 index 000000000..be222d559 --- /dev/null +++ b/tests/dibi/PdoDriver.providedConnection.phpt @@ -0,0 +1,48 @@ + $pdo]; + return new \Dibi\Drivers\PdoDriver($driverConfig); +} + +// PDO error mode: exception +Assert::exception(function() use ($config) { + ($pdoConnection = new PDO($config['dsn']))->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + buildPdoDriver($pdoConnection); +}, \Dibi\DriverException::class, 'PDO connection in exception or warning error mode is currently not supported. Consider upgrading to Dibi >=4.1.0.'); + + +// PDO error mode: warning +Assert::exception(function() use ($config) { + ($pdoConnection = new PDO($config['dsn']))->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + buildPdoDriver($pdoConnection); +}, \Dibi\DriverException::class, 'PDO connection in exception or warning error mode is currently not supported. Consider upgrading to Dibi >=4.1.0.'); + + +// PDO error mode: explicitly set silent +test(function() use ($config) { + ($pdoConnection = new PDO($config['dsn']))->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT); + Assert::type(\Dibi\Drivers\PdoDriver::class, buildPdoDriver($pdoConnection)); +}); + + +// PDO error mode: implicitly set silent +test(function() use ($config) { + $pdoConnection = new PDO($config['dsn']); + Assert::type(\Dibi\Drivers\PdoDriver::class, buildPdoDriver($pdoConnection)); +}); From 4e2725cc035a8f7f29346cd06de7757376835701 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Wed, 25 Jul 2018 14:26:32 +0200 Subject: [PATCH 05/11] fixed test for ERRMODE_WARNING --- tests/dibi/PdoDriver.providedConnection.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/dibi/PdoDriver.providedConnection.phpt b/tests/dibi/PdoDriver.providedConnection.phpt index be222d559..fd420389a 100644 --- a/tests/dibi/PdoDriver.providedConnection.phpt +++ b/tests/dibi/PdoDriver.providedConnection.phpt @@ -29,7 +29,7 @@ Assert::exception(function() use ($config) { // PDO error mode: warning Assert::exception(function() use ($config) { - ($pdoConnection = new PDO($config['dsn']))->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + ($pdoConnection = new PDO($config['dsn']))->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING); buildPdoDriver($pdoConnection); }, \Dibi\DriverException::class, 'PDO connection in exception or warning error mode is currently not supported. Consider upgrading to Dibi >=4.1.0.'); From add5836955bf9b4ac56dff34467a7d9886a7e03d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Wed, 25 Jul 2018 14:35:21 +0200 Subject: [PATCH 06/11] trying @dataProvider condition instead of skipping tests --- tests/dibi/PdoDriver.providedConnection.phpt | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/tests/dibi/PdoDriver.providedConnection.phpt b/tests/dibi/PdoDriver.providedConnection.phpt index fd420389a..eb83e1230 100644 --- a/tests/dibi/PdoDriver.providedConnection.phpt +++ b/tests/dibi/PdoDriver.providedConnection.phpt @@ -1,7 +1,7 @@ $pdo]; From 2af985f485207faefcad66679de945c5e4ec9ac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Wed, 25 Jul 2018 14:39:42 +0200 Subject: [PATCH 07/11] fixup! --- tests/dibi/PdoDriver.providedConnection.phpt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/dibi/PdoDriver.providedConnection.phpt b/tests/dibi/PdoDriver.providedConnection.phpt index eb83e1230..9f8f0eae6 100644 --- a/tests/dibi/PdoDriver.providedConnection.phpt +++ b/tests/dibi/PdoDriver.providedConnection.phpt @@ -1,7 +1,7 @@ Date: Wed, 25 Jul 2018 15:04:44 +0200 Subject: [PATCH 08/11] PdoDriver test: refacted creation of PDO connection (now extracting from PDO connection using Dibi internal factories) --- tests/dibi/PdoDriver.providedConnection.phpt | 53 +++++++++++++++----- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/tests/dibi/PdoDriver.providedConnection.phpt b/tests/dibi/PdoDriver.providedConnection.phpt index 9f8f0eae6..868a8706b 100644 --- a/tests/dibi/PdoDriver.providedConnection.phpt +++ b/tests/dibi/PdoDriver.providedConnection.phpt @@ -10,35 +10,62 @@ use Tester\Assert; require __DIR__ . '/bootstrap.php'; + +function buildPDOConnection(int $errorMode = NULL): PDO { + global $config; + + // used to parse config, establish connection + $connection = new \Dibi\Connection($config); + $dibiDriver = $connection->getDriver(); + \assert($dibiDriver instanceof \Dibi\Drivers\PdoDriver); + + // hack: extract PDO connection from driver (no public interface for that) + $connectionProperty = (new ReflectionClass($dibiDriver)) + ->getProperty('connection'); + $connectionProperty->setAccessible(TRUE); + $pdo = $connectionProperty->getValue($dibiDriver); + \assert($pdo instanceof PDO); + + // check that error reporting is in PHPs default value + \assert($pdo->getAttribute(\PDO::ATTR_ERRMODE) === \PDO::ERRMODE_SILENT); + + // override PDO error mode if provided + if ($errorMode !== NULL) { + $pdo->setAttribute(\PDO::ATTR_ERRMODE, $errorMode); + } + return $pdo; +} + /** @throws \Dibi\NotSupportedException */ -function buildPdoDriver(PDO $pdo): \Dibi\Drivers\PdoDriver { +function buildPdoDriverWithProvidedConnection(PDO $pdo): \Dibi\Drivers\PdoDriver { $driverConfig = ['resource' => $pdo]; return new \Dibi\Drivers\PdoDriver($driverConfig); } + // PDO error mode: exception -Assert::exception(function() use ($config) { - ($pdoConnection = new PDO($config['dsn']))->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); - buildPdoDriver($pdoConnection); +Assert::exception(function() { + $pdoConnection = buildPDOConnection(\PDO::ERRMODE_EXCEPTION); + buildPdoDriverWithProvidedConnection($pdoConnection); }, \Dibi\DriverException::class, 'PDO connection in exception or warning error mode is currently not supported. Consider upgrading to Dibi >=4.1.0.'); // PDO error mode: warning -Assert::exception(function() use ($config) { - ($pdoConnection = new PDO($config['dsn']))->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_WARNING); - buildPdoDriver($pdoConnection); +Assert::exception(function() { + $pdoConnection = buildPDOConnection(\PDO::ERRMODE_WARNING); + buildPdoDriverWithProvidedConnection($pdoConnection); }, \Dibi\DriverException::class, 'PDO connection in exception or warning error mode is currently not supported. Consider upgrading to Dibi >=4.1.0.'); // PDO error mode: explicitly set silent -test(function() use ($config) { - ($pdoConnection = new PDO($config['dsn']))->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_SILENT); - Assert::type(\Dibi\Drivers\PdoDriver::class, buildPdoDriver($pdoConnection)); +test(function() { + $pdoConnection = buildPDOConnection(\PDO::ERRMODE_SILENT); + Assert::type(\Dibi\Drivers\PdoDriver::class, buildPdoDriverWithProvidedConnection($pdoConnection)); }); // PDO error mode: implicitly set silent -test(function() use ($config) { - $pdoConnection = new PDO($config['dsn']); - Assert::type(\Dibi\Drivers\PdoDriver::class, buildPdoDriver($pdoConnection)); +test(function() { + $pdoConnection = buildPDOConnection(NULL); + Assert::type(\Dibi\Drivers\PdoDriver::class, buildPdoDriverWithProvidedConnection($pdoConnection)); }); From 52ab9ffed3b2c841b61ce499c55a0577cef8f5e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Thu, 26 Jul 2018 09:20:18 +0200 Subject: [PATCH 09/11] comment for test --- tests/dibi/PdoDriver.providedConnection.phpt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/dibi/PdoDriver.providedConnection.phpt b/tests/dibi/PdoDriver.providedConnection.phpt index 868a8706b..8070cd1e0 100644 --- a/tests/dibi/PdoDriver.providedConnection.phpt +++ b/tests/dibi/PdoDriver.providedConnection.phpt @@ -4,6 +4,12 @@ * @dataProvider ../databases.ini != nothing, pdo */ +// Background: +// When PDO connection is passed into Dibi it can be in (re)configured in various ways. +// This affects how connection is then internally handled. +// There should be no visible difference in Dibi behaviour regardless of PDO configuration (except unsupported configurations). + + declare(strict_types=1); use Tester\Assert; From 9c00e44b36871e5c07e009b2eca47615fe1229ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Tue, 31 Jul 2018 07:40:43 +0200 Subject: [PATCH 10/11] coding standard --- tests/dibi/PdoDriver.providedConnection.phpt | 21 +++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/tests/dibi/PdoDriver.providedConnection.phpt b/tests/dibi/PdoDriver.providedConnection.phpt index 8070cd1e0..0764bc054 100644 --- a/tests/dibi/PdoDriver.providedConnection.phpt +++ b/tests/dibi/PdoDriver.providedConnection.phpt @@ -17,7 +17,8 @@ use Tester\Assert; require __DIR__ . '/bootstrap.php'; -function buildPDOConnection(int $errorMode = NULL): PDO { +function buildPDOConnection(int $errorMode = null): PDO +{ global $config; // used to parse config, establish connection @@ -28,7 +29,7 @@ function buildPDOConnection(int $errorMode = NULL): PDO { // hack: extract PDO connection from driver (no public interface for that) $connectionProperty = (new ReflectionClass($dibiDriver)) ->getProperty('connection'); - $connectionProperty->setAccessible(TRUE); + $connectionProperty->setAccessible(true); $pdo = $connectionProperty->getValue($dibiDriver); \assert($pdo instanceof PDO); @@ -36,42 +37,44 @@ function buildPDOConnection(int $errorMode = NULL): PDO { \assert($pdo->getAttribute(\PDO::ATTR_ERRMODE) === \PDO::ERRMODE_SILENT); // override PDO error mode if provided - if ($errorMode !== NULL) { + if ($errorMode !== null) { $pdo->setAttribute(\PDO::ATTR_ERRMODE, $errorMode); } return $pdo; } + /** @throws \Dibi\NotSupportedException */ -function buildPdoDriverWithProvidedConnection(PDO $pdo): \Dibi\Drivers\PdoDriver { +function buildPdoDriverWithProvidedConnection(PDO $pdo): \Dibi\Drivers\PdoDriver +{ $driverConfig = ['resource' => $pdo]; return new \Dibi\Drivers\PdoDriver($driverConfig); } // PDO error mode: exception -Assert::exception(function() { +Assert::exception(function () { $pdoConnection = buildPDOConnection(\PDO::ERRMODE_EXCEPTION); buildPdoDriverWithProvidedConnection($pdoConnection); }, \Dibi\DriverException::class, 'PDO connection in exception or warning error mode is currently not supported. Consider upgrading to Dibi >=4.1.0.'); // PDO error mode: warning -Assert::exception(function() { +Assert::exception(function () { $pdoConnection = buildPDOConnection(\PDO::ERRMODE_WARNING); buildPdoDriverWithProvidedConnection($pdoConnection); }, \Dibi\DriverException::class, 'PDO connection in exception or warning error mode is currently not supported. Consider upgrading to Dibi >=4.1.0.'); // PDO error mode: explicitly set silent -test(function() { +test(function () { $pdoConnection = buildPDOConnection(\PDO::ERRMODE_SILENT); Assert::type(\Dibi\Drivers\PdoDriver::class, buildPdoDriverWithProvidedConnection($pdoConnection)); }); // PDO error mode: implicitly set silent -test(function() { - $pdoConnection = buildPDOConnection(NULL); +test(function () { + $pdoConnection = buildPDOConnection(null); Assert::type(\Dibi\Drivers\PdoDriver::class, buildPdoDriverWithProvidedConnection($pdoConnection)); }); From 9e178a5f11afcf00bd5ee081efbd6015044221f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Tue, 31 Jul 2018 08:18:23 +0200 Subject: [PATCH 11/11] PdoDriver: check for misconfigured connection earlier; it was causing unexpected exceptions in ODBC-PDO driver. --- src/Dibi/Drivers/PdoDriver.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/Dibi/Drivers/PdoDriver.php b/src/Dibi/Drivers/PdoDriver.php index 3a53f2e30..9e00a34ba 100644 --- a/src/Dibi/Drivers/PdoDriver.php +++ b/src/Dibi/Drivers/PdoDriver.php @@ -69,12 +69,13 @@ public function __construct(array &$config) } } - $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 - if ($this->connection->getAttribute(\PDO::ATTR_ERRMODE) !== \PDO::ERRMODE_SILENT) { throw new Dibi\DriverException('PDO connection in exception or warning error mode is currently not supported. Consider upgrading to Dibi >=4.1.0.'); } + + $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 + }