|
2 | 2 |
|
3 | 3 | namespace LaminasIntegrationTest\Db\Adapter\Driver\Pdo\Mysql;
|
4 | 4 |
|
| 5 | +use Exception; |
5 | 6 | use Laminas\Db\Adapter\Driver\Pdo\Result as PdoResult;
|
| 7 | +use Laminas\Db\Adapter\Driver\ResultInterface; |
| 8 | +use Laminas\Db\Adapter\Driver\StatementInterface; |
6 | 9 | use Laminas\Db\Adapter\Exception\RuntimeException;
|
7 | 10 | use Laminas\Db\ResultSet\ResultSet;
|
8 | 11 | use Laminas\Db\Sql\Sql;
|
@@ -37,8 +40,8 @@ public function getQueriesWithRowResult(): array
|
37 | 40 |
|
38 | 41 | /**
|
39 | 42 | * @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 |
42 | 45 | */
|
43 | 46 | public function testQuery(string $query, array $params, array $expected)
|
44 | 47 | {
|
@@ -69,38 +72,136 @@ public function testSelectWithNotPermittedBindParamName()
|
69 | 72 | }
|
70 | 73 |
|
71 | 74 | /**
|
| 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 | + * |
72 | 82 | * @see https://github.com/laminas/laminas-db/issues/47
|
| 83 | + * @see https://github.com/laminas/laminas-db/issues/214 |
| 84 | + * |
| 85 | + * @return StatementInterface |
73 | 86 | */
|
74 |
| - public function testNamedParameters() |
| 87 | + protected function getStatementForTestBinding() |
75 | 88 | {
|
76 | 89 | $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([ |
80 | 95 | 'name' => ':name',
|
81 | 96 | '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 | + } |
84 | 122 |
|
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 |
90 | 134 | ]);
|
| 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 | + } |
91 | 157 |
|
| 158 | + /** |
| 159 | + * Expected Result, because bind names is valid |
| 160 | + */ |
| 161 | + public function testBindParamByNameIsSuccess() |
| 162 | + { |
| 163 | + $stmt = $this->getStatementForTestBinding(); |
92 | 164 | //"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 |
97 | 169 | ]);
|
| 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 | + } |
98 | 192 |
|
| 193 | + /** |
| 194 | + * Expected Result, because bind filed names is valid |
| 195 | + */ |
| 196 | + public function testBindParamByFieldNameIsSuccess() |
| 197 | + { |
| 198 | + $stmt = $this->getStatementForTestBinding(); |
99 | 199 | //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 |
104 | 204 | ]);
|
| 205 | + $this->assertInstanceOf(ResultInterface::class, $result); |
105 | 206 | }
|
106 | 207 | }
|
0 commit comments