Skip to content

Commit 29f3b1c

Browse files
committed
@fix: #214 "MySQL integration test fail in section testNamedParameters
Signed-off-by: ZVanoZ <edu.ZVanoZ@gmail.com>
1 parent feeb1ff commit 29f3b1c

File tree

1 file changed

+122
-21
lines changed

1 file changed

+122
-21
lines changed

test/integration/Adapter/Driver/Pdo/Mysql/QueryTest.php

Lines changed: 122 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

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

5+
use Exception;
56
use Laminas\Db\Adapter\Driver\Pdo\Result as PdoResult;
7+
use Laminas\Db\Adapter\Driver\ResultInterface;
8+
use Laminas\Db\Adapter\Driver\StatementInterface;
69
use Laminas\Db\Adapter\Exception\RuntimeException;
710
use Laminas\Db\ResultSet\ResultSet;
811
use Laminas\Db\Sql\Sql;
@@ -37,8 +40,8 @@ public function getQueriesWithRowResult(): array
3740

3841
/**
3942
* @dataProvider getQueriesWithRowResult
40-
* @covers \Laminas\Db\Adapter\Adapter::query
41-
* @covers \Laminas\Db\ResultSet\ResultSet::current
43+
* @covers \Laminas\Db\Adapter\Adapter::query
44+
* @covers \Laminas\Db\ResultSet\ResultSet::current
4245
*/
4346
public function testQuery(string $query, array $params, array $expected)
4447
{
@@ -69,38 +72,136 @@ public function testSelectWithNotPermittedBindParamName()
6972
}
7073

7174
/**
75+
* SQL text is: "UPDATE `test` SET `name` = :c_0, `value` = :c_1 WHERE ` id` = :where1"
76+
* Binding map table
77+
* -- Bind Index Bind Name Field name Field type
78+
* -- 0 ":c_0" "name" varchar(255)
79+
* -- 1 ":c_1" "value" varchar(255)
80+
* -- 2 ":where1" "id" int
81+
*
7282
* @see https://github.com/laminas/laminas-db/issues/47
83+
* @see https://github.com/laminas/laminas-db/issues/214
84+
*
85+
* @return StatementInterface
7386
*/
74-
public function testNamedParameters()
87+
protected function getStatementForTestBinding()
7588
{
7689
$sql = new Sql($this->adapter);
77-
78-
$insert = $sql->update('test');
79-
$insert->set([
90+
/**
91+
* @type \Laminas\Db\Sql\Update $update
92+
*/
93+
$update = $sql->update('test');
94+
$update->set([
8095
'name' => ':name',
8196
'value' => ':value',
82-
])->where(['id' => ':id']);
83-
$stmt = $sql->prepareStatementForSqlObject($insert);
97+
])->where([
98+
'id' => ':id',
99+
]);
100+
return $sql->prepareStatementForSqlObject($update);
101+
}
102+
103+
/**
104+
* This test verify exception, if index was confused.
105+
* Index 0 and 2 is confused.
106+
*/
107+
public function testBindParamByIndexIsFail()
108+
{
109+
$stmt = $this->getStatementForTestBinding();
110+
try {
111+
//positional parameters - is invalid
112+
$stmt->execute([
113+
1, // FAIL -- 0 ":c_0" "name" varchar(255)
114+
'foo', //OK -- 1 ":c_1" "value" varchar(255)
115+
'bar', //FAIL -- 2 ":where1" "id" int
116+
]);
117+
$this->assertTrue(false, __METHOD__, "/Fail. Extect exception.");
118+
} catch (Exception $e) {
119+
$this->assertTrue(true, __METHOD__, "/Success. We have an exception: " . $e->getMessage());
120+
}
121+
}
84122

