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