Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@fix: #214 "MySQL integration test fail in section testNamedParameters #224

Open
wants to merge 1 commit into
base: 2.19.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,8 +2,11 @@

namespace LaminasIntegrationTest\Db\Adapter\Driver\Pdo\Mysql;

use Exception;
use Laminas\Db\Adapter\Adapter;
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 @@ -40,8 +43,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 @@ -72,38 +75,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);
}
}