DbSelectTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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_Paginator
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 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_Paginator_Adapter_DbSelect
  24. */
  25. require_once 'Zend/Paginator/Adapter/DbSelect.php';
  26. /**
  27. * @see Zend_Db_Adapter_Pdo_Sqlite
  28. */
  29. require_once 'Zend/Db/Adapter/Pdo/Sqlite.php';
  30. require_once 'Zend/Debug.php';
  31. /**
  32. * @see PHPUnit_Framework_TestCase
  33. */
  34. require_once 'PHPUnit/Framework/TestCase.php';
  35. require_once dirname(__FILE__) . '/../_files/TestTable.php';
  36. /**
  37. * @category Zend
  38. * @package Zend_Paginator
  39. * @subpackage UnitTests
  40. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  41. * @license http://framework.zend.com/license/new-bsd New BSD License
  42. * @group Zend_Paginator
  43. */
  44. class Zend_Paginator_Adapter_DbSelectTest extends PHPUnit_Framework_TestCase
  45. {
  46. /**
  47. * @var Zend_Paginator_Adapter_DbSelect
  48. */
  49. protected $_adapter;
  50. /**
  51. * @var Zend_Db_Adapter_Pdo_Sqlite
  52. */
  53. protected $_db;
  54. /**
  55. * @var Zend_Db_Select
  56. */
  57. protected $_query;
  58. /**
  59. * @var Zend_Db_Table_Abstract
  60. */
  61. protected $_table;
  62. /**
  63. * Prepares the environment before running a test.
  64. */
  65. protected function setUp()
  66. {
  67. if (!extension_loaded('pdo_sqlite')) {
  68. $this->markTestSkipped('Pdo_Sqlite extension is not loaded');
  69. }
  70. parent::setUp();
  71. $this->_db = new Zend_Db_Adapter_Pdo_Sqlite(array(
  72. 'dbname' => dirname(__FILE__) . '/../_files/test.sqlite'
  73. ));
  74. $this->_table = new TestTable($this->_db);
  75. $this->_query = $this->_db->select()->from('test')
  76. ->order('number ASC'); // ZF-3740
  77. //->limit(1000, 0); // ZF-3727
  78. $this->_adapter = new Zend_Paginator_Adapter_DbSelect($this->_query);
  79. }
  80. /**
  81. * Cleans up the environment after running a test.
  82. */
  83. protected function tearDown()
  84. {
  85. $this->_adapter = null;
  86. parent::tearDown();
  87. }
  88. public function testGetsItemsAtOffsetZero()
  89. {
  90. $actual = $this->_adapter->getItems(0, 10);
  91. $i = 1;
  92. foreach ($actual as $item) {
  93. $this->assertEquals($i, $item['number']);
  94. $i++;
  95. }
  96. }
  97. public function testGetsItemsAtOffsetTen()
  98. {
  99. $actual = $this->_adapter->getItems(10, 10);
  100. $i = 11;
  101. foreach ($actual as $item) {
  102. $this->assertEquals($i, $item['number']);
  103. $i++;
  104. }
  105. }
  106. public function testAcceptsIntegerValueForRowCount()
  107. {
  108. $this->_adapter->setRowCount(101);
  109. $this->assertEquals(101, $this->_adapter->count());
  110. }
  111. public function testThrowsExceptionIfInvalidQuerySuppliedForRowCount()
  112. {
  113. try {
  114. $this->_adapter->setRowCount($this->_db->select()->from('test'));
  115. } catch (Exception $e) {
  116. $this->assertType('Zend_Paginator_Exception', $e);
  117. $this->assertContains('Row count column not found', $e->getMessage());
  118. }
  119. try {
  120. $wrongcolumn = $this->_db->quoteIdentifier('wrongcolumn');
  121. $expr = new Zend_Db_Expr("COUNT(*) AS $wrongcolumn");
  122. $query = $this->_db->select($expr)->from('test');
  123. $this->_adapter->setRowCount($query);
  124. } catch (Exception $e) {
  125. $this->assertType('Zend_Paginator_Exception', $e);
  126. $this->assertEquals('Row count column not found', $e->getMessage());
  127. }
  128. }
  129. public function testAcceptsQueryForRowCount()
  130. {
  131. $row_count_column = $this->_db->quoteIdentifier(Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN);
  132. $expression = new Zend_Db_Expr("COUNT(*) AS $row_count_column");
  133. $rowCount = clone $this->_query;
  134. $rowCount->reset(Zend_Db_Select::COLUMNS)
  135. ->reset(Zend_Db_Select::ORDER) // ZF-3740
  136. ->reset(Zend_Db_Select::LIMIT_OFFSET) // ZF-3727
  137. ->reset(Zend_Db_Select::GROUP) // ZF-4001
  138. ->columns($expression);
  139. $this->_adapter->setRowCount($rowCount);
  140. $this->assertEquals(500, $this->_adapter->count());
  141. }
  142. public function testThrowsExceptionIfInvalidRowCountValueSupplied()
  143. {
  144. try {
  145. $this->_adapter->setRowCount('invalid');
  146. } catch (Exception $e) {
  147. $this->assertType('Zend_Paginator_Exception', $e);
  148. $this->assertEquals('Invalid row count', $e->getMessage());
  149. }
  150. }
  151. public function testReturnsCorrectCountWithAutogeneratedQuery()
  152. {
  153. $expected = 500;
  154. $actual = $this->_adapter->count();
  155. $this->assertEquals($expected, $actual);
  156. }
  157. public function testDbTableSelectDoesNotThrowException()
  158. {
  159. $adapter = new Zend_Paginator_Adapter_DbSelect($this->_table->select());
  160. $count = $adapter->count();
  161. $this->assertEquals(500, $count);
  162. }
  163. /**
  164. * @group ZF-4001
  165. */
  166. public function testGroupByQueryReturnsOneRow()
  167. {
  168. $query = $this->_db->select()->from('test')
  169. ->order('number ASC')
  170. ->limit(1000, 0)
  171. ->group('number');
  172. $adapter = new Zend_Paginator_Adapter_DbSelect($query);
  173. $this->assertEquals(500, $adapter->count());
  174. }
  175. /**
  176. * @group ZF-4001
  177. */
  178. public function testGroupByQueryOnEmptyTableReturnsRowCountZero()
  179. {
  180. $db = new Zend_Db_Adapter_Pdo_Sqlite(array(
  181. 'dbname' => dirname(__FILE__) . '/../_files/testempty.sqlite'
  182. ));
  183. $query = $db->select()->from('test')
  184. ->order('number ASC')
  185. ->limit(1000, 0);
  186. $adapter = new Zend_Paginator_Adapter_DbSelect($query);
  187. $this->assertEquals(0, $adapter->count());
  188. }
  189. /**
  190. * @group ZF-4001
  191. */
  192. public function testGroupByQueryReturnsCorrectResult()
  193. {
  194. $query = $this->_db->select()->from('test')
  195. ->order('number ASC')
  196. ->limit(1000, 0)
  197. ->group('testgroup');
  198. $adapter = new Zend_Paginator_Adapter_DbSelect($query);
  199. $this->assertEquals(2, $adapter->count());
  200. }
  201. /**
  202. * @group ZF-4032
  203. */
  204. public function testDistinctColumnQueryReturnsCorrectResult()
  205. {
  206. $query = $this->_db->select()->from('test', 'testgroup')
  207. ->order('number ASC')
  208. ->limit(1000, 0)
  209. ->distinct();
  210. $adapter = new Zend_Paginator_Adapter_DbSelect($query);
  211. $this->assertEquals(2, $adapter->count());
  212. }
  213. /**
  214. * @group ZF-4094
  215. */
  216. public function testSelectSpecificColumns()
  217. {
  218. $number = $this->_db->quoteIdentifier('number');
  219. $query = $this->_db->select()->from('test', array('testgroup', 'number'))
  220. ->where("$number >= ?", '1');
  221. $adapter = new Zend_Paginator_Adapter_DbSelect($query);
  222. $this->assertEquals(500, $adapter->count());
  223. }
  224. /**
  225. * @group ZF-4177
  226. */
  227. public function testSelectDistinctAllUsesRegularCountAll()
  228. {
  229. $query = $this->_db->select()->from('test')
  230. ->distinct();
  231. $adapter = new Zend_Paginator_Adapter_DbSelect($query);
  232. $this->assertEquals(500, $adapter->count());
  233. }
  234. /**
  235. * @group ZF-5233
  236. */
  237. public function testSelectHasAliasedColumns()
  238. {
  239. $db = $this->_db;
  240. $db->query('DROP TABLE IF EXISTS `sandboxTransaction`');
  241. $db->query('DROP TABLE IF EXISTS `sandboxForeign`');
  242. // A transaction table
  243. $db->query(
  244. 'CREATE TABLE `sandboxTransaction` (
  245. `id` INTEGER PRIMARY KEY,
  246. `foreign_id` INT( 1 ) NOT NULL ,
  247. `name` TEXT NOT NULL
  248. ) '
  249. );
  250. // A foreign table
  251. $db->query(
  252. 'CREATE TABLE `sandboxForeign` (
  253. `id` INTEGER PRIMARY KEY,
  254. `name` TEXT NOT NULL
  255. ) '
  256. );
  257. // Insert some data
  258. $db->insert('sandboxTransaction',
  259. array(
  260. 'foreign_id' => 1,
  261. 'name' => 'transaction 1 with foreign_id 1',
  262. )
  263. );
  264. $db->insert('sandboxTransaction',
  265. array(
  266. 'foreign_id' => 1,
  267. 'name' => 'transaction 2 with foreign_id 1',
  268. )
  269. );
  270. $db->insert('sandboxForeign',
  271. array(
  272. 'name' => 'John Doe',
  273. )
  274. );
  275. $db->insert('sandboxForeign',
  276. array(
  277. 'name' => 'Jane Smith',
  278. )
  279. );
  280. $query = $db->select()->from(array('a'=>'sandboxTransaction'), array())
  281. ->join(array('b'=>'sandboxForeign'), 'a.foreign_id = b.id', array('name'))
  282. ->distinct(true);
  283. try {
  284. $adapter = new Zend_Paginator_Adapter_DbSelect($query);
  285. $adapter->count();
  286. } catch (Exception $e) {
  287. $this->fail($e->getMessage());
  288. }
  289. }
  290. /**
  291. * @group ZF-5956
  292. */
  293. public function testUnionSelect()
  294. {
  295. $union = $this->_db->select()->union(array(
  296. $this->_db->select()->from('test')->where('number <= 250'),
  297. $this->_db->select()->from('test')->where('number > 250')
  298. ));
  299. $adapter = new Zend_Paginator_Adapter_DbSelect($union);
  300. $expected = 500;
  301. $actual = $adapter->count();
  302. $this->assertEquals($expected, $actual);
  303. }
  304. /**
  305. * @group ZF-7045
  306. */
  307. public function testGetCountSelect()
  308. {
  309. $union = $this->_db->select()->union(array(
  310. $this->_db->select()->from('test')->where('number <= 250'),
  311. $this->_db->select()->from('test')->where('number > 250')
  312. ));
  313. $adapter = new Zend_Paginator_Adapter_DbSelect($union);
  314. $expected = 'SELECT COUNT(1) AS "zend_paginator_row_count" FROM (SELECT "test".* FROM "test" WHERE (number <= 250) UNION SELECT "test".* FROM "test" WHERE (number > 250)) AS "t"';
  315. $this->assertEquals($expected, $adapter->getCountSelect()->__toString());
  316. }
  317. /**
  318. * @group ZF-5295
  319. */
  320. public function testMultipleDistinctColumns()
  321. {
  322. $select = $this->_db->select()->from('test', array('testgroup', 'number'))
  323. ->distinct(true);
  324. $adapter = new Zend_Paginator_Adapter_DbSelect($select);
  325. $expected = 'SELECT COUNT(1) AS "zend_paginator_row_count" FROM (SELECT DISTINCT "test"."testgroup", "test"."number" FROM "test") AS "t"';
  326. $this->assertEquals($expected, $adapter->getCountSelect()->__toString());
  327. $this->assertEquals(500, $adapter->count());
  328. }
  329. /**
  330. * @group ZF-5295
  331. */
  332. public function testSingleDistinctColumn()
  333. {
  334. $select = $this->_db->select()->from('test', 'testgroup')
  335. ->distinct(true);
  336. $adapter = new Zend_Paginator_Adapter_DbSelect($select);
  337. $expected = 'SELECT COUNT(DISTINCT "test"."testgroup") AS "zend_paginator_row_count" FROM "test"';
  338. $this->assertEquals($expected, $adapter->getCountSelect()->__toString());
  339. $this->assertEquals(2, $adapter->count());
  340. }
  341. /**
  342. * @group ZF-6330
  343. */
  344. public function testGroupByMultipleColumns()
  345. {
  346. $select = $this->_db->select()->from('test', 'testgroup')
  347. ->group(array('number', 'testgroup'));
  348. $adapter = new Zend_Paginator_Adapter_DbSelect($select);
  349. $expected = 'SELECT COUNT(1) AS "zend_paginator_row_count" FROM (SELECT "test"."testgroup" FROM "test" GROUP BY "number"' . ",\n\t" . '"testgroup") AS "t"';
  350. $this->assertEquals($expected, $adapter->getCountSelect()->__toString());
  351. $this->assertEquals(500, $adapter->count());
  352. }
  353. /**
  354. * @group ZF-6330
  355. */
  356. public function testGroupBySingleColumn()
  357. {
  358. $select = $this->_db->select()->from('test', 'testgroup')
  359. ->group('test.testgroup');
  360. $adapter = new Zend_Paginator_Adapter_DbSelect($select);
  361. $expected = 'SELECT COUNT(DISTINCT "test"."testgroup") AS "zend_paginator_row_count" FROM "test"';
  362. $this->assertEquals($expected, $adapter->getCountSelect()->__toString());
  363. $this->assertEquals(2, $adapter->count());
  364. }
  365. /**
  366. * @group ZF-6562
  367. */
  368. public function testSelectWithHaving()
  369. {
  370. $select = $this->_db->select()->from('test')
  371. ->group('number')
  372. ->having('number > 250');
  373. $adapter = new Zend_Paginator_Adapter_DbSelect($select);
  374. $expected = 'SELECT COUNT(1) AS "zend_paginator_row_count" FROM (SELECT "test".* FROM "test" GROUP BY "number" HAVING (number > 250)) AS "t"';
  375. $this->assertEquals($expected, $adapter->getCountSelect()->__toString());
  376. $this->assertEquals(250, $adapter->count());
  377. }
  378. /**
  379. * @group ZF-7127
  380. */
  381. public function testMultipleGroupSelect()
  382. {
  383. $select = $this->_db->select()->from('test')
  384. ->group('testgroup')
  385. ->group('number')
  386. ->where('number > 250');
  387. $adapter = new Zend_Paginator_Adapter_DbSelect($select);
  388. $expected = 'SELECT COUNT(1) AS "zend_paginator_row_count" FROM (SELECT "test".* FROM "test" WHERE (number > 250) GROUP BY "testgroup"' . ",\n\t" . '"number") AS "t"';
  389. $this->assertEquals($expected, $adapter->getCountSelect()->__toString());
  390. $this->assertEquals(250, $adapter->count());
  391. }
  392. }