PageRenderTime 2072ms CodeModel.GetById 30ms RepoModel.GetById 6ms app.codeStats 0ms

/ZendFramework/tests/Zend/Db/Adapter/Pdo/PgsqlTest.php

https://bitbucket.org/Dal-Papa/is-340-publish-base
PHP | 240 lines | 185 code | 18 blank | 37 comment | 0 complexity | 0ed03507ca9ebaf1685328c584aa66b9 MD5 | raw file
  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-2012 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. require_once 'Zend/Db/Adapter/Pdo/TestCommon.php';
  23. /**
  24. * @category Zend
  25. * @package Zend_Db
  26. * @subpackage UnitTests
  27. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. * @group Zend_Db
  30. * @group Zend_Db_Adapter
  31. */
  32. class Zend_Db_Adapter_Pdo_PgsqlTest extends Zend_Db_Adapter_Pdo_TestCommon
  33. {
  34. protected $_numericDataTypes = array(
  35. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  36. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  37. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  38. 'INTEGER' => Zend_Db::INT_TYPE,
  39. 'SERIAL' => Zend_Db::INT_TYPE,
  40. 'SMALLINT' => Zend_Db::INT_TYPE,
  41. 'BIGINT' => Zend_Db::BIGINT_TYPE,
  42. 'BIGSERIAL' => Zend_Db::BIGINT_TYPE,
  43. 'DECIMAL' => Zend_Db::FLOAT_TYPE,
  44. 'DOUBLE PRECISION' => Zend_Db::FLOAT_TYPE,
  45. 'NUMERIC' => Zend_Db::FLOAT_TYPE,
  46. 'REAL' => Zend_Db::FLOAT_TYPE
  47. );
  48. public function testAdapterDescribeTablePrimaryAuto()
  49. {
  50. $desc = $this->_db->describeTable('zfbugs');
  51. $this->assertTrue($desc['bug_id']['PRIMARY']);
  52. $this->assertEquals(1, $desc['bug_id']['PRIMARY_POSITION']);
  53. $this->assertTrue($desc['bug_id']['IDENTITY']);
  54. }
  55. /**
  56. * Test the Adapter's insert() method.
  57. * This requires providing an associative array of column=>value pairs.
  58. */
  59. public function testAdapterInsert()
  60. {
  61. $row = array (
  62. 'bug_description' => 'New bug',
  63. 'bug_status' => 'NEW',
  64. 'created_on' => '2007-04-02',
  65. 'updated_on' => '2007-04-02',
  66. 'reported_by' => 'micky',
  67. 'assigned_to' => 'goofy'
  68. );
  69. $rowsAffected = $this->_db->insert('zfbugs', $row);
  70. $this->assertEquals(1, $rowsAffected);
  71. $lastInsertId = $this->_db->lastInsertId('zfbugs', 'bug_id');
  72. $lastSequenceId = $this->_db->lastSequenceId('zfbugs_bug_id_seq');
  73. $this->assertEquals((string) $lastInsertId, (string) $lastSequenceId,
  74. 'Expected last insert id to be equal to last sequence id');
  75. $this->assertEquals('5', (string) $lastInsertId,
  76. 'Expected new id to be 5');
  77. }
  78. public function testAdapterInsertSequence()
  79. {
  80. $row = array (
  81. 'product_id' => $this->_db->nextSequenceId('zfproducts_seq'),
  82. 'product_name' => 'Solaris',
  83. );
  84. $rowsAffected = $this->_db->insert('zfproducts', $row);
  85. $this->assertEquals(1, $rowsAffected);
  86. $lastInsertId = $this->_db->lastInsertId('zfproducts');
  87. $lastSequenceId = $this->_db->lastSequenceId('zfproducts_seq');
  88. $this->assertEquals((string) $lastInsertId, (string) $lastSequenceId,
  89. 'Expected last insert id to be equal to last sequence id');
  90. $this->assertEquals('4', (string) $lastInsertId,
  91. 'Expected new id to be 4');
  92. }
  93. public function testAdapterInsertDbExpr()
  94. {
  95. $bugs = $this->_db->quoteIdentifier('zfbugs');
  96. $bug_id = $this->_db->quoteIdentifier('bug_id', true);
  97. $bug_description = $this->_db->quoteIdentifier('bug_description', true);
  98. $expr = new Zend_Db_Expr('2+3');
  99. $row = array (
  100. 'bug_id' => $expr,
  101. 'bug_description' => 'New bug 5',
  102. 'bug_status' => 'NEW',
  103. 'created_on' => '2007-04-02',
  104. 'updated_on' => '2007-04-02',
  105. 'reported_by' => 'micky',
  106. 'assigned_to' => 'goofy',
  107. 'verified_by' => 'dduck'
  108. );
  109. $rowsAffected = $this->_db->insert('zfbugs', $row);
  110. $this->assertEquals(1, $rowsAffected);
  111. $value = $this->_db->fetchOne("SELECT $bug_description FROM $bugs WHERE $bug_id = 5");
  112. $this->assertEquals('New bug 5', $value);
  113. }
  114. /**
  115. * Test that quote() takes an array and returns
  116. * an imploded string of comma-separated, quoted elements.
  117. */
  118. public function testAdapterQuoteArray()
  119. {
  120. $array = array("it's", 'all', 'right!');
  121. $value = $this->_db->quote($array);
  122. $this->assertEquals("'it''s', 'all', 'right!'", $value);
  123. }
  124. /**
  125. * test that quote() escapes a double-quote
  126. * character in a string.
  127. */
  128. public function testAdapterQuoteDoubleQuote()
  129. {
  130. $value = $this->_db->quote('St John"s Wort');
  131. $this->assertEquals("'St John\"s Wort'", $value);
  132. }
  133. /**
  134. * test that quote() escapes a single-quote
  135. * character in a string.
  136. */
  137. public function testAdapterQuoteSingleQuote()
  138. {
  139. $string = "St John's Wort";
  140. $value = $this->_db->quote($string);
  141. $this->assertEquals("'St John''s Wort'", $value);
  142. }
  143. /**
  144. * test that quoteInto() escapes a double-quote
  145. * character in a string.
  146. */
  147. public function testAdapterQuoteIntoDoubleQuote()
  148. {
  149. $value = $this->_db->quoteInto('id=?', 'St John"s Wort');
  150. $this->assertEquals("id='St John\"s Wort'", $value);
  151. }
  152. /**
  153. * test that quoteInto() escapes a single-quote
  154. * character in a string.
  155. */
  156. public function testAdapterQuoteIntoSingleQuote()
  157. {
  158. $value = $this->_db->quoteInto('id = ?', 'St John\'s Wort');
  159. $this->assertEquals("id = 'St John''s Wort'", $value);
  160. }
  161. function getDriver()
  162. {
  163. return 'Pdo_Pgsql';
  164. }
  165. /**
  166. * @group ZF-3972
  167. */
  168. public function testAdapterCharacterVarying()
  169. {
  170. $this->_util->createTable('zf_pgsql_charvary',
  171. array('pg_id' => 'character varying(4) NOT NULL',
  172. 'pg_info' => "character varying(1) NOT NULL DEFAULT 'A'::character varying"));
  173. $description = $this->_db->describeTable('zf_pgsql_charvary');
  174. $this->_util->dropTable('zf_pgsql_charvary');
  175. $this->assertEquals(null , $description['pg_id']['DEFAULT']);
  176. $this->assertEquals('A', $description['pg_info']['DEFAULT']);
  177. }
  178. /**
  179. * @group ZF-7640
  180. */
  181. public function testAdapterBpchar()
  182. {
  183. $this->_util->createTable('zf_pgsql_bpchar',
  184. array('pg_name' => "character(100) DEFAULT 'Default'::bpchar"));
  185. $description = $this->_db->describeTable('zf_pgsql_bpchar');
  186. $this->_util->dropTable('zf_pgsql_bpchar');
  187. $this->assertEquals('Default', $description['pg_name']['DEFAULT']);
  188. }
  189. /**
  190. * @group ZF-10160
  191. * @group ZF-10257
  192. */
  193. public function testQuoteIdentifiersInSequence()
  194. {
  195. $this->_util->createSequence('camelCase_id_seq');
  196. $this->_util->createSequence("single'quotes");
  197. $this->_db->nextSequenceId('camelCase_id_seq');
  198. $this->_db->nextSequenceId($this->_db->quoteIdentifier('camelCase_id_seq', true));
  199. $this->_db->lastSequenceId('camelCase_id_seq');
  200. $this->_db->lastSequenceId($this->_db->quoteIdentifier('camelCase_id_seq', true));
  201. require_once 'Zend/Db/Expr.php';
  202. $this->_db->lastSequenceId(new Zend_Db_Expr('camelCase_id_seq'));
  203. $lastId = $this->_db->lastSequenceId(new Zend_Db_Expr('camelCase_id_seq'));
  204. $this->assertEquals(2, $lastId);
  205. $this->_db->nextSequenceId('"public"."camelCase_id_seq"');
  206. $lastId = $this->_db->lastSequenceId('"public"."camelCase_id_seq"');
  207. $this->assertEquals(3, $lastId);
  208. $this->_db->nextSequenceId("single'quotes");
  209. $lastId = $this->_db->lastSequenceId("single'quotes");
  210. $this->assertEquals(1, $lastId);
  211. $this->_util->dropSequence("single'quotes");
  212. $this->_util->dropSequence('camelCase_id_seq');
  213. }
  214. }