Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes problem when paginator doesn't calculate countPages correctly in constructor #928

Merged
merged 3 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 3.7.1 - 2023-04-21
- **Bug Fixes**
- [spiral/filters] Fixed InputScope to allow retrieval of non-bag input sources
- [spiral/pagination] Fixed problem when paginator doesn't calculate `countPages` correctly in constructor

## 3.7.0 - 2023-04-13
- **Medium Impact Changes**
Expand Down
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
53 changes: 30 additions & 23 deletions src/Pagination/tests/PaginatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,32 +10,40 @@

class PaginatorTest extends TestCase
{
public function testInterfaces()
public function testInterfaces(): void
{
$paginator = new Paginator(25);
$paginator = new Paginator(limit: 25);

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

public function testParameterTracking()
public function testParameterTracking(): void
{
$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()
public function testLimit(): void
{
$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 testCountsAndPages()
public function testLimitWithCounts(): void
{
$paginator = new Paginator(25);
$paginator = new Paginator(limit: 25, count: 100);

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

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

$this->assertSame(0, $paginator->count());
$this->assertSame($paginator->count(), $paginator->count());
Expand All @@ -47,9 +55,9 @@ public function testCountsAndPages()
$this->assertSame(0, $paginator->countDisplayed());
}

public function testFirstPage()
public function testFirstPage(): void
{
$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()
public function testSecondPage(): void
{
$paginator = new Paginator(25);
$paginator = new Paginator(limit: 25);
$paginator = $paginator->withCount(110);

$this->assertSame(110, $paginator->count());
Expand All @@ -89,9 +96,9 @@ public function testSecondPage()
$this->assertSame(25, $paginator->countDisplayed());
}

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

$this->assertSame(1, $paginator->getPage());
Expand All @@ -114,9 +121,9 @@ public function testLastPage()
$this->assertSame(25, $paginator->countDisplayed());
}

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

Expand All @@ -127,9 +134,9 @@ public function testNegativePage()
$this->assertSame($paginator->count(), count($paginator));
}

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

$paginator = $paginator->withPage(-10);
Expand All @@ -144,9 +151,9 @@ public function testNegativeCount()
$this->assertSame(0, $paginator->countDisplayed());
}

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

$this->assertSame(110, $paginator->count());
Expand All @@ -164,9 +171,9 @@ public function testLastPageNumber()
$this->assertSame(10, $paginator->countDisplayed());
}

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

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