TestCommon.php 72 KB

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