[limb-svn] r6517 - in 3.x/trunk/limb/macro: src/tags/pager tests/cases/tags

svn at limb-project.com svn at limb-project.com
Mon Nov 12 10:44:07 MSK 2007


Author: serega
Date: 2007-11-12 10:44:05 +0300 (Mon, 12 Nov 2007)
New Revision: 6517
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6517

Added:
   3.x/trunk/limb/macro/tests/cases/tags/lmbMacroPagerHelperTest.class.php
Modified:
   3.x/trunk/limb/macro/src/tags/pager/current.tag.php
   3.x/trunk/limb/macro/src/tags/pager/lmbMacroPagerHelper.class.php
   3.x/trunk/limb/macro/src/tags/pager/next.tag.php
   3.x/trunk/limb/macro/src/tags/pager/pager.tag.php
   3.x/trunk/limb/macro/src/tags/pager/prev.tag.php
Log:
-- some refactorings in lmbMacroPagerHelper
-- added tests for lmbMacroPagerHelper

Modified: 3.x/trunk/limb/macro/src/tags/pager/current.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/pager/current.tag.php	2007-11-10 11:58:17 UTC (rev 6516)
+++ 3.x/trunk/limb/macro/src/tags/pager/current.tag.php	2007-11-12 07:44:05 UTC (rev 6517)
@@ -20,7 +20,7 @@
 
     $code->writePhp("if ({$pager}->isDisplayedPage()) {\n");
 
-    $code->writePhp("\$href = {$pager}->getDisplayedPageUri();\n");
+    $code->writePhp("\$href = {$pager}->getCurrentPageUri();\n");
     $code->writePhp("\$number = {$pager}->getPage();\n");
 
     parent :: generate($code);

Modified: 3.x/trunk/limb/macro/src/tags/pager/lmbMacroPagerHelper.class.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/pager/lmbMacroPagerHelper.class.php	2007-11-10 11:58:17 UTC (rev 6516)
+++ 3.x/trunk/limb/macro/src/tags/pager/lmbMacroPagerHelper.class.php	2007-11-12 07:44:05 UTC (rev 6517)
@@ -20,7 +20,7 @@
   protected $total_items = 0;
   protected $total_page_count = 0;
   protected $page_counter = 0;
-  protected $displayed_page = 0;
+  protected $current_page = 0;
   protected $items_per_page = 20;
   protected $base_url = null;
 
@@ -34,6 +34,8 @@
   function __construct($id)
   {
     $this->id = $id;
+
+    $this->_initBaseUrl();
   }
 
   function prepare()
@@ -43,7 +45,7 @@
     if ($this->total_page_count < 1)
       $this->total_page_count = 1;
 
-    $this->_initBaseUrl();
+    $this->_initCurrentPage();
 
     $this->page_counter = 1;
   }
@@ -108,25 +110,23 @@
   {
     $this->items_per_page = $items;
   }
-
-  //implementing WACT pager interface
-  function getStartingItem()
+  
+  function setCurrentPage($page)
   {
-    $number = $this->getDisplayedPageBeginItem();
-    return ($number == 0) ? 0 : $number - 1;
+    $this->current_page = $page;
   }
 
-  function getDisplayedPageBeginItem()
+  function getCurrentPageBeginItem()
   {
     if($this->total_items < 1)
       return 0;
 
-    return $this->items_per_page * ($this->displayed_page - 1) + 1;
+    return $this->items_per_page * ($this->current_page - 1) + 1;
   }
 
