diff --git a/test/integration/Adapter/Driver/Pdo/Mysql/QueryTest.php b/test/integration/Adapter/Driver/Pdo/Mysql/QueryTest.php index 43087a4c..890bb8a7 100644 --- a/test/integration/Adapter/Driver/Pdo/Mysql/QueryTest.php +++ b/test/integration/Adapter/Driver/Pdo/Mysql/QueryTest.php @@ -3,6 +3,7 @@ namespace LaminasIntegrationTest\Db\Adapter\Driver\Pdo\Mysql; use Laminas\Db\Adapter\Driver\Pdo\Result as PdoResult; +use Laminas\Db\Adapter\Driver\ResultInterface; use Laminas\Db\Adapter\Exception\RuntimeException; use Laminas\Db\ResultSet\ResultSet; use Laminas\Db\Sql\Sql; @@ -37,8 +38,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) { @@ -46,7 +47,7 @@ public function testQuery(string $query, array $params, array $expected) $this->assertInstanceOf(ResultSet::class, $result); $current = $result->current(); // test as array value - $this->assertEquals($expected, (array) $current); + $this->assertEquals($expected, (array)$current); // test as object value foreach ($expected as $key => $value) { $this->assertEquals($value, $current->$key); @@ -69,38 +70,125 @@ 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 \Laminas\Db\Adapter\Driver\StatementInterface */ - public function testNamedParameters() + protected function getStatementForTestBinding() { $sql = new Sql($this->adapter); - - $insert = $sql->update('test'); - $insert->set([ - 'name' => ':name', + /** + * @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' + ]); + $stmt = $sql->prepareStatementForSqlObject($update); - //positional parameters + return $stmt; + } + + /** + * This test verify exception, if index was confused. + * Index 0 and 2 is confused. + */ + public function testBindParamByIndexIsFail() + { + $stmt = $this->getStatementForTestBinding(); + $this->expectExceptionMessage('Statement could not be executed (22007 - 1292 - Truncated incorrect DOUBLE value: \'bar\')'); + //positional parameters - is invalid $stmt->execute([ - 1, - 'foo', - 'bar', + 1, //FAIL -- 0 ":c_0" "name" varchar(255) + 'foo', //OK -- 1 ":c_1" "value" varchar(255) + 'bar', //FAIL -- 2 ":where1" "id" int + ]); + } + + /** + * 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(); + $this->expectExceptionMessage('Statement could not be executed (22007 - 1292 - Truncated incorrect DOUBLE value: \'bar\')'); //"mapped" named parameters $stmt->execute([ - 'c_0' => 1, - 'c_1' => 'foo', - 'where1' => 'bar', + '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 + ]); + } + + /** + * Expected Result, because bind names is valid + */ + public function testBindParamByNameIsSuccess() + { + $stmt = $this->getStatementForTestBinding(); + //"mapped" named parameters + $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 names "name" and "id" is confused. + */ + public function testBindParamByFieldNameIsFail() + { + $stmt = $this->getStatementForTestBinding(); + $this->expectExceptionMessage('Statement could not be executed (22007 - 1292 - Truncated incorrect DOUBLE value: \'bar\')'); //real named parameters $stmt->execute([ - 'id' => 1, - 'name' => 'foo', - 'value' => 'bar', + 'name' => 1, //FAIL -- 0 ":c_0" "name" varchar(255) + 'value' => 'foo', //OK -- 1 ":c_1" "value" varchar(255) + 'id' => 'bar', //FAIL -- 2 ":where1" "id" int ]); } + + /** + * Expected Result, because bind filed names is valid + */ + public function testBindParamByFieldNameIsSuccess() + { + $stmt = $this->getStatementForTestBinding(); + //real named parameters + $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); + } + }