MysqliTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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-2015 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. /**
  23. * @see Zend_Db_Adapter_TestCommon
  24. */
  25. require_once 'Zend/Db/Adapter/TestCommon.php';
  26. /**
  27. * @see Zend_Db_Adapter_Mysqli
  28. */
  29. require_once 'Zend/Db/Adapter/Mysqli.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Db
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. * @group Zend_Db
  37. * @group Zend_Db_Adapter
  38. */
  39. class Zend_Db_Adapter_MysqliTest extends Zend_Db_Adapter_TestCommon
  40. {
  41. protected $_numericDataTypes = array(
  42. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  43. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  44. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  45. 'INT' => Zend_Db::INT_TYPE,
  46. 'INTEGER' => Zend_Db::INT_TYPE,
  47. 'MEDIUMINT' => Zend_Db::INT_TYPE,
  48. 'SMALLINT' => Zend_Db::INT_TYPE,
  49. 'TINYINT' => Zend_Db::INT_TYPE,
  50. 'BIGINT' => Zend_Db::BIGINT_TYPE,
  51. 'SERIAL' => Zend_Db::BIGINT_TYPE,
  52. 'DEC' => Zend_Db::FLOAT_TYPE,
  53. 'DECIMAL' => Zend_Db::FLOAT_TYPE,
  54. 'DOUBLE' => Zend_Db::FLOAT_TYPE,
  55. 'DOUBLE PRECISION' => Zend_Db::FLOAT_TYPE,
  56. 'FIXED' => Zend_Db::FLOAT_TYPE,
  57. 'FLOAT' => Zend_Db::FLOAT_TYPE
  58. );
  59. /**
  60. * Test AUTO_QUOTE_IDENTIFIERS option
  61. * Case: Zend_Db::AUTO_QUOTE_IDENTIFIERS = true
  62. *
  63. * MySQL actually allows delimited identifiers to remain
  64. * case-insensitive, so this test overrides its parent.
  65. */
  66. public function testAdapterAutoQuoteIdentifiersTrue()
  67. {
  68. $params = $this->_util->getParams();
  69. $params['options'] = array(
  70. Zend_Db::AUTO_QUOTE_IDENTIFIERS => true
  71. );
  72. $db = Zend_Db::factory($this->getDriver(), $params);
  73. $db->getConnection();
  74. $select = $this->_db->select();
  75. $select->from('zfproducts');
  76. $stmt = $this->_db->query($select);
  77. $result1 = $stmt->fetchAll();
  78. $this->assertEquals(3, count($result1), 'Expected 3 rows in first query result');
  79. $this->assertEquals(1, $result1[0]['product_id']);
  80. $select = $this->_db->select();
  81. $select->from('zfproducts');
  82. try {
  83. $stmt = $this->_db->query($select);
  84. $result2 = $stmt->fetchAll();
  85. $this->assertEquals(3, count($result2), 'Expected 3 rows in second query result');
  86. $this->assertEquals($result1, $result2);
  87. } catch (Zend_Exception $e) {
  88. $this->fail('exception caught where none was expected.');
  89. }
  90. }
  91. public function testAdapterInsertSequence()
  92. {
  93. $this->markTestSkipped($this->getDriver() . ' does not support sequences');
  94. }
  95. /**
  96. * test that quoteColumnAs() accepts a string
  97. * and an alias, and returns each as delimited
  98. * identifiers, with 'AS' in between.
  99. */
  100. public function testAdapterQuoteColumnAs()
  101. {
  102. $string = "foo";
  103. $alias = "bar";
  104. $value = $this->_db->quoteColumnAs($string, $alias);
  105. $this->assertEquals('`foo` AS `bar`', $value);
  106. }
  107. /**
  108. * test that quoteColumnAs() accepts a string
  109. * and an alias, but ignores the alias if it is
  110. * the same as the base identifier in the string.
  111. */
  112. public function testAdapterQuoteColumnAsSameString()
  113. {
  114. $string = 'foo.bar';
  115. $alias = 'bar';
  116. $value = $this->_db->quoteColumnAs($string, $alias);
  117. $this->assertEquals('`foo`.`bar`', $value);
  118. }
  119. /**
  120. * test that quoteIdentifier() accepts a string
  121. * and returns a delimited identifier.
  122. */
  123. public function testAdapterQuoteIdentifier()
  124. {
  125. $value = $this->_db->quoteIdentifier('table_name');
  126. $this->assertEquals('`table_name`', $value);
  127. }
  128. /**
  129. * test that quoteIdentifier() accepts an array
  130. * and returns a qualified delimited identifier.
  131. */
  132. public function testAdapterQuoteIdentifierArray()
  133. {
  134. $array = array('foo', 'bar');
  135. $value = $this->_db->quoteIdentifier($array);
  136. $this->assertEquals('`foo`.`bar`', $value);
  137. }
  138. /**
  139. * test that quoteIdentifier() accepts an array
  140. * containing a Zend_Db_Expr, and returns strings
  141. * as delimited identifiers, and Exprs as unquoted.
  142. */
  143. public function testAdapterQuoteIdentifierArrayDbExpr()
  144. {
  145. $expr = new Zend_Db_Expr('*');
  146. $array = array('foo', $expr);
  147. $value = $this->_db->quoteIdentifier($array);
  148. $this->assertEquals('`foo`.*', $value);
  149. }
  150. /**
  151. * test that quoteIdentifer() escapes a double-quote
  152. * character in a string.
  153. */
  154. public function testAdapterQuoteIdentifierDoubleQuote()
  155. {
  156. $string = 'table_"_name';
  157. $value = $this->_db->quoteIdentifier($string);
  158. $this->assertEquals('`table_"_name`', $value);
  159. }
  160. /**
  161. * test that quoteIdentifer() accepts an integer
  162. * and returns a delimited identifier as with a string.
  163. */
  164. public function testAdapterQuoteIdentifierInteger()
  165. {
  166. $int = 123;
  167. $value = $this->_db->quoteIdentifier($int);
  168. $this->assertEquals('`123`', $value);
  169. }
  170. /**
  171. * test that quoteIdentifier() accepts a string
  172. * containing a dot (".") character, splits the
  173. * string, quotes each segment individually as
  174. * delimited identifers, and returns the imploded
  175. * string.
  176. */
  177. public function testAdapterQuoteIdentifierQualified()
  178. {
  179. $string = 'table.column';
  180. $value = $this->_db->quoteIdentifier($string);
  181. $this->assertEquals('`table`.`column`', $value);
  182. }
  183. /**
  184. * test that quoteIdentifer() escapes a single-quote
  185. * character in a string.
  186. */
  187. public function testAdapterQuoteIdentifierSingleQuote()
  188. {
  189. $string = "table_'_name";
  190. $value = $this->_db->quoteIdentifier($string);
  191. $this->assertEquals('`table_\'_name`', $value);
  192. }
  193. /**
  194. * test that quoteTableAs() accepts a string and an alias,
  195. * and returns each as delimited identifiers.
  196. * Most RDBMS want an 'AS' in between.
  197. */
  198. public function testAdapterQuoteTableAs()
  199. {
  200. $string = "foo";
  201. $alias = "bar";
  202. $value = $this->_db->quoteTableAs($string, $alias);
  203. $this->assertEquals('`foo` AS `bar`', $value);
  204. }
  205. /**
  206. * test that describeTable() returns correct types
  207. * @group ZF-3624
  208. *
  209. */
  210. public function testAdapterDescribeTableAttributeColumnFloat()
  211. {
  212. $desc = $this->_db->describeTable('zfprice');
  213. $this->assertEquals('zfprice', $desc['price']['TABLE_NAME']);
  214. $this->assertRegExp('/float/i', $desc['price']['DATA_TYPE']);
  215. }
  216. /**
  217. * Ensures that the PDO Buffered Query does not throw the error
  218. * 2014 General error
  219. *
  220. * @group ZF-2101
  221. * @link http://framework.zend.com/issues/browse/ZF-2101
  222. * @return void
  223. */
  224. public function testAdapterToEnsurePdoBufferedQueryThrowsNoError()
  225. {
  226. $params = $this->_util->getParams();
  227. $db = Zend_Db::factory($this->getDriver(), $params);
  228. // Set default bound value
  229. $customerId = 1;
  230. // Stored procedure returns a single row
  231. $stmt = $db->prepare('CALL zf_test_procedure(?)');
  232. $stmt->bindParam(1, $customerId);
  233. $stmt->execute();
  234. $result = $stmt->fetchAll();
  235. $this->assertEquals(1, $result[0]['product_id']);
  236. // Reset statement
  237. $stmt->closeCursor();
  238. // Stored procedure returns a single row
  239. $stmt = $db->prepare('CALL zf_test_procedure(?)');
  240. $stmt->bindParam(1, $customerId);
  241. $stmt->execute();
  242. $this->assertEquals(1, $result[0]['product_id']);
  243. }
  244. public function testAdapterAlternateStatement()
  245. {
  246. $this->_testAdapterAlternateStatement('Test_MysqliStatement');
  247. }
  248. public function testMySqliInitCommand()
  249. {
  250. $params = $this->_util->getParams();
  251. $params['driver_options'] = array(
  252. 'mysqli_init_command' => 'SET AUTOCOMMIT=0;'
  253. );
  254. $db = Zend_Db::factory($this->getDriver(), $params);
  255. $sql = 'SELECT @@AUTOCOMMIT as autocommit';
  256. $row = $db->fetchRow($sql);
  257. $this->assertEquals(0, $row['autocommit']);
  258. }
  259. public function getDriver()
  260. {
  261. return 'Mysqli';
  262. }
  263. }