MysqlTest.php 10 KB

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