-  function getDisplayedPageEndItem()
+  function getCurrentPageEndItem()
   {
-    $res = $this->items_per_page * $this->displayed_page;
+    $res = $this->items_per_page * $this->current_page;
 
     if($res > $this->total_items)
       return $this->total_items;
@@ -146,22 +146,22 @@
 
   function isFirst()
   {
-    return ($this->displayed_page == 1);
+    return ($this->current_page == 1);
   }
 
   function hasPrev()
   {
-    return ($this->displayed_page > 1);
+    return ($this->current_page > 1);
   }
 
   function hasNext()
   {
-    return ($this->displayed_page < $this->total_page_count);
+    return ($this->current_page < $this->total_page_count);
   }
 
   function isLast()
   {
-    return ($this->displayed_page == $this->total_page_count);
+    return ($this->current_page == $this->total_page_count);
   }
 
   protected function _initBaseUrl()
@@ -169,18 +169,19 @@
     $this->base_url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
     $pos = strpos($this->base_url, '?');
     if (is_integer($pos))
-        $this->base_url = substr($this->base_url, 0, $pos);
+      $this->base_url = substr($this->base_url, 0, $pos);
+  }
+  
+  protected function _initCurrentPage()
+  {
+    if(!$this->current_page && isset($_GET[$this->id]))
+      $this->current_page = $_GET[$this->id];
 
-    $this->displayed_page = @$_GET[$this->id];
-    if (empty($this->displayed_page)) {
-        $this->displayed_page = 1;
-    }
+    if (empty($this->current_page))
+      $this->current_page = 1;
 
-    if (empty($this->displayed_page))
-      $this->displayed_page = 1;
-
-    if($this->displayed_page > $this->total_page_count)
-      $this->displayed_page = $this->total_page_count;
+    if($this->current_page > $this->total_page_count)
+      $this->current_page = $this->total_page_count;
   }
 
   function nextPage()
@@ -209,7 +210,7 @@
 
   function isDisplayedPage()
   {
-    return $this->page_counter == $this->displayed_page;
+    return $this->page_counter == $this->current_page;
   }
 
   function shouldDisplayPage()
@@ -221,12 +222,12 @@
     return (
         $this->page_counter <= $this->pages_in_sides ||
         $this->page_counter > $this->total_page_count - $this->pages_in_sides ||
-        ($this->page_counter >= $this->displayed_page - $half_windows_size &&
-        $this->page_counter <= $this->displayed_page + $half_windows_size) ||
+        ($this->page_counter >= $this->current_page - $half_windows_size &&
+        $this->page_counter <= $this->current_page + $half_windows_size) ||
         ($this->page_counter == $this->pages_in_sides + 1 &&
-        $this->page_counter == $this->displayed_page - $half_windows_size - 1) ||
+        $this->page_counter == $this->current_page - $half_windows_size - 1) ||
         ($this->page_counter == $this->total_page_count - $this->pages_in_sides &&
-        $this->page_counter == $this->displayed_page + $half_windows_size + 1));
+        $this->page_counter == $this->current_page + $half_windows_size + 1));
   }
 
   function isDisplayedSection()
@@ -244,7 +245,7 @@
 
   function getDisplayedSection()
   {
-    return ceil($this->displayed_page / $this->pages_per_section);
+    return ceil($this->current_page / $this->pages_per_section);
   }
 
   function getSectionUri()
@@ -277,14 +278,14 @@
     return $result;
   }
 
-  function getDisplayedPageUri()
+  function getCurrentPageUri()
   {
-    return $this->getPageUri($this->displayed_page);
+    return $this->getPageUri($this->current_page);
   }
 
-  function getDisplayedPage()
+  function getCurrentPage()
   {
-    return $this->displayed_page;
+    return $this->current_page;
   }
 
   function getPageUri($page = null)

Modified: 3.x/trunk/limb/macro/src/tags/pager/next.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/pager/next.tag.php	2007-11-10 11:58:17 UTC (rev 6516)
+++ 3.x/trunk/limb/macro/src/tags/pager/next.tag.php	2007-11-12 07:44:05 UTC (rev 6517)
@@ -19,7 +19,7 @@
     $pager = $this->findParentByClass('lmbMacroPagerTag')->getPagerVar();
     
     $code->writePhp("if ({$pager}->hasNext()) {\n");
-    $code->writePhp("\$href = {$pager}->getPageUri({$pager}->getDisplayedPage() + 1 );\n");
+    $code->writePhp("\$href = {$pager}->getPageUri({$pager}->getCurrentPage() + 1 );\n");
 
     parent :: generate($code);
 

