2
0

PgsqlTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. */
  21. require_once 'Zend/Db/Adapter/Pdo/TestCommon.php';
  22. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  23. class Zend_Db_Adapter_Pdo_PgsqlTest extends Zend_Db_Adapter_Pdo_TestCommon
  24. {
  25. protected $_numericDataTypes = array(
  26. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  27. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  28. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  29. 'INTEGER' => Zend_Db::INT_TYPE,
  30. 'SERIAL' => Zend_Db::INT_TYPE,
  31. 'SMALLINT' => Zend_Db::INT_TYPE,
  32. 'BIGINT' => Zend_Db::BIGINT_TYPE,
  33. 'BIGSERIAL' => Zend_Db::BIGINT_TYPE,
  34. 'DECIMAL' => Zend_Db::FLOAT_TYPE,
  35. 'DOUBLE PRECISION' => Zend_Db::FLOAT_TYPE,
  36. 'NUMERIC' => Zend_Db::FLOAT_TYPE,
  37. 'REAL' => Zend_Db::FLOAT_TYPE
  38. );
  39. public function testAdapterDescribeTablePrimaryAuto()
  40. {
  41. $desc = $this->_db->describeTable('zfbugs');
  42. $this->assertTrue($desc['bug_id']['PRIMARY']);
  43. $this->assertEquals(1, $desc['bug_id']['PRIMARY_POSITION']);
  44. $this->assertTrue($desc['bug_id']['IDENTITY']);
  45. }
  46. /**
  47. * Test the Adapter's insert() method.
  48. * This requires providing an associative array of column=>value pairs.
  49. */
  50. public function testAdapterInsert()
  51. {
  52. $row = array (
  53. 'bug_description' => 'New bug',
  54. 'bug_status' => 'NEW',
  55. 'created_on' => '2007-04-02',
  56. 'updated_on' => '2007-04-02',
  57. 'reported_by' => 'micky',
  58. 'assigned_to' => 'goofy'
  59. );
  60. $rowsAffected = $this->_db->insert('zfbugs', $row);
  61. $this->assertEquals(1, $rowsAffected);
  62. $lastInsertId = $this->_db->lastInsertId('zfbugs', 'bug_id');
  63. $lastSequenceId = $this->_db->lastSequenceId('zfbugs_bug_id_seq');
  64. $this->assertEquals((string) $lastInsertId, (string) $lastSequenceId,
  65. 'Expected last insert id to be equal to last sequence id');
  66. $this->assertEquals('5', (string) $lastInsertId,
  67. 'Expected new id to be 5');
  68. }
  69. public function testAdapterInsertSequence()
  70. {
  71. $row = array (
  72. 'product_id' => $this->_db->nextSequenceId('zfproducts_seq'),
  73. 'product_name' => 'Solaris',
  74. );
  75. $rowsAffected = $this->_db->insert('zfproducts', $row);
  76. $this->assertEquals(1, $rowsAffected);
  77. $lastInsertId = $this->_db->lastInsertId('zfproducts');
  78. $lastSequenceId = $this->_db->lastSequenceId('zfproducts_seq');
  79. $this->assertEquals((string) $lastInsertId, (string) $lastSequenceId,
  80. 'Expected last insert id to be equal to last sequence id');
  81. $this->assertEquals('4', (string) $lastInsertId,
  82. 'Expected new id to be 4');
  83. }
  84. public function testAdapterInsertDbExpr()
  85. {
  86. $bugs = $this->_db->quoteIdentifier('zfbugs');
  87. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  88. $bug_description = $this->_db->quoteIdentifier('bug_description', true);
  89. $expr = new Zend_Db_Expr('2+3');
  90. $row = array (
  91. 'bug_id' => $expr,
  92. 'bug_description' => 'New bug 5',
  93. 'bug_status' => 'NEW',
  94. 'created_on' => '2007-04-02',
  95. 'updated_on' => '2007-04-02',
  96. 'reported_by' => 'micky',
  97. 'assigned_to' => 'goofy',
  98. 'verified_by' => 'dduck'
  99. );
  100. $rowsAffected = $this->_db->insert('zfbugs', $row);
  101. $this->assertEquals(1, $rowsAffected);
  102. $value = $this->_db->fetchOne("SELECT $bug_description FROM $bugs WHERE $bug_id = 5");
  103. $this->assertEquals('New bug 5', $value);
  104. }
  105. /**
  106. * Test that quote() takes an array and returns
  107. * an imploded string of comma-separated, quoted elements.
  108. */
  109. public function testAdapterQuoteArray()
  110. {
  111. $array = array("it's", 'all', 'right!');
  112. $value = $this->_db->quote($array);
  113. $this->assertEquals("'it''s', 'all', 'right!'", $value);
  114. }
  115. /**
  116. * test that quote() escapes a double-quote
  117. * character in a string.
  118. */
  119. public function testAdapterQuoteDoubleQuote()
  120. {
  121. $value = $this->_db->quote('St John"s Wort');
  122. $this->assertEquals("'St John\"s Wort'", $value);
  123. }
  124. /**
  125. * test that quote() escapes a single-quote
  126. * character in a string.
  127. */
  128. public function testAdapterQuoteSingleQuote()
  129. {
  130. $string = "St John's Wort";
  131. $value = $this->_db->quote($string);
  132. $this->assertEquals("'St John''s Wort'", $value);
  133. }
  134. /**
  135. * test that quoteInto() escapes a double-quote
  136. * character in a string.
  137. */
  138. public function testAdapterQuoteIntoDoubleQuote()
  139. {
  140. $value = $this->_db->quoteInto('id=?', 'St John"s Wort');
  141. $this->assertEquals("id='St John\"s Wort'", $value);
  142. }
  143. /**
  144. * test that quoteInto() escapes a single-quote
  145. * character in a string.
  146. */
  147. public function testAdapterQuoteIntoSingleQuote()
  148. {
  149. $value = $this->_db->quoteInto('id = ?', 'St John\'s Wort');
  150. $this->assertEquals("id = 'St John''s Wort'", $value);
  151. }
  152. function getDriver()
  153. {
  154. return 'Pdo_Pgsql';
  155. }
  156. }