MssqlTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Db_Adapter_Pdo_TestCommon
  24. */
  25. require_once 'Zend/Db/Adapter/Pdo/TestCommon.php';
  26. /**
  27. * @see Zend_Db_Adapter_Pdo_Mysql
  28. */
  29. require_once 'Zend/Db/Adapter/Pdo/Mysql.php';
  30. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  31. /**
  32. * @category Zend
  33. * @package Zend_Db
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. class Zend_Db_Adapter_Pdo_MssqlTest extends Zend_Db_Adapter_Pdo_TestCommon
  39. {
  40. protected $_numericDataTypes = array(
  41. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  42. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  43. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE,
  44. 'INT' => Zend_Db::INT_TYPE,
  45. 'SMALLINT' => Zend_Db::INT_TYPE,
  46. 'TINYINT' => Zend_Db::INT_TYPE,
  47. 'BIGINT' => Zend_Db::BIGINT_TYPE,
  48. 'DECIMAL' => Zend_Db::FLOAT_TYPE,
  49. 'FLOAT' => Zend_Db::FLOAT_TYPE,
  50. 'MONEY' => Zend_Db::FLOAT_TYPE,
  51. 'NUMERIC' => Zend_Db::FLOAT_TYPE,
  52. 'REAL' => Zend_Db::FLOAT_TYPE,
  53. 'SMALLMONEY' => Zend_Db::FLOAT_TYPE
  54. );
  55. /**
  56. * Test AUTO_QUOTE_IDENTIFIERS option
  57. * Case: Zend_Db::AUTO_QUOTE_IDENTIFIERS = true
  58. */
  59. public function testAdapterAutoQuoteIdentifiersTrue()
  60. {
  61. $params = $this->_util->getParams();
  62. $params['options'] = array(
  63. Zend_Db::AUTO_QUOTE_IDENTIFIERS => true
  64. );
  65. $db = Zend_Db::factory($this->getDriver(), $params);
  66. $db->getConnection();
  67. $select = $this->_db->select();
  68. $select->from('zfproducts');
  69. $stmt = $this->_db->query($select);
  70. $result = $stmt->fetchAll();
  71. $this->assertEquals(3, count($result), 'Expected 3 rows in first query result');
  72. $this->assertEquals(1, $result[0]['product_id']);
  73. }
  74. public function testAdapterDescribeTableAttributeColumn()
  75. {
  76. $desc = $this->_db->describeTable('zfproducts');
  77. $this->assertEquals('zfproducts', $desc['product_name']['TABLE_NAME']);
  78. $this->assertEquals('product_name', $desc['product_name']['COLUMN_NAME']);
  79. $this->assertEquals(2, $desc['product_name']['COLUMN_POSITION']);
  80. $this->assertRegExp('/varchar/i', $desc['product_name']['DATA_TYPE']);
  81. $this->assertEquals('', $desc['product_name']['DEFAULT']);
  82. $this->assertFalse( $desc['product_name']['NULLABLE'], 'Expected product_name not to be nullable');
  83. $this->assertNull( $desc['product_name']['SCALE'], 'scale is not 0');
  84. // MS SQL Server reports varchar length in the PRECISION field. Whaaa?!?
  85. $this->assertEquals(100, $desc['product_name']['PRECISION'], 'precision is not 100');
  86. $this->assertFalse( $desc['product_name']['PRIMARY'], 'Expected product_name not to be a primary key');
  87. $this->assertNull( $desc['product_name']['PRIMARY_POSITION'], 'Expected product_name to return null for PRIMARY_POSITION');
  88. $this->assertFalse( $desc['product_name']['IDENTITY'], 'Expected product_name to return false for IDENTITY');
  89. }
  90. public function testAdapterDescribeTablePrimaryKeyColumn()
  91. {
  92. $desc = $this->_db->describeTable('zfproducts');
  93. $this->assertEquals('zfproducts', $desc['product_id']['TABLE_NAME']);
  94. $this->assertEquals('product_id', $desc['product_id']['COLUMN_NAME']);
  95. $this->assertEquals(1, $desc['product_id']['COLUMN_POSITION']);
  96. $this->assertEquals('', $desc['product_id']['DEFAULT']);
  97. $this->assertFalse( $desc['product_id']['NULLABLE'], 'Expected product_id not to be nullable');
  98. $this->assertEquals(0, $desc['product_id']['SCALE'], 'scale is not 0');
  99. $this->assertEquals(10, $desc['product_id']['PRECISION'], 'precision is not 10');
  100. $this->assertTrue( $desc['product_id']['PRIMARY'], 'Expected product_id to be a primary key');
  101. $this->assertEquals(1, $desc['product_id']['PRIMARY_POSITION']);
  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. $string = 'St John"s Wort';
  120. $value = $this->_db->quote($string);
  121. $this->assertEquals("'St John\"s Wort'", $value);
  122. }
  123. /**
  124. * test that quote() escapes a single-quote
  125. * character in a string.
  126. */
  127. public function testAdapterQuoteSingleQuote()
  128. {
  129. $string = "St John's Wort";
  130. $value = $this->_db->quote($string);
  131. $this->assertEquals("'St John''s Wort'", $value);
  132. }
  133. /**
  134. * test that quoteInto() escapes a double-quote
  135. * character in a string.
  136. */
  137. public function testAdapterQuoteIntoDoubleQuote()
  138. {
  139. $string = 'id=?';
  140. $param = 'St John"s Wort';
  141. $value = $this->_db->quoteInto($string, $param);
  142. $this->assertEquals("id='St John\"s Wort'", $value);
  143. }
  144. /**
  145. * test that quoteInto() escapes a single-quote
  146. * character in a string.
  147. */
  148. public function testAdapterQuoteIntoSingleQuote()
  149. {
  150. $string = 'id = ?';
  151. $param = 'St John\'s Wort';
  152. $value = $this->_db->quoteInto($string, $param);
  153. $this->assertEquals("id = 'St John''s Wort'", $value);
  154. }
  155. public function testAdapterInsertSequence()
  156. {
  157. $this->markTestSkipped($this->getDriver() . ' does not support sequences.');
  158. }
  159. public function testAdapterInsertDbExpr()
  160. {
  161. $bugs = $this->_db->quoteIdentifier('zfbugs');
  162. $bug_id = $this->_db->quoteIdentifier('bug_id');
  163. $expr = new Zend_Db_Expr('2+3');
  164. $row = array (
  165. 'bug_id' => $expr,
  166. 'bug_description' => 'New bug',
  167. 'bug_status' => 'NEW',
  168. 'created_on' => '2007-04-02',
  169. 'updated_on' => '2007-04-02',
  170. 'reported_by' => 'micky',
  171. 'assigned_to' => 'goofy',
  172. 'verified_by' => 'dduck'
  173. );
  174. $this->_db->query("SET IDENTITY_INSERT $bugs ON");
  175. $rowsAffected = $this->_db->insert('zfbugs', $row);
  176. $this->assertEquals(1, $rowsAffected);
  177. $this->_db->query("SET IDENTITY_INSERT $bugs OFF");
  178. $value = $this->_db->fetchOne("SELECT $bug_id FROM $bugs WHERE $bug_id = 5");
  179. $this->assertEquals(5, $value);
  180. }
  181. public function testAdapterTransactionCommit()
  182. {
  183. $bugs = $this->_db->quoteIdentifier('zfbugs');
  184. $bug_id = $this->_db->quoteIdentifier('bug_id');
  185. // notice the number of rows in connection 2
  186. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  187. $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
  188. // start an explicit transaction in connection 1
  189. $this->_db->beginTransaction();
  190. // delete a row in connection 1
  191. $rowsAffected = $this->_db->delete(
  192. 'zfbugs',
  193. "$bug_id = 1"
  194. );
  195. $this->assertEquals(1, $rowsAffected);
  196. // we should still see all rows in connection 2
  197. // because the DELETE has not been committed yet
  198. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  199. $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table (step 2); perhaps Adapter is still in autocommit mode?');
  200. // commit the DELETE
  201. $this->_db->commit();
  202. // now we should see one fewer rows in connection 2
  203. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  204. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 3)');
  205. // delete another row in connection 1
  206. $rowsAffected = $this->_db->delete(
  207. 'zfbugs',
  208. "$bug_id = 2"
  209. );
  210. $this->assertEquals(1, $rowsAffected);
  211. // we should see results immediately, because
  212. // the db connection returns to auto-commit mode
  213. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  214. $this->assertEquals(2, $count);
  215. }
  216. public function testAdapterTransactionRollback()
  217. {
  218. $bugs = $this->_db->quoteIdentifier('zfbugs');
  219. $bug_id = $this->_db->quoteIdentifier('bug_id');
  220. // notice the number of rows in connection 2
  221. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  222. $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
  223. // start an explicit transaction in connection 1
  224. $this->_db->beginTransaction();
  225. // delete a row in connection 1
  226. $rowsAffected = $this->_db->delete(
  227. 'zfbugs',
  228. "$bug_id = 1"
  229. );
  230. $this->assertEquals(1, $rowsAffected);
  231. // we should still see all rows in connection 2
  232. // because the DELETE has not been committed yet
  233. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  234. $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table (step 2); perhaps Adapter is still in autocommit mode?');
  235. // rollback the DELETE
  236. $this->_db->rollback();
  237. // now we should see the same number of rows
  238. // because the DELETE was rolled back
  239. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  240. $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table after DELETE is rolled back (step 3)');
  241. // delete another row in connection 1
  242. $rowsAffected = $this->_db->delete(
  243. 'zfbugs',
  244. "$bug_id = 2"
  245. );
  246. $this->assertEquals(1, $rowsAffected);
  247. // we should see results immediately, because
  248. // the db connection returns to auto-commit mode
  249. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  250. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 4)');
  251. }
  252. /**
  253. * Test the Adapter's insert() method.
  254. * This requires providing an associative array of column=>value pairs.
  255. */
  256. public function testAdapterInsert()
  257. {
  258. $row = array (
  259. 'bug_description' => 'New bug',
  260. 'bug_status' => 'NEW',
  261. 'created_on' => '2007-04-02',
  262. 'updated_on' => '2007-04-02',
  263. 'reported_by' => 'micky',
  264. 'assigned_to' => 'goofy',
  265. 'verified_by' => 'dduck'
  266. );
  267. $rowsAffected = $this->_db->insert('zfbugs', $row);
  268. $this->assertEquals(1, $rowsAffected);
  269. $lastInsertId = $this->_db->lastInsertId();
  270. $this->assertType('integer', $lastInsertId);
  271. $this->assertEquals('5', (string) $lastInsertId,
  272. 'Expected new id to be 5');
  273. }
  274. public function getDriver()
  275. {
  276. return 'Pdo_Mssql';
  277. }
  278. }