FirebirdTest.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 ZendX
  16. * @package ZendX_Db
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see Zend_Db_Adapter_TestCommon
  23. */
  24. require_once 'Zend/Db/Adapter/TestCommon.php';
  25. /**
  26. * @see Zend_Db_Adapter_Firebird
  27. */
  28. require_once 'ZendX/Db/Adapter/Firebird.php';
  29. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  30. class ZendX_Db_Adapter_FirebirdTest extends Zend_Db_Adapter_TestCommon
  31. {
  32. protected $_numericDataTypes = array(
  33. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  34. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  35. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  36. 'INT' => Zend_Db::INT_TYPE,
  37. 'INTEGER' => Zend_Db::INT_TYPE,
  38. 'SMALLINT' => Zend_Db::INT_TYPE,
  39. 'BIGINT' => Zend_Db::BIGINT_TYPE,
  40. 'INT64' => Zend_Db::BIGINT_TYPE,
  41. 'DECIMAL' => Zend_Db::FLOAT_TYPE,
  42. 'DOUBLE' => Zend_Db::FLOAT_TYPE,
  43. 'DOUBLE PRECISION' => Zend_Db::FLOAT_TYPE,
  44. 'NUMERIC' => Zend_Db::FLOAT_TYPE,
  45. 'FLOAT' => Zend_Db::FLOAT_TYPE
  46. );
  47. public function testAdapterDescribeTablePrimaryAuto()
  48. {
  49. $this->markTestSkipped($this->getDriver() . ' does not support auto-increment');
  50. }
  51. public function testAdapterInsert()
  52. {
  53. $row = array (
  54. 'product_id' => $this->_db->nextSequenceId('zfproducts_seq'),
  55. 'product_name' => 'Solaris',
  56. );
  57. $rowsAffected = $this->_db->insert('zfproducts', $row);
  58. $this->assertEquals(1, $rowsAffected);
  59. $lastInsertId = $this->_db->lastInsertId('zfproducts', null); // implies 'zfproducts_seq'
  60. $lastSequenceId = $this->_db->lastSequenceId('zfproducts_seq');
  61. $this->assertEquals('4', (string) $lastInsertId, 'Expected new id to be 4');
  62. $this->assertEquals('4', (string) $lastSequenceId, 'Expected new id to be 4');
  63. }
  64. /**
  65. * test that quote() escapes a single-quote
  66. * character in a string.
  67. */
  68. public function testAdapterQuoteSingleQuote()
  69. {
  70. $string = "St John's Wort";
  71. $value = $this->_db->quote($string);
  72. $this->assertEquals("'St John''s Wort'", $value);
  73. }
  74. /**
  75. * test that quoteTableAs() accepts a string and an alias,
  76. * and returns each as delimited identifiers.
  77. * Oracle does not want the 'AS' in between.
  78. */
  79. public function testAdapterQuoteTableAs()
  80. {
  81. $string = "foo";
  82. $alias = "bar";
  83. $value = $this->_db->quoteTableAs($string, $alias);
  84. $this->assertEquals('"foo" "bar"', $value);
  85. }
  86. /**
  87. * test that quote() escapes a double-quote
  88. * character in a string.
  89. */
  90. public function testAdapterQuoteDoubleQuote()
  91. {
  92. $value = $this->_db->quote('St John"s Wort');
  93. $this->assertEquals("'St John\"s Wort'", $value);
  94. }
  95. /**
  96. * Test that quote() takes an array and returns
  97. * an imploded string of comma-separated, quoted elements.
  98. */
  99. public function testAdapterQuoteArray()
  100. {
  101. $array = array("it's", 'all', 'right!');
  102. $value = $this->_db->quote($array);
  103. $this->assertEquals("'it''s', 'all', 'right!'", $value);
  104. }
  105. /**
  106. * test that quoteInto() escapes a single-quote
  107. * character in a string.
  108. */
  109. public function testAdapterQuoteIntoSingleQuote()
  110. {
  111. $value = $this->_db->quoteInto('id = ?', 'St John\'s Wort');
  112. $this->assertEquals("id = 'St John''s Wort'", $value);
  113. }
  114. /**
  115. * test that quoteInto() escapes a double-quote
  116. * character in a string.
  117. */
  118. public function testAdapterQuoteIntoDoubleQuote()
  119. {
  120. $value = $this->_db->quoteInto('id=?', 'St John"s Wort');
  121. $this->assertEquals("id='St John\"s Wort'", $value);
  122. }
  123. public function testZF2059()
  124. {
  125. $this->markTestSkipped($this->getDriver() . ' not affected by ZF-2059');
  126. }
  127. // Deffers from others RDBMS, Firebird always is in a transaction, and for Zend_Db the default
  128. // transaction Isolation is snapshot, changes made by other transaction is only visible with new
  129. // transaction, commiting retaining or rollback retaining
  130. public function testAdapterTransactionCommit()
  131. {
  132. $bugs = $this->_db->quoteIdentifier('zfbugs');
  133. $bug_id = $this->_db->quoteIdentifier('bug_id');
  134. // use our default connection as the Connection1
  135. $dbConnection1 = $this->_db;
  136. // create a second connection to the same database
  137. $dbConnection2 = Zend_Db::factory($this->getDriver(), $this->_util->getParams());
  138. $dbConnection2->getConnection();
  139. // notice the number of rows in connection 2
  140. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  141. $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
  142. // start an explicit transaction in connection 1
  143. $dbConnection1->beginTransaction();
  144. // delete a row in connection 1
  145. $rowsAffected = $dbConnection1->delete(
  146. 'zfbugs',
  147. "$bug_id = 1"
  148. );
  149. $this->assertEquals(1, $rowsAffected);
  150. // we should still see all rows in connection 2
  151. // because the DELETE has not been committed yet
  152. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  153. $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table (step 2); perhaps Adapter is still in autocommit mode?');
  154. // commit the DELETE
  155. $dbConnection1->commit();
  156. // now we should see one fewer rows in connection 2
  157. $dbConnection2->commit();
  158. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  159. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 3)');
  160. // delete another row in connection 1
  161. $rowsAffected = $dbConnection1->delete(
  162. 'zfbugs',
  163. "$bug_id = 2"
  164. );
  165. $this->assertEquals(1, $rowsAffected);
  166. // we should see results immediately, because
  167. // the db connection returns to auto-commit mode
  168. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  169. $this->assertEquals(2, $count);
  170. }
  171. /**
  172. * Used by _testAdapterOptionCaseFoldingNatural()
  173. * DB2, Oracle and Firebird return identifiers in uppercase naturally,
  174. * so those test suites will override this method.
  175. */
  176. protected function _testAdapterOptionCaseFoldingNaturalIdentifier()
  177. {
  178. return 'CASE_FOLDED_IDENTIFIER';
  179. }
  180. public function testAdapterOptionCaseFoldingLower()
  181. {
  182. $this->markTestSkipped($this->getDriver() . ' always return UPPERCASE Natural Identifiers');
  183. }
  184. public function getDriver()
  185. {
  186. return 'Firebird';
  187. }
  188. }