PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/DevApp/library/ServerLibraries/ZendFramework/1.7/tests/Zend/Db/Adapter/Pdo/MysqlTest.php

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