From 411ce51cf27f0afe647c1564564baf2e29e59753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Mon, 11 Jun 2018 14:39:33 +0200 Subject: [PATCH 1/8] PdoDriver: fix pdo connection in exception mode --- src/Dibi/Drivers/MySqliDriver.php | 10 ++++----- src/Dibi/Drivers/OracleDriver.php | 10 ++++----- src/Dibi/Drivers/PdoDriver.php | 33 +++++++++++++++++++++--------- src/Dibi/Drivers/PostgreDriver.php | 12 +++++------ src/Dibi/Drivers/SqliteDriver.php | 10 ++++----- 5 files changed, 44 insertions(+), 31 deletions(-) diff --git a/src/Dibi/Drivers/MySqliDriver.php b/src/Dibi/Drivers/MySqliDriver.php index 769555575..47874f6bc 100644 --- a/src/Dibi/Drivers/MySqliDriver.php +++ b/src/Dibi/Drivers/MySqliDriver.php @@ -150,19 +150,19 @@ public function query(string $sql): ?Dibi\ResultDriver } - public static function createException(string $message, $code, string $sql): Dibi\DriverException + public static function createException(string $message, $code, string $sql, \Throwable $previous = null): Dibi\DriverException { if (in_array($code, [1216, 1217, 1451, 1452, 1701], true)) { - return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql); + return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql, $previous); } elseif (in_array($code, [1062, 1557, 1569, 1586], true)) { - return new Dibi\UniqueConstraintViolationException($message, $code, $sql); + return new Dibi\UniqueConstraintViolationException($message, $code, $sql, $previous); } elseif (in_array($code, [1048, 1121, 1138, 1171, 1252, 1263, 1566], true)) { - return new Dibi\NotNullConstraintViolationException($message, $code, $sql); + return new Dibi\NotNullConstraintViolationException($message, $code, $sql, $previous); } else { - return new Dibi\DriverException($message, $code, $sql); + return new Dibi\DriverException($message, $code, $sql, $previous); } } diff --git a/src/Dibi/Drivers/OracleDriver.php b/src/Dibi/Drivers/OracleDriver.php index 2f5d8e031..026309880 100644 --- a/src/Dibi/Drivers/OracleDriver.php +++ b/src/Dibi/Drivers/OracleDriver.php @@ -112,19 +112,19 @@ public function query(string $sql): ?Dibi\ResultDriver } - public static function createException(string $message, $code, string $sql): Dibi\DriverException + public static function createException(string $message, $code, string $sql, \Throwable $previous = null): Dibi\DriverException { if (in_array($code, [1, 2299, 38911], true)) { - return new Dibi\UniqueConstraintViolationException($message, $code, $sql); + return new Dibi\UniqueConstraintViolationException($message, $code, $sql, $previous); } elseif (in_array($code, [1400], true)) { - return new Dibi\NotNullConstraintViolationException($message, $code, $sql); + return new Dibi\NotNullConstraintViolationException($message, $code, $sql, $previous); } elseif (in_array($code, [2266, 2291, 2292], true)) { - return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql); + return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql, $previous); } else { - return new Dibi\DriverException($message, $code, $sql); + return new Dibi\DriverException($message, $code, $sql, $previous); } } diff --git a/src/Dibi/Drivers/PdoDriver.php b/src/Dibi/Drivers/PdoDriver.php index 66321c153..b61f297a8 100644 --- a/src/Dibi/Drivers/PdoDriver.php +++ b/src/Dibi/Drivers/PdoDriver.php @@ -89,31 +89,44 @@ public function disconnect(): void */ public function query(string $sql): ?Dibi\ResultDriver { - $res = $this->connection->query($sql); - if ($res) { - $this->affectedRows = $res->rowCount(); - return $res->columnCount() ? $this->createResultDriver($res) : null; + try { + $res = $this->connection->query($sql); + + if ($res) { + $this->affectedRows = $res->rowCount(); + return $res->columnCount() ? $this->createResultDriver($res) : null; + } + + } catch (\PDOException $pdoException) { + $this->affectedRows = null; + throw $this->createException($pdoException->errorInfo, $sql, $pdoException); } $this->affectedRows = null; + throw $this->createException($this->connection->errorInfo(), $sql, null); + + } + + + private function createException(array $errorInfo, string $sql, ?\Throwable $previous): Dibi\DriverException { + [$sqlState, $code, $message] = $errorInfo; - [$sqlState, $code, $message] = $this->connection->errorInfo(); $message = "SQLSTATE[$sqlState]: $message"; switch ($this->driverName) { case 'mysql': - throw MySqliDriver::createException($message, $code, $sql); + return MySqliDriver::createException($message, $code, $sql, $previous); case 'oci': - throw OracleDriver::createException($message, $code, $sql); + return OracleDriver::createException($message, $code, $sql, $previous); case 'pgsql': - throw PostgreDriver::createException($message, $sqlState, $sql); + return PostgreDriver::createException($message, $sqlState, $sql, $previous); case 'sqlite': - throw SqliteDriver::createException($message, $code, $sql); + return SqliteDriver::createException($message, $code, $sql, $previous); default: - throw new Dibi\DriverException($message, $code, $sql); + return new Dibi\DriverException($message, $code, $sql, $previous); } } diff --git a/src/Dibi/Drivers/PostgreDriver.php b/src/Dibi/Drivers/PostgreDriver.php index a0af40644..a2cdf80af 100644 --- a/src/Dibi/Drivers/PostgreDriver.php +++ b/src/Dibi/Drivers/PostgreDriver.php @@ -132,7 +132,7 @@ public function query(string $sql): ?Dibi\ResultDriver } - public static function createException(string $message, $code = null, string $sql = null): Dibi\DriverException + public static function createException(string $message, $code = null, string $sql = null, \Throwable $previous = null): Dibi\DriverException { if ($code === null && preg_match('#^ERROR:\s+(\S+):\s*#', $message, $m)) { $code = $m[1]; @@ -140,19 +140,19 @@ public static function createException(string $message, $code = null, string $sq } if ($code === '0A000' && strpos($message, 'truncate') !== false) { - return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql); + return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql, $previous); } elseif ($code === '23502') { - return new Dibi\NotNullConstraintViolationException($message, $code, $sql); + return new Dibi\NotNullConstraintViolationException($message, $code, $sql, $previous); } elseif ($code === '23503') { - return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql); + return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql, $previous); } elseif ($code === '23505') { - return new Dibi\UniqueConstraintViolationException($message, $code, $sql); + return new Dibi\UniqueConstraintViolationException($message, $code, $sql, $previous); } else { - return new Dibi\DriverException($message, $code, $sql); + return new Dibi\DriverException($message, $code, $sql, $previous); } } diff --git a/src/Dibi/Drivers/SqliteDriver.php b/src/Dibi/Drivers/SqliteDriver.php index 2ea27a084..c11d65ee6 100644 --- a/src/Dibi/Drivers/SqliteDriver.php +++ b/src/Dibi/Drivers/SqliteDriver.php @@ -98,7 +98,7 @@ public function query(string $sql): ?Dibi\ResultDriver } - public static function createException(string $message, $code, string $sql): Dibi\DriverException + public static function createException(string $message, $code, string $sql, \Throwable $throwable = null): Dibi\DriverException { if ($code !== 19) { return new Dibi\DriverException($message, $code, $sql); @@ -107,20 +107,20 @@ public static function createException(string $message, $code, string $sql): Dib || strpos($message, 'is not unique') !== false || strpos($message, 'UNIQUE constraint failed') !== false ) { - return new Dibi\UniqueConstraintViolationException($message, $code, $sql); + return new Dibi\UniqueConstraintViolationException($message, $code, $sql, $throwable); } elseif (strpos($message, 'may not be null') !== false || strpos($message, 'NOT NULL constraint failed') !== false ) { - return new Dibi\NotNullConstraintViolationException($message, $code, $sql); + return new Dibi\NotNullConstraintViolationException($message, $code, $sql, $throwable); } elseif (strpos($message, 'foreign key constraint failed') !== false || strpos($message, 'FOREIGN KEY constraint failed') !== false ) { - return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql); + return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql, $throwable); } else { - return new Dibi\ConstraintViolationException($message, $code, $sql); + return new Dibi\ConstraintViolationException($message, $code, $sql, $throwable); } } From 1472f06d1198860755b0bb258e3bea8528669f34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Mon, 11 Jun 2018 15:14:00 +0200 Subject: [PATCH 2/8] code style --- src/Dibi/Drivers/PdoDriver.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Dibi/Drivers/PdoDriver.php b/src/Dibi/Drivers/PdoDriver.php index b61f297a8..1657db428 100644 --- a/src/Dibi/Drivers/PdoDriver.php +++ b/src/Dibi/Drivers/PdoDriver.php @@ -108,7 +108,8 @@ public function query(string $sql): ?Dibi\ResultDriver } - private function createException(array $errorInfo, string $sql, ?\Throwable $previous): Dibi\DriverException { + private function createException(array $errorInfo, string $sql, ?\Throwable $previous): Dibi\DriverException + { [$sqlState, $code, $message] = $errorInfo; $message = "SQLSTATE[$sqlState]: $message"; From 7d87a70f404f2be79ff0f6d9b234dc28b4c1721a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Fri, 6 Jul 2018 17:39:09 +0200 Subject: [PATCH 3/8] removed previous exception --- src/Dibi/Drivers/MySqliDriver.php | 10 +++++----- src/Dibi/Drivers/OracleDriver.php | 10 +++++----- src/Dibi/Drivers/PdoDriver.php | 16 ++++++++-------- src/Dibi/Drivers/PostgreDriver.php | 12 ++++++------ src/Dibi/Drivers/SqliteDriver.php | 10 +++++----- 5 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/Dibi/Drivers/MySqliDriver.php b/src/Dibi/Drivers/MySqliDriver.php index 47874f6bc..769555575 100644 --- a/src/Dibi/Drivers/MySqliDriver.php +++ b/src/Dibi/Drivers/MySqliDriver.php @@ -150,19 +150,19 @@ public function query(string $sql): ?Dibi\ResultDriver } - public static function createException(string $message, $code, string $sql, \Throwable $previous = null): Dibi\DriverException + public static function createException(string $message, $code, string $sql): Dibi\DriverException { if (in_array($code, [1216, 1217, 1451, 1452, 1701], true)) { - return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql, $previous); + return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql); } elseif (in_array($code, [1062, 1557, 1569, 1586], true)) { - return new Dibi\UniqueConstraintViolationException($message, $code, $sql, $previous); + return new Dibi\UniqueConstraintViolationException($message, $code, $sql); } elseif (in_array($code, [1048, 1121, 1138, 1171, 1252, 1263, 1566], true)) { - return new Dibi\NotNullConstraintViolationException($message, $code, $sql, $previous); + return new Dibi\NotNullConstraintViolationException($message, $code, $sql); } else { - return new Dibi\DriverException($message, $code, $sql, $previous); + return new Dibi\DriverException($message, $code, $sql); } } diff --git a/src/Dibi/Drivers/OracleDriver.php b/src/Dibi/Drivers/OracleDriver.php index 026309880..2f5d8e031 100644 --- a/src/Dibi/Drivers/OracleDriver.php +++ b/src/Dibi/Drivers/OracleDriver.php @@ -112,19 +112,19 @@ public function query(string $sql): ?Dibi\ResultDriver } - public static function createException(string $message, $code, string $sql, \Throwable $previous = null): Dibi\DriverException + public static function createException(string $message, $code, string $sql): Dibi\DriverException { if (in_array($code, [1, 2299, 38911], true)) { - return new Dibi\UniqueConstraintViolationException($message, $code, $sql, $previous); + return new Dibi\UniqueConstraintViolationException($message, $code, $sql); } elseif (in_array($code, [1400], true)) { - return new Dibi\NotNullConstraintViolationException($message, $code, $sql, $previous); + return new Dibi\NotNullConstraintViolationException($message, $code, $sql); } elseif (in_array($code, [2266, 2291, 2292], true)) { - return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql, $previous); + return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql); } else { - return new Dibi\DriverException($message, $code, $sql, $previous); + return new Dibi\DriverException($message, $code, $sql); } } diff --git a/src/Dibi/Drivers/PdoDriver.php b/src/Dibi/Drivers/PdoDriver.php index 1657db428..db31bae85 100644 --- a/src/Dibi/Drivers/PdoDriver.php +++ b/src/Dibi/Drivers/PdoDriver.php @@ -99,35 +99,35 @@ public function query(string $sql): ?Dibi\ResultDriver } catch (\PDOException $pdoException) { $this->affectedRows = null; - throw $this->createException($pdoException->errorInfo, $sql, $pdoException); + throw $this->createException($pdoException->errorInfo, $sql); } $this->affectedRows = null; - throw $this->createException($this->connection->errorInfo(), $sql, null); + throw $this->createException($this->connection->errorInfo(), $sql); } - private function createException(array $errorInfo, string $sql, ?\Throwable $previous): Dibi\DriverException + private function createException(array $errorInfo, string $sql): Dibi\DriverException { [$sqlState, $code, $message] = $errorInfo; $message = "SQLSTATE[$sqlState]: $message"; switch ($this->driverName) { case 'mysql': - return MySqliDriver::createException($message, $code, $sql, $previous); + return MySqliDriver::createException($message, $code, $sql); case 'oci': - return OracleDriver::createException($message, $code, $sql, $previous); + return OracleDriver::createException($message, $code, $sql); case 'pgsql': - return PostgreDriver::createException($message, $sqlState, $sql, $previous); + return PostgreDriver::createException($message, $sqlState, $sql); case 'sqlite': - return SqliteDriver::createException($message, $code, $sql, $previous); + return SqliteDriver::createException($message, $code, $sql); default: - return new Dibi\DriverException($message, $code, $sql, $previous); + return new Dibi\DriverException($message, $code, $sql); } } diff --git a/src/Dibi/Drivers/PostgreDriver.php b/src/Dibi/Drivers/PostgreDriver.php index a2cdf80af..a0af40644 100644 --- a/src/Dibi/Drivers/PostgreDriver.php +++ b/src/Dibi/Drivers/PostgreDriver.php @@ -132,7 +132,7 @@ public function query(string $sql): ?Dibi\ResultDriver } - public static function createException(string $message, $code = null, string $sql = null, \Throwable $previous = null): Dibi\DriverException + public static function createException(string $message, $code = null, string $sql = null): Dibi\DriverException { if ($code === null && preg_match('#^ERROR:\s+(\S+):\s*#', $message, $m)) { $code = $m[1]; @@ -140,19 +140,19 @@ public static function createException(string $message, $code = null, string $sq } if ($code === '0A000' && strpos($message, 'truncate') !== false) { - return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql, $previous); + return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql); } elseif ($code === '23502') { - return new Dibi\NotNullConstraintViolationException($message, $code, $sql, $previous); + return new Dibi\NotNullConstraintViolationException($message, $code, $sql); } elseif ($code === '23503') { - return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql, $previous); + return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql); } elseif ($code === '23505') { - return new Dibi\UniqueConstraintViolationException($message, $code, $sql, $previous); + return new Dibi\UniqueConstraintViolationException($message, $code, $sql); } else { - return new Dibi\DriverException($message, $code, $sql, $previous); + return new Dibi\DriverException($message, $code, $sql); } } diff --git a/src/Dibi/Drivers/SqliteDriver.php b/src/Dibi/Drivers/SqliteDriver.php index c11d65ee6..2ea27a084 100644 --- a/src/Dibi/Drivers/SqliteDriver.php +++ b/src/Dibi/Drivers/SqliteDriver.php @@ -98,7 +98,7 @@ public function query(string $sql): ?Dibi\ResultDriver } - public static function createException(string $message, $code, string $sql, \Throwable $throwable = null): Dibi\DriverException + public static function createException(string $message, $code, string $sql): Dibi\DriverException { if ($code !== 19) { return new Dibi\DriverException($message, $code, $sql); @@ -107,20 +107,20 @@ public static function createException(string $message, $code, string $sql, \Thr || strpos($message, 'is not unique') !== false || strpos($message, 'UNIQUE constraint failed') !== false ) { - return new Dibi\UniqueConstraintViolationException($message, $code, $sql, $throwable); + return new Dibi\UniqueConstraintViolationException($message, $code, $sql); } elseif (strpos($message, 'may not be null') !== false || strpos($message, 'NOT NULL constraint failed') !== false ) { - return new Dibi\NotNullConstraintViolationException($message, $code, $sql, $throwable); + return new Dibi\NotNullConstraintViolationException($message, $code, $sql); } elseif (strpos($message, 'foreign key constraint failed') !== false || strpos($message, 'FOREIGN KEY constraint failed') !== false ) { - return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql, $throwable); + return new Dibi\ForeignKeyConstraintViolationException($message, $code, $sql); } else { - return new Dibi\ConstraintViolationException($message, $code, $sql, $throwable); + return new Dibi\ConstraintViolationException($message, $code, $sql); } } From b9d0ba1d96f4e3bbc8d01b8164c0acc0290d23fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Fri, 6 Jul 2018 17:41:10 +0200 Subject: [PATCH 4/8] PdoDriver: simplified query flow --- src/Dibi/Drivers/PdoDriver.php | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Dibi/Drivers/PdoDriver.php b/src/Dibi/Drivers/PdoDriver.php index db31bae85..53434c1f8 100644 --- a/src/Dibi/Drivers/PdoDriver.php +++ b/src/Dibi/Drivers/PdoDriver.php @@ -90,21 +90,19 @@ public function disconnect(): void public function query(string $sql): ?Dibi\ResultDriver { try { - $res = $this->connection->query($sql); - - if ($res) { + if ($res = $this->connection->query($sql)) { $this->affectedRows = $res->rowCount(); return $res->columnCount() ? $this->createResultDriver($res) : null; } } catch (\PDOException $pdoException) { - $this->affectedRows = null; - throw $this->createException($pdoException->errorInfo, $sql); } $this->affectedRows = null; - throw $this->createException($this->connection->errorInfo(), $sql); - + throw $this->createException( + isset($pdoException) ? $pdoException->errorInfo : $this->connection->errorInfo(), + $sql + ); } From 98e10a47cc189a4c3c19019797a33dba7595e8e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Fri, 6 Jul 2018 17:50:59 +0200 Subject: [PATCH 5/8] PdoDriver: Experimental support for warning error mode? --- 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 53434c1f8..24840cf8d 100644 --- a/src/Dibi/Drivers/PdoDriver.php +++ b/src/Dibi/Drivers/PdoDriver.php @@ -90,7 +90,7 @@ public function disconnect(): void public function query(string $sql): ?Dibi\ResultDriver { try { - if ($res = $this->connection->query($sql)) { + if ($res = @$this->connection->query($sql)) { $this->affectedRows = $res->rowCount(); return $res->columnCount() ? $this->createResultDriver($res) : null; } From 1e1d8b578cd36534ab3fa7efc472f8477da35b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Fri, 6 Jul 2018 17:51:47 +0200 Subject: [PATCH 6/8] comment --- 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 24840cf8d..d5ab5ba90 100644 --- a/src/Dibi/Drivers/PdoDriver.php +++ b/src/Dibi/Drivers/PdoDriver.php @@ -90,7 +90,7 @@ public function disconnect(): void public function query(string $sql): ?Dibi\ResultDriver { try { - if ($res = @$this->connection->query($sql)) { + if ($res = @$this->connection->query($sql)) { // intentionally @ to catch warnings in warning PDO mode $this->affectedRows = $res->rowCount(); return $res->columnCount() ? $this->createResultDriver($res) : null; } From 39f03e9d326ed0ce73c3de737eff3b37b18197a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Mon, 30 Jul 2018 21:02:27 +0200 Subject: [PATCH 7/8] PdoDriver: provided connection test --- tests/dibi/PdoDriver.providedConnection.phpt | 100 +++++++++++++++++++ 1 file changed, 100 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..8815445c4 --- /dev/null +++ b/tests/dibi/PdoDriver.providedConnection.phpt @@ -0,0 +1,100 @@ + result returned +// 2. When query fails for various reasons -> proper exception should be generated + + +declare(strict_types=1); + +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; +} + +function buildDibiConnection(PDO $pdo): \Dibi\Connection { + $conn = new \Dibi\Connection(['resource' => $pdo, 'driver' => 'pdo']); + \assert($conn->getDriver() instanceof \Dibi\Drivers\PdoDriver); + return $conn; +} + + +$runTests = function(\Dibi\Connection $connection) use ($config) { + $connection->loadFile(__DIR__ . "/data/$config[system].sql"); + if ($config['system'] === 'sqlite') { // @see issue #301 + $connection->query('PRAGMA foreign_keys=true'); + } + + // successful SELECT + test(function() use ($connection) { + $result = $connection->query("SELECT `product_id`, `title` FROM `products` WHERE `product_id` = 1")->fetch(); + Assert::equal(['product_id' => 1, 'title' => 'Chair'], $result->toArray()); + }); + + // Non-existing table: General exception should be generated + Assert::exception(function() use ($connection) { + $connection->query("SELECT * FROM `nonexisting`"); + }, \Dibi\DriverException::class); + + // Duplicated INSERT: UniqueConstraintViolationException + Assert::exception(function() use ($connection) { + $connection->query("INSERT INTO `products` (`product_id`, `title`) VALUES (1, 'Chair')"); + }, \Dibi\UniqueConstraintViolationException::class); + + // INSERT with NULL: NotNullConstraintViolationException + Assert::exception(function() use ($connection) { + $connection->query("INSERT INTO `products` (`title`) VALUES (NULL)"); + }, \Dibi\NotNullConstraintViolationException::class); + + // INSERT with NULL: ForeignKeyConstraintViolationException + Assert::exception(function() use ($connection) { + $connection->query("INSERT INTO `orders` (`customer_id`, `product_id`, `amount`) VALUES (99999 /*non-existing*/, 1, 7)"); + }, \Dibi\ForeignKeyConstraintViolationException::class); + + +}; + +// PDO error mode: exception +$runTests(buildDibiConnection(buildPDOConnection(\PDO::ERRMODE_EXCEPTION))); + +// PDO error mode: warning +$runTests(buildDibiConnection(buildPDOConnection(\PDO::ERRMODE_WARNING))); + +// PDO error mode: explicitly set silent +$runTests(buildDibiConnection(buildPDOConnection(\PDO::ERRMODE_SILENT))); + +// PDO error mode: implicitly set silent +$runTests(buildDibiConnection(buildPDOConnection(NULL))); From f55f22f8261ae26a5ba8d26a25550031c1407293 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kucha=C5=99?= Date: Tue, 31 Jul 2018 07:39:00 +0200 Subject: [PATCH 8/8] coding standard --- tests/dibi/PdoDriver.providedConnection.phpt | 35 ++++++++++---------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/tests/dibi/PdoDriver.providedConnection.phpt b/tests/dibi/PdoDriver.providedConnection.phpt index 8815445c4..20d161063 100644 --- a/tests/dibi/PdoDriver.providedConnection.phpt +++ b/tests/dibi/PdoDriver.providedConnection.phpt @@ -20,7 +20,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 @@ -31,7 +32,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); @@ -39,52 +40,52 @@ 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; } -function buildDibiConnection(PDO $pdo): \Dibi\Connection { + +function buildDibiConnection(PDO $pdo): \Dibi\Connection +{ $conn = new \Dibi\Connection(['resource' => $pdo, 'driver' => 'pdo']); \assert($conn->getDriver() instanceof \Dibi\Drivers\PdoDriver); return $conn; } -$runTests = function(\Dibi\Connection $connection) use ($config) { +$runTests = function (\Dibi\Connection $connection) use ($config) { $connection->loadFile(__DIR__ . "/data/$config[system].sql"); if ($config['system'] === 'sqlite') { // @see issue #301 $connection->query('PRAGMA foreign_keys=true'); } // successful SELECT - test(function() use ($connection) { - $result = $connection->query("SELECT `product_id`, `title` FROM `products` WHERE `product_id` = 1")->fetch(); + test(function () use ($connection) { + $result = $connection->query('SELECT `product_id`, `title` FROM `products` WHERE `product_id` = 1')->fetch(); Assert::equal(['product_id' => 1, 'title' => 'Chair'], $result->toArray()); }); // Non-existing table: General exception should be generated - Assert::exception(function() use ($connection) { - $connection->query("SELECT * FROM `nonexisting`"); + Assert::exception(function () use ($connection) { + $connection->query('SELECT * FROM `nonexisting`'); }, \Dibi\DriverException::class); // Duplicated INSERT: UniqueConstraintViolationException - Assert::exception(function() use ($connection) { + Assert::exception(function () use ($connection) { $connection->query("INSERT INTO `products` (`product_id`, `title`) VALUES (1, 'Chair')"); }, \Dibi\UniqueConstraintViolationException::class); // INSERT with NULL: NotNullConstraintViolationException - Assert::exception(function() use ($connection) { - $connection->query("INSERT INTO `products` (`title`) VALUES (NULL)"); + Assert::exception(function () use ($connection) { + $connection->query('INSERT INTO `products` (`title`) VALUES (NULL)'); }, \Dibi\NotNullConstraintViolationException::class); // INSERT with NULL: ForeignKeyConstraintViolationException - Assert::exception(function() use ($connection) { - $connection->query("INSERT INTO `orders` (`customer_id`, `product_id`, `amount`) VALUES (99999 /*non-existing*/, 1, 7)"); + Assert::exception(function () use ($connection) { + $connection->query('INSERT INTO `orders` (`customer_id`, `product_id`, `amount`) VALUES (99999 /*non-existing*/, 1, 7)'); }, \Dibi\ForeignKeyConstraintViolationException::class); - - }; // PDO error mode: exception @@ -97,4 +98,4 @@ $runTests(buildDibiConnection(buildPDOConnection(\PDO::ERRMODE_WARNING))); $runTests(buildDibiConnection(buildPDOConnection(\PDO::ERRMODE_SILENT))); // PDO error mode: implicitly set silent -$runTests(buildDibiConnection(buildPDOConnection(NULL))); +$runTests(buildDibiConnection(buildPDOConnection(null)));