PageRenderTime 26ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/Zend/Paginator/Adapter/DbSelectTest.php

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