Skip to content

Instantly share code, notes, and snippets.

@KorsaR-ZN
Created May 5, 2015 18:57
Show Gist options
  • Save KorsaR-ZN/e649cf5a1325112472d3 to your computer and use it in GitHub Desktop.
Save KorsaR-ZN/e649cf5a1325112472d3 to your computer and use it in GitHub Desktop.
Phalcon Paginator tests
<?php
/*
+------------------------------------------------------------------------+
| Phalcon Framework |
+------------------------------------------------------------------------+
| Copyright (c) 2011-2015 Phalcon Team (http://www.phalconphp.com) |
+------------------------------------------------------------------------+
| This source file is subject to the New BSD License that is bundled |
| with this package in the file docs/LICENSE.txt. |
| |
| 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@phalconphp.com so we can send you a copy immediately. |
+------------------------------------------------------------------------+
| Authors: Andres Gutierrez <andres@phalconphp.com> |
| Eduar Carvajal <eduar@phalconphp.com> |
+------------------------------------------------------------------------+
*/
use Phalcon\Logger\Adapter\File as FileLogger,
Phalcon\Db\Adapter\Pdo\Mysql as DbAdapter,
Phalcon\Events\Manager as EventsManager;
class PaginatorTest extends PHPUnit_Framework_TestCase
{
public function __construct()
{
spl_autoload_register(array($this, 'modelsAutoloader'));
}
public function __destruct()
{
spl_autoload_unregister(array($this, 'modelsAutoloader'));
}
public function modelsAutoloader($className)
{
if (file_exists('unit-tests/models/' . $className . '.php')) {
require 'unit-tests/models/' . $className . '.php';
}
}
protected function _loadDI()
{
Phalcon\DI::reset();
$di = new Phalcon\DI();
$di->set('modelsManager', function() {
return new Phalcon\Mvc\Model\Manager();
});
$di->set('modelsMetadata', function() {
return new Phalcon\Mvc\Model\Metadata\Memory();
});
$di->set('db', function() {
require 'unit-tests/config.db.php';
return new Phalcon\Db\Adapter\Pdo\Mysql($configMysql);
}, true);
/*$di->set('db', function() {
$eventsManager = new EventsManager();
$logger = new FileLogger("debug.log");
//Listen all the database events
$eventsManager->attach('db', function($event, $connection) use ($logger) {
if ($event->getType() == 'beforeQuery') {
$logger->log($connection->getSQLStatement(), \Phalcon\Logger::INFO);
}
});
$connection = new DbAdapter(array(
"host" => "localhost",
"username" => "root",
"password" => "",
"dbname" => "phalcon_test"
));
//Assign the eventsManager to the db adapter instance
$connection->setEventsManager($eventsManager);
return $connection;
}, true);*/
return $di;
}
public function testArrayPaginator()
{
$personas = array(
0 => array(
'id' => 1,
'name' => 'PETER'
),
1 => array(
'id' => 2,
'name' => 'PETER'
),
2 => array(
'id' => 3,
'name' => 'PETER'
),
3 => array(
'id' => 4,
'name' => 'PETER'
),
4 => array(
'id' => 5,
'name' => 'PETER'
),
5 => array(
'id' => 6,
'name' => 'PETER'
),
6 => array(
'id' => 7,
'name' => 'PETER'
),
7 => array(
'id' => 8,
'name' => 'PETER'
),
8 => array(
'id' => 9,
'name' => 'PETER'
),
9 => array(
'id' => 10,
'name' => 'PETER'
),
10 => array(
'id' => 11,
'name' => 'PETER'
),
11 => array(
'id' => 12,
'name' => 'PETER'
),
12 => array(
'id' => 13,
'name' => 'PETER'
),
13 => array(
'id' => 14,
'name' => 'PETER'
),
14 => array(
'id' => 15,
'name' => 'PETER'
),
15 => array(
'id' => 16,
'name' => 'PETER'
),
16 => array(
'id' => 17,
'name' => 'PETER'
),
17 => array(
'id' => 18,
'name' => 'PETER'
)
);
$paginator = new Phalcon\Paginator\Adapter\NativeArray(array(
'data' => $personas,
'limit' => 3,
'page' => 1
));
$this->_executeTests($paginator, $personas);
}
public function testArrayPaginator_t445()
{
$paginator = new \Phalcon\Paginator\Adapter\NativeArray(array(
"data" => array_fill(0, 30, 'banana'),
"limit"=> 25,
"page" => 1,
));
$page = $paginator->getPaginate();
$this->assertEquals(get_class($page), 'stdClass');
$this->assertEquals(count($page->items), 25);
$this->assertEquals($page->before, 1);
$this->assertEquals($page->next, 2);
$this->assertEquals($page->last, 2);
$this->assertEquals($page->limit, 25);
$this->assertEquals($page->current, 1);
$this->assertEquals($page->total_pages, 2);
$paginator->setCurrentPage(2);
$page = $paginator->getPaginate();
$this->assertEquals(get_class($page), 'stdClass');
$this->assertEquals(count($page->items), 5);
$this->assertEquals($page->before, 1);
$this->assertEquals($page->next, 2);
$this->assertEquals($page->last, 2);
$this->assertEquals($page->current, 2);
$this->assertEquals($page->total_pages, 2);
}
public function testModelPaginator()
{
require 'unit-tests/config.db.php';
if (empty($configMysql)) {
$this->markTestSkipped('Test skipped');
return;
}
$this->_loadDI();
$personnes = Personnes::find();
$paginator = new Phalcon\Paginator\Adapter\Model(array(
'data' => $personnes,
'limit' => 10,
'page' => 1
));
$this->_executeTests($paginator, $personnes);
}
public function testModelPaginatorBind()
{
require 'unit-tests/config.db.php';
if (empty($configMysql)) {
$this->markTestSkipped('Test skipped');
return;
}
$this->_loadDI();
$personnes = Personnes::find(array(
"conditions" => "cedula >=:d1: AND cedula>=:d2: ",
"bind" => array("d1" => '1', "d2" => "5"),
"order" => "cedula, nombres",
"limit" => "33"
));
$paginator = new Phalcon\Paginator\Adapter\Model(array(
'data' => $personnes,
'limit' => 10,
'page' => 1
));
//First Page
$page = $paginator->getPaginate();
$this->assertEquals(get_class($page), 'stdClass');
$this->assertEquals(count($page->items), 10);
$this->assertEquals($page->before, 1);
$this->assertEquals($page->next, 2);
$this->assertEquals($page->last, 4);
$this->assertEquals($page->limit, 10);
$this->assertEquals($page->current, 1);
$this->assertEquals($page->total_pages, 4);
}
public function testQueryBuilderPaginator()
{
require 'unit-tests/config.db.php';
if (empty($configMysql)) {
$this->markTestSkipped('Test skipped');
return;
}
$di = $this->_loadDI();
$builder = $di['modelsManager']->createBuilder()
->columns('cedula, nombres')
->from('Personnes')
->orderBy('cedula');
$paginator = new Phalcon\Paginator\Adapter\QueryBuilder(array(
"builder" => $builder,
"limit"=> 10,
"page" => 1
));
$page = $paginator->getPaginate();
$this->assertEquals(get_class($page), 'stdClass');
$this->assertEquals(count($page->items), 10);
$this->assertEquals($page->before, 1);
$this->assertEquals($page->next, 2);
$this->assertEquals($page->last, 218);
$this->assertEquals($page->limit, 10);
$this->assertEquals($page->current, 1);
$this->assertEquals($page->total_pages, 218);
$this->assertInternalType('int', $page->total_items);
$this->assertInternalType('int', $page->total_pages);
//Middle page
$paginator->setCurrentPage(100);
$page = $paginator->getPaginate();
$this->assertEquals(get_class($page), 'stdClass');
$this->assertEquals(count($page->items), 10);
$this->assertEquals($page->before, 99);
$this->assertEquals($page->next, 101);
$this->assertEquals($page->last, 218);
$this->assertEquals($page->current, 100);
$this->assertEquals($page->total_pages, 218);
$this->assertInternalType('int', $page->total_items);
$this->assertInternalType('int', $page->total_pages);
//Last page
$paginator->setCurrentPage(218);
$page = $paginator->getPaginate();
$this->assertEquals(get_class($page), 'stdClass');
$this->assertEquals(count($page->items), 10);
$this->assertEquals($page->before, 217);
$this->assertEquals($page->next, 218);
$this->assertEquals($page->last, 218);
$this->assertEquals($page->current, 218);
$this->assertEquals($page->total_pages, 218);
$this->assertInternalType('int', $page->total_items);
$this->assertInternalType('int', $page->total_pages);
// test of getter/setters of querybuilder adapter
// -- current page --
$currentPage = $paginator->getCurrentPage();
$this->assertEquals($currentPage, 218);
// -- limit --
$rowsLimit = $paginator->getLimit();
$this->assertEquals($rowsLimit, 10);
$setterResult = $paginator->setLimit(25);
$rowsLimit = $paginator->getLimit();
$this->assertEquals($rowsLimit, 25);
$this->assertEquals($setterResult, $paginator);
// -- builder --
$queryBuilder = $paginator->getQueryBuilder();
$this->assertEquals($builder, $queryBuilder);
$builder2 = $di['modelsManager']->createBuilder()
->columns('cedula, nombres')
->from('Personnes');
$setterResult = $paginator->setQueryBuilder($builder2);
$queryBuilder = $paginator->getQueryBuilder();
$this->assertEquals($builder2, $queryBuilder);
$this->assertEquals($setterResult, $paginator);
}
protected function _executeTests(Phalcon\Paginator\AdapterInterface $adapter, $data)
{
$limit = $adapter->getLimit();
$count = count($data);
$pageTotal = ceil($count / $limit);
// Gets random page
$pages = range(1, $pageTotal);
shuffle($pages);
$pages = array_slice($pages, 0, 3);
// Test pages
foreach ($pages as $currentPage) {
$adapter->setCurrentPage($currentPage);
$page = $adapter->getPaginate();
$this->assertEquals(get_class($page), 'stdClass');
$this->assertEquals($page->first, 1);
$this->assertEquals($page->last, $pageTotal);
$this->assertEquals($page->current, $currentPage);
$this->assertEquals(count($page->items), $limit);
$this->assertEquals($page->total_pages, $pageTotal);
if ($currentPage > $page->first) {
$this->assertEquals($page->before, $currentPage - 1);
}
if ($currentPage < $page->last) {
$this->assertEquals($page->next, $currentPage + 1);
}
$offset = ($currentPage - 1) * $limit;
$this->_testEquals($page->items, $data, $offset, $limit);
}
}
protected function _testEquals($items, $data, $offset, $limit)
{
if (is_array($data)) {
$this->assertEquals($items, array_slice($data, $offset, $limit));
return;
}
if (is_object($data) && ($data instanceof \Phalcon\Mvc\Model\Resultset)) {
$origin = array();
$position = 0;
$maxPosition = $offset + $limit;
// TODO: Replace on \Phalcon\Mvc\Model\Resultset::seek() after fix #10100
foreach ($data as $row) {
if ($position >= $offset) {
$origin[] = $row;
}
if (++$position >= $maxPosition) {
break;
}
}
$this->assertEquals($items, $origin);
return;
}
$this->markTestSkipped('Test skipped');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment