1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
|
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Paginator
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
/**
* @see Zend_Paginator_Adapter_DbSelect
*/
require_once 'Zend/Paginator/Adapter/DbSelect.php';
/**
* @see Zend_Db_Adapter_Pdo_Sqlite
*/
require_once 'Zend/Db/Adapter/Pdo/Sqlite.php';
require_once 'Zend/Debug.php';
require_once 'Zend/Db/Adapter/Abstract.php';
/**
* @see PHPUnit_Framework_TestCase
*/
require_once dirname(__FILE__) . '/../_files/TestTable.php';
/**
* @category Zend
* @package Zend_Paginator
* @subpackage UnitTests
* @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @group Zend_Paginator
*/
class Zend_Paginator_Adapter_DbSelectTest extends PHPUnit_Framework_TestCase
{
/**
* @var Zend_Paginator_Adapter_DbSelect
*/
protected $_adapter;
/**
* @var Zend_Db_Adapter_Pdo_Sqlite
*/
protected $_db;
/**
* @var Zend_Db_Select
*/
protected $_query;
/**
* @var Zend_Db_Table_Abstract
*/
protected $_table;
/**
* Prepares the environment before running a test.
*/
protected function setUp()
{
if (!extension_loaded('pdo_sqlite')) {
$this->markTestSkipped('Pdo_Sqlite extension is not loaded');
}
parent::setUp();
$this->_db = new Zend_Db_Adapter_Pdo_Sqlite(array(
'dbname' => dirname(__FILE__) . '/../_files/test.sqlite'
));
$this->_table = new TestTable($this->_db);
$this->_query = $this->_db->select()->from('test')
->order('number ASC'); // ZF-3740
//->limit(1000, 0); // ZF-3727
$this->_adapter = new Zend_Paginator_Adapter_DbSelect($this->_query);
}
/**
* Cleans up the environment after running a test.
*/
protected function tearDown()
{
$this->_adapter = null;
parent::tearDown();
}
/**
* @group ZF-6989
*/
public function testCacheIdentifierIsHashOfAssembledSelect()
{
$dbAdapter = $this->getMockForAbstractClass('Zend_Db_Adapter_Abstract', array(''), '', false);
$select = new Zend_Db_Select($dbAdapter);
$select->from('ZF_6989');
$paginatorAdapter = new Zend_Paginator_Adapter_DbSelect($select);
$this->assertSame(md5($select->assemble()), $paginatorAdapter->getCacheIdentifier(),
'Cache identifier incorrect!');
}
public function testGetsItemsAtOffsetZero()
{
$actual = $this->_adapter->getItems(0, 10);
$i = 1;
foreach ($actual as $item) {
$this->assertEquals($i, $item['number']);
$i++;
}
}
public function testGetsItemsAtOffsetTen()
{
$actual = $this->_adapter->getItems(10, 10);
$i = 11;
foreach ($actual as $item) {
$this->assertEquals($i, $item['number']);
$i++;
}
}
public function testAcceptsIntegerValueForRowCount()
{
$this->_adapter->setRowCount(101);
$this->assertEquals(101, $this->_adapter->count());
}
public function testThrowsExceptionIfInvalidQuerySuppliedForRowCount()
{
try {
$this->_adapter->setRowCount($this->_db->select()->from('test'));
} catch (Exception $e) {
$this->assertTrue($e instanceof Zend_Paginator_Exception);
$this->assertContains('Row count column not found', $e->getMessage());
}
try {
$wrongcolumn = $this->_db->quoteIdentifier('wrongcolumn');
$expr = new Zend_Db_Expr("COUNT(*) AS $wrongcolumn");
$query = $this->_db->select($expr)->from('test');
$this->_adapter->setRowCount($query);
} catch (Exception $e) {
$this->assertTrue($e instanceof Zend_Paginator_Exception);
$this->assertEquals('Row count column not found', $e->getMessage());
}
}
public function testAcceptsQueryForRowCount()
{
$row_count_column = $this->_db->quoteIdentifier(Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN);
$expression = new Zend_Db_Expr("COUNT(*) AS $row_count_column");
$rowCount = clone $this->_query;
$rowCount->reset(Zend_Db_Select::COLUMNS)
->reset(Zend_Db_Select::ORDER) // ZF-3740
->reset(Zend_Db_Select::LIMIT_OFFSET) // ZF-3727
->reset(Zend_Db_Select::GROUP) // ZF-4001
->columns($expression);
$this->_adapter->setRowCount($rowCount);
$this->assertEquals(500, $this->_adapter->count());
}
public function testThrowsExceptionIfInvalidRowCountValueSupplied()
{
try {
$this->_adapter->setRowCount('invalid');
} catch (Exception $e) {
$this->assertTrue($e instanceof Zend_Paginator_Exception);
$this->assertEquals('Invalid row count', $e->getMessage());
}
}
public function testReturnsCorrectCountWithAutogeneratedQuery()
{
$expected = 500;
$actual = $this->_adapter->count();
$this->assertEquals($expected, $actual);
}
public function testDbTableSelectDoesNotThrowException()
{
$adapter = new Zend_Paginator_Adapter_DbSelect($this->_table->select());
$count = $adapter->count();
$this->assertEquals(500, $count);
}
/**
* @group ZF-4001
*/
public function testGroupByQueryReturnsOneRow()
{
$query = $this->_db->select()->from('test')
->order('number ASC')
->limit(1000, 0)
->group('number');
$adapter = new Zend_Paginator_Adapter_DbSelect($query);
$this->assertEquals(500, $adapter->count());
}
/**
* @group ZF-4001
*/
public function testGroupByQueryOnEmptyTableReturnsRowCountZero()
{
$db = new Zend_Db_Adapter_Pdo_Sqlite(array(
'dbname' => dirname(__FILE__) . '/../_files/testempty.sqlite'
));
$query = $db->select()->from('test')
->order('number ASC')
->limit(1000, 0);
$adapter = new Zend_Paginator_Adapter_DbSelect($query);
$this->assertEquals(0, $adapter->count());
}
/**
* @group ZF-4001
*/
public function testGroupByQueryReturnsCorrectResult()
{
$query = $this->_db->select()->from('test')
->order('number ASC')
->limit(1000, 0)
->group('testgroup');
$adapter = new Zend_Paginator_Adapter_DbSelect($query);
$this->assertEquals(2, $adapter->count());
}
/**
* @group ZF-4032
*/
public function testDistinctColumnQueryReturnsCorrectResult()
{
$query = $this->_db->select()->from('test', 'testgroup')
->order('number ASC')
->limit(1000, 0)
->distinct();
$adapter = new Zend_Paginator_Adapter_DbSelect($query);
$this->assertEquals(2, $adapter->count());
}
/**
* @group ZF-4094
*/
public function testSelectSpecificColumns()
{
$number = $this->_db->quoteIdentifier('number');
$query = $this->_db->select()->from('test', array('testgroup', 'number'))
->where("$number >= ?", '1');
$adapter = new Zend_Paginator_Adapter_DbSelect($query);
$this->assertEquals(500, $adapter->count());
}
/**
* @group ZF-4177
*/
public function testSelectDistinctAllUsesRegularCountAll()
{
$query = $this->_db->select()->from('test')
->distinct();
$adapter = new Zend_Paginator_Adapter_DbSelect($query);
$this->assertEquals(500, $adapter->count());
}
/**
* @group ZF-5233
*/
public function testSelectHasAliasedColumns()
{
$db = $this->_db;
$db->query('DROP TABLE IF EXISTS `sandboxTransaction`');
$db->query('DROP TABLE IF EXISTS `sandboxForeign`');
// A transaction table
$db->query(
'CREATE TABLE `sandboxTransaction` (
`id` INTEGER PRIMARY KEY,
`foreign_id` INT( 1 ) NOT NULL ,
`name` TEXT NOT NULL
) '
);
// A foreign table
$db->query(
'CREATE TABLE `sandboxForeign` (
`id` INTEGER PRIMARY KEY,
`name` TEXT NOT NULL
) '
);
// Insert some data
$db->insert('sandboxTransaction',
array(
'foreign_id' => 1,
'name' => 'transaction 1 with foreign_id 1',
)
);
$db->insert('sandboxTransaction',
array(
'foreign_id' => 1,
'name' => 'transaction 2 with foreign_id 1',
)
);
$db->insert('sandboxForeign',
array(
'name' => 'John Doe',
)
);
$db->insert('sandboxForeign',
array(
'name' => 'Jane Smith',
)
);
$query = $db->select()->from(array('a'=>'sandboxTransaction'), array())
->join(array('b'=>'sandboxForeign'), 'a.foreign_id = b.id', array('name'))
->distinct(true);
try {
$adapter = new Zend_Paginator_Adapter_DbSelect($query);
$adapter->count();
} catch (Exception $e) {
$this->fail($e->getMessage());
}
}
/**
* @group ZF-5956
*/
public function testUnionSelect()
{
$union = $this->_db->select()->union(array(
$this->_db->select()->from('test')->where('number <= 250'),
$this->_db->select()->from('test')->where('number > 250')
));
$adapter = new Zend_Paginator_Adapter_DbSelect($union);
$expected = 500;
$actual = $adapter->count();
$this->assertEquals($expected, $actual);
}
/**
* @group ZF-7045
*/
public function testGetCountSelect()
{
$union = $this->_db->select()->union(array(
$this->_db->select()->from('test')->where('number <= 250'),
$this->_db->select()->from('test')->where('number > 250')
));
$adapter = new Zend_Paginator_Adapter_DbSelect($union);
$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"';
$this->assertEquals($expected, $adapter->getCountSelect()->__toString());
}
/**
* @group ZF-5295
*/
public function testMultipleDistinctColumns()
{
$select = $this->_db->select()->from('test', array('testgroup', 'number'))
->distinct(true);
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$expected = 'SELECT COUNT(1) AS "zend_paginator_row_count" FROM (SELECT DISTINCT "test"."testgroup", "test"."number" FROM "test") AS "t"';
$this->assertEquals($expected, $adapter->getCountSelect()->__toString());
$this->assertEquals(500, $adapter->count());
}
/**
* @group ZF-5295
*/
public function testSingleDistinctColumn()
{
$select = $this->_db->select()->from('test', 'testgroup')
->distinct(true);
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$expected = 'SELECT COUNT(DISTINCT "test"."testgroup") AS "zend_paginator_row_count" FROM "test"';
$this->assertEquals($expected, $adapter->getCountSelect()->__toString());
$this->assertEquals(2, $adapter->count());
}
/**
* @group ZF-6330
*/
public function testGroupByMultipleColumns()
{
$select = $this->_db->select()->from('test', 'testgroup')
->group(array('number', 'testgroup'));
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$expected = 'SELECT COUNT(1) AS "zend_paginator_row_count" FROM (SELECT "test"."testgroup" FROM "test" GROUP BY "number"' . ",\n\t" . '"testgroup") AS "t"';
$this->assertEquals($expected, $adapter->getCountSelect()->__toString());
$this->assertEquals(500, $adapter->count());
}
/**
* @group ZF-6330
*/
public function testGroupBySingleColumn()
{
$select = $this->_db->select()->from('test', 'testgroup')
->group('test.testgroup');
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$expected = 'SELECT COUNT(DISTINCT "test"."testgroup") AS "zend_paginator_row_count" FROM "test"';
$this->assertEquals($expected, $adapter->getCountSelect()->__toString());
$this->assertEquals(2, $adapter->count());
}
/**
* @group ZF-6562
*/
public function testSelectWithHaving()
{
$select = $this->_db->select()->from('test')
->group('number')
->having('number > 250');
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$expected = 'SELECT COUNT(1) AS "zend_paginator_row_count" FROM (SELECT "test".* FROM "test" GROUP BY "number" HAVING (number > 250)) AS "t"';
$this->assertEquals($expected, $adapter->getCountSelect()->__toString());
$this->assertEquals(250, $adapter->count());
}
/**
* @group ZF-7127
*/
public function testMultipleGroupSelect()
{
$select = $this->_db->select()->from('test')
->group('testgroup')
->group('number')
->where('number > 250');
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$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"';
$this->assertEquals($expected, $adapter->getCountSelect()->__toString());
$this->assertEquals(250, $adapter->count());
}
/**
* @group ZF-10884
*/
public function testSetRowCountWithAlias()
{
$select = $this->_db->select();
$select->from('test', array(
Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN => new Zend_Db_Expr('COUNT(DISTINCT number)')
));
$this->_db->setProfiler(true);
$adapter = new Zend_Paginator_Adapter_DbSelect($this->_db->select());
$adapter->setRowCount($select);
$adapter->count();
$expected = 'SELECT COUNT(DISTINCT number) AS "zend_paginator_row_count" FROM "test"';
$lastQuery = $this->_db->getProfiler()
->getLastQueryProfile()
->getQuery();
$this->assertEquals($expected, $lastQuery);
}
/**
* @group ZF-7434
*/
public function testGroupByOneColumnWithZendExpr()
{
$select = $this->_db->select();
$select->from('test', 'testgroup')
->group(new Zend_Db_Expr('testgroup'));
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$this->assertEquals(2, $adapter->count());
}
/**
* @group ZF-10704
*/
public function testObjectSelectWithBind()
{
$select = $this->_db->select();
$select->from('test', array('number'))
->where('number = ?')
->distinct(true)
->bind(array(250));
$adapter = new Zend_Paginator_Adapter_DbSelect($select);
$this->assertEquals(1, $adapter->count());
$select->reset(Zend_Db_Select::DISTINCT);
$select2 = clone $select;
$select2->reset(Zend_Db_Select::WHERE)
->where('number = 500');
$selectUnion = $this->_db
->select()
->bind(array(250));
$selectUnion->union(array($select, $select2));
$adapter = new Zend_Paginator_Adapter_DbSelect($selectUnion);
$this->assertEquals(2, $adapter->count());
}
}
|