Modified: 3.x/trunk/limb/macro/src/tags/pager/pager.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/pager/pager.tag.php	2007-11-10 11:58:17 UTC (rev 6516)
+++ 3.x/trunk/limb/macro/src/tags/pager/pager.tag.php	2007-11-12 07:44:05 UTC (rev 6517)
@@ -23,6 +23,8 @@
     $pager = $this->getPagerVar();
     
     $this->_generatePagerHelperWithInitialParams($code, $pager);
+
+    $code->writePhp("{$pager}->prepare();\n");
     
     $this->_generatePagerVariables($code, $pager);
     
@@ -63,8 +65,6 @@
       if ($pages_per_section = $this->getEscaped('pages_per_section'))
         $code->writeToInit("{$pager}->setPagesPerSection({$pages_per_section});\n");
     }
-    
-    $code->writeToInit("{$pager}->prepare();\n");
   }
   
   protected function _generatePagerVariables($code, $pager)
@@ -72,8 +72,8 @@
      $code->writePhp("\$total_items = {$pager}->getTotalItems();\n");
      $code->writePhp("\$total_pages = {$pager}->getTotalPages();\n");
      $code->writePhp("\$items_per_page = {$pager}->getItemsPerPage();\n");
-     $code->writePhp("\$begin_item_number = {$pager}->getDisplayedPageBeginItem();\n");
-     $code->writePhp("\$end_item_number = {$pager}->getDisplayedPageEndItem();\n");
+     $code->writePhp("\$begin_item_number = {$pager}->getCurrentPageBeginItem();\n");
+     $code->writePhp("\$end_item_number = {$pager}->getCurrentPageEndItem();\n");
   }
 
   function getPagerVar()

Modified: 3.x/trunk/limb/macro/src/tags/pager/prev.tag.php
===================================================================
--- 3.x/trunk/limb/macro/src/tags/pager/prev.tag.php	2007-11-10 11:58:17 UTC (rev 6516)
+++ 3.x/trunk/limb/macro/src/tags/pager/prev.tag.php	2007-11-12 07:44:05 UTC (rev 6517)
@@ -19,7 +19,7 @@
     $pager = $this->findParentByClass('lmbMacroPagerTag')->getPagerVar();
     
     $code->writePhp("if ({$pager}->hasPrev()) {\n");
-    $code->writePhp("\$href = {$pager}->getPageUri({$pager}->getDisplayedPage() - 1 );\n");
+    $code->writePhp("\$href = {$pager}->getPageUri({$pager}->getCurrentPage() - 1 );\n");
     
     parent :: generate($code);
     

