TestCommon.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701
  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 _selectOrderByPosition()
  1091. {
  1092. $select = $this->_db->select()
  1093. ->from('zfproducts')
  1094. ->order('2');
  1095. return $select;
  1096. }
  1097. public function testSelectOrderByPosition()
  1098. {
  1099. $select = $this->_selectOrderByPosition();
  1100. $stmt = $this->_db->query($select);
  1101. $result = $stmt->fetchAll();
  1102. $this->assertEquals(2, $result[0]['product_id']);
  1103. $this->assertEquals(3, $result[1]['product_id']);
  1104. $this->assertEquals(1, $result[2]['product_id']);
  1105. }
  1106. protected function _selectOrderByPositionAsc()
  1107. {
  1108. $select = $this->_db->select()
  1109. ->from('zfproducts')
  1110. ->order('2 ASC');
  1111. return $select;
  1112. }
  1113. public function testSelectOrderByPositionAsc()
  1114. {
  1115. $select = $this->_selectOrderByPositionAsc();
  1116. $stmt = $this->_db->query($select);
  1117. $result = $stmt->fetchAll();
  1118. $this->assertEquals(2, $result[0]['product_id']);
  1119. $this->assertEquals(3, $result[1]['product_id']);
  1120. $this->assertEquals(1, $result[2]['product_id']);
  1121. }
  1122. protected function _selectOrderByPositionDesc()
  1123. {
  1124. $select = $this->_db->select()
  1125. ->from('zfproducts')
  1126. ->order('2 DESC');
  1127. return $select;
  1128. }
  1129. public function testSelectOrderByPositionDesc()
  1130. {
  1131. $select = $this->_selectOrderByPositionDesc();
  1132. $stmt = $this->_db->query($select);
  1133. $result = $stmt->fetchAll();
  1134. $this->assertEquals(1, $result[0]['product_id']);
  1135. $this->assertEquals(3, $result[1]['product_id']);
  1136. $this->assertEquals(2, $result[2]['product_id']);
  1137. }
  1138. protected function _selectOrderByMultiplePositions()
  1139. {
  1140. $select = $this->_db->select()
  1141. ->from('zfproducts')
  1142. ->order(array('2 DESC', '1 DESC'));
  1143. return $select;
  1144. }
  1145. public function testSelectOrderByMultiplePositions()
  1146. {
  1147. $select = $this->_selectOrderByMultiplePositions();
  1148. $stmt = $this->_db->query($select);
  1149. $result = $stmt->fetchAll();
  1150. $this->assertEquals(1, $result[0]['product_id']);
  1151. $this->assertEquals(3, $result[1]['product_id']);
  1152. $this->assertEquals(2, $result[2]['product_id']);
  1153. }
  1154. protected function _selectOrderByDesc()
  1155. {
  1156. $select = $this->_db->select()
  1157. ->from('zfproducts')
  1158. ->order("product_id DESC");
  1159. return $select;
  1160. }
  1161. public function testSelectOrderByDesc()
  1162. {
  1163. $select = $this->_selectOrderByDesc();
  1164. $stmt = $this->_db->query($select);
  1165. $result = $stmt->fetchAll();
  1166. $this->assertEquals(3, count($result),
  1167. 'Expected count of result set to be 2');
  1168. $this->assertEquals(3, $result[0]['product_id']);
  1169. }
  1170. /**
  1171. * Test support for qualified table in order(),
  1172. * e.g. order('schema.table').
  1173. */
  1174. protected function _selectOrderByQualified()
  1175. {
  1176. $select = $this->_db->select()
  1177. ->from('zfproducts')
  1178. ->order("zfproducts.product_id");
  1179. return $select;
  1180. }
  1181. public function testSelectOrderByQualified()
  1182. {
  1183. $select = $this->_selectOrderByQualified();
  1184. $stmt = $this->_db->query($select);
  1185. $result = $stmt->fetchAll();
  1186. $this->assertEquals(1, $result[0]['product_id']);
  1187. }
  1188. /**
  1189. * Test support for Zend_Db_Expr in order(),
  1190. * e.g. order(new Zend_Db_Expr('id+1')).
  1191. */
  1192. protected function _selectOrderByExpr()
  1193. {
  1194. $select = $this->_db->select()
  1195. ->from('zfproducts')
  1196. ->order(new Zend_Db_Expr("1"));
  1197. return $select;
  1198. }
  1199. public function testSelectOrderByExpr()
  1200. {
  1201. $select = $this->_selectOrderByExpr();
  1202. $stmt = $this->_db->query($select);
  1203. $result = $stmt->fetchAll();
  1204. $this->assertEquals(1, $result[0]['product_id']);
  1205. }
  1206. /**
  1207. * Test automatic conversion of SQL functions to
  1208. * Zend_Db_Expr, e.g. order('LOWER(title)')
  1209. * should give the same result as
  1210. * order(new Zend_Db_Expr('LOWER(title)')).
  1211. */
  1212. protected function _selectOrderByAutoExpr()
  1213. {
  1214. $products = $this->_db->quoteIdentifier('zfproducts');
  1215. $product_id = $this->_db->quoteIdentifier('product_id');
  1216. $select = $this->_db->select()
  1217. ->from('zfproducts')
  1218. ->order("ABS($products.$product_id)");
  1219. return $select;
  1220. }
  1221. public function testSelectOrderByAutoExpr()
  1222. {
  1223. $select = $this->_selectOrderByAutoExpr();
  1224. $stmt = $this->_db->query($select);
  1225. $result = $stmt->fetchAll();
  1226. $this->assertEquals(1, $result[0]['product_id']);
  1227. }
  1228. /**
  1229. * Test ORDER BY clause that contains multiple lines.
  1230. * See ZF-1822, which says that the regexp matching
  1231. * ASC|DESC fails when string is multi-line.
  1232. */
  1233. protected function _selectOrderByMultiLine()
  1234. {
  1235. $select = $this->_db->select()
  1236. ->from('zfproducts')
  1237. ->order("product_id\nDESC");
  1238. return $select;
  1239. }
  1240. public function testSelectOrderByMultiLine()
  1241. {
  1242. $select = $this->_selectOrderByMultiLine();
  1243. $stmt = $this->_db->query($select);
  1244. $result = $stmt->fetchAll();
  1245. $this->assertEquals(3, $result[0]['product_id']);
  1246. }
  1247. /**
  1248. * @group ZF-4246
  1249. */
  1250. protected function _checkExtraField($result)
  1251. {
  1252. // Check that extra field ZEND_DB_ROWNUM isn't present
  1253. // (particulary with Db2 & Oracle)
  1254. $this->assertArrayNotHasKey('zend_db_rownum', $result);
  1255. $this->assertArrayNotHasKey('ZEND_DB_ROWNUM', $result);
  1256. }
  1257. /**
  1258. * Test adding a LIMIT clause to a Zend_Db_Select object.
  1259. */
  1260. protected function _selectLimit()
  1261. {
  1262. $select = $this->_db->select()
  1263. ->from('zfproducts')
  1264. ->order('product_id')
  1265. ->limit(1);
  1266. return $select;
  1267. }
  1268. /**
  1269. * @group ZF-4246
  1270. */
  1271. public function testSelectLimit()
  1272. {
  1273. $select = $this->_selectLimit();
  1274. $stmt = $this->_db->query($select);
  1275. $result = $stmt->fetchAll();
  1276. $this->assertEquals(1, count($result));
  1277. $this->assertEquals(1, $result[0]['product_id']);
  1278. $this->_checkExtraField($result[0]);
  1279. }
  1280. /**
  1281. * @group ZF-5263
  1282. * @group ZF-4246
  1283. */
  1284. public function testSelectLimitFetchCol()
  1285. {
  1286. $product_id = $this->_db->quoteIdentifier('product_id');
  1287. $select = $this->_db->select()
  1288. ->from('zfproducts', 'product_name')
  1289. ->where($product_id . ' = ?', 3)
  1290. ->limit(1);
  1291. $result = $this->_db->fetchCol($select);
  1292. $this->assertEquals(1, count($result));
  1293. $this->assertEquals('OS X', $result[0]);
  1294. $this->_checkExtraField($result);
  1295. }
  1296. protected function _selectLimitNone()
  1297. {
  1298. $select = $this->_db->select()
  1299. ->from('zfproducts')
  1300. ->order('product_id')
  1301. ->limit(); // no limit
  1302. return $select;
  1303. }
  1304. /**
  1305. * @group ZF-4246
  1306. */
  1307. public function testSelectLimitNone()
  1308. {
  1309. $select = $this->_selectLimitNone();
  1310. $stmt = $this->_db->query($select);
  1311. $result = $stmt->fetchAll();
  1312. $this->assertEquals(3, count($result));
  1313. $this->_checkExtraField($result[0]);
  1314. }
  1315. protected function _selectLimitOffset()
  1316. {
  1317. $select = $this->_db->select()
  1318. ->from('zfproducts')
  1319. ->order('product_id')
  1320. ->limit(1, 1);
  1321. return $select;
  1322. }
  1323. /**
  1324. * @group ZF-4246
  1325. */
  1326. public function testSelectLimitOffset()
  1327. {
  1328. $select = $this->_selectLimitOffset();
  1329. $stmt = $this->_db->query($select);
  1330. $result = $stmt->fetchAll();
  1331. $this->assertEquals(1, count($result));
  1332. $this->assertEquals(2, $result[0]['product_id']);
  1333. $this->_checkExtraField($result[0]);
  1334. }
  1335. /**
  1336. * Test the limitPage() method of a Zend_Db_Select object.
  1337. */
  1338. protected function _selectLimitPageOne()
  1339. {
  1340. $select = $this->_db->select()
  1341. ->from('zfproducts')
  1342. ->order('product_id')
  1343. ->limitPage(1, 1); // first page, length 1
  1344. return $select;
  1345. }
  1346. /**
  1347. * @group ZF-4246
  1348. */
  1349. public function testSelectLimitPageOne()
  1350. {
  1351. $select = $this->_selectLimitPageOne();
  1352. $stmt = $this->_db->query($select);
  1353. $result = $stmt->fetchAll();
  1354. $this->assertEquals(1, count($result));
  1355. $this->assertEquals(1, $result[0]['product_id']);
  1356. $this->_checkExtraField($result[0]);
  1357. }
  1358. protected function _selectLimitPageTwo()
  1359. {
  1360. $select = $this->_db->select()
  1361. ->from('zfproducts')
  1362. ->order('product_id')
  1363. ->limitPage(2, 1); // second page, length 1
  1364. return $select;
  1365. }
  1366. /**
  1367. * @group ZF-4246
  1368. */
  1369. public function testSelectLimitPageTwo()
  1370. {
  1371. $select = $this->_selectLimitPageTwo();
  1372. $stmt = $this->_db->query($select);
  1373. $result = $stmt->fetchAll();
  1374. $this->assertEquals(1, count($result));
  1375. $this->assertEquals(2, $result[0]['product_id']);
  1376. $this->_checkExtraField($result[0]);
  1377. }
  1378. /**
  1379. * Test the getPart() and reset() methods of a Zend_Db_Select object.
  1380. */
  1381. public function testSelectGetPartAndReset()
  1382. {
  1383. $select = $this->_db->select()
  1384. ->from('zfproducts')
  1385. ->limit(1);
  1386. $count = $select->getPart(Zend_Db_Select::LIMIT_COUNT);
  1387. $this->assertEquals(1, $count);
  1388. $select->reset(Zend_Db_Select::LIMIT_COUNT);
  1389. $count = $select->getPart(Zend_Db_Select::LIMIT_COUNT);
  1390. $this->assertNull($count);
  1391. $select->reset(); // reset the whole object
  1392. $from = $select->getPart(Zend_Db_Select::FROM);
  1393. $this->assertTrue(empty($from));
  1394. }
  1395. /**
  1396. * Test the UNION statement for a Zend_Db_Select object.
  1397. */
  1398. protected function _selectUnionString()
  1399. {
  1400. $bugs = $this->_db->quoteIdentifier('zfbugs');
  1401. $bug_id = $this->_db->quoteIdentifier('bug_id');
  1402. $bug_status = $this->_db->quoteIdentifier('bug_status');
  1403. $products = $this->_db->quoteIdentifier('zfproducts');
  1404. $product_id = $this->_db->quoteIdentifier('product_id');
  1405. $product_name = $this->_db->quoteIdentifier('product_name');
  1406. $id = $this->_db->quoteIdentifier('id');
  1407. $name = $this->_db->quoteIdentifier('name');
  1408. $sql1 = "SELECT $bug_id AS $id, $bug_status AS $name FROM $bugs";
  1409. $sql2 = "SELECT $product_id AS $id, $product_name AS $name FROM $products";
  1410. $select = $this->_db->select()
  1411. ->union(array($sql1, $sql2))
  1412. ->order('id');
  1413. return $select;
  1414. }
  1415. public function testSelectUnionString()
  1416. {
  1417. $select = $this->_selectUnionString();
  1418. $stmt = $this->_db->query($select);
  1419. $result = $stmt->fetchAll();
  1420. $this->assertEquals(7, count($result));
  1421. $this->assertEquals(1, $result[0]['id']);
  1422. }
  1423. /**
  1424. * @group ZF-4772
  1425. * @expectedException Zend_Db_Select_Exception
  1426. */
  1427. public function testSelectUnionNoArrayThrowsException()
  1428. {
  1429. $this->_db->select()->union('string');
  1430. }
  1431. /**
  1432. * @group ZF-4772
  1433. * @expectedException Zend_Db_Select_Exception
  1434. */
  1435. public function testSelectUnionInvalidUnionTypeThrowsException()
  1436. {
  1437. $this->_db->select()->union(array(), 'foo');
  1438. }
  1439. /**
  1440. * @group ZF-6653
  1441. */
  1442. public function testSelectIsTheSameWhenCallingFromAndJoinInDifferentOrders()
  1443. {
  1444. $selectFromThenJoin = $this->_db->select();
  1445. $selectFromThenJoin->from(array('f' => 'foo'), array('columnfoo'))
  1446. ->joinLeft(array('b' => 'bar'), 'f.columnfoo2 = b.barcolumn2', array('baralias' => 'barcolumn'));
  1447. $selectJoinThenFrom = $this->_db->select();
  1448. $selectJoinThenFrom->joinLeft(array('b' => 'bar'), 'f.columnfoo2 = b.barcolumn2', array('baralias' => 'barcolumn'))
  1449. ->from(array('f' => 'foo'), array('columnfoo'));
  1450. $sqlSelectFromThenJoin = $selectFromThenJoin->assemble();
  1451. $sqlSelectJoinThenFrom = $selectJoinThenFrom->assemble();
  1452. $this->assertEquals($sqlSelectFromThenJoin, $sqlSelectJoinThenFrom);
  1453. }
  1454. /**
  1455. * @group ZF-6653
  1456. */
  1457. public function testSelectIsTheSameWhenCallingMultipleFromsAfterJoin()
  1458. {
  1459. $selectFromThenJoin = $this->_db->select();
  1460. $selectFromThenJoin->from(array('f' => 'foo'), array('columnfoo'))
  1461. ->from(array('d' => 'doo'), array('columndoo'))
  1462. ->joinLeft(array('b' => 'bar'), 'f.columnfoo2 = b.barcolumn2', array('baralias' => 'barcolumn'));
  1463. $selectJoinThenFrom = $this->_db->select();
  1464. $selectJoinThenFrom->joinLeft(array('b' => 'bar'), 'f.columnfoo2 = b.barcolumn2', array('baralias' => 'barcolumn'))
  1465. ->from(array('f' => 'foo'), array('columnfoo'))
  1466. ->from(array('d' => 'doo'), array('columndoo'));
  1467. $sqlSelectFromThenJoin = $selectFromThenJoin->assemble();
  1468. $sqlSelectJoinThenFrom = $selectJoinThenFrom->assemble();
  1469. $this->assertEquals($sqlSelectFromThenJoin, $sqlSelectJoinThenFrom);
  1470. }
  1471. /**
  1472. * @group ZF-6653
  1473. */
  1474. public function testSelectWithMultipleFromsAfterAJoinWillProperlyOrderColumns()
  1475. {
  1476. $select = $this->_selectWithMultipleFromsAfterAJoinWillProperlyOrderColumns();
  1477. $quote = $this->_db->getQuoteIdentifierSymbol();
  1478. $target = 'SELECT `f`.`columnfoo`, `d`.`columndoo`, `b`.`barcolumn` AS `baralias` FROM ' . $this->_db->quoteTableAs('foo', 'f')
  1479. . "\n" . ' INNER JOIN ' . $this->_db->quoteTableAs('doo', 'd')
  1480. . "\n" . ' LEFT JOIN ' . $this->_db->quoteTableAs('bar', 'b') . ' ON f.columnfoo2 = b.barcolumn2';
  1481. if ($quote != '`') {
  1482. $target = str_replace('`', $quote, $target);
  1483. }
  1484. $this->assertEquals($target, $select->assemble());
  1485. }
  1486. protected function _selectWithMultipleFromsAfterAJoinWillProperlyOrderColumns()
  1487. {
  1488. $selectJoinThenFrom = $this->_db->select();
  1489. $selectJoinThenFrom->joinLeft(array('b' => 'bar'), 'f.columnfoo2 = b.barcolumn2', array('baralias' => 'barcolumn'))
  1490. ->from(array('f' => 'foo'), array('columnfoo'))
  1491. ->from(array('d' => 'doo'), array('columndoo'));
  1492. return $selectJoinThenFrom;
  1493. }
  1494. public function testSerializeSelect()
  1495. {
  1496. /* checks if the adapter has effectively gotten serialized,
  1497. no exceptions are thrown here, so it's all right */
  1498. $serialize = serialize($this->_select());
  1499. $this->assertType('string',$serialize);
  1500. }
  1501. }