2
0

TestCommon.php 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714
  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-2012 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. * @category Zend
  28. * @package Zend_Db
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. abstract class Zend_Db_Select_TestCommon extends Zend_Db_TestSetup
  34. {
  35. /**
  36. * Test basic use of the Zend_Db_Select class.
  37. *
  38. * @return Zend_Db_Select
  39. */
  40. protected function _select()
  41. {
  42. $select = $this->_db->select();
  43. $select->from('zfproducts');
  44. return $select;
  45. }
  46. public function testSelect()
  47. {
  48. $select = $this->_select();
  49. $this->assertType('Zend_Db_Select', $select,
  50. 'Expecting object of type Zend_Db_Select, got '.get_class($select));
  51. $stmt = $this->_db->query($select);
  52. $row = $stmt->fetch();
  53. $stmt->closeCursor();
  54. $this->assertEquals(2, count($row)); // correct number of fields
  55. $this->assertEquals(1, $row['product_id']); // correct data
  56. }
  57. public function testSelectToString()
  58. {
  59. $select = $this->_select();
  60. $this->assertEquals($select->__toString(), $select->assemble()); // correct data
  61. }
  62. /**
  63. * Test basic use of the Zend_Db_Select class.
  64. */
  65. public function testSelectQuery()
  66. {
  67. $select = $this->_select();
  68. $this->assertType('Zend_Db_Select', $select,
  69. 'Expecting object of type Zend_Db_Select, got '.get_class($select));
  70. $stmt = $select->query();
  71. $row = $stmt->fetch();
  72. $stmt->closeCursor();
  73. $this->assertEquals(2, count($row)); // correct number of fields
  74. $this->assertEquals(1, $row['product_id']); // correct data
  75. }
  76. /**
  77. * ZF-2017: Test bind use of the Zend_Db_Select class.
  78. * @group ZF-2017
  79. */
  80. public function testSelectQueryWithBinds()
  81. {
  82. $product_id = $this->_db->quoteIdentifier('product_id');
  83. $select = $this->_select()->where("$product_id = :product_id")
  84. ->bind(array(':product_id' => 1));
  85. $this->assertType('Zend_Db_Select', $select,
  86. 'Expecting object of type Zend_Db_Select, got '.get_class($select));
  87. $stmt = $select->query();
  88. $row = $stmt->fetch();
  89. $stmt->closeCursor();
  90. $this->assertEquals(2, count($row)); // correct number of fields
  91. $this->assertEquals(1, $row['product_id']); // correct data
  92. }
  93. /**
  94. * Test Zend_Db_Select specifying columns
  95. */
  96. protected function _selectColumnsScalar()
  97. {
  98. $select = $this->_db->select()
  99. ->from('zfproducts', 'product_name'); // scalar
  100. return $select;
  101. }
  102. public function testSelectColumnsScalar()
  103. {
  104. $select = $this->_selectColumnsScalar();
  105. $stmt = $this->_db->query($select);
  106. $result = $stmt->fetchAll();
  107. $this->assertEquals(3, count($result), 'Expected count of result set to be 2');
  108. $this->assertEquals(1, count($result[0]), 'Expected column count of result set to be 1');
  109. $this->assertThat($result[0], $this->arrayHasKey('product_name'));
  110. }
  111. protected function _selectColumnsArray()
  112. {
  113. $select = $this->_db->select()
  114. ->from('zfproducts', array('product_id', 'product_name')); // array
  115. return $select;
  116. }
  117. public function testSelectColumnsArray()
  118. {
  119. $select = $this->_selectColumnsArray();
  120. $stmt = $this->_db->query($select);
  121. $result = $stmt->fetchAll();
  122. $this->assertEquals(3, count($result), 'Expected count of result set to be 2');
  123. $this->assertEquals(2, count($result[0]), 'Expected column count of result set to be 2');
  124. $this->assertThat($result[0], $this->arrayHasKey('product_id'));
  125. $this->assertThat($result[0], $this->arrayHasKey('product_name'));
  126. }
  127. /**
  128. * Test support for column aliases.
  129. * e.g. from('table', array('alias' => 'col1')).
  130. */
  131. protected function _selectColumnsAliases()
  132. {
  133. $select = $this->_db->select()
  134. ->from('zfproducts', array('alias' => 'product_name'));
  135. return $select;
  136. }
  137. public function testSelectColumnsAliases()
  138. {
  139. $select = $this->_selectColumnsAliases();
  140. $stmt = $this->_db->query($select);
  141. $result = $stmt->fetchAll();
  142. $this->assertEquals(3, count($result), 'Expected count of result set to be 2');
  143. $this->assertThat($result[0], $this->arrayHasKey('alias'));
  144. $this->assertThat($result[0], $this->logicalNot($this->arrayHasKey('product_name')));
  145. }
  146. /**
  147. * Test syntax to support qualified column names,
  148. * e.g. from('table', array('table.col1', 'table.col2')).
  149. */
  150. protected function _selectColumnsQualified()
  151. {
  152. $select = $this->_db->select()
  153. ->from('zfproducts', "zfproducts.product_name");
  154. return $select;
  155. }
  156. public function testSelectColumnsQualified()
  157. {
  158. $select = $this->_selectColumnsQualified();
  159. $stmt = $this->_db->query($select);
  160. $result = $stmt->fetchAll();
  161. $this->assertThat($result[0], $this->arrayHasKey('product_name'));
  162. }
  163. /**
  164. * Test support for columns defined by Zend_Db_Expr.
  165. */
  166. protected function _selectColumnsExpr()
  167. {
  168. $products = $this->_db->quoteIdentifier('zfproducts');
  169. $product_name = $this->_db->quoteIdentifier('product_name');
  170. $select = $this->_db->select()
  171. ->from('zfproducts', new Zend_Db_Expr($products.'.'.$product_name));
  172. return $select;
  173. }
  174. public function testSelectColumnsExpr()
  175. {
  176. $select = $this->_selectColumnsExpr();
  177. $stmt = $this->_db->query($select);
  178. $result = $stmt->fetchAll();
  179. $this->assertThat($result[0], $this->arrayHasKey('product_name'));
  180. }
  181. /**
  182. * Test support for automatic conversion of SQL functions to
  183. * Zend_Db_Expr, e.g. from('table', array('COUNT(*)'))
  184. * should generate the same result as
  185. * from('table', array(new Zend_Db_Expr('COUNT(*)')))
  186. */
  187. protected function _selectColumnsAutoExpr()
  188. {
  189. $select = $this->_db->select()
  190. ->from('zfproducts', array('count' => 'COUNT(*)'));
  191. return $select;
  192. }
  193. public function testSelectColumnsAutoExpr()
  194. {
  195. $select = $this->_selectColumnsAutoExpr();
  196. $stmt = $this->_db->query($select);
  197. $result = $stmt->fetchAll();
  198. $this->assertThat($result[0], $this->arrayHasKey('count'));
  199. $this->assertEquals(3, $result[0]['count']);
  200. }
  201. /**
  202. * Test adding the DISTINCT query modifier to a Zend_Db_Select object.
  203. */
  204. protected function _selectDistinctModifier()
  205. {
  206. $select = $this->_db->select()
  207. ->distinct()
  208. ->from('zfproducts', new Zend_Db_Expr(327));
  209. return $select;
  210. }
  211. public function testSelectDistinctModifier()
  212. {
  213. $select = $this->_selectDistinctModifier();
  214. $stmt = $this->_db->query($select);
  215. $result = $stmt->fetchAll();
  216. $this->assertEquals(1, count($result));
  217. }
  218. /**
  219. * Test adding the FOR UPDATE query modifier to a Zend_Db_Select object.
  220. *
  221. public function testSelectForUpdateModifier()
  222. {
  223. }
  224. */
  225. /**
  226. * Test support for schema-qualified table names in from()
  227. * e.g. from('schema.table').
  228. */
  229. protected function _selectFromQualified()
  230. {
  231. $schema = $this->_util->getSchema();
  232. $select = $this->_db->select()
  233. ->from("$schema.zfproducts");
  234. return $select;
  235. }
  236. public function testSelectFromQualified()
  237. {
  238. $select = $this->_selectFromQualified();
  239. $stmt = $this->_db->query($select);
  240. $result = $stmt->fetchAll();
  241. $this->assertEquals(3, count($result));
  242. }
  243. /**
  244. * Test support for nested select in from()
  245. */
  246. protected function _selectFromSelectObject()
  247. {
  248. $subquery = $this->_db->select()
  249. ->from('subqueryTable');
  250. $select = $this->_db->select()
  251. ->from($subquery);
  252. return $select;
  253. }
  254. public function testSelectFromSelectObject()
  255. {
  256. $select = $this->_selectFromSelectObject();
  257. $query = $select->assemble();
  258. $cmp = 'SELECT ' . $this->_db->quoteIdentifier('t') . '.* FROM (SELECT '
  259. . $this->_db->quoteIdentifier('subqueryTable') . '.* FROM '
  260. . $this->_db->quoteIdentifier('subqueryTable') . ') AS '
  261. . $this->_db->quoteIdentifier('t');
  262. $this->assertEquals($query, $cmp);
  263. }
  264. /**
  265. * Test support for nested select in from()
  266. */
  267. protected function _selectColumnsReset()
  268. {
  269. $select = $this->_db->select()
  270. ->from(array('p' => 'zfproducts'), array('product_id', 'product_name'));
  271. return $select;
  272. }
  273. public function testSelectColumnsReset()
  274. {
  275. $select = $this->_selectColumnsReset()
  276. ->reset(Zend_Db_Select::COLUMNS)
  277. ->columns('product_name');
  278. $stmt = $this->_db->query($select);
  279. $result = $stmt->fetchAll();
  280. $this->assertContains('product_name', array_keys($result[0]));
  281. $this->assertNotContains('product_id', array_keys($result[0]));
  282. $select = $this->_selectColumnsReset()
  283. ->reset(Zend_Db_Select::COLUMNS)
  284. ->columns('p.product_name');
  285. $stmt = $this->_db->query($select);
  286. $result = $stmt->fetchAll();
  287. $this->assertContains('product_name', array_keys($result[0]));
  288. $this->assertNotContains('product_id', array_keys($result[0]));
  289. $select = $this->_selectColumnsReset()
  290. ->reset(Zend_Db_Select::COLUMNS)
  291. ->columns('product_name', 'p');
  292. $stmt = $this->_db->query($select);
  293. $result = $stmt->fetchAll();
  294. $this->assertContains('product_name', array_keys($result[0]));
  295. $this->assertNotContains('product_id', array_keys($result[0]));
  296. }
  297. public function testSelectColumnsResetBeforeFrom()
  298. {
  299. $select = $this->_selectColumnsReset();
  300. try {
  301. $select->reset(Zend_Db_Select::COLUMNS)
  302. ->reset(Zend_Db_Select::FROM)
  303. ->columns('product_id');
  304. $this->fail('Expected exception of type "Zend_Db_Select_Exception"');
  305. } catch (Zend_Exception $e) {
  306. $this->assertType('Zend_Db_Select_Exception', $e,
  307. 'Expected exception of type "Zend_Db_Select_Exception", got ' . get_class($e));
  308. $this->assertEquals("No table has been specified for the FROM clause", $e->getMessage());
  309. }
  310. }
  311. protected function _selectColumnWithColonQuotedParameter()
  312. {
  313. $product_id = $this->_db->quoteIdentifier('product_id');
  314. $select = $this->_db->select()
  315. ->from('zfproducts')
  316. ->where($product_id . ' = ?', "as'as:x");
  317. return $select;
  318. }
  319. public function testSelectColumnWithColonQuotedParameter()
  320. {
  321. $stmt = $select = $this->_selectColumnWithColonQuotedParameter()
  322. ->query();
  323. $result = $stmt->fetchAll();
  324. $this->assertEquals(0, count($result));
  325. }
  326. /**
  327. * Test support for FOR UPDATE
  328. * e.g. from('schema.table').
  329. */
  330. public function testSelectFromForUpdate()
  331. {
  332. $select = $this->_db->select()
  333. ->from("zfproducts")
  334. ->forUpdate();
  335. $stmt = $this->_db->query($select);
  336. $result = $stmt->fetchAll();
  337. $this->assertEquals(3, count($result));
  338. }
  339. /**
  340. * Test adding a JOIN to a Zend_Db_Select object.
  341. */
  342. protected function _selectJoin()
  343. {
  344. $products = $this->_db->quoteIdentifier('zfproducts');
  345. $product_id = $this->_db->quoteIdentifier('product_id');
  346. $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
  347. $select = $this->_db->select()
  348. ->from('zfproducts')
  349. ->join('zfbugs_products', "$products.$product_id = $bugs_products.$product_id");
  350. return $select;
  351. }
  352. public function testSelectJoin()
  353. {
  354. $select = $this->_selectJoin();
  355. $stmt = $this->_db->query($select);
  356. $result = $stmt->fetchAll();
  357. $this->assertEquals(6, count($result));
  358. $this->assertEquals(3, count($result[0]));
  359. }
  360. /**
  361. * Test adding an INNER JOIN to a Zend_Db_Select object.
  362. * This should be exactly the same as the plain JOIN clause.
  363. */
  364. protected function _selectJoinWithCorrelationName()
  365. {
  366. $product_id = $this->_db->quoteIdentifier('product_id');
  367. $xyz1 = $this->_db->quoteIdentifier('xyz1');
  368. $xyz2 = $this->_db->quoteIdentifier('xyz2');
  369. $select = $this->_db->select()
  370. ->from( array('xyz1' => 'zfproducts') )
  371. ->join( array('xyz2' => 'zfbugs_products'), "$xyz1.$product_id = $xyz2.$product_id")
  372. ->where("$xyz1.$product_id = 1");
  373. return $select;
  374. }
  375. public function testSelectJoinWithCorrelationName()
  376. {
  377. $select = $this->_selectJoinWithCorrelationName();
  378. $stmt = $this->_db->query($select);
  379. $result = $stmt->fetchAll();
  380. $this->assertEquals(1, count($result));
  381. $this->assertEquals(3, count($result[0]));
  382. }
  383. /**
  384. * Test adding an INNER JOIN to a Zend_Db_Select object.
  385. * This should be exactly the same as the plain JOIN clause.
  386. */
  387. protected function _selectJoinInner()
  388. {
  389. $products = $this->_db->quoteIdentifier('zfproducts');
  390. $product_id = $this->_db->quoteIdentifier('product_id');
  391. $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
  392. $select = $this->_db->select()
  393. ->from('zfproducts')
  394. ->joinInner('zfbugs_products', "$products.$product_id = $bugs_products.$product_id");
  395. return $select;
  396. }
  397. public function testSelectJoinInner()
  398. {
  399. $select = $this->_selectJoinInner();
  400. $stmt = $this->_db->query($select);
  401. $result = $stmt->fetchAll();
  402. $this->assertEquals(6, count($result));
  403. $this->assertEquals(3, count($result[0]));
  404. }
  405. /**
  406. * Test adding a JOIN to a Zend_Db_Select object.
  407. */
  408. protected function _selectJoinWithNocolumns()
  409. {
  410. $products = $this->_db->quoteIdentifier('zfproducts');
  411. $bug_id = $this->_db->quoteIdentifier('bug_id');
  412. $product_id = $this->_db->quoteIdentifier('product_id');
  413. $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
  414. $bugs = $this->_db->quoteIdentifier('zfbugs');
  415. $select = $this->_db->select()
  416. ->from('zfproducts')
  417. ->join('zfbugs', "$bugs.$bug_id = 1", array())
  418. ->join('zfbugs_products', "$products.$product_id = $bugs_products.$product_id AND $bugs_products.$bug_id = $bugs.$bug_id", null);
  419. return $select;
  420. }
  421. public function testSelectJoinWithNocolumns()
  422. {
  423. $select = $this->_selectJoinWithNocolumns();
  424. $stmt = $this->_db->query($select);
  425. $result = $stmt->fetchAll();
  426. $this->assertEquals(3, count($result));
  427. $this->assertEquals(2, count($result[0]));
  428. }
  429. /**
  430. * Test adding an outer join to a Zend_Db_Select object.
  431. */
  432. protected function _selectJoinLeft()
  433. {
  434. $bugs = $this->_db->quoteIdentifier('zfbugs');
  435. $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
  436. $bug_id = $this->_db->quoteIdentifier('bug_id');
  437. $select = $this->_db->select()
  438. ->from('zfbugs')
  439. ->joinLeft('zfbugs_products', "$bugs.$bug_id = $bugs_products.$bug_id");
  440. return $select;
  441. }
  442. public function testSelectJoinLeft()
  443. {
  444. $select = $this->_selectJoinLeft();
  445. $stmt = $this->_db->query($select);
  446. $result = $stmt->fetchAll();
  447. $this->assertEquals(7, count($result));
  448. $this->assertEquals(9, count($result[0]));
  449. $this->assertEquals(3, $result[3]['product_id']);
  450. $this->assertNull($result[6]['product_id']);
  451. }
  452. /**
  453. * Returns a select object that uses table aliases and specifies a mixed ordering of columns,
  454. * for testing whether the user-specified ordering is preserved.
  455. *
  456. * @return Zend_Db_Select
  457. */
  458. protected function _selectJoinLeftTableAliasesColumnOrderPreserve()
  459. {
  460. $bugsBugId = $this->_db->quoteIdentifier('b.bug_id');
  461. $bugsProductBugId = $this->_db->quoteIdentifier('bp.bug_id');
  462. $select = $this->_db->select()
  463. ->from(array('b' => 'zfbugs'), array('b.bug_id', 'bp.product_id', 'b.bug_description'))
  464. ->joinLeft(array('bp' => 'zfbugs_products'), "$bugsBugId = $bugsProductBugId", array());
  465. return $select;
  466. }
  467. /**
  468. * Ensures that when table aliases are used with a mixed ordering of columns, the user-specified
  469. * column ordering is preserved.
  470. *
  471. * @return void
  472. */
  473. public function testJoinLeftTableAliasesColumnOrderPreserve()
  474. {
  475. $select = $this->_selectJoinLeftTableAliasesColumnOrderPreserve();
  476. $this->assertRegExp('/^.*b.*bug_id.*,.*bp.*product_id.*,.*b.*bug_description.*$/s', $select->assemble());
  477. }
  478. /**
  479. * Test adding an outer join to a Zend_Db_Select object.
  480. */
  481. protected function _selectJoinRight()
  482. {
  483. $bugs = $this->_db->quoteIdentifier('zfbugs');
  484. $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
  485. $bug_id = $this->_db->quoteIdentifier('bug_id');
  486. $select = $this->_db->select()
  487. ->from('zfbugs_products')
  488. ->joinRight('zfbugs', "$bugs_products.$bug_id = $bugs.$bug_id");
  489. return $select;
  490. }
  491. public function testSelectJoinRight()
  492. {
  493. $select = $this->_selectJoinRight();
  494. $stmt = $this->_db->query($select);
  495. $result = $stmt->fetchAll();
  496. $this->assertEquals(7, count($result));
  497. $this->assertEquals(9, count($result[0]));
  498. $this->assertEquals(3, $result[3]['product_id']);
  499. $this->assertNull($result[6]['product_id']);
  500. }
  501. /**
  502. * Test adding a cross join to a Zend_Db_Select object.
  503. */
  504. protected function _selectJoinCross()
  505. {
  506. $select = $this->_db->select()
  507. ->from('zfproducts')
  508. ->joinCross('zfbugs_products');
  509. return $select;
  510. }
  511. public function testSelectJoinCross()
  512. {
  513. $select = $this->_selectJoinCross();
  514. $stmt = $this->_db->query($select);
  515. $result = $stmt->fetchAll();
  516. $this->assertEquals(18, count($result));
  517. $this->assertEquals(3, count($result[0]));
  518. }
  519. /**
  520. * Test support for schema-qualified table names in join(),
  521. * e.g. join('schema.table', 'condition')
  522. */
  523. protected function _selectJoinQualified()
  524. {
  525. $products = $this->_db->quoteIdentifier('zfproducts');
  526. $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
  527. $product_id = $this->_db->quoteIdentifier('product_id');
  528. $schema = $this->_util->getSchema();
  529. $select = $this->_db->select()
  530. ->from('zfproducts')
  531. ->join("$schema.zfbugs_products", "$products.$product_id = $bugs_products.$product_id");
  532. return $select;
  533. }
  534. public function testSelectJoinQualified()
  535. {
  536. $select = $this->_selectJoinQualified();
  537. $stmt = $this->_db->query($select);
  538. $result = $stmt->fetchAll();
  539. $this->assertEquals(6, count($result));
  540. $this->assertEquals(3, count($result[0]));
  541. }
  542. protected function _selectJoinUsing()
  543. {
  544. $products = $this->_db->quoteIdentifier('zfproducts');
  545. $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
  546. $product_id = $this->_db->quoteIdentifier('product_id');
  547. $select = $this->_db->select()
  548. ->from('zfproducts')
  549. ->joinUsing("zfbugs_products", "$product_id")
  550. ->where("$bugs_products.$product_id < ?", 3);
  551. return $select;
  552. }
  553. public function testSelectMagicMethod()
  554. {
  555. $select = $this->_selectJoinUsing();
  556. try {
  557. $select->foo();
  558. $this->fail('Expected exception of type "Zend_Db_Select_Exception"');
  559. } catch (Zend_Exception $e) {
  560. $this->assertType('Zend_Db_Select_Exception', $e,
  561. 'Expected exception of type "Zend_Db_Select_Exception", got ' . get_class($e));
  562. $this->assertEquals("Unrecognized method 'foo()'", $e->getMessage());
  563. }
  564. }
  565. public function testSelectJoinUsing()
  566. {
  567. $select = $this->_selectJoinUsing();
  568. $sql = preg_replace('/\\s+/', ' ', $select->assemble());
  569. $stmt = $this->_db->query($select);
  570. $result = $stmt->fetchAll();
  571. $this->assertEquals(3, count($result));
  572. $this->assertEquals(1, $result[0]['product_id']);
  573. }
  574. protected function _selectJoinInnerUsing()
  575. {
  576. $products = $this->_db->quoteIdentifier('zfproducts');
  577. $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
  578. $product_id = $this->_db->quoteIdentifier('product_id');
  579. $select = $this->_db->select()
  580. ->from('zfproducts')
  581. ->joinInnerUsing("zfbugs_products", "$product_id")
  582. ->where("$bugs_products.$product_id < ?", 3);
  583. return $select;
  584. }
  585. public function testSelectJoinInnerUsing()
  586. {
  587. $select = $this->_selectJoinInnerUsing();
  588. $sql = preg_replace('/\\s+/', ' ', $select->assemble());
  589. $stmt = $this->_db->query($select);
  590. $result = $stmt->fetchAll();
  591. $this->assertEquals(3, count($result));
  592. $this->assertEquals(1, $result[0]['product_id']);
  593. }
  594. public function testSelectJoinInnerUsingException()
  595. {
  596. $select = $this->_selectJoinInnerUsing();
  597. try {
  598. $select->joinFooUsing();
  599. $this->fail('Expected exception of type "Zend_Db_Select_Exception"');
  600. } catch (Zend_Exception $e) {
  601. $this->assertType('Zend_Db_Select_Exception', $e,
  602. 'Expected exception of type "Zend_Db_Select_Exception", got ' . get_class($e));
  603. $this->assertEquals("Unrecognized method 'joinFooUsing()'", $e->getMessage());
  604. }
  605. }
  606. protected function _selectJoinCrossUsing()
  607. {
  608. $products = $this->_db->quoteIdentifier('zfproducts');
  609. $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
  610. $product_id = $this->_db->quoteIdentifier('product_id');
  611. $select = $this->_db->select()
  612. ->from('zfproducts')
  613. ->where("$bugs_products.$product_id < ?", 3);
  614. return $select;
  615. }
  616. public function testSelectJoinCrossUsing()
  617. {
  618. $product_id = $this->_db->quoteIdentifier('product_id');
  619. $select = $this->_selectJoinCrossUsing();
  620. try {
  621. $select->joinCrossUsing("zfbugs_products", "$product_id");
  622. $this->fail('Expected exception of type "Zend_Db_Select_Exception"');
  623. } catch (Zend_Exception $e) {
  624. $this->assertType('Zend_Db_Select_Exception', $e,
  625. 'Expected exception of type "Zend_Db_Select_Exception", got ' . get_class($e));
  626. $this->assertEquals("Cannot perform a joinUsing with method 'joinCrossUsing()'", $e->getMessage());
  627. }
  628. }
  629. /**
  630. * Test adding a WHERE clause to a Zend_Db_Select object.
  631. */
  632. protected function _selectWhere()
  633. {
  634. $product_id = $this->_db->quoteIdentifier('product_id');
  635. $select = $this->_db->select()
  636. ->from('zfproducts')
  637. ->where("$product_id = 2");
  638. return $select;
  639. }
  640. public function testSelectWhere()
  641. {
  642. $select = $this->_selectWhere();
  643. $stmt = $this->_db->query($select);
  644. $result = $stmt->fetchAll();
  645. $this->assertEquals(1, count($result));
  646. $this->assertEquals(2, $result[0]['product_id']);
  647. }
  648. /**
  649. * Test support for nested select in from()
  650. */
  651. protected function _selectWhereSelectObject()
  652. {
  653. $subquery = $this->_db->select()
  654. ->from('subqueryTable');
  655. $select = $this->_db->select()
  656. ->from('table')
  657. ->where('foo IN ?', $subquery);
  658. return $select;
  659. }
  660. public function testSelectWhereSelectObject()
  661. {
  662. $select = $this->_selectWhereSelectObject();
  663. $query = $select->assemble();
  664. $cmp = 'SELECT ' . $this->_db->quoteIdentifier('table') . '.* FROM '
  665. . $this->_db->quoteIdentifier('table') . ' WHERE (foo IN (SELECT '
  666. . $this->_db->quoteIdentifier('subqueryTable') . '.* FROM '
  667. . $this->_db->quoteIdentifier('subqueryTable') . '))';
  668. $this->assertEquals($query, $cmp);
  669. }
  670. protected function _selectWhereArray()
  671. {
  672. $product_id = $this->_db->quoteIdentifier('product_id');
  673. $select = $this->_db->select()
  674. ->from('zfproducts')
  675. ->where("$product_id IN (?)", array(1, 2, 3));
  676. return $select;
  677. }
  678. public function testSelectWhereArray()
  679. {
  680. $select = $this->_selectWhereArray();
  681. $stmt = $this->_db->query($select);
  682. $result = $stmt->fetchAll();
  683. $this->assertEquals(3, count($result));
  684. }
  685. /**
  686. * test adding more WHERE conditions,
  687. * which should be combined with AND by default.
  688. */
  689. protected function _selectWhereAnd()
  690. {
  691. $product_id = $this->_db->quoteIdentifier('product_id');
  692. $select = $this->_db->select()
  693. ->from('zfproducts')
  694. ->where("$product_id = 2")
  695. ->where("$product_id = 1");
  696. return $select;
  697. }
  698. public function testSelectWhereAnd()
  699. {
  700. $select = $this->_selectWhereAnd();
  701. $stmt = $this->_db->query($select);
  702. $result = $stmt->fetchAll();
  703. $this->assertEquals(0, count($result));
  704. }
  705. /**
  706. * Test support for where() with a parameter,
  707. * e.g. where('id = ?', 1).
  708. */
  709. protected function _selectWhereWithParameter()
  710. {
  711. $product_id = $this->_db->quoteIdentifier('product_id');
  712. $select = $this->_db->select()
  713. ->from('zfproducts')
  714. ->where("$product_id = ?", 2);
  715. return $select;
  716. }
  717. public function testSelectWhereWithParameter()
  718. {
  719. $select = $this->_selectWhereWithParameter();
  720. $stmt = $this->_db->query($select);
  721. $result = $stmt->fetchAll();
  722. $this->assertEquals(1, count($result));
  723. $this->assertEquals(2, $result[0]['product_id']);
  724. }
  725. /**
  726. * Test support for where() with a specified type,
  727. * e.g. where('id = ?', 1, 'int').
  728. */
  729. protected function _selectWhereWithType()
  730. {
  731. $product_id = $this->_db->quoteIdentifier('product_id');
  732. $select = $this->_db->select()
  733. ->from('zfproducts')
  734. ->where("$product_id = ?", 2, 'int');
  735. return $select;
  736. }
  737. public function testSelectWhereWithType()
  738. {
  739. $select = $this->_selectWhereWithType();
  740. $stmt = $this->_db->query($select);
  741. $result = $stmt->fetchAll();
  742. $this->assertEquals(1, count($result));
  743. $this->assertEquals(2, $result[0]['product_id']);
  744. }
  745. /**
  746. * Test support for where() with a specified type,
  747. * e.g. where('id = ?', 1, 'int').
  748. */
  749. protected function _selectWhereWithTypeFloat()
  750. {
  751. $price_total = $this->_db->quoteIdentifier('price_total');
  752. $select = $this->_db->select()
  753. ->from('zfprice')
  754. ->where("$price_total = ?", 200.45, Zend_Db::FLOAT_TYPE);
  755. return $select;
  756. }
  757. public function testSelectWhereWithTypeFloat()
  758. {
  759. $locale = setlocale(LC_ALL, null);
  760. $select = $this->_selectWhereWithTypeFloat();
  761. $stmt = $this->_db->query($select);
  762. $result = $stmt->fetchAll();
  763. $this->assertEquals(1, count($result));
  764. $this->assertEquals(200.45, $result[0]['price_total']);
  765. try {
  766. setlocale(LC_ALL, 'fr_BE.UTF-8');
  767. $select = $this->_selectWhereWithTypeFloat();
  768. $stmt = $this->_db->query($select);
  769. $result = $stmt->fetchAll();
  770. $this->assertEquals(1, count($result));
  771. $this->assertEquals(200.45, $result[0]['price_total']);
  772. } catch (Zend_Exception $e) {
  773. setlocale(LC_ALL, $locale);
  774. throw $e;
  775. }
  776. setlocale(LC_ALL, $locale);
  777. }
  778. /**
  779. * Test adding an OR WHERE clause to a Zend_Db_Select object.
  780. */
  781. protected function _selectWhereOr()
  782. {
  783. $product_id = $this->_db->quoteIdentifier('product_id');
  784. $select = $this->_db->select()
  785. ->from('zfproducts')
  786. ->orWhere("$product_id = 1")
  787. ->orWhere("$product_id = 2");
  788. return $select;
  789. }
  790. public function testSelectWhereOr()
  791. {
  792. $select = $this->_selectWhereOr();
  793. $stmt = $this->_db->query($select);
  794. $result = $stmt->fetchAll();
  795. $this->assertEquals(2, count($result));
  796. $this->assertEquals(1, $result[0]['product_id']);
  797. $this->assertEquals(2, $result[1]['product_id']);
  798. }
  799. /**
  800. * Test support for where() with a parameter,
  801. * e.g. orWhere('id = ?', 2).
  802. */
  803. protected function _selectWhereOrWithParameter()
  804. {
  805. $product_id = $this->_db->quoteIdentifier('product_id');
  806. $select = $this->_db->select()
  807. ->from('zfproducts')
  808. ->orWhere("$product_id = ?", 1)
  809. ->orWhere("$product_id = ?", 2);
  810. return $select;
  811. }
  812. public function testSelectWhereOrWithParameter()
  813. {
  814. $select = $this->_selectWhereOrWithParameter();
  815. $stmt = $this->_db->query($select);
  816. $result = $stmt->fetchAll();
  817. $this->assertEquals(2, count($result));
  818. $this->assertEquals(1, $result[0]['product_id']);
  819. $this->assertEquals(2, $result[1]['product_id']);
  820. }
  821. /**
  822. * Test adding a GROUP BY clause to a Zend_Db_Select object.
  823. */
  824. protected function _selectGroupBy()
  825. {
  826. $thecount = $this->_db->quoteIdentifier('thecount');
  827. $select = $this->_db->select()
  828. ->from('zfbugs_products', array('bug_id', new Zend_Db_Expr("COUNT(*) AS $thecount")))
  829. ->group('bug_id')
  830. ->order('bug_id');
  831. return $select;
  832. }
  833. public function testSelectGroupBy()
  834. {
  835. $select = $this->_selectGroupBy();
  836. $stmt = $this->_db->query($select);
  837. $result = $stmt->fetchAll();
  838. $this->assertEquals(3, count($result),
  839. 'Expected count of first result set to be 2');
  840. $this->assertEquals(1, $result[0]['bug_id']);
  841. $this->assertEquals(3, $result[0]['thecount'],
  842. 'Expected count(*) of first result set to be 2');
  843. $this->assertEquals(2, $result[1]['bug_id']);
  844. $this->assertEquals(1, $result[1]['thecount']);
  845. }
  846. /**
  847. * Test support for qualified table in group(),
  848. * e.g. group('schema.table').
  849. */
  850. protected function _selectGroupByQualified()
  851. {
  852. $thecount = $this->_db->quoteIdentifier('thecount');
  853. $select = $this->_db->select()
  854. ->from('zfbugs_products', array('bug_id', new Zend_Db_Expr("COUNT(*) AS $thecount")))
  855. ->group("zfbugs_products.bug_id")
  856. ->order('bug_id');
  857. return $select;
  858. }
  859. public function testSelectGroupByQualified()
  860. {
  861. $select = $this->_selectGroupByQualified();
  862. $stmt = $this->_db->query($select);
  863. $result = $stmt->fetchAll();
  864. $this->assertEquals(3, count($result),
  865. 'Expected count of first result set to be 2');
  866. $this->assertEquals(1, $result[0]['bug_id']);
  867. $this->assertEquals(3, $result[0]['thecount'],
  868. 'Expected count(*) of first result set to be 2');
  869. $this->assertEquals(2, $result[1]['bug_id']);
  870. $this->assertEquals(1, $result[1]['thecount']);
  871. }
  872. /**
  873. * Test support for Zend_Db_Expr in group(),
  874. * e.g. group(new Zend_Db_Expr('id+1'))
  875. */
  876. protected function _selectGroupByExpr()
  877. {
  878. $thecount = $this->_db->quoteIdentifier('thecount');
  879. $bug_id = $this->_db->quoteIdentifier('bug_id');
  880. $select = $this->_db->select()
  881. ->from('zfbugs_products', array('bug_id'=>new Zend_Db_Expr("$bug_id+1"), new Zend_Db_Expr("COUNT(*) AS $thecount")))
  882. ->group(new Zend_Db_Expr("$bug_id+1"))
  883. ->order(new Zend_Db_Expr("$bug_id+1"));
  884. return $select;
  885. }
  886. public function testSelectGroupByExpr()
  887. {
  888. $select = $this->_selectGroupByExpr();
  889. $stmt = $this->_db->query($select);
  890. $result = $stmt->fetchAll();
  891. $this->assertEquals(3, count($result),
  892. 'Expected count of first result set to be 2');
  893. $this->assertEquals(2, $result[0]['bug_id'],
  894. 'Expected first bug_id to be 2');
  895. $this->assertEquals(3, $result[0]['thecount'],
  896. 'Expected count(*) of first group to be 2');
  897. $this->assertEquals(3, $result[1]['bug_id'],
  898. 'Expected second bug_id to be 3');
  899. $this->assertEquals(1, $result[1]['thecount'],
  900. 'Expected count(*) of second group to be 1');
  901. }
  902. /**
  903. * Test support for automatic conversion of a SQL
  904. * function to a Zend_Db_Expr in group(),
  905. * e.g. group('LOWER(title)') should give the same
  906. * result as group(new Zend_Db_Expr('LOWER(title)')).
  907. */
  908. protected function _selectGroupByAutoExpr()
  909. {
  910. $thecount = $this->_db->quoteIdentifier('thecount');
  911. $bugs_products = $this->_db->quoteIdentifier('zfbugs_products');
  912. $bug_id = $this->_db->quoteIdentifier('bug_id');
  913. $select = $this->_db->select()
  914. ->from('zfbugs_products', array('bug_id'=>"ABS($bugs_products.$bug_id)", new Zend_Db_Expr("COUNT(*) AS $thecount")))
  915. ->group("ABS($bugs_products.$bug_id)")
  916. ->order("ABS($bugs_products.$bug_id)");
  917. return $select;
  918. }
  919. public function testSelectGroupByAutoExpr()
  920. {
  921. $select = $this->_selectGroupByAutoExpr();
  922. $stmt = $this->_db->query($select);
  923. $result = $stmt->fetchAll();
  924. $this->assertEquals(3, count($result), 'Expected count of first result set to be 2');
  925. $this->assertEquals(1, $result[0]['bug_id']);
  926. $this->assertEquals(3, $result[0]['thecount'], 'Expected count(*) of first result set to be 2');
  927. $this->assertEquals(2, $result[1]['bug_id']);
  928. $this->assertEquals(1, $result[1]['thecount']);
  929. }
  930. /**
  931. * Test adding a HAVING clause to a Zend_Db_Select object.
  932. */
  933. protected function _selectHaving()
  934. {
  935. $select = $this->_db->select()
  936. ->from('zfbugs_products', array('bug_id', 'COUNT(*) AS thecount'))
  937. ->group('bug_id')
  938. ->having('COUNT(*) > 1')
  939. ->order('bug_id');
  940. return $select;
  941. }
  942. public function testSelectHaving()
  943. {
  944. $select = $this->_selectHaving();
  945. $stmt = $this->_db->query($select);
  946. $result = $stmt->fetchAll();
  947. $this->assertEquals(2, count($result));
  948. $this->assertEquals(1, $result[0]['bug_id']);
  949. $this->assertEquals(3, $result[0]['thecount']);
  950. }
  951. protected function _selectHavingAnd()
  952. {
  953. $select = $this->_db->select()
  954. ->from('zfbugs_products', array('bug_id', 'COUNT(*) AS thecount'))
  955. ->group('bug_id')
  956. ->having('COUNT(*) > 1')
  957. ->having('COUNT(*) = 1')
  958. ->order('bug_id');
  959. return $select;
  960. }
  961. public function testSelectHavingAnd()
  962. {
  963. $select = $this->_selectHavingAnd();
  964. $stmt = $this->_db->query($select);
  965. $result = $stmt->fetchAll();
  966. $this->assertEquals(0, count($result));
  967. }
  968. /**
  969. * Test support for parameter in having(),
  970. * e.g. having('count(*) > ?', 1).
  971. */
  972. protected function _selectHavingWithParameter()
  973. {
  974. $select = $this->_db->select()
  975. ->from('zfbugs_products', array('bug_id', 'COUNT(*) AS thecount'))
  976. ->group('bug_id')
  977. ->having('COUNT(*) > ?', 1)
  978. ->order('bug_id');
  979. return $select;
  980. }
  981. public function testSelectHavingWithParameter()
  982. {
  983. $select = $this->_selectHavingWithParameter();
  984. $stmt = $this->_db->query($select);
  985. $result = $stmt->fetchAll();
  986. $this->assertEquals(2, count($result));
  987. $this->assertEquals(1, $result[0]['bug_id']);
  988. $this->assertEquals(3, $result[0]['thecount']);
  989. }
  990. /**
  991. * Test adding a HAVING clause to a Zend_Db_Select object.
  992. */
  993. protected function _selectHavingOr()
  994. {
  995. $select = $this->_db->select()
  996. ->from('zfbugs_products', array('bug_id', 'COUNT(*) AS thecount'))
  997. ->group('bug_id')
  998. ->orHaving('COUNT(*) > 1')
  999. ->orHaving('COUNT(*) = 1')
  1000. ->order('bug_id');
  1001. return $select;
  1002. }
  1003. public function testSelectHavingOr()
  1004. {
  1005. $select = $this->_selectHavingOr();
  1006. $stmt = $this->_db->query($select);
  1007. $result = $stmt->fetchAll();
  1008. $this->assertEquals(3, count($result));
  1009. $this->assertEquals(1, $result[0]['bug_id']);
  1010. $this->assertEquals(3, $result[0]['thecount']);
  1011. $this->assertEquals(2, $result[1]['bug_id']);
  1012. $this->assertEquals(1, $result[1]['thecount']);
  1013. }
  1014. /**
  1015. * Test support for parameter in orHaving(),
  1016. * e.g. orHaving('count(*) > ?', 1).
  1017. */
  1018. protected function _selectHavingOrWithParameter()
  1019. {
  1020. $select = $this->_db->select()
  1021. ->from('zfbugs_products', array('bug_id', 'COUNT(*) AS thecount'))
  1022. ->group('bug_id')
  1023. ->orHaving('COUNT(*) > ?', 1)
  1024. ->orHaving('COUNT(*) = ?', 1)
  1025. ->order('bug_id');
  1026. return $select;
  1027. }
  1028. public function testSelectHavingOrWithParameter()
  1029. {
  1030. $select = $this->_selectHavingOrWithParameter();
  1031. $stmt = $this->_db->query($select);
  1032. $result = $stmt->fetchAll();
  1033. $this->assertEquals(3, count($result));
  1034. $this->assertEquals(1, $result[0]['bug_id']);
  1035. $this->assertEquals(3, $result[0]['thecount']);
  1036. $this->assertEquals(2, $result[1]['bug_id']);
  1037. $this->assertEquals(1, $result[1]['thecount']);
  1038. }
  1039. /**
  1040. * Test adding an ORDER BY clause to a Zend_Db_Select object.
  1041. */
  1042. protected function _selectOrderBy()
  1043. {
  1044. $select = $this->_db->select()
  1045. ->from('zfproducts')
  1046. ->order('product_id');
  1047. return $select;
  1048. }
  1049. public function testSelectOrderBy()
  1050. {
  1051. $select = $this->_selectOrderBy();
  1052. $stmt = $this->_db->query($select);
  1053. $result = $stmt->fetchAll();
  1054. $this->assertEquals(1, $result[0]['product_id']);
  1055. }
  1056. protected function _selectOrderByArray()
  1057. {
  1058. $select = $this->_db->select()
  1059. ->from('zfproducts')
  1060. ->order(array('product_name', 'product_id'));
  1061. return $select;
  1062. }
  1063. public function testSelectOrderByArray()
  1064. {
  1065. $select = $this->_selectOrderByArray();
  1066. $stmt = $this->_db->query($select);
  1067. $result = $stmt->fetchAll();
  1068. $this->assertEquals(3, count($result),
  1069. 'Expected count of result set to be 3');
  1070. $this->assertEquals('Linux', $result[0]['product_name']);
  1071. $this->assertEquals(2, $result[0]['product_id']);
  1072. }
  1073. protected function _selectOrderByAsc()
  1074. {
  1075. $select = $this->_db->select()
  1076. ->from('zfproducts')
  1077. ->order("product_id ASC");
  1078. return $select;
  1079. }
  1080. public function testSelectOrderByAsc()
  1081. {
  1082. $select = $this->_selectOrderByAsc();
  1083. $stmt = $this->_db->query($select);
  1084. $result = $stmt->fetchAll();
  1085. $this->assertEquals(3, count($result),
  1086. 'Expected count of result set to be 2');
  1087. $this->assertEquals(1, $result[0]['product_id']);
  1088. }
  1089. protected function _selectOrderByPosition()
  1090. {
  1091. $select = $this->_db->select()
  1092. ->from('zfproducts')
  1093. ->order('2');
  1094. return $select;
  1095. }
  1096. public function testSelectOrderByPosition()
  1097. {
  1098. $select = $this->_selectOrderByPosition();
  1099. $stmt = $this->_db->query($select);
  1100. $result = $stmt->fetchAll();
  1101. $this->assertEquals(2, $result[0]['product_id']);
  1102. $this->assertEquals(3, $result[1]['product_id']);
  1103. $this->assertEquals(1, $result[2]['product_id']);
  1104. }
  1105. protected function _selectOrderByPositionAsc()
  1106. {
  1107. $select = $this->_db->select()
  1108. ->from('zfproducts')
  1109. ->order('2 ASC');
  1110. return $select;
  1111. }
  1112. public function testSelectOrderByPositionAsc()
  1113. {
  1114. $select = $this->_selectOrderByPositionAsc();
  1115. $stmt = $this->_db->query($select);
  1116. $result = $stmt->fetchAll();
  1117. $this->assertEquals(2, $result[0]['product_id']);
  1118. $this->assertEquals(3, $result[1]['product_id']);
  1119. $this->assertEquals(1, $result[2]['product_id']);
  1120. }
  1121. protected function _selectOrderByPositionDesc()
  1122. {
  1123. $select = $this->_db->select()
  1124. ->from('zfproducts')
  1125. ->order('2 DESC');
  1126. return $select;
  1127. }
  1128. public function testSelectOrderByPositionDesc()
  1129. {
  1130. $select = $this->_selectOrderByPositionDesc();
  1131. $stmt = $this->_db->query($select);
  1132. $result = $stmt->fetchAll();
  1133. $this->assertEquals(1, $result[0]['product_id']);
  1134. $this->assertEquals(3, $result[1]['product_id']);
  1135. $this->assertEquals(2, $result[2]['product_id']);
  1136. }
  1137. protected function _selectOrderByMultiplePositions()
  1138. {
  1139. $select = $this->_db->select()
  1140. ->from('zfproducts')
  1141. ->order(array('2 DESC', '1 DESC'));
  1142. return $select;
  1143. }
  1144. public function testSelectOrderByMultiplePositions()
  1145. {
  1146. $select = $this->_selectOrderByMultiplePositions();
  1147. $stmt = $this->_db->query($select);
  1148. $result = $stmt->fetchAll();
  1149. $this->assertEquals(1, $result[0]['product_id']);
  1150. $this->assertEquals(3, $result[1]['product_id']);
  1151. $this->assertEquals(2, $result[2]['product_id']);
  1152. }
  1153. protected function _selectOrderByDesc()
  1154. {
  1155. $select = $this->_db->select()
  1156. ->from('zfproducts')
  1157. ->order("product_id DESC");
  1158. return $select;
  1159. }
  1160. public function testSelectOrderByDesc()
  1161. {
  1162. $select = $this->_selectOrderByDesc();
  1163. $stmt = $this->_db->query($select);
  1164. $result = $stmt->fetchAll();
  1165. $this->assertEquals(3, count($result),
  1166. 'Expected count of result set to be 2');
  1167. $this->assertEquals(3, $result[0]['product_id']);
  1168. }
  1169. /**
  1170. * Test support for qualified table in order(),
  1171. * e.g. order('schema.table').
  1172. */
  1173. protected function _selectOrderByQualified()
  1174. {
  1175. $select = $this->_db->select()
  1176. ->from('zfproducts')
  1177. ->order("zfproducts.product_id");
  1178. return $select;
  1179. }
  1180. public function testSelectOrderByQualified()
  1181. {
  1182. $select = $this->_selectOrderByQualified();
  1183. $stmt = $this->_db->query($select);
  1184. $result = $stmt->fetchAll();
  1185. $this->assertEquals(1, $result[0]['product_id']);
  1186. }
  1187. /**
  1188. * Test support for Zend_Db_Expr in order(),
  1189. * e.g. order(new Zend_Db_Expr('id+1')).
  1190. */
  1191. protected function _selectOrderByExpr()
  1192. {
  1193. $select = $this->_db->select()
  1194. ->from('zfproducts')
  1195. ->order(new Zend_Db_Expr("1"));
  1196. return $select;
  1197. }
  1198. public function testSelectOrderByExpr()
  1199. {
  1200. $select = $this->_selectOrderByExpr();
  1201. $stmt = $this->_db->query($select);
  1202. $result = $stmt->fetchAll();
  1203. $this->assertEquals(1, $result[0]['product_id']);
  1204. }
  1205. /**
  1206. * Test automatic conversion of SQL functions to
  1207. * Zend_Db_Expr, e.g. order('LOWER(title)')
  1208. * should give the same result as
  1209. * order(new Zend_Db_Expr('LOWER(title)')).
  1210. */
  1211. protected function _selectOrderByAutoExpr()
  1212. {
  1213. $products = $this->_db->quoteIdentifier('zfproducts');
  1214. $product_id = $this->_db->quoteIdentifier('product_id');
  1215. $select = $this->_db->select()
  1216. ->from('zfproducts')
  1217. ->order("ABS($products.$product_id)");
  1218. return $select;
  1219. }
  1220. public function testSelectOrderByAutoExpr()
  1221. {
  1222. $select = $this->_selectOrderByAutoExpr();
  1223. $stmt = $this->_db->query($select);
  1224. $result = $stmt->fetchAll();
  1225. $this->assertEquals(1, $result[0]['product_id']);
  1226. }
  1227. /**
  1228. * Test ORDER BY clause that contains multiple lines.
  1229. * See ZF-1822, which says that the regexp matching
  1230. * ASC|DESC fails when string is multi-line.
  1231. */
  1232. protected function _selectOrderByMultiLine()
  1233. {
  1234. $select = $this->_db->select()
  1235. ->from('zfproducts')
  1236. ->order("product_id\nDESC");
  1237. return $select;
  1238. }
  1239. public function testSelectOrderByMultiLine()
  1240. {
  1241. $select = $this->_selectOrderByMultiLine();
  1242. $stmt = $this->_db->query($select);
  1243. $result = $stmt->fetchAll();
  1244. $this->assertEquals(3, $result[0]['product_id']);
  1245. }
  1246. /**
  1247. * @group ZF-4246
  1248. */
  1249. protected function _checkExtraField($result)
  1250. {
  1251. // Check that extra field ZEND_DB_ROWNUM isn't present
  1252. // (particulary with Db2 & Oracle)
  1253. $this->assertArrayNotHasKey('zend_db_rownum', $result);
  1254. $this->assertArrayNotHasKey('ZEND_DB_ROWNUM', $result);
  1255. }
  1256. /**
  1257. * Test adding a LIMIT clause to a Zend_Db_Select object.
  1258. */
  1259. protected function _selectLimit()
  1260. {
  1261. $select = $this->_db->select()
  1262. ->from('zfproducts')
  1263. ->order('product_id')
  1264. ->limit(1);
  1265. return $select;
  1266. }
  1267. /**
  1268. * @group ZF-4246
  1269. */
  1270. public function testSelectLimit()
  1271. {
  1272. $select = $this->_selectLimit();
  1273. $stmt = $this->_db->query($select);
  1274. $result = $stmt->fetchAll();
  1275. $this->assertEquals(1, count($result));
  1276. $this->assertEquals(1, $result[0]['product_id']);
  1277. $this->_checkExtraField($result[0]);
  1278. }
  1279. /**
  1280. * @group ZF-5263
  1281. * @group ZF-4246
  1282. */
  1283. public function testSelectLimitFetchCol()
  1284. {
  1285. $product_id = $this->_db->quoteIdentifier('product_id');
  1286. $select = $this->_db->select()
  1287. ->from('zfproducts', 'product_name')
  1288. ->where($product_id . ' = ?', 3)
  1289. ->limit(1);
  1290. $result = $this->_db->fetchCol($select);
  1291. $this->assertEquals(1, count($result));
  1292. $this->assertEquals('OS X', $result[0]);
  1293. $this->_checkExtraField($result);
  1294. }
  1295. protected function _selectLimitNone()
  1296. {
  1297. $select = $this->_db->select()
  1298. ->from('zfproducts')
  1299. ->order('product_id')
  1300. ->limit(); // no limit
  1301. return $select;
  1302. }
  1303. /**
  1304. * @group ZF-4246
  1305. */
  1306. public function testSelectLimitNone()
  1307. {
  1308. $select = $this->_selectLimitNone();
  1309. $stmt = $this->_db->query($select);
  1310. $result = $stmt->fetchAll();
  1311. $this->assertEquals(3, count($result));
  1312. $this->_checkExtraField($result[0]);
  1313. }
  1314. protected function _selectLimitOffset()
  1315. {
  1316. $select = $this->_db->select()
  1317. ->from('zfproducts')
  1318. ->order('product_id')
  1319. ->limit(1, 1);
  1320. return $select;
  1321. }
  1322. /**
  1323. * @group ZF-4246
  1324. */
  1325. public function testSelectLimitOffset()
  1326. {
  1327. $select = $this->_selectLimitOffset();
  1328. $stmt = $this->_db->query($select);
  1329. $result = $stmt->fetchAll();
  1330. $this->assertEquals(1, count($result));
  1331. $this->assertEquals(2, $result[0]['product_id']);
  1332. $this->_checkExtraField($result[0]);
  1333. }
  1334. /**
  1335. * Test the limitPage() method of a Zend_Db_Select object.
  1336. */
  1337. protected function _selectLimitPageOne()
  1338. {
  1339. $select = $this->_db->select()
  1340. ->from('zfproducts')
  1341. ->order('product_id')
  1342. ->limitPage(1, 1); // first page, length 1
  1343. return $select;
  1344. }
  1345. /**
  1346. * @group ZF-4246
  1347. */
  1348. public function testSelectLimitPageOne()
  1349. {
  1350. $select = $this->_selectLimitPageOne();
  1351. $stmt = $this->_db->query($select);
  1352. $result = $stmt->fetchAll();
  1353. $this->assertEquals(1, count($result));
  1354. $this->assertEquals(1, $result[0]['product_id']);
  1355. $this->_checkExtraField($result[0]);
  1356. }
  1357. protected function _selectLimitPageTwo()
  1358. {
  1359. $select = $this->_db->select()
  1360. ->from('zfproducts')
  1361. ->order('product_id')
  1362. ->limitPage(2, 1); // second page, length 1
  1363. return $select;
  1364. }
  1365. /**
  1366. * @group ZF-4246
  1367. */
  1368. public function testSelectLimitPageTwo()
  1369. {
  1370. $select = $this->_selectLimitPageTwo();
  1371. $stmt = $this->_db->query($select);
  1372. $result = $stmt->fetchAll();
  1373. $this->assertEquals(1, count($result));
  1374. $this->assertEquals(2, $result[0]['product_id']);
  1375. $this->_checkExtraField($result[0]);
  1376. }
  1377. /**
  1378. * Test the getPart() and reset() methods of a Zend_Db_Select object.
  1379. */
  1380. public function testSelectGetPartAndReset()
  1381. {
  1382. $select = $this->_db->select()
  1383. ->from('zfproducts')
  1384. ->limit(1);
  1385. $count = $select->getPart(Zend_Db_Select::LIMIT_COUNT);
  1386. $this->assertEquals(1, $count);
  1387. $select->reset(Zend_Db_Select::LIMIT_COUNT);
  1388. $count = $select->getPart(Zend_Db_Select::LIMIT_COUNT);
  1389. $this->assertNull($count);
  1390. $select->reset(); // reset the whole object
  1391. $from = $select->getPart(Zend_Db_Select::FROM);
  1392. $this->assertTrue(empty($from));
  1393. }
  1394. /**
  1395. * Test the UNION statement for a Zend_Db_Select object.
  1396. */
  1397. protected function _selectUnionString()
  1398. {
  1399. $bugs = $this->_db->quoteIdentifier('zfbugs');
  1400. $bug_id = $this->_db->quoteIdentifier('bug_id');
  1401. $bug_status = $this->_db->quoteIdentifier('bug_status');
  1402. $products = $this->_db->quoteIdentifier('zfproducts');
  1403. $product_id = $this->_db->quoteIdentifier('product_id');
  1404. $product_name = $this->_db->quoteIdentifier('product_name');
  1405. $id = $this->_db->quoteIdentifier('id');
  1406. $name = $this->_db->quoteIdentifier('name');
  1407. $sql1 = "SELECT $bug_id AS $id, $bug_status AS $name FROM $bugs";
  1408. $sql2 = "SELECT $product_id AS $id, $product_name AS $name FROM $products";
  1409. $select = $this->_db->select()
  1410. ->union(array($sql1, $sql2))
  1411. ->order('id');
  1412. return $select;
  1413. }
  1414. public function testSelectUnionString()
  1415. {
  1416. $select = $this->_selectUnionString();
  1417. $stmt = $this->_db->query($select);
  1418. $result = $stmt->fetchAll();
  1419. $this->assertEquals(7, count($result));
  1420. $this->assertEquals(1, $result[0]['id']);
  1421. }
  1422. /**
  1423. * @group ZF-4772
  1424. * @expectedException Zend_Db_Select_Exception
  1425. */
  1426. public function testSelectUnionNoArrayThrowsException()
  1427. {
  1428. $this->_db->select()->union('string');
  1429. }
  1430. /**
  1431. * @group ZF-4772
  1432. * @expectedException Zend_Db_Select_Exception
  1433. */
  1434. public function testSelectUnionInvalidUnionTypeThrowsException()
  1435. {
  1436. $this->_db->select()->union(array(), 'foo');
  1437. }
  1438. /**
  1439. * @group ZF-6653
  1440. */
  1441. public function testSelectIsTheSameWhenCallingFromAndJoinInDifferentOrders()
  1442. {
  1443. $selectFromThenJoin = $this->_db->select();
  1444. $selectFromThenJoin->from(array('f' => 'foo'), array('columnfoo'))
  1445. ->joinLeft(array('b' => 'bar'), 'f.columnfoo2 = b.barcolumn2', array('baralias' => 'barcolumn'));
  1446. $selectJoinThenFrom = $this->_db->select();
  1447. $selectJoinThenFrom->joinLeft(array('b' => 'bar'), 'f.columnfoo2 = b.barcolumn2', array('baralias' => 'barcolumn'))
  1448. ->from(array('f' => 'foo'), array('columnfoo'));
  1449. $sqlSelectFromThenJoin = $selectFromThenJoin->assemble();
  1450. $sqlSelectJoinThenFrom = $selectJoinThenFrom->assemble();
  1451. $this->assertEquals($sqlSelectFromThenJoin, $sqlSelectJoinThenFrom);
  1452. }
  1453. /**
  1454. * @group ZF-6653
  1455. */
  1456. public function testSelectIsTheSameWhenCallingMultipleFromsAfterJoin()
  1457. {
  1458. $selectFromThenJoin = $this->_db->select();
  1459. $selectFromThenJoin->from(array('f' => 'foo'), array('columnfoo'))
  1460. ->from(array('d' => 'doo'), array('columndoo'))
  1461. ->joinLeft(array('b' => 'bar'), 'f.columnfoo2 = b.barcolumn2', array('baralias' => 'barcolumn'));
  1462. $selectJoinThenFrom = $this->_db->select();
  1463. $selectJoinThenFrom->joinLeft(array('b' => 'bar'), 'f.columnfoo2 = b.barcolumn2', array('baralias' => 'barcolumn'))
  1464. ->from(array('f' => 'foo'), array('columnfoo'))
  1465. ->from(array('d' => 'doo'), array('columndoo'));
  1466. $sqlSelectFromThenJoin = $selectFromThenJoin->assemble();
  1467. $sqlSelectJoinThenFrom = $selectJoinThenFrom->assemble();
  1468. $this->assertEquals($sqlSelectFromThenJoin, $sqlSelectJoinThenFrom);
  1469. }
  1470. /**
  1471. * @group ZF-6653
  1472. */
  1473. public function testSelectWithMultipleFromsAfterAJoinWillProperlyOrderColumns()
  1474. {
  1475. $select = $this->_selectWithMultipleFromsAfterAJoinWillProperlyOrderColumns();
  1476. $quote = $this->_db->getQuoteIdentifierSymbol();
  1477. $target = 'SELECT `f`.`columnfoo`, `d`.`columndoo`, `b`.`barcolumn` AS `baralias` FROM ' . $this->_db->quoteTableAs('foo', 'f')
  1478. . "\n" . ' INNER JOIN ' . $this->_db->quoteTableAs('doo', 'd')
  1479. . "\n" . ' LEFT JOIN ' . $this->_db->quoteTableAs('bar', 'b') . ' ON f.columnfoo2 = b.barcolumn2';
  1480. if ($quote != '`') {
  1481. $target = str_replace('`', $quote, $target);
  1482. }
  1483. $this->assertEquals($target, $select->assemble());
  1484. }
  1485. protected function _selectWithMultipleFromsAfterAJoinWillProperlyOrderColumns()
  1486. {
  1487. $selectJoinThenFrom = $this->_db->select();
  1488. $selectJoinThenFrom->joinLeft(array('b' => 'bar'), 'f.columnfoo2 = b.barcolumn2', array('baralias' => 'barcolumn'))
  1489. ->from(array('f' => 'foo'), array('columnfoo'))
  1490. ->from(array('d' => 'doo'), array('columndoo'));
  1491. return $selectJoinThenFrom;
  1492. }
  1493. public function testSerializeSelect()
  1494. {
  1495. /* checks if the adapter has effectively gotten serialized,
  1496. no exceptions are thrown here, so it's all right */
  1497. $serialize = serialize($this->_select());
  1498. $this->assertType('string',$serialize);
  1499. }
  1500. /**
  1501. * @group ZF-3792
  1502. */
  1503. public function testJoinUsingActuallyGeneratesAnInnerJoinOnForCompatibilityReasons()
  1504. {
  1505. $table_A = $this->_db->quoteTableAs('A');
  1506. $table_B = $this->_db->quoteTableAs('B');
  1507. $colname = $this->_db->quoteIdentifier('colname');
  1508. $s = $this->_db->select()->from('A')->joinUsing('B', $colname);
  1509. $this->assertContains("JOIN {$table_B} ON {$table_B}.{$colname} = {$table_A}.{$colname}", $s->assemble());
  1510. }
  1511. }