PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://firephp.googlecode.com/
PHP | 357 lines | 249 code | 40 blank | 68 comment | 6 complexity | 801a52925927f066d44d035c47907de6 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. */
  21. /**
  22. * @see Zend_Db_Adapter_TestCommon
  23. */
  24. require_once 'Zend/Db/Adapter/TestCommon.php';
  25. /**
  26. * @see Zend_Db_Adapter_Db2
  27. */
  28. require_once 'Zend/Db/Adapter/Db2.php';
  29. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  30. class Zend_Db_Adapter_Db2Test 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. 'INTEGER' => Zend_Db::INT_TYPE,
  37. 'SMALLINT' => Zend_Db::INT_TYPE,
  38. 'BIGINT' => Zend_Db::BIGINT_TYPE,
  39. 'DECIMAL' => Zend_Db::FLOAT_TYPE,
  40. 'NUMERIC' => Zend_Db::FLOAT_TYPE
  41. );
  42. public function testAdapterDescribeTablePrimaryAuto()
  43. {
  44. $desc = $this->_db->describeTable('zfbugs');
  45. $this->assertTrue($desc['bug_id']['PRIMARY']);
  46. $this->assertEquals(1, $desc['bug_id']['PRIMARY_POSITION']);
  47. $this->assertTrue($desc['bug_id']['IDENTITY']);
  48. }
  49. public function testAdapterDescribeTableAttributeColumn()
  50. {
  51. $desc = $this->_db->describeTable('zfproducts');
  52. $this->assertEquals('zfproducts', $desc['product_name']['TABLE_NAME'], 'Expected table name to be zfproducts');
  53. $this->assertEquals('product_name', $desc['product_name']['COLUMN_NAME'], 'Expected column name to be product_name');
  54. $this->assertEquals(2, $desc['product_name']['COLUMN_POSITION'], 'Expected column position to be 2');
  55. $this->assertRegExp('/varchar/i', $desc['product_name']['DATA_TYPE'], 'Expected data type to be VARCHAR');
  56. $this->assertEquals('', $desc['product_name']['DEFAULT'], 'Expected default to be empty string');
  57. $this->assertTrue( $desc['product_name']['NULLABLE'], 'Expected product_name to be nullable');
  58. if (!$this->_db->isI5()) {
  59. $this->assertEquals(0, $desc['product_name']['SCALE'], 'Expected scale to be 0');
  60. } else {
  61. $this->assertNull( $desc['product_name']['SCALE'], 'Expected scale to be 0');
  62. }
  63. $this->assertEquals(0, $desc['product_name']['PRECISION'], 'Expected precision to be 0');
  64. $this->assertFalse( $desc['product_name']['PRIMARY'], 'Expected product_name not to be a primary key');
  65. $this->assertNull( $desc['product_name']['PRIMARY_POSITION'], 'Expected product_name to return null for PRIMARY_POSITION');
  66. $this->assertFalse( $desc['product_name']['IDENTITY'], 'Expected product_name to return false for IDENTITY');
  67. }
  68. public function testAdapterDescribeTablePrimaryKeyColumn()
  69. {
  70. $desc = $this->_db->describeTable('zfproducts');
  71. $this->assertEquals('zfproducts', $desc['product_id']['TABLE_NAME'], 'Expected table name to be zfproducts');
  72. $this->assertEquals('product_id', $desc['product_id']['COLUMN_NAME'], 'Expected column name to be product_id');
  73. $this->assertEquals(1, $desc['product_id']['COLUMN_POSITION'], 'Expected column position to be 1');
  74. $this->assertEquals('', $desc['product_id']['DEFAULT'], 'Expected default to be empty string');
  75. $this->assertFalse( $desc['product_id']['NULLABLE'], 'Expected product_id not to be nullable');
  76. $this->assertEquals(0, $desc['product_id']['SCALE'], 'Expected scale to be 0');
  77. $this->assertEquals(0, $desc['product_id']['PRECISION'], 'Expected precision to be 0');
  78. $this->assertTrue( $desc['product_id']['PRIMARY'], 'Expected product_id to be a primary key');
  79. $this->assertEquals(1, $desc['product_id']['PRIMARY_POSITION']);
  80. }
  81. /**
  82. * Used by _testAdapterOptionCaseFoldingNatural()
  83. * DB2 and Oracle return identifiers in uppercase naturally,
  84. * so those test suites will override this method.
  85. */
  86. protected function _testAdapterOptionCaseFoldingNaturalIdentifier()
  87. {
  88. return 'CASE_FOLDED_IDENTIFIER';
  89. }
  90. public function testAdapterTransactionCommit()
  91. {
  92. $bugs = $this->_db->quoteIdentifier('zfbugs');
  93. $bug_id = $this->_db->quoteIdentifier('bug_id');
  94. // use our default connection as the Connection1
  95. $dbConnection1 = $this->_db;
  96. // create a second connection to the same database
  97. $dbConnection2 = Zend_Db::factory($this->getDriver(), $this->_util->getParams());
  98. $dbConnection2->getConnection();
  99. if ($dbConnection2->isI5()) {
  100. $dbConnection2->query('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE');
  101. } else {
  102. $dbConnection2->query('SET ISOLATION LEVEL = UR');
  103. }
  104. // notice the number of rows in connection 2
  105. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  106. $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
  107. // start an explicit transaction in connection 1
  108. $dbConnection1->beginTransaction();
  109. // delete a row in connection 1
  110. $rowsAffected = $dbConnection1->delete(
  111. 'zfbugs',
  112. "$bug_id = 1"
  113. );
  114. $this->assertEquals(1, $rowsAffected);
  115. // we should see one less row in connection 2
  116. // because it is doing an uncommitted read
  117. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  118. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table (step 2) because conn2 is doing an uncommitted read');
  119. // commit the DELETE
  120. $dbConnection1->commit();
  121. // now we should see one fewer rows in connection 2
  122. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  123. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 3)');
  124. // delete another row in connection 1
  125. $rowsAffected = $dbConnection1->delete(
  126. 'zfbugs',
  127. "$bug_id = 2"
  128. );
  129. $this->assertEquals(1, $rowsAffected);
  130. // we should see results immediately, because
  131. // the db connection returns to auto-commit mode
  132. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  133. $this->assertEquals(2, $count);
  134. }
  135. public function testAdapterTransactionRollback()
  136. {
  137. $bugs = $this->_db->quoteIdentifier('zfbugs');
  138. $bug_id = $this->_db->quoteIdentifier('bug_id');
  139. // use our default connection as the Connection1
  140. $dbConnection1 = $this->_db;
  141. // create a second connection to the same database
  142. $dbConnection2 = Zend_Db::factory($this->getDriver(), $this->_util->getParams());
  143. $dbConnection2->getConnection();
  144. if ($dbConnection2->isI5()) {
  145. $dbConnection2->query('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE');
  146. } else {
  147. $dbConnection2->query('SET ISOLATION LEVEL = UR');
  148. }
  149. // notice the number of rows in connection 2
  150. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  151. $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
  152. // start an explicit transaction in connection 1
  153. $dbConnection1->beginTransaction();
  154. // delete a row in connection 1
  155. $rowsAffected = $dbConnection1->delete(
  156. 'zfbugs',
  157. "$bug_id = 1"
  158. );
  159. $this->assertEquals(1, $rowsAffected);
  160. // we should see one less row in connection 2
  161. // because it is doing an uncommitted read
  162. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  163. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table (step 2) because conn2 is doing an uncommitted read');
  164. // rollback the DELETE
  165. $dbConnection1->rollback();
  166. // now we should see the same number of rows
  167. // because the DELETE was rolled back
  168. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  169. $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table after DELETE is rolled back (step 3)');
  170. // delete another row in connection 1
  171. $rowsAffected = $dbConnection1->delete(
  172. 'zfbugs',
  173. "$bug_id = 2"
  174. );
  175. $this->assertEquals(1, $rowsAffected);
  176. // we should see results immediately, because
  177. // the db connection returns to auto-commit mode
  178. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  179. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 4)');
  180. }
  181. public function testAdapterAlternateStatement()
  182. {
  183. $this->_testAdapterAlternateStatement('Test_Db2Statement');
  184. }
  185. /**
  186. * OVERRIDDEN COMMON TEST CASE
  187. *
  188. * This test case will produce a value with two internally set values,
  189. * autocommit = 1
  190. * DB2_ATTR_CASE = 0
  191. */
  192. public function testAdapterZendConfigEmptyDriverOptions()
  193. {
  194. Zend_Loader::loadClass('Zend_Config');
  195. $params = $this->_util->getParams();
  196. $params['driver_options'] = '';
  197. $params = new Zend_Config($params);
  198. $db = Zend_Db::factory($this->getDriver(), $params);
  199. $db->getConnection();
  200. $config = $db->getConfig();
  201. $expectedValue = array(
  202. 'autocommit' => 1,
  203. 'DB2_ATTR_CASE' => 0
  204. );
  205. $this->assertEquals($expectedValue, $config['driver_options']);
  206. }
  207. /**
  208. * OVERRIDDEN COMMON TEST CASE
  209. *
  210. * Test that quote() takes an array and returns
  211. * an imploded string of comma-separated, quoted elements.
  212. */
  213. public function testAdapterQuoteArray()
  214. {
  215. $array = array("it's", 'all', 'right!');
  216. $value = $this->_db->quote($array);
  217. $this->assertEquals("'it''s', 'all', 'right!'", $value);
  218. }
  219. /**
  220. * OVERRRIDEEN COMMON TEST CASE
  221. *
  222. * test that quote() escapes a double-quote
  223. * character in a string.
  224. */
  225. public function testAdapterQuoteDoubleQuote()
  226. {
  227. $string = 'St John"s Wort';
  228. $value = $this->_db->quote($string);
  229. $this->assertEquals("'St John\"s Wort'", $value);
  230. }
  231. /**
  232. * OVERRIDDEN FROM COMMON TEST CASE
  233. *
  234. * test that quote() escapes a single-quote
  235. * character in a string.
  236. */
  237. public function testAdapterQuoteSingleQuote()
  238. {
  239. $string = "St John's Wort";
  240. $value = $this->_db->quote($string);
  241. $this->assertEquals("'St John''s Wort'", $value);
  242. }
  243. /**
  244. * OVERRIDDEN FROM COMMON TEST CASE
  245. *
  246. * test that quoteInto() escapes a double-quote
  247. * character in a string.
  248. */
  249. public function testAdapterQuoteIntoDoubleQuote()
  250. {
  251. $string = 'id=?';
  252. $param = 'St John"s Wort';
  253. $value = $this->_db->quoteInto($string, $param);
  254. $this->assertEquals("id='St John\"s Wort'", $value);
  255. }
  256. /**
  257. * OVERRIDDEN FROM COMMON TEST CASE
  258. *
  259. * test that quoteInto() escapes a single-quote
  260. * character in a string.
  261. */
  262. public function testAdapterQuoteIntoSingleQuote()
  263. {
  264. $string = 'id = ?';
  265. $param = 'St John\'s Wort';
  266. $value = $this->_db->quoteInto($string, $param);
  267. $this->assertEquals("id = 'St John''s Wort'", $value);
  268. }
  269. /**
  270. * This is "related" to the issue. It appears the fix for
  271. * describeTable is relatively untestable due to the fact that
  272. * its primary focus is to reduce the query time, not the result
  273. * set.
  274. *
  275. * @group ZF-5169
  276. */
  277. public function testAdapterSchemaOptionInListTables()
  278. {
  279. $params = $this->_util->getParams();
  280. unset($params['schema']);
  281. $connection = Zend_Db::factory($this->getDriver(), $params);
  282. $tableCountNoSchema = count($connection->listTables());
  283. $dbConfig = $this->_db->getConfig();
  284. if ($this->_db->isI5()) {
  285. if (isset($dbConfig['driver_options']['i5_lib'])) {
  286. $schema = $dbConfig['driver_options']['i5_lib'];
  287. }
  288. } elseif (!$this->_db->isI5()) {
  289. $schema = $this->_util->getSchema();
  290. } else {
  291. $this->markTestSkipped('No valid schema to test against.');
  292. return;
  293. }
  294. $params = $this->_util->getParams();
  295. $params['schema'] = $schema;
  296. $connection = Zend_Db::factory($this->getDriver(), $params);
  297. $tableCountSchema = count($connection->listTables());
  298. $this->assertGreaterThan(0, $tableCountNoSchema, 'Adapter without schema should produce large result');
  299. $this->assertGreaterThan(0, $tableCountSchema, 'Adapter with schema should produce large result');
  300. $this->assertTrue(($tableCountNoSchema > $tableCountSchema), 'Table count with schema provided should be less than without.');
  301. }
  302. public function getDriver()
  303. {
  304. return 'Db2';
  305. }
  306. }