TestCommon.php 76 KB

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