Skip to content

Commit

Permalink
Fixes problem when paginator doesn't calculate countPages correctl…
Browse files Browse the repository at this point in the history
…y in constructor

 fixed #900
  • Loading branch information
butschster committed Apr 22, 2023
1 parent 0f2b962 commit c9f0531
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
8 changes: 5 additions & 3 deletions src/Pagination/src/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ final class Paginator implements PaginatorInterface, \Countable
{
private int $pageNumber = 1;
private int $countPages = 1;
private int $count;

public function __construct(
private int $limit = 25,
private int $count = 0,
private readonly ?string $parameter = null
int $count = 0,
private readonly ?string $parameter = null,
) {
$this->setCount($count);
}

/**
Expand Down Expand Up @@ -129,7 +131,7 @@ public function previousPage(): ?int
private function setCount(int $count): self
{
$this->count = \max($count, 0);
$this->countPages = $this->count > 0 ? (int) \ceil($this->count / $this->limit) : 1;
$this->countPages = $this->count > 0 ? (int)\ceil($this->count / $this->limit) : 1;

return $this;
}
Expand Down
31 changes: 19 additions & 12 deletions src/Pagination/tests/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,38 @@ class PaginatorTest extends TestCase
{
public function testInterfaces()
{
$paginator = new Paginator(25);
$paginator = new Paginator(limit: 25);

$this->assertInstanceOf(PaginatorInterface::class, $paginator);
}

public function testParameterTracking()
{
$paginator = new Paginator(25, 0, 'request:page');
$paginator = new Paginator(limit: 25, count: 0, parameter: 'request:page');
$this->assertSame('request:page', $paginator->getParameter());
}

public function testLimit()
{
$paginator = new Paginator(25);
$paginator = new Paginator(limit: 25);

$this->assertSame(25, $paginator->getLimit());
$newPaginator = $paginator->withLimit(50);
$this->assertSame(25, $paginator->getLimit());
$this->assertSame(50, $newPaginator->getLimit());
}

public function testLimitWithCounts()
{
$paginator = new Paginator(limit: 25, count: 100);

$this->assertSame(100, $paginator->count());
$this->assertSame(4, $paginator->countPages());
}

public function testCountsAndPages()
{
$paginator = new Paginator(25);
$paginator = new Paginator(limit: 25);

$this->assertSame(0, $paginator->count());
$this->assertSame($paginator->count(), $paginator->count());
Expand All @@ -49,7 +57,7 @@ public function testCountsAndPages()

public function testFirstPage()
{
$paginator = new Paginator(25);
$paginator = new Paginator(limit: 25);
$paginator = $paginator->withCount(100);

$this->assertSame(1, $paginator->getPage());
Expand All @@ -66,10 +74,9 @@ public function testFirstPage()
$this->assertSame(25, $paginator->countDisplayed());
}


public function testSecondPage()
{
$paginator = new Paginator(25);
$paginator = new Paginator(limit: 25);
$paginator = $paginator->withCount(110);

$this->assertSame(110, $paginator->count());
Expand All @@ -91,7 +98,7 @@ public function testSecondPage()

public function testLastPage()
{
$paginator = new Paginator(25);
$paginator = new Paginator(limit: 25);
$paginator = $paginator->withCount(100);

$this->assertSame(1, $paginator->getPage());
Expand All @@ -116,7 +123,7 @@ public function testLastPage()

public function testNegativePage()
{
$paginator = new Paginator(25);
$paginator = new Paginator(limit: 25);
$paginator = $paginator->withCount(100);
$paginator = $paginator->withPage(-1);

Expand All @@ -129,7 +136,7 @@ public function testNegativePage()

public function testNegativeCount()
{
$paginator = new Paginator(25);
$paginator = new Paginator(limit: 25);
$paginator = $paginator->withCount(-100);

$paginator = $paginator->withPage(-10);
Expand All @@ -146,7 +153,7 @@ public function testNegativeCount()

public function testLastPageNumber()
{
$paginator = new Paginator(25);
$paginator = new Paginator(limit: 25);
$paginator = $paginator->withCount(110);

$this->assertSame(110, $paginator->count());
Expand All @@ -166,7 +173,7 @@ public function testLastPageNumber()

public function testIsRequired()
{
$paginator = new Paginator(25);
$paginator = new Paginator(limit: 25);

$paginator = $paginator->withCount(24);
$this->assertFalse($paginator->isRequired());
Expand Down

0 comments on commit c9f0531

Please sign in to comment.