This repository was archived by the owner on May 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 810
Expand file tree
/
Copy pathMysqliTest.php
More file actions
327 lines (268 loc) · 10.7 KB
/
Copy pathMysqliTest.php
File metadata and controls
327 lines (268 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Db
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id $
*/
require_once 'Zend/Db/Statement/TestCommon.php';
require_once 'Zend/Db/Statement/Mysqli.php';
/**
* Wrapper class for test protected function _stripQuoted
*/
class Zend_Db_Statement_Mysqli_Test_Class extends Zend_Db_Statement_Mysqli
{
public function stripQuoted($sql)
{
return $this->_stripQuoted($sql);
}
}
/**
* @category Zend
* @package Zend_Db
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @group Zend_Db
* @group Zend_Db_Statement
*/
class Zend_Db_Statement_MysqliTest extends Zend_Db_Statement_TestCommon
{
protected $_Zend_Db_Statement_Mysqli_Test_Class = null;
/**
* @group ZF-7911
*/
public function testStripQuoted()
{
$this->_Zend_Db_Statement_Mysqli_Test_Class = new Zend_Db_Statement_Mysqli_Test_Class($this->_db, "SELECT 1");
$input = <<<INPUT
in: [SELECT * FROM `strange`table1`]
out: [SELECT * FROM table1`]
in: [SELECT * FROM `strange``table2`]
out: [SELECT * FROM ]
in: [SELECT * FROM `strange```table3`]
out: [SELECT * FROM table3`]
in: [SELECT * FROM `strange\`table4`]
out: [SELECT * FROM table4`]
in: [SELECT * FROM `strange\``table5`]
out: [SELECT * FROM ]
in: [SELECT * FROM `strange\```table6`]
out: [SELECT * FROM table6`]
in: [SELECT 'value7' AS identifier]
out: [SELECT AS identifier]
in: [SELECT 'strange:value8' AS identifier]
out: [SELECT AS identifier]
in: [SELECT 'strange'value9' AS identifier]
out: [SELECT value9' AS identifier]
in: [SELECT 'strange''value10' AS identifier]
out: [SELECT AS identifier]
in: [SELECT 'strange'''value11' AS identifier]
out: [SELECT value11' AS identifier]
in: [SELECT 'strange\'value12' AS identifier]
out: [SELECT AS identifier]
in: [SELECT 'strange\''value13' AS identifier]
out: [SELECT value13' AS identifier]
in: [SELECT 'strange'''value14' AS identifier]
out: [SELECT value14' AS identifier]
in: [SELECT "value15" AS identifier]
out: [SELECT AS identifier]
in: [SELECT "strange:value16" AS identifier]
out: [SELECT AS identifier]
in: [SELECT "strange"value17" AS identifier]
out: [SELECT value17" AS identifier]
in: [SELECT "strange""value18" AS identifier]
out: [SELECT AS identifier]
in: [SELECT "strange"""value19" AS identifier]
out: [SELECT value19" AS identifier]
in: [SELECT "strange\"value20" AS identifier]
out: [SELECT AS identifier]
in: [SELECT "strange\""value21" AS identifier]
out: [SELECT value21" AS identifier]
in: [SELECT "strange"""value22" AS identifier]
out: [SELECT value22" AS identifier]
in: [SELECT 'strange\'''value23' AS identifier]
out: [SELECT AS identifier]
in: [SELECT '?`' `x`, col `y` FROM t WHERE u = ?;]
out: [SELECT , col FROM t WHERE u = ?;]
in: [SELECT "?`" `x`, col `y` FROM t WHERE u = ?;]
out: [SELECT , col FROM t WHERE u = ?;]
in: [INSERT INTO `pcre` (`test`) VALUES ('In MySQL, the backtick (`) is used to quoted identifiers, and here is another backtick: ` ...ooops');]
out: [INSERT INTO () VALUES ();]
INPUT;
// parse the input
$inputOutputLines = explode('in:', $input);
$count = 0;
foreach ($inputOutputLines as $ioLine) {
if (!trim($ioLine)) {
continue;
}
$count++;
$io = explode('out:', $ioLine);
$in = str_replace(array('[', ']'),'', trim($io[0]));
$out = str_replace(array('[', ']'),'', trim($io[1]));
$actual = $this->_Zend_Db_Statement_Mysqli_Test_Class->stripQuoted($in);
$this->assertSame($out, $actual, $count . ' - unexpected output');
}
}
public function testStripQuotedForLongQuery()
{
$statementClass = 'Zend_Db_Statement_' . $this->getDriver();
$table = $this->_db->quoteIdentifier('zfproducts');
$column = $this->_db->quoteIdentifier('product_name');
$sql = 'SELECT * FROM `zfproducts` WHERE `product_name` = "%s"';
$columnContent = str_repeat('a', 15000) . '\\"' . str_repeat('b', 15000);
$sql = sprintf($sql, $columnContent);
$stmt = new $statementClass($this->_db, $sql);
$this->assertNotNull($stmt->getDriverStatement());
}
public function testStatementRowCount()
{
$products = $this->_db->quoteIdentifier('zfproducts');
$product_id = $this->_db->quoteIdentifier('product_id');
$stmt = $this->_db->prepare("DELETE FROM $products WHERE $product_id = 1");
$n = $stmt->rowCount();
$this->assertTrue(is_int($n));
$this->assertEquals(-1, $n, 'Expecting row count to be -1 before executing query');
$stmt->execute();
$n = $stmt->rowCount();
$stmt->closeCursor();
$this->assertTrue(is_int($n));
$this->assertEquals(1, $n, 'Expected row count to be one after executing query');
}
public function testStatementBindParamByName()
{
$products = $this->_db->quoteIdentifier('zfproducts');
$product_id = $this->_db->quoteIdentifier('product_id');
$product_name = $this->_db->quoteIdentifier('product_name');
$productIdValue = 4;
$productNameValue = 'AmigaOS';
try {
$stmt = $this->_db->prepare("INSERT INTO $products ($product_id, $product_name) VALUES (:id, :name)");
// test with colon prefix
$this->assertTrue($stmt->bindParam(':id', $productIdValue), 'Expected bindParam(\':id\') to return true');
// test with no colon prefix
$this->assertTrue($stmt->bindParam('name', $productNameValue), 'Expected bindParam(\'name\') to return true');
$this->fail('Expected to catch Zend_Db_Statement_Exception');
} catch (Zend_Exception $e) {
$this->assertTrue($e instanceof Zend_Db_Statement_Exception,
'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
$this->assertEquals("Invalid bind-variable name ':id'", $e->getMessage());
}
}
public function testStatementBindValueByName()
{
$products = $this->_db->quoteIdentifier('zfproducts');
$product_id = $this->_db->quoteIdentifier('product_id');
$product_name = $this->_db->quoteIdentifier('product_name');
$productIdValue = 4;
$productNameValue = 'AmigaOS';
try {
$stmt = $this->_db->prepare("INSERT INTO $products ($product_id, $product_name) VALUES (:id, :name)");
// test with colon prefix
$this->assertTrue($stmt->bindParam(':id', $productIdValue), 'Expected bindParam(\':id\') to return true');
// test with no colon prefix
$this->assertTrue($stmt->bindParam('name', $productNameValue), 'Expected bindParam(\'name\') to return true');
$this->fail('Expected to catch Zend_Db_Statement_Exception');
} catch (Zend_Exception $e) {
$this->assertTrue($e instanceof Zend_Db_Statement_Exception,
'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
$this->assertEquals("Invalid bind-variable name ':id'", $e->getMessage());
}
}
public function testStatementGetColumnMeta()
{
$this->markTestIncomplete($this->getDriver() . ' has not implemented getColumnMeta() yet [ZF-1424]');
}
/**
* Tests ZF-3216, that the statement object throws exceptions that
* contain the numerica MySQL SQLSTATE error code
* @group ZF-3216
*/
public function testStatementExceptionShouldContainErrorCode()
{
$sql = "SELECT * FROM *";
try {
$stmt = $this->_db->query($sql);
$this->fail('Expected to catch Zend_Db_Statement_Exception');
} catch (Zend_Exception $e) {
$this->assertTrue(is_int($e->getCode()));
}
}
/**
* @group ZF-7706
*/
public function testStatementCanReturnDriverStatement()
{
$statement = parent::testStatementCanReturnDriverStatement();
$this->assertTrue($statement->getDriverStatement() instanceof mysqli_stmt);
}
/**
* Tests that the statement returns FALSE when no records are found
* @group ZF-5675
*/
public function testStatementReturnsFalseOnEmpty()
{
$products = $this->_db->quoteIdentifier('zfproducts');
$sql = 'SELECT * FROM ' . $products . ' WHERE 1=2';
$stmt = $this->_db->query($sql);
$result = $stmt->fetch();
$this->assertFalse($result);
}
/**
* Test to verify valid report of issue
*
* @group ZF-8986
*/
public function testNumberOfBoundParamsDoesNotMatchNumberOfTokens()
{
$this->_util->createTable('zf_objects', array(
'object_id' => 'INTEGER NOT NULL',
'object_type' => 'INTEGER NOT NULL',
'object_status' => 'INTEGER NOT NULL',
'object_lati' => 'REAL',
'object_long' => 'REAL',
));
$tableName = $this->_util->getTableName('zf_objects');
$numRows = $this->_db->insert($tableName, array (
'object_id' => 1,
'object_type' => 1,
'object_status' => 1,
'object_lati' => 1.12345,
'object_long' => 1.54321,
));
$sql = 'SELECT object_id, object_type, object_status,'
. ' object_lati, object_long FROM ' . $tableName
. ' WHERE object_id = ?';
try {
$stmt = $this->_db->query($sql, 1);
} catch (Exception $e) {
$this->fail('Bounding params failed: ' . $e->getMessage());
}
$result = $stmt->fetch();
$this->assertTrue(is_array($result));
$this->assertEquals(5, count($result));
$this->assertEquals(1, $result['object_id']);
$this->assertEquals(1, $result['object_type']);
$this->assertEquals(1, $result['object_status']);
$this->assertEquals(1.12345, $result['object_lati']);
$this->assertEquals(1.54321, $result['object_long']);
}
public function getDriver()
{
return 'Mysqli';
}
}