From 6efbebaee6588a4ccf4cda67d3d5789653d1e42f Mon Sep 17 00:00:00 2001 From: Master Yoda Date: Wed, 23 May 2018 01:30:33 -0700 Subject: [PATCH 1/2] Complete testing of PagerRenderer and tweak dox --- system/Pager/PagerRenderer.php | 6 +- tests/system/Pager/PagerRendererTest.php | 114 ++++++++++++------ .../source/libraries/pagination.rst | 17 ++- 3 files changed, 85 insertions(+), 52 deletions(-) diff --git a/system/Pager/PagerRenderer.php b/system/Pager/PagerRenderer.php index 26d7377aff43..66e60d0e5526 100644 --- a/system/Pager/PagerRenderer.php +++ b/system/Pager/PagerRenderer.php @@ -74,11 +74,11 @@ public function __construct(array $details) * side of the current page. Adjusts the first and last counts * to reflect it. * - * @param int $count + * @param int|null $count * * @return PagerRenderer */ - public function setSurroundCount(int $count) + public function setSurroundCount(int $count = null) { $this->updatePages($count); @@ -226,7 +226,7 @@ public function links(): array * which is the number of links surrounding the active page * to show. * - * @param int|null $count + * @param int|null $count The new "surroundCount" */ protected function updatePages(int $count = null) { diff --git a/tests/system/Pager/PagerRendererTest.php b/tests/system/Pager/PagerRendererTest.php index 17571a0e450a..5833bac03765 100644 --- a/tests/system/Pager/PagerRendererTest.php +++ b/tests/system/Pager/PagerRendererTest.php @@ -4,6 +4,7 @@ class PagerRendererTest extends \CIUnitTestCase { + protected $uri; //-------------------------------------------------------------------- @@ -11,17 +12,18 @@ class PagerRendererTest extends \CIUnitTestCase public function setUp() { $this->uri = new URI('http://example.com/foo'); + $this->expect = 'http://example.com/foo?page='; } //-------------------------------------------------------------------- public function testHasPreviousReturnsFalseWhenFirstIsOne() { - $details = [ - 'uri' => $this->uri, - 'pageCount' => 5, - 'currentPage' => 1, - 'total' => 100 + $details = [ + 'uri' => $this->uri, + 'pageCount' => 5, + 'currentPage' => 1, + 'total' => 100 ]; $pager = new PagerRenderer($details); @@ -37,10 +39,10 @@ public function testHasPreviousReturnsTrueWhenFirstIsMoreThanOne() $uri->addQuery('foo', 'bar'); $details = [ - 'uri' => $uri, - 'pageCount' => 10, - 'currentPage' => 5, - 'total' => 100 + 'uri' => $uri, + 'pageCount' => 10, + 'currentPage' => 5, + 'total' => 100 ]; $pager = new PagerRenderer($details); @@ -58,10 +60,10 @@ public function testGetPreviousWhenSurroundCountIsZero() $uri->addQuery('foo', 'bar'); $details = [ - 'uri' => $uri, - 'pageCount' => 50, - 'currentPage' => 4, - 'total' => 100 + 'uri' => $uri, + 'pageCount' => 50, + 'currentPage' => 4, + 'total' => 100 ]; $pager = new PagerRenderer($details); @@ -79,10 +81,10 @@ public function testHasNextReturnsFalseWhenLastIsTotal() $uri->addQuery('foo', 'bar'); $details = [ - 'uri' => $uri, - 'pageCount' => 5, - 'currentPage' => 4, - 'total' => 100 + 'uri' => $uri, + 'pageCount' => 5, + 'currentPage' => 4, + 'total' => 100 ]; $pager = new PagerRenderer($details); @@ -99,10 +101,10 @@ public function testHasNextReturnsTrueWhenLastIsSmallerThanTotal() $uri->addQuery('foo', 'bar'); $details = [ - 'uri' => $uri, - 'pageCount' => 50, - 'currentPage' => 4, - 'total' => 100 + 'uri' => $uri, + 'pageCount' => 50, + 'currentPage' => 4, + 'total' => 100 ]; $pager = new PagerRenderer($details); @@ -120,10 +122,10 @@ public function testGetNextWhenSurroundCountIsZero() $uri->addQuery('foo', 'bar'); $details = [ - 'uri' => $uri, - 'pageCount' => 50, - 'currentPage' => 4, - 'total' => 100 + 'uri' => $uri, + 'pageCount' => 50, + 'currentPage' => 4, + 'total' => 100 ]; $pager = new PagerRenderer($details); @@ -138,10 +140,10 @@ public function testGetNextWhenSurroundCountIsZero() public function testLinksBasics() { $details = [ - 'uri' => $this->uri, - 'pageCount' => 50, - 'currentPage' => 4, - 'total' => 100 + 'uri' => $this->uri, + 'pageCount' => 50, + 'currentPage' => 4, + 'total' => 100 ]; $pager = new PagerRenderer($details); @@ -149,18 +151,18 @@ public function testLinksBasics() $expected = [ [ - 'uri' => 'http://example.com/foo?page=3', - 'title' => 3, + 'uri' => 'http://example.com/foo?page=3', + 'title' => 3, 'active' => false ], [ - 'uri' => 'http://example.com/foo?page=4', - 'title' => 4, + 'uri' => 'http://example.com/foo?page=4', + 'title' => 4, 'active' => true ], [ - 'uri' => 'http://example.com/foo?page=5', - 'title' => 5, + 'uri' => 'http://example.com/foo?page=5', + 'title' => 5, 'active' => false ], ]; @@ -176,10 +178,10 @@ public function testGetFirstAndGetLast() $uri->addQuery('foo', 'bar'); $details = [ - 'uri' => $uri, - 'pageCount' => 50, - 'currentPage' => 4, - 'total' => 100 + 'uri' => $uri, + 'pageCount' => 50, + 'currentPage' => 4, + 'total' => 100 ]; $pager = new PagerRenderer($details); @@ -189,4 +191,38 @@ public function testGetFirstAndGetLast() } //-------------------------------------------------------------------- + + public function testSurroundCount() + { + $uri = $this->uri; + + $details = [ + 'uri' => $uri, + 'pageCount' => 10, // 10 pages + 'currentPage' => 4, + 'total' => 100 // 100 records, so 10 per page + ]; + + $pager = new PagerRenderer($details); + + // without any surround count + $this->assertEquals(null, $pager->getPrevious()); + $this->assertEquals(null, $pager->getNext()); + + // with surropund count of 2 + $pager->setSurroundCount(2); + $this->assertEquals($this->expect . '1', $pager->getPrevious()); + $this->assertEquals($this->expect . '7', $pager->getNext()); + + // with unchanged surround count + $pager->setSurroundCount(); + $this->assertEquals($this->expect . '1', $pager->getPrevious()); + $this->assertEquals($this->expect . '7', $pager->getNext()); + + // and with huge surround count + $pager->setSurroundCount(100); + $this->assertEquals(null, $pager->getPrevious()); + $this->assertEquals(null, $pager->getNext()); + } + } diff --git a/user_guide_src/source/libraries/pagination.rst b/user_guide_src/source/libraries/pagination.rst index 980f277561ba..86a0219aded7 100644 --- a/user_guide_src/source/libraries/pagination.rst +++ b/user_guide_src/source/libraries/pagination.rst @@ -147,12 +147,12 @@ the view file through it's namespace:: 'default_full' => 'App\Views\Pagers\foundation_full', Since it is under the standard **application/Views** directory, though, you do not need to namespace it since the -``view()`` method will locate. In that case, you can simple give the sub-directory and file name:: +``view()`` method can locate it by filename. In that case, you can simple give the sub-directory and file name:: 'default_full' => 'Pagers/foundation_full', Once you have created the view and set it in the configuration, it will automatically be used. You don't have to -just replace the existing templates. You can create as many additional templates as you need in the configuration +replace the existing templates. You can create as many additional templates as you need in the configuration file. A common situation would be needing different styles for the frontend and the backend of your application. :: @@ -173,7 +173,7 @@ Creating the View ================= When you create a new view, you only need to create the code that is needed for creating the pagination links themselves. -You should never create unnecessary wrapping divs since it might be used in multiple places and you only limit their +You should not create unnecessary wrapping divs since it might be used in multiple places and you only limit their usefulness. It is easiest to demonstrate creating a new view by showing you the existing default_full template:: setSurroundCount(2) ?> @@ -221,23 +221,20 @@ usefulness. It is easiest to demonstrate creating a new view by showing you the In the first line, the ``setSurroundCount()`` method specifies that we want to show two links to either side of the current page link. The only parameter that it accepts is the number of links to show. -**hasPrevious()** -**hasNext()** +**hasPrevious()** & **hasNext()** -These methods return a boolean true if it has more links than can be displayed on either side of the current page, +These methods return a boolean true if there are more links than can be displayed on either side of the current page, based on the value passed to ``setSurroundCount``. For example, let's say we have 20 pages of data. The current page is page 3. If the surround count is 2, then the following links would show up in the list: 1, 2, 3, 4, and 5. Since the first link displayed is page one, ``hasPrevious()`` would return **false** since there is no page zero. However, ``hasNext()`` would return **true** since there are 15 additional pages of results after page five. -**getPrevious()** -**getNext()** +**getPrevious()** & **getNext()** These methods return the URL for the previous or next pages of results on either side of the numbered links. See the previous paragraph for a full explanation. -**getFirst()** -**getLast()** +**getFirst()** & **getLast()** Much like ``getPrevious()`` and ``getNext()``, these methods return links to the first and last pages in the result set. From 0138d8cbd73d13651a09f113b9828b418f466615 Mon Sep 17 00:00:00 2001 From: Master Yoda Date: Wed, 23 May 2018 02:54:42 -0700 Subject: [PATCH 2/2] Flesh out PagerTest --- system/Pager/Views/default_full.php | 20 ++-- tests/system/Pager/PagerTest.php | 148 ++++++++++++++++------------ 2 files changed, 97 insertions(+), 71 deletions(-) diff --git a/system/Pager/Views/default_full.php b/system/Pager/Views/default_full.php index 5f57afcfe8d9..4eb10aa79235 100644 --- a/system/Pager/Views/default_full.php +++ b/system/Pager/Views/default_full.php @@ -3,16 +3,16 @@