DbSelectTest.php 13 KB

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