85-
//positional parameters
86-
$stmt->execute([
87-
1,
88-
'foo',
89-
'bar',
123+
/**
124+
* Expected Result, because bind index is valid
125+
*/
126+
public function testBindParamByIndexIsSuccess()
127+
{
128+
$stmt = $this->getStatementForTestBinding();
129+
//positional parameters - is valid
130+
$result = $stmt->execute([
131+
'bar', //OK -- 0 ":c_0" "name" varchar(255)
132+
'foo', //OK -- 1 ":c_1" "value" varchar(255)
133+
1, // OK -- 2 ":where1" "id" int
90134
]);
135+
$this->assertInstanceOf(ResultInterface::class, $result);
136+
}
137+
138+
/**
139+
* This test verify exception, if names was confused.
140+
* Names "c_0" and "where1" is confused.
141+
*/
142+
public function testBindParamByNameIsFail()
143+
{
144+
$stmt = $this->getStatementForTestBinding();
145+
try {
146+
//"mapped" named parameters
147+
$stmt->execute([
148+
'c_0' => 1, // FAIL -- 0 ":c_0" "name" varchar(255)
149+
'c_1' => 'foo', //OK -- 1 ":c_1" "value" varchar(255)
150+
'where1' => 'bar', //FAIL -- 2 ":where1" "id" int
151+
]);
152+
$this->assertTrue(false, __METHOD__, "/Fail. Extect exception.");
153+
} catch (Exception $e) {
154+
$this->assertTrue(true, __METHOD__, "/Success. We have an exception: " . $e->getMessage());
155+
}
156+
}
91157

158+
/**
159+
* Expected Result, because bind names is valid
160+
*/
161+
public function testBindParamByNameIsSuccess()
162+
{
163+
$stmt = $this->getStatementForTestBinding();
92164
//"mapped" named parameters
93-
$stmt->execute([
94-
'c_0' => 1,
95-
'c_1' => 'foo',
96-
'where1' => 'bar',
165+
$result = $stmt->execute([
166+
'c_0' => 'bar', //OK -- 0 ":c_0" "name" varchar(255)
167+
'c_1' => 'foo', //OK -- 1 ":c_1" "value" varchar(255)
168+
'where1' => 1, // OK -- 2 ":where1" "id" int
97169
]);
170+
$this->assertInstanceOf(ResultInterface::class, $result);
171+
}
172+
173+
/**
174+
* This test verify exception, if field names was confused.
175+
* Field name "id" named "idFieldName" - it is wrong.
176+
*/
177+
public function testBindParamByFieldNameIsFail()
178+
{
179+
$stmt = $this->getStatementForTestBinding();
180+
try {
181+
//real named parameters
182+
$stmt->execute([
183+
'name' => 'bar', // OK -- 0 ":c_0" "name" varchar(255)
184+
'value' => 'foo', // OK -- 1 ":c_1" "value" varchar(255)
185+
'idFieldName' => 1, // FAIL -- 2 ":where1" "id" int
186+
]);
187+
$this->assertTrue(false, __METHOD__, "/Fail. Extect exception.");
188+
} catch (Exception $e) {
189+
$this->assertTrue(true, __METHOD__, "/Success. We have an exception: " . $e->getMessage());
190+
}
191+
}
98192

193+
/**
194+
* Expected Result, because bind filed names is valid
195+
*/
196+
public function testBindParamByFieldNameIsSuccess()
197+
{
198+
$stmt = $this->getStatementForTestBinding();
99199
//real named parameters
100-
$stmt->execute([
101-
'id' => 1,
102-
'name' => 'foo',
103-
'value' => 'bar',
200+
$result = $stmt->execute([
201+
'name' => 'bar', //OK -- 0 ":c_0" "name" varchar(255)
202+
'value' => 'foo', //OK -- 1 ":c_1" "value" varchar(255)
203+
'id' => 1, // OK -- 2 ":where1" "id" int
104204
]);
205+
$this->assertInstanceOf(ResultInterface::class, $result);
105206
}
106207
}

0 commit comments

Comments
 (0)