TestCommon.php 49 KB

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