Skip to content

Commit

Permalink
laminas#214 @fix: "MySQL integration test fail in section testNamedPa…
Browse files Browse the repository at this point in the history
…rameters"
  • Loading branch information
ZVanoZ committed Sep 24, 2021
1 parent feeb1ff commit 1bef71c
Showing 1 changed file with 108 additions and 20 deletions.
128 changes: 108 additions & 20 deletions test/integration/Adapter/Driver/Pdo/Mysql/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -37,16 +38,16 @@ 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)
{
$result = $this->adapter->query($query, $params);
$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);
Expand All @@ -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);
}

}

0 comments on commit 1bef71c

Please sign in to comment.