SqliteTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. * @category Zend
  28. * @package Zend_Db
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. * @group Zend_Db
  33. * @group Zend_Db_Adapter
  34. */
  35. class Zend_Db_Adapter_Pdo_SqliteTest extends Zend_Db_Adapter_Pdo_TestCommon
  36. {
  37. protected $_numericDataTypes = array(
  38. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  39. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  40. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  41. 'INTEGER' => Zend_Db::BIGINT_TYPE,
  42. 'REAL' => Zend_Db::FLOAT_TYPE
  43. );
  44. /**
  45. * Test AUTO_QUOTE_IDENTIFIERS option
  46. * Case: Zend_Db::AUTO_QUOTE_IDENTIFIERS = true
  47. *
  48. * SQLite actually allows delimited identifiers to remain
  49. * case-insensitive, so this test overrides its parent.
  50. */
  51. public function testAdapterAutoQuoteIdentifiersTrue()
  52. {
  53. $params = $this->_util->getParams();
  54. $params['options'] = array(
  55. Zend_Db::AUTO_QUOTE_IDENTIFIERS => true
  56. );
  57. $db = Zend_Db::factory($this->getDriver(), $params);
  58. $db->getConnection();
  59. $select = $this->_db->select();
  60. $select->from('zfproducts');
  61. $stmt = $this->_db->query($select);
  62. $result1 = $stmt->fetchAll();
  63. $this->assertEquals(1, $result1[0]['product_id']);
  64. $select = $this->_db->select();
  65. $select->from('ZFPRODUCTS');
  66. try {
  67. $stmt = $this->_db->query($select);
  68. $result2 = $stmt->fetchAll();
  69. } catch (Zend_Exception $e) {
  70. $this->assertTrue($e instanceof Zend_Db_Statement_Exception,
  71. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  72. $this->fail('Unexpected exception '.get_class($e).' received: '.$e->getMessage());
  73. }
  74. $this->assertEquals($result1, $result2);
  75. }
  76. public function testAdapterConstructInvalidParamDbnameException()
  77. {
  78. $this->markTestSkipped($this->getDriver() . ' does not throw exception on missing dbname');
  79. }
  80. public function testAdapterConstructInvalidParamUsernameException()
  81. {
  82. $this->markTestSkipped($this->getDriver() . ' does not support login credentials');
  83. }
  84. public function testAdapterConstructInvalidParamPasswordException()
  85. {
  86. $this->markTestSkipped($this->getDriver() . ' does not support login credentials');
  87. }
  88. public function testAdapterInsertSequence()
  89. {
  90. $this->markTestSkipped($this->getDriver() . ' does not support sequences');
  91. }
  92. /**
  93. * Used by:
  94. * - testAdapterOptionCaseFoldingNatural()
  95. * - testAdapterOptionCaseFoldingUpper()
  96. * - testAdapterOptionCaseFoldingLower()
  97. */
  98. protected function _testAdapterOptionCaseFoldingSetup(Zend_Db_Adapter_Abstract $db)
  99. {
  100. $db->getConnection();
  101. $this->_util->setUp($db);
  102. }
  103. /**
  104. * Test that quote() takes an array and returns
  105. * an imploded string of comma-separated, quoted elements.
  106. */
  107. public function testAdapterQuoteArray()
  108. {
  109. $array = array("it's", 'all', 'right!');
  110. $value = $this->_db->quote($array);
  111. $this->assertEquals("'it''s', 'all', 'right!'", $value);
  112. }
  113. /**
  114. * test that quote() escapes a double-quote
  115. * character in a string.
  116. */
  117. public function testAdapterQuoteDoubleQuote()
  118. {
  119. $value = $this->_db->quote('St John"s Wort');
  120. $this->assertEquals("'St John\"s Wort'", $value);
  121. }
  122. /**
  123. * test that quote() escapes a single-quote
  124. * character in a string.
  125. */
  126. public function testAdapterQuoteSingleQuote()
  127. {
  128. $string = "St John's Wort";
  129. $value = $this->_db->quote($string);
  130. $this->assertEquals("'St John''s Wort'", $value);
  131. }
  132. /**
  133. * test that quoteInto() escapes a double-quote
  134. * character in a string.
  135. */
  136. public function testAdapterQuoteIntoDoubleQuote()
  137. {
  138. $value = $this->_db->quoteInto('id=?', 'St John"s Wort');
  139. $this->assertEquals("id='St John\"s Wort'", $value);
  140. }
  141. /**
  142. * test that quoteInto() escapes a single-quote
  143. * character in a string.
  144. */
  145. public function testAdapterQuoteIntoSingleQuote()
  146. {
  147. $value = $this->_db->quoteInto('id = ?', 'St John\'s Wort');
  148. $this->assertEquals("id = 'St John''s Wort'", $value);
  149. }
  150. public function testAdapterTransactionAutoCommit()
  151. {
  152. $this->markTestSkipped($this->getDriver() . ' does not support transactions or concurrency');
  153. }
  154. public function testAdapterTransactionCommit()
  155. {
  156. $this->markTestSkipped($this->getDriver() . ' does not support transactions or concurrency');
  157. }
  158. public function testAdapterTransactionRollback()
  159. {
  160. $this->markTestSkipped($this->getDriver() . ' does not support transactions or concurrency');
  161. }
  162. /**
  163. * @group ZF-2293
  164. */
  165. public function testAdapterSupportsLengthInTableMetadataForVarcharFields()
  166. {
  167. $metadata = $this->_db->describeTable('zfbugs');
  168. $this->assertEquals(100, $metadata['bug_description']['LENGTH']);
  169. $this->assertEquals(20, $metadata['bug_status']['LENGTH']);
  170. }
  171. public function getDriver()
  172. {
  173. return 'Pdo_Sqlite';
  174. }
  175. public function testAdapterOptionFetchMode()
  176. {
  177. $params = $this->_util->getParams();
  178. $params['options'] = array(
  179. Zend_Db::FETCH_MODE => 'obj'
  180. );
  181. $db = Zend_Db::factory($this->getDriver(), $params);
  182. //two extra lines to make SQLite work
  183. $db->query('CREATE TABLE zfproducts (id)');
  184. $db->insert('zfproducts', array('id' => 1));
  185. $select = $db->select()->from('zfproducts');
  186. $row = $db->fetchRow($select);
  187. $this->assertTrue($row instanceof stdClass);
  188. }
  189. protected function _testAdapterAlternateStatement($stmtClass)
  190. {
  191. $ip = get_include_path();
  192. $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '_files';
  193. $newIp = $dir . PATH_SEPARATOR . $ip;
  194. set_include_path($newIp);
  195. $params = $this->_util->getParams();
  196. $params['options'] = array(
  197. Zend_Db::AUTO_QUOTE_IDENTIFIERS => false
  198. );
  199. $db = Zend_Db::factory($this->getDriver(), $params);
  200. $db->getConnection();
  201. $db->setStatementClass($stmtClass);
  202. $currentStmtClass = $db->getStatementClass();
  203. $this->assertEquals($stmtClass, $currentStmtClass);
  204. //extra fix for SQLite
  205. $db->query('CREATE TABLE zfbugs (id)');
  206. $bugs = $this->_db->quoteIdentifier('zfbugs');
  207. $stmt = $db->prepare("SELECT COUNT(*) FROM $bugs");
  208. $this->assertTrue($stmt instanceof $stmtClass,
  209. 'Expecting object of type ' . $stmtClass . ', got ' . get_class($stmt));
  210. }
  211. /**
  212. * test that quote() escapes null byte character
  213. * in a string.
  214. */
  215. public function testAdapterQuoteNullByteCharacter()
  216. {
  217. $string = "1\0";
  218. $value = $this->_db->quote($string);
  219. $this->assertEquals("'1\\000'", $value);
  220. }
  221. }