Skip to content

Commit

Permalink
@fix: #214 "MySQL integration test fail in section testNamedParameters
Browse files Browse the repository at this point in the history
Signed-off-by: ZVanoZ <edu.ZVanoZ@gmail.com>
  • Loading branch information
ZVanoZ committed Sep 29, 2021
1 parent feeb1ff commit 29f3b1c
Showing 1 changed file with 122 additions and 21 deletions.
143 changes: 122 additions & 21 deletions test/integration/Adapter/Driver/Pdo/Mysql/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 29f3b1c

Please sign in to comment.