2
0

MysqlTest.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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_Pdo_TestCommon
  24. */
  25. require_once 'Zend/Db/Adapter/Pdo/TestCommon.php';
  26. /**
  27. * @see Zend_Db_Adapter_Pdo_Mysql
  28. */
  29. require_once 'Zend/Db/Adapter/Pdo/Mysql.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_Pdo_MysqlTest extends Zend_Db_Adapter_Pdo_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(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. } catch (Zend_Exception $e) {
  86. $this->assertType('Zend_Db_Statement_Exception', $e,
  87. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  88. $this->fail('Unexpected exception '.get_class($e).' received: '.$e->getMessage());
  89. }
  90. $this->assertEquals($result1, $result2);
  91. }
  92. /**
  93. * Ensures that driver_options are properly passed along to PDO
  94. *
  95. * @see http://framework.zend.com/issues/browse/ZF-285
  96. * @return void
  97. */
  98. public function testAdapterDriverOptions()
  99. {
  100. $params = $this->_util->getParams();
  101. $params['driver_options'] = array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true);
  102. $db = Zend_Db::factory($this->getDriver(), $params);
  103. $this->assertTrue((boolean) $db->getConnection()->getAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY));
  104. $params['driver_options'] = array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => false);
  105. $db = Zend_Db::factory($this->getDriver(), $params);
  106. $this->assertFalse((boolean) $db->getConnection()->getAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY));
  107. }
  108. public function testAdapterInsertSequence()
  109. {
  110. $this->markTestSkipped($this->getDriver() . ' does not support sequences');
  111. }
  112. /**
  113. * test that quoteColumnAs() accepts a string
  114. * and an alias, and returns each as delimited
  115. * identifiers, with 'AS' in between.
  116. */
  117. public function testAdapterQuoteColumnAs()
  118. {
  119. $string = "foo";
  120. $alias = "bar";
  121. $value = $this->_db->quoteColumnAs($string, $alias);
  122. $this->assertEquals('`foo` AS `bar`', $value);
  123. }
  124. /**
  125. * test that quoteColumnAs() accepts a string
  126. * and an alias, but ignores the alias if it is
  127. * the same as the base identifier in the string.
  128. */
  129. public function testAdapterQuoteColumnAsSameString()
  130. {
  131. $string = 'foo.bar';
  132. $alias = 'bar';
  133. $value = $this->_db->quoteColumnAs($string, $alias);
  134. $this->assertEquals('`foo`.`bar`', $value);
  135. }
  136. /**
  137. * test that quoteIdentifier() accepts a string
  138. * and returns a delimited identifier.
  139. */
  140. public function testAdapterQuoteIdentifier()
  141. {
  142. $value = $this->_db->quoteIdentifier('table_name');
  143. $this->assertEquals('`table_name`', $value);
  144. }
  145. /**
  146. * test that quoteIdentifier() accepts an array
  147. * and returns a qualified delimited identifier.
  148. */
  149. public function testAdapterQuoteIdentifierArray()
  150. {
  151. $array = array('foo', 'bar');
  152. $value = $this->_db->quoteIdentifier($array);
  153. $this->assertEquals('`foo`.`bar`', $value);
  154. }
  155. /**
  156. * test that quoteIdentifier() accepts an array
  157. * containing a Zend_Db_Expr, and returns strings
  158. * as delimited identifiers, and Exprs as unquoted.
  159. */
  160. public function testAdapterQuoteIdentifierArrayDbExpr()
  161. {
  162. $expr = new Zend_Db_Expr('*');
  163. $array = array('foo', $expr);
  164. $value = $this->_db->quoteIdentifier($array);
  165. $this->assertEquals('`foo`.*', $value);
  166. }
  167. /**
  168. * test that quoteIdentifer() escapes a double-quote
  169. * character in a string.
  170. */
  171. public function testAdapterQuoteIdentifierDoubleQuote()
  172. {
  173. $string = 'table_"_name';
  174. $value = $this->_db->quoteIdentifier($string);
  175. $this->assertEquals('`table_"_name`', $value);
  176. }
  177. /**
  178. * test that quoteIdentifer() accepts an integer
  179. * and returns a delimited identifier as with a string.
  180. */
  181. public function testAdapterQuoteIdentifierInteger()
  182. {
  183. $int = 123;
  184. $value = $this->_db->quoteIdentifier($int);
  185. $this->assertEquals('`123`', $value);
  186. }
  187. /**
  188. * test that quoteIdentifier() accepts a string
  189. * containing a dot (".") character, splits the
  190. * string, quotes each segment individually as
  191. * delimited identifers, and returns the imploded
  192. * string.
  193. */
  194. public function testAdapterQuoteIdentifierQualified()
  195. {
  196. $string = 'table.column';
  197. $value = $this->_db->quoteIdentifier($string);
  198. $this->assertEquals('`table`.`column`', $value);
  199. }
  200. /**
  201. * test that quoteIdentifer() escapes a single-quote
  202. * character in a string.
  203. */
  204. public function testAdapterQuoteIdentifierSingleQuote()
  205. {
  206. $string = "table_'_name";
  207. $value = $this->_db->quoteIdentifier($string);
  208. $this->assertEquals('`table_\'_name`', $value);
  209. }
  210. /**
  211. * test that describeTable() returns correct types
  212. * @group ZF-3624
  213. *
  214. */
  215. public function testAdapterDescribeTableAttributeColumnFloat()
  216. {
  217. $desc = $this->_db->describeTable('zfprice');
  218. $this->assertEquals('zfprice', $desc['price']['TABLE_NAME']);
  219. $this->assertRegExp('/float/i', $desc['price']['DATA_TYPE']);
  220. }
  221. /**
  222. * test that quoteTableAs() accepts a string and an alias,
  223. * and returns each as delimited identifiers.
  224. * Most RDBMS want an 'AS' in between.
  225. */
  226. public function testAdapterQuoteTableAs()
  227. {
  228. $string = "foo";
  229. $alias = "bar";
  230. $value = $this->_db->quoteTableAs($string, $alias);
  231. $this->assertEquals('`foo` AS `bar`', $value);
  232. }
  233. /**
  234. * Ensures that the character sequence ":0'" is handled properly
  235. *
  236. * @link http://framework.zend.com/issues/browse/ZF-2059
  237. * @return void
  238. */
  239. public function testZF2059()
  240. {
  241. $this->markTestIncomplete('Inconsistent test results');
  242. }
  243. /**
  244. * Ensures that the PDO Buffered Query does not throw the error
  245. * 2014 General error
  246. *
  247. * @link http://framework.zend.com/issues/browse/ZF-2101
  248. * @return void
  249. */
  250. public function testZF2101()
  251. {
  252. $params = $this->_util->getParams();
  253. $params['driver_options'] = array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true);
  254. $db = Zend_Db::factory($this->getDriver(), $params);
  255. // Set default bound value
  256. $customerId = 1;
  257. // Stored procedure returns a single row
  258. $stmt = $db->prepare('CALL zf_test_procedure(:customerId)');
  259. $stmt->bindParam('customerId', $customerId, PDO::PARAM_INT);
  260. $stmt->execute();
  261. $result = $stmt->fetchAll();
  262. $this->assertEquals(1, $result[0]['product_id']);
  263. // Reset statement
  264. $stmt->closeCursor();
  265. // Stored procedure returns a single row
  266. $stmt = $db->prepare('CALL zf_test_procedure(:customerId)');
  267. $stmt->bindParam('customerId', $customerId, PDO::PARAM_INT);
  268. $stmt->execute();
  269. $result = $stmt->fetchAll();
  270. $this->assertEquals(1, $result[0]['product_id']);
  271. }
  272. public function getDriver()
  273. {
  274. return 'Pdo_Mysql';
  275. }
  276. }