Skip to content

Commit

Permalink
Merge pull request codeigniter4#1044 from jim-parry/testing/pager
Browse files Browse the repository at this point in the history
Testing/pager
  • Loading branch information
lonnieezell authored May 23, 2018
2 parents d1618ff + 0138d8c commit 81cb807
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 123 deletions.
6 changes: 3 additions & 3 deletions system/Pager/PagerRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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)
{
Expand Down
20 changes: 10 additions & 10 deletions system/Pager/Views/default_full.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
<nav aria-label="<?= lang('Pager.pageNavigation') ?>">
<ul class="pagination">
<?php if ($pager->hasPrevious()) : ?>
<li>
<a href="<?= $pager->getFirst() ?>" aria-label="<?= lang('Pager.first') ?>">
<span aria-hidden="true"><?= lang('Pager.first') ?></span>
</a>
</li>
<li>
<a href="<?= $pager->getPrevious() ?>" aria-label="<?= lang('Pager.previous') ?>">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<li>
<a href="<?= $pager->getFirst() ?>" aria-label="<?= lang('Pager.first') ?>">
<span aria-hidden="true"><?= lang('Pager.first') ?></span>
</a>
</li>
<li>
<a href="<?= $pager->getPrevious() ?>" aria-label="<?= lang('Pager.previous') ?>">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
<?php endif ?>

<?php foreach ($pager->links() as $link) : ?>
Expand Down
114 changes: 75 additions & 39 deletions tests/system/Pager/PagerRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@

class PagerRendererTest extends \CIUnitTestCase
{

protected $uri;

//--------------------------------------------------------------------

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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -138,29 +140,29 @@ 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);
$pager->setSurroundCount(1);

$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
],
];
Expand All @@ -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);
Expand All @@ -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());
}

}
Loading

0 comments on commit 81cb807

Please sign in to comment.