TestCommon.php 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124
  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-2014 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-2014 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' => 'IDENTITY',
  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. }