TestCommon.php 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057
  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-2009 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_TestSetup
  24. */
  25. require_once 'Zend/Db/TestSetup.php';
  26. /**
  27. * @see Zend_Loader
  28. */
  29. require_once 'Zend/Loader.php';
  30. PHPUnit_Util_Filter::addFileToFilter(__FILE__);
  31. /**
  32. * @category Zend
  33. * @package Zend_Db
  34. * @subpackage UnitTests
  35. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  36. * @license http://framework.zend.com/license/new-bsd New BSD License
  37. */
  38. abstract class Zend_Db_Adapter_TestCommon extends Zend_Db_TestSetup
  39. {
  40. /**
  41. * Test AUTO_QUOTE_IDENTIFIERS option
  42. * Case: Zend_Db::AUTO_QUOTE_IDENTIFIERS = true
  43. */
  44. public function testAdapterZendConfig()
  45. {
  46. Zend_Loader::loadClass('Zend_Config');
  47. $params = new Zend_Config($this->_util->getParams());
  48. $db = Zend_Db::factory($this->getDriver(), $params);
  49. $db->getConnection();
  50. }
  51. /**
  52. * Test empty adapterNamespace issue
  53. * Case: Zend_Db::AUTO_QUOTE_IDENTIFIERS = true
  54. */
  55. public function testAdapterZendConfigEmptyNamespace()
  56. {
  57. Zend_Loader::loadClass('Zend_Config');
  58. $params = $this->_util->getParams();
  59. $params['adapterNamespace'] = '';
  60. $params = new Zend_Config($params);
  61. $db = Zend_Db::factory($this->getDriver(), $params);
  62. $db->getConnection();
  63. }
  64. /**
  65. * Test empty driver_options issue
  66. * Case: $config['driver_options'] == ''
  67. */
  68. public function testAdapterZendConfigEmptyDriverOptions()
  69. {
  70. Zend_Loader::loadClass('Zend_Config');
  71. $params = $this->_util->getParams();
  72. $params['driver_options'] = '';
  73. $params = new Zend_Config($params);
  74. $db = Zend_Db::factory($this->getDriver(), $params);
  75. $db->getConnection();
  76. $config = $db->getConfig();
  77. $this->assertEquals(array(), $config['driver_options']);
  78. }
  79. /**
  80. * Test AUTO_QUOTE_IDENTIFIERS option
  81. * Case: Zend_Db::AUTO_QUOTE_IDENTIFIERS = true
  82. */
  83. public function testAdapterAutoQuoteIdentifiersTrue()
  84. {
  85. $params = $this->_util->getParams();
  86. $params['options'] = array(
  87. Zend_Db::AUTO_QUOTE_IDENTIFIERS => true
  88. );
  89. $db = Zend_Db::factory($this->getDriver(), $params);
  90. $db->getConnection();
  91. $select = $this->_db->select();
  92. $select->from('zfproducts');
  93. $stmt = $this->_db->query($select);
  94. $result = $stmt->fetchAll();
  95. $this->assertEquals(3, count($result), 'Expected 3 rows in first query result');
  96. $this->assertEquals(1, $result[0]['product_id']);
  97. $select = $this->_db->select();
  98. $select->from('ZFPRODUCTS');
  99. try {
  100. $stmt = $this->_db->query($select);
  101. $result = $stmt->fetchAll();
  102. $this->fail('Expected exception not thrown');
  103. } catch (Zend_Exception $e) {
  104. $this->assertType('Zend_Db_Statement_Exception', $e,
  105. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  106. }
  107. }
  108. /**
  109. * Test AUTO_QUOTE_IDENTIFIERS option
  110. * Case: Zend_Db::AUTO_QUOTE_IDENTIFIERS = false
  111. */
  112. public function testAdapterAutoQuoteIdentifiersFalse()
  113. {
  114. $params = $this->_util->getParams();
  115. $params['options'] = array(
  116. Zend_Db::AUTO_QUOTE_IDENTIFIERS => false
  117. );
  118. $db = Zend_Db::factory($this->getDriver(), $params);
  119. $db->getConnection();
  120. // create a new util object, with the new db adapter
  121. $driver = $this->getDriver();
  122. $utilClass = "Zend_Db_TestUtil_{$driver}";
  123. $util = new $utilClass();
  124. $util->setAdapter($db);
  125. // create test table using no identifier quoting
  126. $util->createTable('noquote', array(
  127. 'id' => 'INT NOT NULL PRIMARY KEY',
  128. 'stuff' => 'CHAR(10)'
  129. ));
  130. $tableName = $this->_util->getTableName('noquote');
  131. // insert into the table
  132. $numRows = $db->insert($tableName, array(
  133. 'id' => 1,
  134. 'stuff' => 'no quote 1'
  135. ));
  136. $this->assertEquals(1, $numRows,
  137. 'number of rows in first insert not as expected');
  138. // check if the row was inserted as expected
  139. $sql = "SELECT id, stuff FROM $tableName ORDER BY id";
  140. $stmt = $db->query($sql);
  141. $fetched = $stmt->fetchAll(Zend_Db::FETCH_NUM);
  142. $a = array(
  143. 0 => array(0 => 1, 1 => 'no quote 1')
  144. );
  145. $this->assertEquals($a, $fetched,
  146. 'result of first query not as expected');
  147. // insert into the table using other case
  148. $numRows = $db->insert($tableName, array(
  149. 'ID' => 2,
  150. 'STUFF' => 'no quote 2'
  151. ));
  152. $this->assertEquals(1, $numRows,
  153. 'number of rows in second insert not as expected');
  154. // check if the row was inserted as expected
  155. $sql = 'SELECT ID, STUFF FROM ' . $tableName . ' ORDER BY ID';
  156. $stmt = $db->query($sql);
  157. $fetched = $stmt->fetchAll(Zend_Db::FETCH_NUM);
  158. $a = array(
  159. 0 => array(0 => 1, 1 => 'no quote 1'),
  160. 1 => array(0 => 2, 1 => 'no quote 2'),
  161. );
  162. $this->assertEquals($a, $fetched,
  163. 'result of second query not as expected');
  164. // clean up
  165. unset($stmt);
  166. $util->dropTable($tableName);
  167. }
  168. protected function _testAdapterConstructInvalidParam($param, $adapterClass = null)
  169. {
  170. $exceptionClass = 'Zend_Db_Adapter_Exception';
  171. if ($adapterClass === null) {
  172. $adapterClass = 'Zend_Db_Adapter_' . $this->getDriver();
  173. }
  174. $params = $this->_util->getParams();
  175. unset($params[$param]);
  176. Zend_Loader::loadClass($adapterClass);
  177. Zend_Loader::loadClass($exceptionClass);
  178. try {
  179. $db = new $adapterClass($params);
  180. $db->getConnection(); // force a connection
  181. $this->fail("Expected to catch $exceptionClass");
  182. } catch (Zend_Exception $e) {
  183. $this->assertType($exceptionClass, $e, "Expected to catch $exceptionClass, got ".get_class($e));
  184. $this->assertContains("Configuration array must have a key for '$param'", $e->getMessage());
  185. }
  186. }
  187. public function testAdapterConstructInvalidParamDbnameException()
  188. {
  189. $this->_testAdapterConstructInvalidParam('dbname');
  190. }
  191. public function testAdapterConstructInvalidParamUsernameException()
  192. {
  193. $this->_testAdapterConstructInvalidParam('username');
  194. }
  195. public function testAdapterConstructInvalidParamPasswordException()
  196. {
  197. $this->_testAdapterConstructInvalidParam('password');
  198. }
  199. /**
  200. * Test Adapter's delete() method.
  201. * Delete one row from test table, and verify it was deleted.
  202. * Then try to delete a row that doesn't exist, and verify it had no effect.
  203. */
  204. public function testAdapterDelete()
  205. {
  206. $product_id = $this->_db->quoteIdentifier('product_id');
  207. $select = $this->_db->select()->from('zfproducts')->order('product_id ASC');
  208. $result = $this->_db->fetchAll($select);
  209. $this->assertEquals(3, count($result), 'Expected count of result to be 2');
  210. $this->assertEquals(1, $result[0]['product_id'], 'Expecting product_id of 0th row to be 1');
  211. $rowsAffected = $this->_db->delete('zfproducts', "$product_id = 2");
  212. $this->assertEquals(1, $rowsAffected, 'Expecting rows affected to be 1');
  213. $select = $this->_db->select()->from('zfproducts')->order('product_id ASC');
  214. $result = $this->_db->fetchAll($select);
  215. $this->assertEquals(2, count($result), 'Expected count of result to be 2');
  216. $this->assertEquals(1, $result[0]['product_id'], 'Expecting product_id of 0th row to be 1');
  217. $rowsAffected = $this->_db->delete('zfproducts', "$product_id = 327");
  218. $this->assertEquals(0, $rowsAffected, 'Expected rows affected to return 0');
  219. }
  220. public function testAdapterDeleteWhereArray()
  221. {
  222. $products = $this->_db->quoteIdentifier('zfproducts');
  223. $product_id = $this->_db->quoteIdentifier('product_id');
  224. $product_name = $this->_db->quoteIdentifier('product_name');
  225. $ids = $this->_db->fetchCol("SELECT $product_id FROM $products ORDER BY $product_id");
  226. $this->assertEquals(array(1, 2, 3), $ids);
  227. $rowsAffected = $this->_db->delete(
  228. 'zfproducts',
  229. array("$product_id = 1", "$product_name = 'Windows'")
  230. );
  231. $this->assertEquals(1, $rowsAffected);
  232. $ids = $this->_db->fetchCol("SELECT $product_id FROM $products ORDER BY $product_id");
  233. $this->assertEquals(array(2, 3), $ids);
  234. }
  235. /**
  236. * @group ZF-1726
  237. */
  238. public function testAdapterDeleteWhereArrayWithVariable()
  239. {
  240. $products = $this->_db->quoteIdentifier('zfproducts');
  241. $product_id = $this->_db->quoteIdentifier('product_id');
  242. $product_name = $this->_db->quoteIdentifier('product_name');
  243. $ids = $this->_db->fetchCol("SELECT $product_id FROM $products ORDER BY $product_id");
  244. $this->assertEquals(array(1, 2, 3), $ids);
  245. $rowsAffected = $this->_db->delete(
  246. 'zfproducts',
  247. array("$product_id = ?" => 1, "$product_name = ?" => 'Windows')
  248. );
  249. $this->assertEquals(1, $rowsAffected);
  250. $ids = $this->_db->fetchCol("SELECT $product_id FROM $products ORDER BY $product_id");
  251. $this->assertEquals(array(2, 3), $ids);
  252. }
  253. public function testAdapterDeleteWhereDbExpr()
  254. {
  255. $products = $this->_db->quoteIdentifier('zfproducts');
  256. $product_id = $this->_db->quoteIdentifier('product_id');
  257. $expr = new Zend_Db_Expr("$product_id = 1");
  258. $ids = $this->_db->fetchCol("SELECT $product_id FROM $products ORDER BY $product_id");
  259. $this->assertEquals(array(1, 2, 3), $ids);
  260. $rowsAffected = $this->_db->delete(
  261. 'zfproducts',
  262. $expr
  263. );
  264. $this->assertEquals(1, $rowsAffected);
  265. $ids = $this->_db->fetchCol("SELECT $product_id FROM $products ORDER BY $product_id");
  266. $this->assertEquals(array(2, 3), $ids);
  267. }
  268. public function testAdapterDeleteEmptyWhere()
  269. {
  270. $bugs = $this->_db->quoteIdentifier('zfbugs');
  271. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  272. $this->assertEquals(4, $count);
  273. $rowsAffected = $this->_db->delete(
  274. 'zfbugs'
  275. // intentionally no where clause
  276. );
  277. $count = $this->_db->fetchOne("SELECT COUNT(*) FROM $bugs");
  278. $this->assertEquals(0, $count);
  279. }
  280. public function testAdapterDescribeTableAttributeColumn()
  281. {
  282. $desc = $this->_db->describeTable('zfproducts');
  283. $this->assertEquals('zfproducts', $desc['product_name']['TABLE_NAME'], 'Expected table name to be zfproducts');
  284. $this->assertEquals('product_name', $desc['product_name']['COLUMN_NAME'], 'Expected column name to be product_name');
  285. $this->assertEquals(2, $desc['product_name']['COLUMN_POSITION'], 'Expected column position to be 2');
  286. $this->assertRegExp('/varchar/i', $desc['product_name']['DATA_TYPE'], 'Expected data type to be VARCHAR');
  287. $this->assertEquals('', $desc['product_name']['DEFAULT'], 'Expected default to be empty string');
  288. $this->assertTrue( $desc['product_name']['NULLABLE'], 'Expected product_name to be nullable');
  289. $this->assertNull( $desc['product_name']['SCALE'], 'Expected scale to be null');
  290. $this->assertNull( $desc['product_name']['PRECISION'], 'Expected precision to be null');
  291. $this->assertFalse( $desc['product_name']['PRIMARY'], 'Expected product_name not to be a primary key');
  292. $this->assertNull( $desc['product_name']['PRIMARY_POSITION'], 'Expected product_name to return null for PRIMARY_POSITION');
  293. $this->assertFalse( $desc['product_name']['IDENTITY'], 'Expected product_name to return false for IDENTITY');
  294. }
  295. /**
  296. * Test Adapter's describeTable() method.
  297. * Retrieve the adapter's description of the test table and examine it.
  298. */
  299. public function testAdapterDescribeTableHasColumns()
  300. {
  301. $desc = $this->_db->describeTable('zfproducts');
  302. $cols = array(
  303. 'product_id',
  304. 'product_name'
  305. );
  306. $this->assertEquals($cols, array_keys($desc));
  307. }
  308. public function testAdapterDescribeTableMetadataFields()
  309. {
  310. $desc = $this->_db->describeTable('zfproducts');
  311. $keys = array(
  312. 'SCHEMA_NAME',
  313. 'TABLE_NAME',
  314. 'COLUMN_NAME',
  315. 'COLUMN_POSITION',
  316. 'DATA_TYPE',
  317. 'DEFAULT',
  318. 'NULLABLE',
  319. 'LENGTH',
  320. 'SCALE',
  321. 'PRECISION',
  322. 'UNSIGNED',
  323. 'PRIMARY',
  324. 'PRIMARY_POSITION',
  325. 'IDENTITY'
  326. );
  327. $this->assertEquals($keys, array_keys($desc['product_name']));
  328. }
  329. /**
  330. * Test that an auto-increment key reports itself.
  331. * This is not supported in all RDBMS brands, so we test
  332. * it separately from the full describe tests above.
  333. */
  334. public function testAdapterDescribeTablePrimaryAuto()
  335. {
  336. $desc = $this->_db->describeTable('zfproducts');
  337. $auto = $desc['product_id']['IDENTITY'];
  338. if ($auto === null) {
  339. $this->markTestIncomplete($this->getDriver() . ' needs to learn how to discover auto-increment keys');
  340. }
  341. $this->assertTrue($desc['product_id']['IDENTITY']);
  342. }
  343. public function testAdapterDescribeTablePrimaryKeyColumn()
  344. {
  345. $desc = $this->_db->describeTable('zfproducts');
  346. $this->assertEquals('zfproducts', $desc['product_id']['TABLE_NAME'], 'Expected table name to be zfproducts');
  347. $this->assertEquals('product_id', $desc['product_id']['COLUMN_NAME'], 'Expected column name to be product_id');
  348. $this->assertEquals(1, $desc['product_id']['COLUMN_POSITION'], 'Expected column position to be 1');
  349. $this->assertEquals('', $desc['product_id']['DEFAULT'], 'Expected default to be empty string');
  350. $this->assertFalse( $desc['product_id']['NULLABLE'], 'Expected product_id not to be nullable');
  351. $this->assertNull( $desc['product_id']['SCALE'], 'Expected scale to be null');
  352. $this->assertNull( $desc['product_id']['PRECISION'], 'Expected precision to be null');
  353. $this->assertTrue( $desc['product_id']['PRIMARY'], 'Expected product_id to be a primary key');
  354. $this->assertEquals(1, $desc['product_id']['PRIMARY_POSITION']);
  355. }
  356. /**
  357. * @group ZF-2927
  358. */
  359. public function testAdapterDescribeView()
  360. {
  361. $describe = $this->_db->describeTable('temp_view');
  362. $this->assertEquals(8, count($describe));
  363. }
  364. /**
  365. * Test the Adapter's fetchAll() method.
  366. */
  367. public function testAdapterFetchAll()
  368. {
  369. $products = $this->_db->quoteIdentifier('zfproducts');
  370. $product_id = $this->_db->quoteIdentifier('product_id');
  371. $result = $this->_db->fetchAll("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id ASC", 1);
  372. $this->assertEquals(2, count($result));
  373. $this->assertEquals('2', $result[0]['product_id']);
  374. }
  375. /**
  376. * Test that fetchAssoc() still fetched an associative array
  377. * after the adapter's default fetch mode is set to something else.
  378. */
  379. public function testAdapterFetchAllOverrideFetchMode()
  380. {
  381. $products = $this->_db->quoteIdentifier('zfproducts');
  382. $product_id = $this->_db->quoteIdentifier('product_id');
  383. $col_name = $this->_db->foldCase('product_id');
  384. $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
  385. // Test associative array
  386. $result = $this->_db->fetchAll("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id ASC", 1, Zend_Db::FETCH_ASSOC);
  387. $this->assertEquals(2, count($result));
  388. $this->assertType('array', $result[0]);
  389. $this->assertEquals(2, count($result[0])); // count columns
  390. $this->assertEquals(2, $result[0][$col_name]);
  391. // Test numeric and associative array
  392. $result = $this->_db->fetchAll("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id ASC", 1, Zend_Db::FETCH_BOTH);
  393. $this->assertEquals(2, count($result));
  394. $this->assertType('array', $result[0]);
  395. $this->assertEquals(4, count($result[0])); // count columns
  396. $this->assertEquals(2, $result[0][$col_name]);
  397. $this->assertEquals(2, $result[0][0]);
  398. // Ensure original fetch mode has been retained
  399. $result = $this->_db->fetchAll("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id", 1);
  400. $this->assertEquals(2, count($result));
  401. $this->assertType('object', $result[0]);
  402. $this->assertEquals(2, $result[0]->$col_name);
  403. }
  404. /**
  405. * Test the Adapter's fetchAssoc() method.
  406. */
  407. public function testAdapterFetchAssoc()
  408. {
  409. $products = $this->_db->quoteIdentifier('zfproducts');
  410. $product_id = $this->_db->quoteIdentifier('product_id');
  411. $result = $this->_db->fetchAssoc("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id DESC", 1);
  412. foreach ($result as $idKey => $row) {
  413. $this->assertEquals($idKey, $row['product_id']);
  414. }
  415. }
  416. /**
  417. * Test that fetchAssoc() still fetched an associative array
  418. * after the adapter's default fetch mode is set to something else.
  419. */
  420. public function testAdapterFetchAssocAfterSetFetchMode()
  421. {
  422. $products = $this->_db->quoteIdentifier('zfproducts');
  423. $product_id = $this->_db->quoteIdentifier('product_id');
  424. $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
  425. $result = $this->_db->fetchAssoc("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id DESC", 1);
  426. $this->assertType('array', $result);
  427. $this->assertEquals(array('product_id', 'product_name'), array_keys(current($result)));
  428. }
  429. /**
  430. * Test the Adapter's fetchCol() method.
  431. */
  432. public function testAdapterFetchCol()
  433. {
  434. $products = $this->_db->quoteIdentifier('zfproducts');
  435. $product_id = $this->_db->quoteIdentifier('product_id');
  436. $result = $this->_db->fetchCol("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id ASC", 1);
  437. $this->assertType('array', $result);
  438. $this->assertEquals(2, count($result)); // count rows
  439. $this->assertEquals(2, $result[0]);
  440. $this->assertEquals(3, $result[1]);
  441. }
  442. /**
  443. * Test that fetchCol() still fetched an associative array
  444. * after the adapter's default fetch mode is set to something else.
  445. */
  446. public function testAdapterFetchColAfterSetFetchMode()
  447. {
  448. $products = $this->_db->quoteIdentifier('zfproducts');
  449. $product_id = $this->_db->quoteIdentifier('product_id');
  450. $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
  451. $result = $this->_db->fetchCol("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id ASC", 1);
  452. $this->assertType('array', $result);
  453. $this->assertEquals(2, count($result)); // count rows
  454. $this->assertEquals(2, $result[0]);
  455. $this->assertEquals(3, $result[1]);
  456. }
  457. /**
  458. * Test the Adapter's fetchOne() method.
  459. */
  460. public function testAdapterFetchOne()
  461. {
  462. $products = $this->_db->quoteIdentifier('zfproducts');
  463. $product_id = $this->_db->quoteIdentifier('product_id');
  464. $product_name = $this->_db->quoteIdentifier('product_name');
  465. $prod = 'Linux';
  466. $result = $this->_db->fetchOne("SELECT $product_name FROM $products WHERE $product_id > ? ORDER BY $product_id", 1);
  467. $this->assertEquals($prod, $result);
  468. }
  469. /**
  470. * Test that fetchCol() still fetched an associative array
  471. * after the adapter's default fetch mode is set to something else.
  472. */
  473. public function testAdapterFetchOneAfterSetFetchMode()
  474. {
  475. $products = $this->_db->quoteIdentifier('zfproducts');
  476. $product_id = $this->_db->quoteIdentifier('product_id');
  477. $product_name = $this->_db->quoteIdentifier('product_name');
  478. $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
  479. $prod = 'Linux';
  480. $result = $this->_db->fetchOne("SELECT $product_name FROM $products WHERE $product_id > ? ORDER BY $product_id", 1);
  481. $this->assertType('string', $result);
  482. $this->assertEquals($prod, $result);
  483. }
  484. /**
  485. * Test the Adapter's fetchPairs() method.
  486. */
  487. public function testAdapterFetchPairs()
  488. {
  489. $products = $this->_db->quoteIdentifier('zfproducts');
  490. $product_id = $this->_db->quoteIdentifier('product_id');
  491. $product_name = $this->_db->quoteIdentifier('product_name');
  492. $prod = 'Linux';
  493. $result = $this->_db->fetchPairs("SELECT $product_id, $product_name FROM $products WHERE $product_id > ? ORDER BY $product_id ASC", 1);
  494. $this->assertEquals(2, count($result)); // count rows
  495. $this->assertEquals($prod, $result[2]);
  496. }
  497. /**
  498. * Test the Adapter's fetchPairs() method.
  499. */
  500. public function testAdapterFetchPairsAfterSetFetchMode()
  501. {
  502. $products = $this->_db->quoteIdentifier('zfproducts');
  503. $product_id = $this->_db->quoteIdentifier('product_id');
  504. $product_name = $this->_db->quoteIdentifier('product_name');
  505. $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
  506. $prod = 'Linux';
  507. $result = $this->_db->fetchPairs("SELECT $product_id, $product_name FROM $products WHERE $product_id > ? ORDER BY $product_id ASC", 1);
  508. $this->assertType('array', $result);
  509. $this->assertEquals(2, count($result)); // count rows
  510. $this->assertEquals($prod, $result[2]);
  511. }
  512. /**
  513. * Test the Adapter's fetchRow() method.
  514. */
  515. public function testAdapterFetchRow()
  516. {
  517. $products = $this->_db->quoteIdentifier('zfproducts');
  518. $product_id = $this->_db->quoteIdentifier('product_id');
  519. $result = $this->_db->fetchRow("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id", 1);
  520. $this->assertEquals(2, count($result)); // count columns
  521. $this->assertEquals(2, $result['product_id']);
  522. }
  523. /**
  524. * Test that fetchAssoc() still fetched an associative array
  525. * after the adapter's default fetch mode is set to something else.
  526. */
  527. public function testAdapterFetchRowOverrideFetchMode()
  528. {
  529. $products = $this->_db->quoteIdentifier('zfproducts');
  530. $product_id = $this->_db->quoteIdentifier('product_id');
  531. $col_name = $this->_db->foldCase('product_id');
  532. $this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
  533. // Test associative array
  534. $result = $this->_db->fetchRow("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id", 1, Zend_Db::FETCH_ASSOC);
  535. $this->assertType('array', $result);
  536. $this->assertEquals(2, count($result)); // count columns
  537. $this->assertEquals(2, $result['product_id']);
  538. // Test numeric and associative array
  539. $result = $this->_db->fetchRow("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id", 1, Zend_Db::FETCH_BOTH);
  540. $this->assertType('array', $result);
  541. $this->assertEquals(4, count($result)); // count columns
  542. $this->assertEquals(2, $result['product_id']);
  543. $this->assertEquals(2, $result[0]);
  544. // Ensure original fetch mode has been retained
  545. $result = $this->_db->fetchRow("SELECT * FROM $products WHERE $product_id > ? ORDER BY $product_id", 1);
  546. $this->assertType('object', $result);
  547. $this->assertEquals(2, $result->$col_name);
  548. }
  549. /**
  550. * Test the Adapter's insert() method.
  551. * This requires providing an associative array of column=>value pairs.
  552. */
  553. public function testAdapterInsert()
  554. {
  555. $row = array (
  556. 'bug_description' => 'New bug',
  557. 'bug_status' => 'NEW',
  558. 'created_on' => '2007-04-02',
  559. 'updated_on' => '2007-04-02',
  560. 'reported_by' => 'micky',
  561. 'assigned_to' => 'goofy',
  562. 'verified_by' => 'dduck'
  563. );
  564. $rowsAffected = $this->_db->insert('zfbugs', $row);
  565. $this->assertEquals(1, $rowsAffected);
  566. $lastInsertId = $this->_db->lastInsertId();
  567. $this->assertType('string', $lastInsertId);
  568. $this->assertEquals('5', (string) $lastInsertId,
  569. 'Expected new id to be 5');
  570. }
  571. public function testAdapterInsertSequence()
  572. {
  573. $row = array (
  574. 'product_id' => $this->_db->nextSequenceId('zfproducts_seq'),
  575. 'product_name' => 'Solaris',
  576. );
  577. $rowsAffected = $this->_db->insert('zfproducts', $row);
  578. $this->assertEquals(1, $rowsAffected);
  579. $lastInsertId = $this->_db->lastInsertId('zfproducts');
  580. $lastSequenceId = $this->_db->lastSequenceId('zfproducts_seq');
  581. $this->assertType('string', $lastInsertId);
  582. $this->assertType('string', $lastSequenceId);
  583. $this->assertEquals('4', (string) $lastInsertId, 'Expected new id to be 4');
  584. }
  585. public function testAdapterInsertDbExpr()
  586. {
  587. $bugs = $this->_db->quoteIdentifier('zfbugs');
  588. $bug_id = $this->_db->quoteIdentifier('bug_id');
  589. $expr = new Zend_Db_Expr('2+3');
  590. $row = array (
  591. 'bug_id' => $expr,
  592. 'bug_description' => 'New bug',
  593. 'bug_status' => 'NEW',
  594. 'created_on' => '2007-04-02',
  595. 'updated_on' => '2007-04-02',
  596. 'reported_by' => 'micky',
  597. 'assigned_to' => 'goofy',
  598. 'verified_by' => 'dduck'
  599. );
  600. $rowsAffected = $this->_db->insert('zfbugs', $row);
  601. $this->assertEquals(1, $rowsAffected);
  602. $value = $this->_db->fetchOne("SELECT $bug_id FROM $bugs WHERE $bug_id = 5");
  603. $this->assertEquals(5, $value);
  604. }
  605. /**
  606. * Test the Adapter's limit() method.
  607. * Fetch 1 row. Then fetch 1 row offset by 1 row.
  608. * @group ZF-4246
  609. */
  610. public function testAdapterLimit()
  611. {
  612. $products = $this->_db->quoteIdentifier('zfproducts');
  613. $product_id = $this->_db->quoteIdentifier('product_id');
  614. $sql = $this->_db->limit("SELECT * FROM $products ORDER BY $product_id", 1);
  615. $stmt = $this->_db->query($sql);
  616. $result = $stmt->fetchAll();
  617. $this->assertEquals(1, count($result),
  618. 'Expecting row count to be 1');
  619. $this->assertEquals(2, count($result[0]),
  620. 'Expecting column count to be 2');
  621. $this->assertEquals(1, $result[0]['product_id'],
  622. 'Expecting to get product_id 1');
  623. // Check that extra field ZEND_DB_ROWNUM isn't present
  624. // (particulary with Db2 & Oracle)
  625. $this->assertArrayNotHasKey('zend_db_rownum', $result[0]);
  626. $this->assertArrayNotHasKey('ZEND_DB_ROWNUM', $result[0]);
  627. }
  628. /**
  629. * @group ZF-4246
  630. */
  631. public function testAdapterLimitOffset()
  632. {
  633. $products = $this->_db->quoteIdentifier('zfproducts');
  634. $product_id = $this->_db->quoteIdentifier('product_id');
  635. $sql = $this->_db->limit("SELECT * FROM $products ORDER BY $product_id", 1, 1);
  636. $stmt = $this->_db->query($sql);
  637. $result = $stmt->fetchAll();
  638. $this->assertEquals(1, count($result),
  639. 'Expecting row count to be 1');
  640. $this->assertEquals(2, count($result[0]),
  641. 'Expecting column count to be 2');
  642. $this->assertEquals(2, $result[0]['product_id'],
  643. 'Expecting to get product_id 2');
  644. // Check that extra field ZEND_DB_ROWNUM isn't present
  645. // (particulary with Db2 & Oracle)
  646. $this->assertArrayNotHasKey('zend_db_rownum', $result[0]);
  647. $this->assertArrayNotHasKey('ZEND_DB_ROWNUM', $result[0]);
  648. }
  649. public function testAdapterLimitInvalidArgumentException()
  650. {
  651. try {
  652. $sql = $this->_db->limit('SELECT * FROM zfproducts', 0);
  653. $this->fail('Expected to catch Zend_Db_Adapter_Exception');
  654. } catch (Zend_Exception $e) {
  655. $this->assertType('Zend_Db_Adapter_Exception', $e,
  656. 'Expecting object of type Zend_Db_Adapter_Exception, got '.get_class($e));
  657. }
  658. try {
  659. $sql = $this->_db->limit('SELECT * FROM zfproducts', 1, -1);
  660. $this->fail('Expected to catch Zend_Db_Adapter_Exception');
  661. } catch (Zend_Exception $e) {
  662. $this->assertType('Zend_Db_Adapter_Exception', $e,
  663. 'Expecting object of type Zend_Db_Adapter_Exception, got '.get_class($e));
  664. }
  665. }
  666. /**
  667. * Test the Adapter's listTables() method.
  668. * Fetch the list of tables and verify that the test table exists in
  669. * the list.
  670. */
  671. public function testAdapterListTables()
  672. {
  673. $tables = $this->_db->listTables();
  674. $this->assertContains('zfproducts', $tables);
  675. }
  676. /**
  677. * Used by _testAdapterOptionCaseFoldingNatural()
  678. * DB2 and Oracle return identifiers in uppercase naturally,
  679. * so those test suites will override this method.
  680. */
  681. protected function _testAdapterOptionCaseFoldingNaturalIdentifier()
  682. {
  683. return 'case_folded_identifier';
  684. }
  685. /**
  686. * Used by _testAdapterOptionCaseFoldingNatural()
  687. * SQLite needs to do metadata setup,
  688. * because it uses the in-memory database,
  689. * so that test suite will override this method.
  690. */
  691. protected function _testAdapterOptionCaseFoldingSetup(Zend_Db_Adapter_Abstract $db)
  692. {
  693. $db->getConnection();
  694. }
  695. /**
  696. * Used by:
  697. * - testAdapterOptionCaseFoldingNatural()
  698. * - testAdapterOptionCaseFoldingUpper()
  699. * - testAdapterOptionCaseFoldingLower()
  700. *
  701. * @param int $case
  702. * @return string
  703. */
  704. public function _testAdapterOptionCaseFoldingCommon($case)
  705. {
  706. $params = $this->_util->getParams();
  707. $params['options'] = array(
  708. Zend_Db::CASE_FOLDING => $case
  709. );
  710. $db = Zend_Db::factory($this->getDriver(), $params);
  711. $this->_testAdapterOptionCaseFoldingSetup($db);
  712. $products = $db->quoteIdentifier('zfproducts');
  713. $product_name = $db->quoteIdentifier('product_name');
  714. /*
  715. * it is important not to delimit the pname identifier
  716. * in the following query
  717. */
  718. $sql = "SELECT $product_name AS case_folded_identifier FROM $products";
  719. $stmt = $db->query($sql);
  720. $result = $stmt->fetchAll(Zend_Db::FETCH_ASSOC);
  721. $keys = array_keys($result[0]);
  722. return $keys[0];
  723. }
  724. /**
  725. * Test the connection's CASE_FOLDING option.
  726. * Case: Zend_Db::CASE_NATURAL
  727. */
  728. public function testAdapterOptionCaseFoldingNatural()
  729. {
  730. $natural = $this->_testAdapterOptionCaseFoldingCommon(Zend_Db::CASE_NATURAL);
  731. $expected = $this->_testAdapterOptionCaseFoldingNaturalIdentifier();
  732. $this->assertEquals($natural, $expected, 'Natural case does not match');
  733. }
  734. /**
  735. * Test the connection's CASE_FOLDING option.
  736. * Case: Zend_Db::CASE_UPPER
  737. */
  738. public function testAdapterOptionCaseFoldingUpper()
  739. {
  740. $upper = $this->_testAdapterOptionCaseFoldingCommon(Zend_Db::CASE_UPPER);
  741. $expected = strtoupper($this->_testAdapterOptionCaseFoldingNaturalIdentifier());
  742. $this->assertEquals($upper, $expected, 'Upper case does not match');
  743. }
  744. /**
  745. * Test the connection's CASE_FOLDING option.
  746. * Case: Zend_Db::CASE_LOWER
  747. */
  748. public function testAdapterOptionCaseFoldingLower()
  749. {
  750. $lower = $this->_testAdapterOptionCaseFoldingCommon(Zend_Db::CASE_LOWER);
  751. $expected = strtolower($this->_testAdapterOptionCaseFoldingNaturalIdentifier());
  752. $this->assertEquals($lower, $expected, 'Lower case does not match');
  753. }
  754. /**
  755. * Test the connection's CASE_FOLDING option.
  756. * Case: invalid value throws exception
  757. */
  758. public function testAdapterOptionCaseFoldingInvalidException()
  759. {
  760. try {
  761. $lower = $this->_testAdapterOptionCaseFoldingCommon(-999);
  762. $this->fail('Expected exception not thrown');
  763. } catch (Zend_Exception $e) {
  764. $this->assertType('Zend_Db_Adapter_Exception', $e,
  765. 'Expecting object of type Zend_Db_Adapter_Exception, got '.get_class($e));
  766. $this->assertEquals("Case must be one of the following constants: Zend_Db::CASE_NATURAL, Zend_Db::CASE_LOWER, Zend_Db::CASE_UPPER", $e->getMessage());
  767. }
  768. }
  769. /**
  770. * Test that we can query twice in a row.
  771. * That is, a query doesn't leave the adapter in an unworking state.
  772. * See bug ZF-1778.
  773. */
  774. public function testAdapterQueryAfterUnclosedQuery()
  775. {
  776. $select = $this->_db->select()
  777. ->from('zfproducts')
  778. ->order('product_id');
  779. $stmt1 = $this->_db->query($select);
  780. $result1 = $stmt1->fetchAll(Zend_Db::FETCH_NUM);
  781. $stmt1 = $this->_db->query($select);
  782. $result2 = $stmt1->fetchAll(Zend_Db::FETCH_NUM);
  783. $this->assertEquals($result1, $result2);
  784. }
  785. /**
  786. * Ensures that query() throws an exception when given a bogus query
  787. *
  788. * @return void
  789. */
  790. public function testAdapterQueryBogus()
  791. {
  792. try {
  793. $this->_db->query('Bogus query');
  794. $this->fail('Expected exception not thrown');
  795. } catch (Zend_Exception $e) {
  796. $this->assertType('Zend_Db_Statement_Exception', $e,
  797. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  798. }
  799. }
  800. /**
  801. * Ensures that query() throws an exception when given a bogus table
  802. *
  803. * @return void
  804. */
  805. public function testAdapterQueryBogusTable()
  806. {
  807. try {
  808. $this->_db->query('SELECT * FROM BogusTable');
  809. $this->fail('Expected exception not thrown');
  810. } catch (Zend_Exception $e) {
  811. $this->assertType('Zend_Db_Statement_Exception', $e,
  812. 'Expecting object of type Zend_Db_Statement_Exception, got '.get_class($e));
  813. }
  814. }
  815. /**
  816. * Ensures that query() provides expected behavior when returning no results
  817. *
  818. * @return void
  819. */
  820. public function testAdapterQueryResultsNone()
  821. {
  822. $stmt = $this->_db->query('SELECT * FROM ' . $this->_db->quoteIdentifier('zfbugs') . ' WHERE '
  823. . $this->_db->quoteIdentifier('bug_id') . ' = -1');
  824. $this->assertTrue(is_object($stmt),
  825. 'Expected query() to return object; got ' . gettype($stmt));
  826. $this->assertType('Zend_Db_Statement_Interface', $stmt,
  827. 'Expected query() to return Zend_Db_Statement or PDOStatement; got ' . get_class($stmt));
  828. $this->assertEquals(0, $count = count($stmt->fetchAll()),
  829. "Expected fetchAll() to return zero rows; got $count");
  830. }
  831. /**
  832. * Test that quote() accepts a string and returns
  833. * a quoted string.
  834. */
  835. public function testAdapterQuote()
  836. {
  837. $string = 'String without quotes';
  838. $value = $this->_db->quote($string);
  839. $this->assertEquals("'String without quotes'", $value);
  840. }
  841. /**
  842. * Test that quote() takes an array and returns
  843. * an imploded string of comma-separated, quoted elements.
  844. */
  845. public function testAdapterQuoteArray()
  846. {
  847. $array = array("it's", 'all', 'right!');
  848. $value = $this->_db->quote($array);
  849. $this->assertEquals("'it\\'s', 'all', 'right!'", $value);
  850. }
  851. /**
  852. * test that quote() accepts a Zend_Db_Expr
  853. * and returns the string representation,
  854. * with no quoting applied.
  855. */
  856. public function testAdapterQuoteDbExpr()
  857. {
  858. $string = 'String with "`\' quotes';
  859. $expr = new Zend_Db_Expr($string);
  860. $value = $this->_db->quote($expr);
  861. $this->assertEquals($string, $value);
  862. }
  863. /**
  864. * test that quote() accepts a string containing
  865. * digits and returns an unquoted string.
  866. */
  867. public function testAdapterQuoteDigitString()
  868. {
  869. $string = '123';
  870. $value = $this->_db->quote($string);
  871. $this->assertEquals("'123'", $value);
  872. }
  873. /**
  874. * test that quote() escapes a double-quote
  875. * character in a string.
  876. */
  877. public function testAdapterQuoteDoubleQuote()
  878. {
  879. $string = 'St John"s Wort';
  880. $value = $this->_db->quote($string);
  881. $this->assertEquals("'St John\\\"s Wort'", $value);
  882. }
  883. /**
  884. * test that quote() accepts an integer and
  885. * returns an unquoted integer.
  886. */
  887. public function testAdapterQuoteInteger()
  888. {
  889. $int = 123;
  890. $value = $this->_db->quote($int);
  891. $this->assertEquals(123, $value);
  892. }
  893. /**
  894. * test that quote() accepts an array and returns
  895. * an imploded string of unquoted elements
  896. */
  897. public function testAdapterQuoteIntegerArray()
  898. {
  899. $array = array(1,'2',3);
  900. $value = $this->_db->quote($array);
  901. $this->assertEquals("1, '2', 3", $value);
  902. }
  903. /**
  904. * test that quote() escapes a single-quote
  905. * character in a string.
  906. */
  907. public function testAdapterQuoteSingleQuote()
  908. {
  909. $string = "St John's Wort";
  910. $value = $this->_db->quote($string);
  911. $this->assertEquals("'St John\'s Wort'", $value);
  912. }
  913. /**
  914. * test that quoteColumnAs() accepts a string
  915. * and an alias, and returns each as delimited
  916. * identifiers, with 'AS' in between.
  917. */
  918. public function testAdapterQuoteColumnAs()
  919. {
  920. $string = "foo";
  921. $alias = "bar";
  922. $value = $this->_db->quoteColumnAs($string, $alias);
  923. $this->assertEquals('"foo" AS "bar"', $value);
  924. }
  925. /**
  926. * test that quoteColumnAs() accepts a string
  927. * and an alias, but ignores the alias if it is
  928. * the same as the base identifier in the string.
  929. */
  930. public function testAdapterQuoteColumnAsSameString()
  931. {
  932. $string = 'foo.bar';
  933. $alias = 'bar';
  934. $value = $this->_db->quoteColumnAs($string, $alias);
  935. $this->assertEquals('"foo"."bar"', $value);
  936. }
  937. /**
  938. * test that quoteIdentifier() accepts a string
  939. * and returns a delimited identifier.
  940. */
  941. public function testAdapterQuoteIdentifier()
  942. {
  943. $string = 'table_name';
  944. $value = $this->_db->quoteIdentifier($string);
  945. $this->assertEquals('"table_name"', $value);
  946. }
  947. /**
  948. * test that quoteIdentifier() accepts an array
  949. * and returns a qualified delimited identifier.
  950. */
  951. public function testAdapterQuoteIdentifierArray()
  952. {
  953. $array = array('foo', 'bar');
  954. $value = $this->_db->quoteIdentifier($array);
  955. $this->assertEquals('"foo"."bar"', $value);
  956. }
  957. /**
  958. * test that quoteIdentifier() accepts an array
  959. * containing a Zend_Db_Expr, and returns strings
  960. * as delimited identifiers, and Exprs as unquoted.
  961. */
  962. public function testAdapterQuoteIdentifierArrayDbExpr()
  963. {
  964. $expr = new Zend_Db_Expr('*');
  965. $array = array('foo', $expr);
  966. $value = $this->_db->quoteIdentifier($array);
  967. $this->assertEquals('"foo".*', $value);
  968. }
  969. /**
  970. * test that quoteIdentifier() accepts a Zend_Db_Expr
  971. * and returns the string representation,
  972. * with no quoting applied.
  973. */
  974. public function testAdapterQuoteIdentifierDbExpr()
  975. {
  976. $string = 'String with "`\' quotes';
  977. $expr = new Zend_Db_Expr($string);
  978. $value = $this->_db->quoteIdentifier($expr);
  979. $this->assertEquals($string, $value);
  980. }
  981. /**
  982. * test that quoteIdentifer() escapes a double-quote
  983. * character in a string.
  984. */
  985. public function testAdapterQuoteIdentifierDoubleQuote()
  986. {
  987. $string = 'table_"_name';
  988. $value = $this->_db->quoteIdentifier($string);
  989. $this->assertEquals('"table_""_name"', $value);
  990. }
  991. /**
  992. * test that quoteIdentifer() accepts an integer
  993. * and returns a delimited identifier as with a string.
  994. */
  995. public function testAdapterQuoteIdentifierInteger()
  996. {
  997. $int = 123;
  998. $value = $this->_db->quoteIdentifier($int);
  999. $this->assertEquals('"123"', $value);
  1000. }
  1001. /**
  1002. * test that quoteIdentifier() accepts a string
  1003. * containing a dot (".") character, splits the
  1004. * string, quotes each segment individually as
  1005. * delimited identifers, and returns the imploded
  1006. * string.
  1007. */
  1008. public function testAdapterQuoteIdentifierQualified()
  1009. {
  1010. $string = 'table.column';
  1011. $value = $this->_db->quoteIdentifier($string);
  1012. $this->assertEquals('"table"."column"', $value);
  1013. }
  1014. /**
  1015. * test that quoteIdentifer() escapes a single-quote
  1016. * character in a string.
  1017. */
  1018. public function testAdapterQuoteIdentifierSingleQuote()
  1019. {
  1020. $string = "table_'_name";
  1021. $value = $this->_db->quoteIdentifier($string);
  1022. $this->assertEquals('"table_\'_name"', $value);
  1023. }
  1024. /**
  1025. * test that quoteTableAs() accepts a string and an alias,
  1026. * and returns each as delimited identifiers.
  1027. * Most RDBMS want an 'AS' in between.
  1028. */
  1029. public function testAdapterQuoteTableAs()
  1030. {
  1031. $string = "foo";
  1032. $alias = "bar";
  1033. $value = $this->_db->quoteTableAs($string, $alias);
  1034. $this->assertEquals('"foo" AS "bar"', $value);
  1035. }
  1036. /**
  1037. * test that quoteInto() accepts a Zend_Db_Expr
  1038. * and returns the string representation,
  1039. * with no quoting applied.
  1040. */
  1041. public function testAdapterQuoteIntoDbExpr()
  1042. {
  1043. $string = 'id=?';
  1044. $expr = new Zend_Db_Expr('CURDATE()');
  1045. $value = $this->_db->quoteInto($string, $expr);
  1046. $this->assertEquals("id=CURDATE()", $value);
  1047. }
  1048. /**
  1049. * test that quoteInto() escapes a double-quote
  1050. * character in a string.
  1051. */
  1052. public function testAdapterQuoteIntoDoubleQuote()
  1053. {
  1054. $string = 'id=?';
  1055. $param = 'St John"s Wort';
  1056. $value = $this->_db->quoteInto($string, $param);
  1057. $this->assertEquals("id='St John\\\"s Wort'", $value);
  1058. }
  1059. /**
  1060. * test that quoteInto() escapes a single-quote
  1061. * character in a string.
  1062. */
  1063. public function testAdapterQuoteIntoSingleQuote()
  1064. {
  1065. $string = 'id = ?';
  1066. $param = 'St John\'s Wort';
  1067. $value = $this->_db->quoteInto($string, $param);
  1068. $this->assertEquals("id = 'St John\\'s Wort'", $value);
  1069. }
  1070. protected $_numericDataTypes = array(
  1071. Zend_Db::INT_TYPE => Zend_Db::INT_TYPE,
  1072. Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
  1073. Zend_Db::FLOAT_TYPE => Zend_Db::FLOAT_TYPE
  1074. );
  1075. public function testAdapterQuoteIntoType()
  1076. {
  1077. $value = $this->_db->quoteInto('foo = ?', 1234, Zend_Db::INT_TYPE);
  1078. $this->assertType('string', $value);
  1079. $this->assertEquals('foo = 1234', $value,
  1080. 'Incorrect quoteInto() result for INT_TYPE');
  1081. $value = $this->_db->quoteInto('foo = ?', 1234, Zend_Db::BIGINT_TYPE);
  1082. $this->assertType('string', $value);
  1083. $this->assertEquals('foo = 1234', $value,
  1084. 'Incorrect quoteInto() result for BIGINT_TYPE');
  1085. $value = $this->_db->quoteInto('foo = ?', 12.34, Zend_Db::FLOAT_TYPE);
  1086. $this->assertType('string', $value);
  1087. $this->assertEquals('foo = 12.340000', $value,
  1088. 'Incorrect quoteInto() result for FLOAT_TYPE');
  1089. $value = $this->_db->quoteInto('foo = ?', 1234, 'CHAR');
  1090. $this->assertType('string', $value);
  1091. $this->assertEquals("foo = 1234", $value,
  1092. 'Incorrect quoteInto() result for CHAR');
  1093. $value = $this->_db->quoteInto('foo = ?', '1234', 'CHAR');
  1094. $this->assertType('string', $value);
  1095. $this->assertEquals("foo = '1234'", $value,
  1096. 'Incorrect quoteInto() result for CHAR');
  1097. }
  1098. public function testAdapterQuoteIntoCount()
  1099. {
  1100. $value = $this->_db->quoteInto('foo = ? and bar = ?', 1234, null, 1);
  1101. $this->assertType('string', $value);
  1102. $this->assertEquals('foo = 1234 and bar = ?', $value,
  1103. 'Incorrect quoteInto() result for count');
  1104. }
  1105. public function testAdapterQuoteTypeInt()
  1106. {
  1107. foreach ($this->_numericDataTypes as $typeName => $type) {
  1108. if ($type != 0) {
  1109. continue;
  1110. }
  1111. // test int value
  1112. $value = $this->_db->quote(1234, $typeName);
  1113. $this->assertType('string', $value);
  1114. $this->assertEquals('1234', $value,
  1115. "Incorrect quote() INT_TYPE result for int");
  1116. // test int string
  1117. $value = $this->_db->quote('1234', $typeName);
  1118. $this->assertType('string', $value);
  1119. $this->assertEquals('1234', $value,
  1120. "Incorrect quote() INT_TYPE result for int string");
  1121. // test int string with + sign
  1122. $value = $this->_db->quote('+1234', $typeName);
  1123. $this->assertType('string', $value);
  1124. $this->assertEquals('1234', $value,
  1125. "Incorrect quote() INT_TYPE result for int string with + sign");
  1126. // test int string with - sign
  1127. $value = $this->_db->quote('-1234', $typeName);
  1128. $this->assertType('string', $value);
  1129. $this->assertEquals('-1234', $value,
  1130. "Incorrect quote() INT_TYPE result for int string with - sign");
  1131. // test int string with non-digit text
  1132. $value = $this->_db->quote('1234abcd', $typeName);
  1133. $this->assertType('string', $value);
  1134. $this->assertEquals('1234', $value,
  1135. "Incorrect quote() INT_TYPE result for int string with non-digit text");
  1136. // test non-digit test; it should return 0
  1137. $value = $this->_db->quote('abcd', $typeName);
  1138. $this->assertType('string', $value);
  1139. $this->assertEquals('0', $value,
  1140. "Incorrect quote() INT_TYPE result for non-digit string");
  1141. // test Zend_Db_Expr is passed through literally
  1142. $value = $this->_db->quote(new Zend_Db_Expr('1+2+3'), $typeName);
  1143. $this->assertType('string', $value);
  1144. $this->assertEquals("1+2+3", $value,
  1145. "Incorrect quote() INT_TYPE result for Zend_Db_Expr");
  1146. }
  1147. }
  1148. public function testAdapterQuoteTypeBigInt()
  1149. {
  1150. foreach ($this->_numericDataTypes as $typeName => $type) {
  1151. if ($type != 1) {
  1152. continue;
  1153. }
  1154. // test int value
  1155. $value = $this->_db->quote(1234, $typeName);
  1156. $this->assertType('string', $value);
  1157. $this->assertEquals(1234, $value,
  1158. "Incorrect quote() BIGINT_TYPE result for int");
  1159. // test int string
  1160. $value = $this->_db->quote('1234', $typeName);
  1161. $this->assertType('string', $value);
  1162. $this->assertEquals('1234', $value,
  1163. "Incorrect quote() BIGINT_TYPE result for int string");
  1164. // test int string with + sign
  1165. $value = $this->_db->quote('+1234', $typeName);
  1166. $this->assertType('string', $value);
  1167. $this->assertEquals('+1234', $value,
  1168. "Incorrect quote() BIGINT_TYPE result for int string");
  1169. // test int string with - sign
  1170. $value = $this->_db->quote('-1234', $typeName);
  1171. $this->assertType('string', $value);
  1172. $this->assertEquals('-1234', $value,
  1173. "Incorrect quote() BIGINT_TYPE result for int string");
  1174. // test int string with positive exponent
  1175. $value = $this->_db->quote('1234e15', $typeName);
  1176. $this->assertType('string', $value);
  1177. $this->assertEquals('1234e15', $value,
  1178. "Incorrect quote() BIGINT_TYPE result for int string");
  1179. $value = $this->_db->quote('1234e+15', $typeName);
  1180. $this->assertType('string', $value);
  1181. $this->assertEquals('1234e+15', $value,
  1182. "Incorrect quote() BIGINT_TYPE result for int string");
  1183. // test int value with negative exponent
  1184. $value = $this->_db->quote('1234e-15', $typeName);
  1185. $this->assertType('string', $value);
  1186. $this->assertEquals('1234e-15', $value,
  1187. "Incorrect quote() BIGINT_TYPE result for int string");
  1188. // test bigint value
  1189. $value = $this->_db->quote('2200000000', $typeName);
  1190. $this->assertType('string', $value);
  1191. $this->assertEquals('2200000000', $value,
  1192. "Incorrect quote() BIGINT_TYPE result for big int string");
  1193. // test octal value with leading zeroes
  1194. $value = $this->_db->quote('020310253000', $typeName);
  1195. $this->assertType('string', $value);
  1196. $this->assertEquals('020310253000', $value,
  1197. "Incorrect quote() BIGINT_TYPE result for big octal int string with leading zeroes");
  1198. // test decimal value with leading zeroes
  1199. $value = $this->_db->quote('09091', $typeName);
  1200. $this->assertType('string', $value);
  1201. $this->assertEquals('09091', $value,
  1202. "Incorrect quote() BIGINT_TYPE result for decimal int string with leading zeroes");
  1203. // test hex value with ODBC-style notation
  1204. $value = $this->_db->quote('0x83215600', $typeName);
  1205. $this->assertType('string', $value);
  1206. $this->assertEquals('0x83215600', $value,
  1207. "Incorrect quote() BIGINT_TYPE result for big hex int string");
  1208. // test non-numeric value; it should be zero
  1209. $value = $this->_db->quote('abcd', $typeName);
  1210. $this->assertType('string', $value);
  1211. $this->assertEquals('0', $value,
  1212. "Incorrect quote() BIGINT_TYPE result for non-digt string");
  1213. // test Zend_Db_Expr is passed through literally
  1214. $value = $this->_db->quote(new Zend_Db_Expr('1+2+3'), $typeName);
  1215. $this->assertType('string', $value);
  1216. $this->assertEquals("1+2+3", $value,
  1217. "Incorrect quote() BIGINT_TYPE result for Zend_Db_Expr");
  1218. }
  1219. }
  1220. public function testAdapterQuoteTypeFloat()
  1221. {
  1222. foreach ($this->_numericDataTypes as $typeName => $type) {
  1223. if ($type != 2) {
  1224. continue;
  1225. }
  1226. $value = $this->_db->quote(12.34, $typeName);
  1227. $this->assertType('string', $value);
  1228. $this->assertEquals('12.34', $value,
  1229. "Incorrect quote() FLOAT_TYPE result");
  1230. $value = $this->_db->quote('12.34', $typeName);
  1231. $this->assertType('string', $value);
  1232. $this->assertEquals('12.34', $value,
  1233. "Incorrect quote() FLOAT_TYPE result");
  1234. $value = $this->_db->quote('+12.34', $typeName);
  1235. $this->assertType('string', $value);
  1236. $this->assertEquals('12.34', $value,
  1237. "Incorrect quote() FLOAT_TYPE result");
  1238. $value = $this->_db->quote('-12.34', $typeName);
  1239. $this->assertType('string', $value);
  1240. $this->assertEquals('-12.34', $value,
  1241. "Incorrect quote() FLOAT_TYPE result");
  1242. $value = $this->_db->quote('12.34abcd', $typeName);
  1243. $this->assertType('string', $value);
  1244. $this->assertEquals('12.34', $value,
  1245. "Incorrect quote() FLOAT_TYPE result");
  1246. $value = $this->_db->quote('abcd', $typeName);
  1247. $this->assertType('string', $value);
  1248. $this->assertEquals('0', $value,
  1249. "Incorrect quote() FLOAT_TYPE result");
  1250. $value = $this->_db->quote(new Zend_Db_Expr('1+2+3'), $typeName);
  1251. $this->assertType('string', $value);
  1252. $this->assertEquals("1+2+3", $value,
  1253. "Incorrect quote() FLOAT_TYPE result");
  1254. }
  1255. }
  1256. public function testAdapterQuoteTypeNonNumeric()
  1257. {
  1258. $value = $this->_db->quote(1234, 'CHAR');
  1259. $this->assertType('integer', $value);
  1260. $this->assertEquals(1234, $value,
  1261. "Incorrect quote() CHAR type result");
  1262. $value = $this->_db->quote('1234', 'CHAR');
  1263. $this->assertType('string', $value);
  1264. $this->assertEquals("'1234'", $value,
  1265. "Incorrect quote() CHAR type result");
  1266. $value = $this->_db->quote('1234abcd', 'CHAR');
  1267. $this->assertType('string', $value);
  1268. $this->assertEquals("'1234abcd'", $value,
  1269. "Incorrect quote() CHAR type result");
  1270. $value = $this->_db->quote('1234abcd56', 'CHAR');
  1271. $this->assertType('string', $value);
  1272. $this->assertEquals("'1234abcd56'", $value,
  1273. "Incorrect quote() CHAR type result");
  1274. // test Zend_Db_Expr is passed through literally
  1275. $value = $this->_db->quote(new Zend_Db_Expr('1+2+3'), 'CHAR');
  1276. $this->assertType('string', $value);
  1277. $this->assertEquals("1+2+3", $value,
  1278. "Incorrect quote() CHAR type result for Zend_Db_Expr");
  1279. }
  1280. /**
  1281. * Ensures that the character sequence ":0'" is handled properly
  1282. *
  1283. * @link http://framework.zend.com/issues/browse/ZF-2059
  1284. * @return void
  1285. */
  1286. public function testZF2059()
  1287. {
  1288. $value = ':0\'';
  1289. $valueQuoted = $this->_db->quote($value);
  1290. $bugs = $this->_db->quoteIdentifier('zfbugs');
  1291. $bug_id = $this->_db->quoteIdentifier('bug_id');
  1292. $bug_status = $this->_db->quoteIdentifier('bug_status');
  1293. $sql = "SELECT $bug_id FROM $bugs WHERE $bug_status != " . $valueQuoted;
  1294. $results = $this->_db->fetchAll($sql);
  1295. $this->assertEquals(4, count($results));
  1296. }
  1297. public function testAdapterSetFetchMode()
  1298. {
  1299. $modes = array(
  1300. Zend_Db::FETCH_ASSOC,
  1301. Zend_Db::FETCH_BOTH,
  1302. Zend_Db::FETCH_NUM,
  1303. Zend_Db::FETCH_OBJ
  1304. );
  1305. foreach ($modes as $mode) {
  1306. $this->_db->setFetchMode($mode);
  1307. $this->assertEquals($mode, $this->_db->getFetchMode());
  1308. }
  1309. }
  1310. public function testAdapterSetFetchModeInvalidException()
  1311. {
  1312. try {
  1313. $this->_db->setFetchMode(-999);
  1314. $this->fail('Expected exception not thrown');
  1315. } catch (Zend_Exception $e) {
  1316. $this->assertType('Zend_Db_Adapter_Exception', $e,
  1317. 'Expecting object of type Zend_Db_Adapter_Exception, got '.get_class($e));
  1318. $this->assertEquals("Invalid fetch mode '-999' specified", $e->getMessage());
  1319. }
  1320. }
  1321. public function testAdapterTransactionAutoCommit()
  1322. {
  1323. $bugs = $this->_db->quoteIdentifier('zfbugs');
  1324. $bug_id = $this->_db->quoteIdentifier('bug_id');
  1325. // use our default connection as the Connection1
  1326. $dbConnection1 = $this->_db;
  1327. // create a second connection to the same database
  1328. $dbConnection2 = Zend_Db::factory($this->getDriver(), $this->_util->getParams());
  1329. $dbConnection2->getConnection();
  1330. // notice the number of rows in connection 2
  1331. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  1332. $this->assertEquals(4, $count);
  1333. // delete a row in connection 1
  1334. $rowsAffected = $dbConnection1->delete(
  1335. 'zfbugs',
  1336. "$bug_id = 1"
  1337. );
  1338. $this->assertEquals(1, $rowsAffected);
  1339. // we should see the results in connection 2 immediately
  1340. // after the DELETE executes, because it's autocommit
  1341. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  1342. $this->assertEquals(3, $count);
  1343. }
  1344. public function testAdapterTransactionCommit()
  1345. {
  1346. $bugs = $this->_db->quoteIdentifier('zfbugs');
  1347. $bug_id = $this->_db->quoteIdentifier('bug_id');
  1348. // use our default connection as the Connection1
  1349. $dbConnection1 = $this->_db;
  1350. // create a second connection to the same database
  1351. $dbConnection2 = Zend_Db::factory($this->getDriver(), $this->_util->getParams());
  1352. $dbConnection2->getConnection();
  1353. // notice the number of rows in connection 2
  1354. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  1355. $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
  1356. // start an explicit transaction in connection 1
  1357. $dbConnection1->beginTransaction();
  1358. // delete a row in connection 1
  1359. $rowsAffected = $dbConnection1->delete(
  1360. 'zfbugs',
  1361. "$bug_id = 1"
  1362. );
  1363. $this->assertEquals(1, $rowsAffected);
  1364. // we should still see all rows in connection 2
  1365. // because the DELETE has not been committed yet
  1366. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  1367. $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table (step 2); perhaps Adapter is still in autocommit mode?');
  1368. // commit the DELETE
  1369. $dbConnection1->commit();
  1370. // now we should see one fewer rows in connection 2
  1371. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  1372. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 3)');
  1373. // delete another row in connection 1
  1374. $rowsAffected = $dbConnection1->delete(
  1375. 'zfbugs',
  1376. "$bug_id = 2"
  1377. );
  1378. $this->assertEquals(1, $rowsAffected);
  1379. // we should see results immediately, because
  1380. // the db connection returns to auto-commit mode
  1381. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  1382. $this->assertEquals(2, $count);
  1383. }
  1384. public function testAdapterTransactionRollback()
  1385. {
  1386. $bugs = $this->_db->quoteIdentifier('zfbugs');
  1387. $bug_id = $this->_db->quoteIdentifier('bug_id');
  1388. // use our default connection as the Connection1
  1389. $dbConnection1 = $this->_db;
  1390. // create a second connection to the same database
  1391. $dbConnection2 = Zend_Db::factory($this->getDriver(), $this->_util->getParams());
  1392. $dbConnection2->getConnection();
  1393. // notice the number of rows in connection 2
  1394. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  1395. $this->assertEquals(4, $count, 'Expecting to see 4 rows in bugs table (step 1)');
  1396. // start an explicit transaction in connection 1
  1397. $dbConnection1->beginTransaction();
  1398. // delete a row in connection 1
  1399. $rowsAffected = $dbConnection1->delete(
  1400. 'zfbugs',
  1401. "$bug_id = 1"
  1402. );
  1403. $this->assertEquals(1, $rowsAffected);
  1404. // we should still see all rows in connection 2
  1405. // because the DELETE has not been committed yet
  1406. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  1407. $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table (step 2); perhaps Adapter is still in autocommit mode?');
  1408. // rollback the DELETE
  1409. $dbConnection1->rollback();
  1410. // now we should see the same number of rows
  1411. // because the DELETE was rolled back
  1412. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  1413. $this->assertEquals(4, $count, 'Expecting to still see 4 rows in bugs table after DELETE is rolled back (step 3)');
  1414. // delete another row in connection 1
  1415. $rowsAffected = $dbConnection1->delete(
  1416. 'zfbugs',
  1417. "$bug_id = 2"
  1418. );
  1419. $this->assertEquals(1, $rowsAffected);
  1420. // we should see results immediately, because
  1421. // the db connection returns to auto-commit mode
  1422. $count = $dbConnection2->fetchOne("SELECT COUNT(*) FROM $bugs");
  1423. $this->assertEquals(3, $count, 'Expecting to see 3 rows in bugs table after DELETE (step 4)');
  1424. }
  1425. /**
  1426. * Test the Adapter's update() method.
  1427. * Update a single row and verify that the change was made.
  1428. * Attempt to update a row that does not exist, and verify
  1429. * that no change was made.
  1430. */
  1431. public function testAdapterUpdate()
  1432. {
  1433. $product_id = $this->_db->quoteIdentifier('product_id');
  1434. // Test that we can change the values in
  1435. // an existing row.
  1436. $rowsAffected = $this->_db->update(
  1437. 'zfproducts',
  1438. array('product_name' => 'Vista'),
  1439. "$product_id = 1"
  1440. );
  1441. $this->assertEquals(1, $rowsAffected);
  1442. // Query the row to see if we have the new values.
  1443. $select = $this->_db->select();
  1444. $select->from('zfproducts');
  1445. $select->where("$product_id = 1");
  1446. $stmt = $this->_db->query($select);
  1447. $result = $stmt->fetchAll();
  1448. $this->assertEquals(1, $result[0]['product_id']);
  1449. $this->assertEquals('Vista', $result[0]['product_name']);
  1450. // Test that update affects no rows if the WHERE
  1451. // clause matches none.
  1452. $rowsAffected = $this->_db->update(
  1453. 'zfproducts',
  1454. array('product_name' => 'Vista'),
  1455. "$product_id = 327"
  1456. );
  1457. $this->assertEquals(0, $rowsAffected);
  1458. }
  1459. public function testAdapterUpdateSetDbExpr()
  1460. {
  1461. $bugs = $this->_db->quoteIdentifier('zfbugs');
  1462. $bug_id = $this->_db->quoteIdentifier('bug_id');
  1463. $bug_status = $this->_db->quoteIdentifier('bug_status');
  1464. $expr = new Zend_Db_Expr("UPPER('setExpr')");
  1465. $rowsAffected = $this->_db->update(
  1466. 'zfbugs',
  1467. array('bug_status' => $expr),
  1468. "$bug_id = 1"
  1469. );
  1470. $this->assertEquals(1, $rowsAffected);
  1471. $value = $this->_db->fetchOne("SELECT $bug_status FROM $bugs WHERE $bug_id = 1");
  1472. $this->assertEquals('SETEXPR', $value);
  1473. }
  1474. public function testAdapterUpdateWhereArray()
  1475. {
  1476. $bugs = $this->_db->quoteIdentifier('zfbugs');
  1477. $bug_id = $this->_db->quoteIdentifier('bug_id');
  1478. $bug_status = $this->_db->quoteIdentifier('bug_status');
  1479. $rowsAffected = $this->_db->update(
  1480. 'zfbugs',
  1481. array('bug_status' => 'ARRAY'),
  1482. array("$bug_id = 1", "$bug_status = 'NEW'")
  1483. );
  1484. $this->assertEquals(1, $rowsAffected);
  1485. $value = $this->_db->fetchOne("SELECT $bug_status FROM $bugs WHERE $bug_id = 1");
  1486. $this->assertEquals('ARRAY', $value);
  1487. }
  1488. /**
  1489. * @group ZF-1726
  1490. */
  1491. public function testAdapterUpdateWhereArrayWithVariable()
  1492. {
  1493. $bugs = $this->_db->quoteIdentifier('zfbugs');
  1494. $bug_id = $this->_db->quoteIdentifier('bug_id');
  1495. $bug_status = $this->_db->quoteIdentifier('bug_status');
  1496. $rowsAffected = $this->_db->update(
  1497. 'zfbugs',
  1498. array('bug_status' => 'ARRAY'),
  1499. array("$bug_id = ?" => 1, "$bug_status = ?" => 'NEW')
  1500. );
  1501. $this->assertEquals(1, $rowsAffected);
  1502. $value = $this->_db->fetchOne("SELECT $bug_status FROM $bugs WHERE $bug_id = 1");
  1503. $this->assertEquals('ARRAY', $value);
  1504. }
  1505. public function testAdapterUpdateWhereDbExpr()
  1506. {
  1507. $bugs = $this->_db->quoteIdentifier('zfbugs');
  1508. $bug_id = $this->_db->quoteIdentifier('bug_id');
  1509. $bug_status = $this->_db->quoteIdentifier('bug_status');
  1510. $whereExpr = new Zend_Db_Expr("$bug_id = 1");
  1511. $rowsAffected = $this->_db->update(
  1512. 'zfbugs',
  1513. array('bug_status' => 'DBEXPR'),
  1514. $whereExpr
  1515. );
  1516. $this->assertEquals(1, $rowsAffected);
  1517. $value = $this->_db->fetchOne("SELECT $bug_status FROM $bugs WHERE $bug_id = 1");
  1518. $this->assertEquals('DBEXPR', $value);
  1519. }
  1520. public function testAdapterUpdateEmptyWhere()
  1521. {
  1522. $bugs = $this->_db->quoteIdentifier('zfbugs');
  1523. $bug_status = $this->_db->quoteIdentifier('bug_status');
  1524. $rowsAffected = $this->_db->update(
  1525. 'zfbugs',
  1526. array('bug_status' => 'EMPTY')
  1527. // intentionally no where clause
  1528. );
  1529. $this->assertEquals(4, $rowsAffected);
  1530. $value = $this->_db->fetchCol("SELECT $bug_status FROM $bugs");
  1531. $this->assertEquals(array_fill(0, 4, 'EMPTY'), $value);
  1532. }
  1533. protected function _testAdapterAlternateStatement($stmtClass)
  1534. {
  1535. $ip = get_include_path();
  1536. $dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '_files';
  1537. $newIp = $dir . PATH_SEPARATOR . $ip;
  1538. set_include_path($newIp);
  1539. $params = $this->_util->getParams();
  1540. $params['options'] = array(
  1541. Zend_Db::AUTO_QUOTE_IDENTIFIERS => false
  1542. );
  1543. $db = Zend_Db::factory($this->getDriver(), $params);
  1544. $db->getConnection();
  1545. $db->setStatementClass($stmtClass);
  1546. $currentStmtClass = $db->getStatementClass();
  1547. $this->assertEquals($stmtClass, $currentStmtClass);
  1548. $bugs = $this->_db->quoteIdentifier('zfbugs');
  1549. $stmt = $db->prepare("SELECT COUNT(*) FROM $bugs");
  1550. $this->assertType($stmtClass, $stmt,
  1551. 'Expecting object of type ' . $stmtClass . ', got ' . get_class($stmt));
  1552. }
  1553. public function testAdapterSetCustomProfiler()
  1554. {
  1555. $profilerOptions = array(
  1556. 'enabled' => true,
  1557. 'class' => 'stdClass',
  1558. 'instance' => new stdClass()
  1559. );
  1560. try {
  1561. $this->_db->setProfiler($profilerOptions);
  1562. $this->fail('Expected to catch Zend_Db_Profiler_Exception');
  1563. } catch (Zend_Exception $e) {
  1564. $this->assertType('Zend_Db_Profiler_Exception', $e);
  1565. $this->assertEquals('Class stdClass does not extend Zend_Db_Profiler', $e->getMessage());
  1566. }
  1567. }
  1568. /**
  1569. * @group ZF-5099
  1570. */
  1571. public function testAdapterGetServerVersion()
  1572. {
  1573. $version = $this->_db->getServerVersion();
  1574. $this->assertNotNull($version);
  1575. $this->assertTrue(version_compare($version, '1.0.0', '>'));
  1576. $this->assertTrue(version_compare($version, '99.0.0', '<'));
  1577. }
  1578. public function testAdapterGetConnection()
  1579. {
  1580. $this->_db->getConnection();
  1581. $this->assertTrue($this->_db->isConnected());
  1582. }
  1583. /**
  1584. * @group ZF-5050
  1585. */
  1586. public function testAdapterCloseConnection()
  1587. {
  1588. $this->_db->closeConnection();
  1589. $this->assertFalse($this->_db->isConnected());
  1590. // Double closing must be without any errors
  1591. $this->_db->closeConnection();
  1592. $this->assertFalse($this->_db->isConnected());
  1593. }
  1594. /**
  1595. * @group ZF-5146
  1596. */
  1597. public function testAdapterReadClobFetchAll()
  1598. {
  1599. $documents = $this->_db->quoteIdentifier('zfdocuments');
  1600. $document_id = $this->_db->quoteIdentifier('doc_id');
  1601. $value = $this->_db->fetchAll("SELECT * FROM $documents WHERE $document_id = 1");
  1602. $expected = 'this is the clob that never ends...'.
  1603. 'this is the clob that never ends...'.
  1604. 'this is the clob that never ends...';
  1605. $this->assertEquals($expected, $value[0]['doc_clob']);
  1606. }
  1607. /**
  1608. * @group ZF-5146
  1609. */
  1610. public function testAdapterReadClobFetchRow()
  1611. {
  1612. $documents = $this->_db->quoteIdentifier('zfdocuments');
  1613. $document_id = $this->_db->quoteIdentifier('doc_id');
  1614. $value = $this->_db->fetchRow("SELECT * FROM $documents WHERE $document_id = 1");
  1615. $expected = 'this is the clob that never ends...'.
  1616. 'this is the clob that never ends...'.
  1617. 'this is the clob that never ends...';
  1618. $this->assertEquals($expected, $value['doc_clob']);
  1619. }
  1620. /**
  1621. * @group ZF-5146
  1622. */
  1623. public function testAdapterReadClobFetchAssoc()
  1624. {
  1625. $documents = $this->_db->quoteIdentifier('zfdocuments');
  1626. $document_id = $this->_db->quoteIdentifier('doc_id');
  1627. $value = $this->_db->fetchAssoc("SELECT * FROM $documents WHERE $document_id = 1");
  1628. $expected = 'this is the clob that never ends...'.
  1629. 'this is the clob that never ends...'.
  1630. 'this is the clob that never ends...';
  1631. $this->assertEquals($expected, $value[1]['doc_clob']);
  1632. }
  1633. /**
  1634. * @group ZF-5146
  1635. */
  1636. public function testAdapterReadClobFetchCol()
  1637. {
  1638. $documents = $this->_db->quoteIdentifier('zfdocuments');
  1639. $document_id = $this->_db->quoteIdentifier('doc_id');
  1640. $document_clob = $this->_db->quoteIdentifier('doc_clob');
  1641. $value = $this->_db->fetchCol("SELECT $document_clob FROM $documents WHERE $document_id = 1");
  1642. $expected = 'this is the clob that never ends...'.
  1643. 'this is the clob that never ends...'.
  1644. 'this is the clob that never ends...';
  1645. $this->assertEquals($expected, $value[0]);
  1646. }
  1647. /**
  1648. * @group ZF-5146
  1649. */
  1650. public function testAdapterReadClobFetchOne()
  1651. {
  1652. $documents = $this->_db->quoteIdentifier('zfdocuments');
  1653. $document_id = $this->_db->quoteIdentifier('doc_id');
  1654. $document_clob = $this->_db->quoteIdentifier('doc_clob');
  1655. $value = $this->_db->fetchOne("SELECT $document_clob FROM $documents WHERE $document_id = 1");
  1656. $expected = 'this is the clob that never ends...'.
  1657. 'this is the clob that never ends...'.
  1658. 'this is the clob that never ends...';
  1659. $this->assertEquals($expected, $value);
  1660. }
  1661. public function testAdapterSerializationIsOkByDefault()
  1662. {
  1663. $serialized = serialize($this->_db);
  1664. $this->assertType('string', $serialized);
  1665. $this->assertThat(unserialize($serialized), new PHPUnit_Framework_Constraint_IsInstanceOf('Zend_Db_Adapter_Abstract'));
  1666. }
  1667. public function testAdapterSerializationFailsWhenNotAllowedToBeSerialized()
  1668. {
  1669. $params = $this->_util->getParams();
  1670. $params['options'] = array(
  1671. Zend_Db::ALLOW_SERIALIZATION => false
  1672. );
  1673. $db = Zend_Db::factory($this->getDriver(), $params);
  1674. $this->setExpectedException('Zend_Db_Adapter_Exception');
  1675. $serialized = serialize($db);
  1676. }
  1677. public function testAdapterUnSerializationAutoReconnection()
  1678. {
  1679. $serialized = serialize($this->_db);
  1680. $db = unserialize($serialized);
  1681. $this->assertFalse($db->isConnected());
  1682. $params = $this->_util->getParams();
  1683. $params['options'] = array(
  1684. Zend_Db::AUTO_RECONNECT_ON_UNSERIALIZE => true
  1685. );
  1686. $db = Zend_Db::factory($this->getDriver(), $params);
  1687. $db = unserialize(serialize($db));
  1688. $this->assertTrue($db->isConnected());
  1689. }
  1690. /**
  1691. * @group ZF-1541
  1692. */
  1693. public function testCharacterSetUtf8()
  1694. {
  1695. // Create a new adapter
  1696. $params = $this->_util->getParams();
  1697. $params['charset'] = 'utf8';
  1698. $db = Zend_Db::factory($this->getDriver(), $params);
  1699. // create a new util object, with the new db adapter
  1700. $driver = $this->getDriver();
  1701. $utilClass = "Zend_Db_TestUtil_{$driver}";
  1702. $util = new $utilClass();
  1703. $util->setAdapter($db);
  1704. // create test table using no identifier quoting
  1705. $util->createTable('charsetutf8', array(
  1706. 'id' => 'IDENTITY',
  1707. 'stuff' => 'VARCHAR(32)'
  1708. ));
  1709. $tableName = $this->_util->getTableName('charsetutf8');
  1710. // insert into the table
  1711. $numRows = $db->insert($tableName, array(
  1712. 'id' => 1,
  1713. 'stuff' => 'äöüß'
  1714. ));
  1715. // check if the row was inserted as expected
  1716. $select = $db->select()->from($tableName, array('id', 'stuff'));
  1717. $stmt = $db->query($select);
  1718. $fetched = $stmt->fetchAll(Zend_Db::FETCH_NUM);
  1719. $a = array(
  1720. 0 => array(0 => 1, 1 => 'äöüß')
  1721. );
  1722. $this->assertEquals($a, $fetched,
  1723. 'result of query not as expected');
  1724. // clean up
  1725. unset($stmt);
  1726. $util->dropTable($tableName);
  1727. }
  1728. /**
  1729. * @group ZF-7737
  1730. */
  1731. public function testQuoteIntoReplacesPlaceholderAtFirstCharacterWhenCountIsNotNull()
  1732. {
  1733. $quotedString = $this->_db->quoteInto('? = bar', 'foo', NULL, 1);
  1734. $this->assertEquals("'foo' = bar", $quotedString);
  1735. }
  1736. /**
  1737. * @group ZF-8399
  1738. */
  1739. public function testLongQueryWithTextField()
  1740. {
  1741. // create test table using no identifier quoting
  1742. $this->_util->createTable('zf_longquery', array(
  1743. 'id' => 'INTEGER NOT NULL',
  1744. 'stuff' => 'TEXT NOT NULL'
  1745. ));
  1746. $tableName = $this->_util->getTableName('zf_longquery');
  1747. // insert into the table
  1748. $longValue = str_repeat('x', 4000);
  1749. $numRows = $this->_db->insert($tableName, array(
  1750. 'id' => 1,
  1751. 'stuff' => $longValue
  1752. ));
  1753. $quotedTableName = $this->_db->quoteIdentifier('zf_longquery');
  1754. $sql = "INSERT INTO $quotedTableName VALUES (2, '$longValue')";
  1755. $this->_db->query($sql);
  1756. // check if the row was inserted as expected
  1757. $select = $this->_db->select()->from($tableName, array('id', 'stuff'));
  1758. $stmt = $this->_db->query($select);
  1759. $fetched = $stmt->fetchAll(Zend_Db::FETCH_NUM);
  1760. $a = array(
  1761. 0 => array(0 => 1, 1 => $longValue),
  1762. 1 => array(0 => 2, 1 => $longValue)
  1763. );
  1764. $this->assertEquals($a, $fetched,
  1765. 'result of query not as expected');
  1766. // clean up
  1767. unset($stmt);
  1768. $this->_util->dropTable($tableName);
  1769. }
  1770. }