TestCommon.php 75 KB

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