MysqliTest.php 10 KB

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