Added: 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroPagerHelperTest.class.php
===================================================================
--- 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroPagerHelperTest.class.php	                        (rev 0)
+++ 3.x/trunk/limb/macro/tests/cases/tags/lmbMacroPagerHelperTest.class.php	2007-11-12 07:44:05 UTC (rev 6517)
@@ -0,0 +1,262 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com 
+ * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
+ * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
+ */
+
+require_once('limb/macro/src/tags/pager/lmbMacroPagerHelper.class.php');
+
+class lmbMacroPagerHelperTest extends UnitTestCase
+{
+  protected $pager_id;
+  protected $pager;
+  protected $old_get;
+  protected $old_server;
+
+  function setUp()
+  {
+    $this->old_get = $_GET;
+    $this->old_server = $_SERVER;
+
+    $_SERVER['REQUEST_URI'] = 'http://test.com';
+    $_GET = array();
+
+    $this->pager_id = 'test_pager';
+    $this->pager = new lmbMacroPagerHelper($this->pager_id);
+  }
+
+  function tearDown()
+  {
+    $_GET = $this->old_get;
+    $_SERVER = $this->old_server;
+  }
+
+  function testPrepare()
+  {
+    $this->pager->setCurrentPage(2);
+    $this->pager->setItemsPerPage(10);
+    $this->pager->setPagesPerSection(5);
+    $this->pager->setTotalItems(100);
+
+    $this->pager->prepare();
+
+    $this->assertEqual($this->pager->getCurrentPage(), 2);
+    $this->assertFalse($this->pager->isDisplayedPage());
+    $this->assertEqual($this->pager->getPage(), 1);
+    $this->assertEqual($this->pager->getTotalPages(), 10);
+    $this->assertEqual($this->pager->getPagesPerSection(), 5);
+    $this->assertTrue($this->pager->hasMoreThanOnePage());
+    $this->assertEqual($this->pager->getSectionBeginPage(), 1);
+    $this->assertEqual($this->pager->getSectionEndPage(), 5);
+    $this->assertTrue($this->pager->hasNext());
+    $this->assertTrue($this->pager->hasPrev());
+    $this->assertEqual($this->pager->getCurrentPageBeginItem(), 11);
+    $this->assertEqual($this->pager->getCurrentPageEndItem(), 20);
+  }
+  
+  function testGettingCurrentPageWithGetIfCurrentPageWasNotSet()
+  {
+    $this->pager->setItemsPerPage(10);
+    $this->pager->setPagesPerSection(5);
+    $this->pager->setTotalItems(100);
+
+    $_GET[$this->pager_id] = 2;
+
+    $this->pager->prepare();
+    
+    $this->assertEqual($this->pager->getCurrentPage(), 2);
+    $this->assertFalse($this->pager->isDisplayedPage());
+    $this->assertEqual($this->pager->getPage(), 1);
+    $this->assertEqual($this->pager->getTotalPages(), 10);
+    $this->assertEqual($this->pager->getPagesPerSection(), 5);
+    $this->assertTrue($this->pager->hasMoreThanOnePage());
+    $this->assertEqual($this->pager->getSectionBeginPage(), 1);
+    $this->assertEqual($this->pager->getSectionEndPage(), 5);
+    $this->assertTrue($this->pager->hasNext());
+    $this->assertTrue($this->pager->hasPrev());
+    $this->assertEqual($this->pager->getCurrentPageBeginItem(), 11);
+    $this->assertEqual($this->pager->getCurrentPageEndItem(), 20);
+  }
+
+  function testTotalItemsZero()
+  {
+    $this->pager->setCurrentPage(2);
+    $this->pager->setItemsPerPage(10);
+    $this->pager->setPagesPerSection(5);
+    $this->pager->setTotalItems(0);
+
+    $this->pager->prepare();
+
+    $this->assertEqual($this->pager->getCurrentPage(), 1);
+    $this->assertEqual($this->pager->getPage(), 1);
+    $this->assertTrue($this->pager->isDisplayedPage());
+    $this->assertEqual($this->pager->getTotalPages(), 1);
+    $this->assertFalse($this->pager->hasMoreThanOnePage());
+    $this->assertEqual($this->pager->getSectionBeginPage(), 1);
+    $this->assertEqual($this->pager->getSectionEndPage(), 1);
+    $this->assertFalse($this->pager->hasNext());
+    $this->assertFalse($this->pager->hasPrev());
+    $this->assertEqual($this->pager->getCurrentPageBeginItem(), 0);
+    $this->assertEqual($this->pager->getCurrentPageEndItem(), 0);
+  }
+
+  function testNextPage()
+  {
+    $this->pager->setCurrentPage(2);
+    $this->pager->setTotalItems(40);
+    $this->pager->setItemsPerPage(10);
+    $this->pager->setPagesPerSection(5);
+
+    $this->pager->prepare();
+
+    $this->assertEqual($this->pager->getPage(), 1);
+
+    $this->assertTrue($this->pager->nextPage());
+    $this->assertTrue($this->pager->isValid());
+
+    $this->assertEqual($this->pager->getPage(), 2);
+  }
+
+  function testNextPageOutOfBounds()
+  {
+    $this->pager->setCurrentPage(2);
+    $this->pager->setTotalItems(40);
+    $this->pager->setItemsPerPage(10);
+
+    $this->pager->prepare();
+
+    $this->assertTrue($this->pager->nextPage());
+    $this->assertTrue($this->pager->isValid());
+
+    $this->assertTrue($this->pager->nextPage());
+    $this->assertTrue($this->pager->isValid());
+
+    $this->assertTrue($this->pager->nextPage());
+    $this->assertTrue($this->pager->isValid());
+
+    $this->assertFalse($this->pager->nextPage());
+    $this->assertFalse($this->pager->isValid());
+  }
+
+  function testSectionNumbers()
+  {
+    $this->pager->setCurrentPage(2);
+    $this->pager->setTotalItems(40);
+    $this->pager->setItemsPerPage(3);
+    $this->pager->setPagesPerSection(10);
+
+    $this->pager->prepare();
+
+    $this->pager->nextPage();
+
+    $this->assertEqual($this->pager->getSection(), 1);
+    $this->assertEqual($this->pager->getSectionBeginPage(), 1);
+    $this->assertEqual($this->pager->getSectionEndPage(), 10);
+  }
+
+  function testSectionNumbersRightBound()
+  {
+    $this->pager->setCurrentPage(2);
+    $this->pager->setTotalItems(40);
+    $this->pager->setItemsPerPage(10);// 4 pages total
+    $this->pager->setPagesPerSection(10);
+
+    $this->pager->prepare();
+
+    $this->pager->nextPage();
+
+    $this->assertEqual($this->pager->getSection(), 1);
+    $this->assertEqual($this->pager->getSectionBeginPage(), 1);
+    $this->assertEqual($this->pager->getSectionEndPage(), 4);
+  }
+
+  function testNextSection()
+  {
+    $this->pager->setCurrentPage(2);
+    $this->pager->setTotalItems(40);
+    $this->pager->setItemsPerPage(5);
+    $this->pager->setPagesPerSection(2);
+
+    $this->pager->prepare();
+
+    $this->assertTrue($this->pager->nextSection());
+    $this->assertTrue($this->pager->nextSection());
+    $this->assertTrue($this->pager->nextSection());
+    $this->assertFalse($this->pager->nextSection());
+  }
+
+  function testGetFirstPageUri()
+  {
+    $_GET['p1'] = ' wow ';
+    $_GET['p2'] = array('3' => 'yo');
+
+    $this->pager->prepare();
+
+    $uri = $this->pager->getPageUri(1);
+
+    $this->assertEqual($uri, 'http://test.com?p1=+wow+&p2[3]=yo');
+  }
+
+  function testGetFirstPageUriNoQuery()
+  {
+    $this->pager->prepare();
+
+    $uri = $this->pager->getPageUri(1);
+
+    $this->assertEqual($uri, 'http://test.com');
+  }
+
+  function testGetPageUri()
+  {
+    $_GET['p1'] = 'wow';
+    $_GET['p2'] = array('3' => ' yo ');
+
+    $this->pager->prepare();
+
+    $uri = $this->pager->getPageUri(2);
+
+    $this->assertEqual($uri, 'http://test.com?p1=wow&p2[3]=+yo+&test_pager=2');
+  }
+
+  function testGetPrevSectionUri()
+  {
+    $this->pager->setCurrentPage(3);
+    $this->pager->setTotalItems(60);
+    $this->pager->setItemsPerPage(10);
+    $this->pager->setPagesPerSection(2);
+
+    $this->pager->prepare();
+
+    $this->pager->nextPage();
+
+    $uri = $this->pager->getSectionUri();
+
+    $this->assertEqual($uri, 'http://test.com?test_pager=2');
+    $this->assertEqual($this->pager->getSectionBeginPage(), 1);
+    $this->assertEqual($this->pager->getSectionEndPage(), 2);
+  }
+
+  function testGetNextSectionUri()
+  {
+    $this->pager->setCurrentPage(3);
+    $this->pager->setTotalItems(60);
+    $this->pager->setItemsPerPage(10);
+    $this->pager->setPagesPerSection(2);
+
+    $this->pager->prepare();
+
+    for($i = 0; $i < 5; $i++)
+      $this->pager->nextPage();
+
+    $uri = $this->pager->getSectionUri(2);
+
+    $this->assertEqual($uri, 'http://test.com?test_pager=5');
+    $this->assertEqual($this->pager->getSectionBeginPage(), 5);
+    $this->assertEqual($this->pager->getSectionEndPage(), 6);
+  }
+}
+
+



More information about the limb-svn mailing list