TestCommon.php 49 KB

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