2
0

MysqliTest.php 9.0 KB

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