TestCommon.php 57 KB

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