TestCommon.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760
  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->assertTrue($select instanceof Zend_Db_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->assertTrue($select instanceof Zend_Db_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->assertTrue($select instanceof Zend_Db_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->assertTrue($e instanceof Zend_Db_Select_Exception,
  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->assertTrue($e instanceof Zend_Db_Select_Exception,
  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->assertTrue($e instanceof Zend_Db_Select_Exception,
  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->assertTrue($e instanceof Zend_Db_Select_Exception,
  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->assertTrue(is_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. /**
  1512. * @group ZF-5953
  1513. */
  1514. public function testJoinUsingAllowsSpecifyingMultipleColumnsViaAnArray()
  1515. {
  1516. $table_A = $this->_db->quoteTableAs('A');
  1517. $table_B = $this->_db->quoteTableAs('B');
  1518. $colOne = $this->_db->quoteIdentifier('colOne');
  1519. $colTwo = $this->_db->quoteIdentifier('colTwo');
  1520. $s = $this->_db->select()->from('A')->joinUsing('B', array($colOne,$colTwo));
  1521. $this->assertContains(
  1522. "JOIN {$table_B} ON {$table_B}.{$colOne} = {$table_A}.{$colOne}"
  1523. . " AND {$table_B}.{$colTwo} = {$table_A}.{$colTwo}",
  1524. $s->assemble()
  1525. );
  1526. }
  1527. /**
  1528. * @group ZF-3309
  1529. */
  1530. public function testJoinUsingUsesTableNameOfTableBeingJoinedWhenAliasNotDefined()
  1531. {
  1532. $table1 = $this->_db->quoteTableAs('table1');
  1533. $table2 = $this->_db->quoteTableAs('table2');
  1534. $colname = $this->_db->quoteIdentifier('column1');
  1535. $select = $this->_db->select();
  1536. $select->from('table1')->joinUsing('table2', $colname);
  1537. $this->assertRegexp("/ON {$table2}.{$colname}/s", $select->assemble());
  1538. }
  1539. /**
  1540. * @group ZF-3309
  1541. */
  1542. public function testJoinUsingUsesAliasOfTableBeingJoinedWhenAliasIsDefined()
  1543. {
  1544. $table1 = $this->_db->quoteTableAs('table1');
  1545. $table2_alias = $this->_db->quoteTableAs('t2');
  1546. $colname = $this->_db->quoteIdentifier('column1');
  1547. $select = $this->_db->select();
  1548. $select->from('table1')->joinUsing(array('t2'=>'table2'), $colname);
  1549. $this->assertRegexp("/ON {$table2_alias}.{$colname}/s", $select->assemble());
  1550. }
  1551. }