From d62c479b8a7fd9b264aa615fdf0dd8ebc6c9d120 Mon Sep 17 00:00:00 2001 From: ZVanoZ Date: Fri, 24 Sep 2021 11:50:25 +0300 Subject: [PATCH] @fix: #214 "MySQL integration test fail in section testNamedParameters --- .../Adapter/Driver/Pdo/Mysql/QueryTest.php | 143 +++++++++++++++--- 1 file changed, 122 insertions(+), 21 deletions(-) diff --git a/test/integration/Adapter/Driver/Pdo/Mysql/QueryTest.php b/test/integration/Adapter/Driver/Pdo/Mysql/QueryTest.php index 43087a4c..29d86677 100644 --- a/test/integration/Adapter/Driver/Pdo/Mysql/QueryTest.php +++ b/test/integration/Adapter/Driver/Pdo/Mysql/QueryTest.php @@ -2,7 +2,10 @@ namespace LaminasIntegrationTest\Db\Adapter\Driver\Pdo\Mysql; +use Exception; use Laminas\Db\Adapter\Driver\Pdo\Result as PdoResult; +use Laminas\Db\Adapter\Driver\ResultInterface; +use Laminas\Db\Adapter\Driver\StatementInterface; use Laminas\Db\Adapter\Exception\RuntimeException; use Laminas\Db\ResultSet\ResultSet; use Laminas\Db\Sql\Sql; @@ -37,8 +40,8 @@ public function getQueriesWithRowResult(): array /** * @dataProvider getQueriesWithRowResult - * @covers \Laminas\Db\Adapter\Adapter::query - * @covers \Laminas\Db\ResultSet\ResultSet::current + * @covers \Laminas\Db\Adapter\Adapter::query + * @covers \Laminas\Db\ResultSet\ResultSet::current */ public function testQuery(string $query, array $params, array $expected) { @@ -69,38 +72,136 @@ public function testSelectWithNotPermittedBindParamName() } /** + * SQL text is: "UPDATE `test` SET `name` = :c_0, `value` = :c_1 WHERE ` id` = :where1" + * Binding map table + * -- Bind Index Bind Name Field name Field type + * -- 0 ":c_0" "name" varchar(255) + * -- 1 ":c_1" "value" varchar(255) + * -- 2 ":where1" "id" int + * * @see https://github.com/laminas/laminas-db/issues/47 + * @see https://github.com/laminas/laminas-db/issues/214 + * + * @return StatementInterface */ - public function testNamedParameters() + protected function getStatementForTestBinding() { $sql = new Sql($this->adapter); - - $insert = $sql->update('test'); - $insert->set([ + /** + * @type \Laminas\Db\Sql\Update $update + */ + $update = $sql->update('test'); + $update->set([ 'name' => ':name', 'value' => ':value', - ])->where(['id' => ':id']); - $stmt = $sql->prepareStatementForSqlObject($insert); + ])->where([ + 'id' => ':id', + ]); + return $sql->prepareStatementForSqlObject($update); + } + + /** + * This test verify exception, if index was confused. + * Index 0 and 2 is confused. + */ + public function testBindParamByIndexIsFail() + { + $stmt = $this->getStatementForTestBinding(); + try { + //positional parameters - is invalid + $stmt->execute([ + 1, // FAIL -- 0 ":c_0" "name" varchar(255) + 'foo', //OK -- 1 ":c_1" "value" varchar(255) + 'bar', //FAIL -- 2 ":where1" "id" int + ]); + $this->assertTrue(false, __METHOD__, "/Fail. Extect exception."); + } catch (Exception $e) { + $this->assertTrue(true, __METHOD__, "/Success. We have an exception: " . $e->getMessage()); + } + } - //positional parameters - $stmt->execute([ - 1, - 'foo', - 'bar', + /** + * Expected Result, because bind index is valid + */ + public function testBindParamByIndexIsSuccess() + { + $stmt = $this->getStatementForTestBinding(); + //positional parameters - is valid + $result = $stmt->execute([ + 'bar', //OK -- 0 ":c_0" "name" varchar(255) + 'foo', //OK -- 1 ":c_1" "value" varchar(255) + 1, // OK -- 2 ":where1" "id" int ]); + $this->assertInstanceOf(ResultInterface::class, $result); + } + + /** + * This test verify exception, if names was confused. + * Names "c_0" and "where1" is confused. + */ + public function testBindParamByNameIsFail() + { + $stmt = $this->getStatementForTestBinding(); + try { + //"mapped" named parameters + $stmt->execute([ + 'c_0' => 1, // FAIL -- 0 ":c_0" "name" varchar(255) + 'c_1' => 'foo', //OK -- 1 ":c_1" "value" varchar(255) + 'where1' => 'bar', //FAIL -- 2 ":where1" "id" int + ]); + $this->assertTrue(false, __METHOD__, "/Fail. Extect exception."); + } catch (Exception $e) { + $this->assertTrue(true, __METHOD__, "/Success. We have an exception: " . $e->getMessage()); + } + } + /** + * Expected Result, because bind names is valid + */ + public function testBindParamByNameIsSuccess() + { + $stmt = $this->getStatementForTestBinding(); //"mapped" named parameters - $stmt->execute([ - 'c_0' => 1, - 'c_1' => 'foo', - 'where1' => 'bar', + $result = $stmt->execute([ + 'c_0' => 'bar', //OK -- 0 ":c_0" "name" varchar(255) + 'c_1' => 'foo', //OK -- 1 ":c_1" "value" varchar(255) + 'where1' => 1, // OK -- 2 ":where1" "id" int ]); + $this->assertInstanceOf(ResultInterface::class, $result); + } + + /** + * This test verify exception, if field names was confused. + * Field name "id" named "idFieldName" - it is wrong. + */ + public function testBindParamByFieldNameIsFail() + { + $stmt = $this->getStatementForTestBinding(); + try { + //real named parameters + $stmt->execute([ + 'name' => 'bar', // OK -- 0 ":c_0" "name" varchar(255) + 'value' => 'foo', // OK -- 1 ":c_1" "value" varchar(255) + 'idFieldName' => 1, // FAIL -- 2 ":where1" "id" int + ]); + $this->assertTrue(false, __METHOD__, "/Fail. Extect exception."); + } catch (Exception $e) { + $this->assertTrue(true, __METHOD__, "/Success. We have an exception: " . $e->getMessage()); + } + } + /** + * Expected Result, because bind filed names is valid + */ + public function testBindParamByFieldNameIsSuccess() + { + $stmt = $this->getStatementForTestBinding(); //real named parameters - $stmt->execute([ - 'id' => 1, - 'name' => 'foo', - 'value' => 'bar', + $result = $stmt->execute([ + 'name' => 'bar', //OK -- 0 ":c_0" "name" varchar(255) + 'value' => 'foo', //OK -- 1 ":c_1" "value" varchar(255) + 'id' => 1, // OK -- 2 ":where1" "id" int ]); + $this->assertInstanceOf(ResultInterface::class, $result); } }